1/********************************************************************
2 * Copyright (c) 2013 - 2014, Pivotal Inc.
3 * All rights reserved.
4 *
5 * Author: Zhanwei Wang
6 ********************************************************************/
7/********************************************************************
8 * 2014 -
9 * open source under Apache License Version 2.0
10 ********************************************************************/
11/**
12 * Licensed to the Apache Software Foundation (ASF) under one
13 * or more contributor license agreements. See the NOTICE file
14 * distributed with this work for additional information
15 * regarding copyright ownership. The ASF licenses this file
16 * to you under the Apache License, Version 2.0 (the
17 * "License"); you may not use this file except in compliance
18 * with the License. You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS,
24 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 */
28#ifndef _HDFS_LIBHDFS3_COMMON_THREAD_H_
29#define _HDFS_LIBHDFS3_COMMON_THREAD_H_
30
31#include "platform.h"
32
33#include <signal.h>
34
35#ifdef NEED_BOOST
36
37#include <boost/thread.hpp>
38
39namespace Hdfs {
40namespace Internal {
41
42using boost::thread;
43using boost::mutex;
44using boost::lock_guard;
45using boost::unique_lock;
46using boost::condition_variable;
47using boost::defer_lock_t;
48using boost::once_flag;
49using boost::call_once;
50using namespace boost::this_thread;
51
52}
53}
54
55#else
56
57#include <thread>
58#include <mutex>
59#include <condition_variable>
60
61namespace Hdfs {
62namespace Internal {
63
64using std::thread;
65using std::mutex;
66using std::lock_guard;
67using std::unique_lock;
68using std::condition_variable;
69using std::defer_lock_t;
70using std::once_flag;
71using std::call_once;
72using namespace std::this_thread;
73
74}
75}
76#endif
77
78namespace Hdfs {
79namespace Internal {
80
81/*
82 * make the background thread ignore these signals (which should allow that
83 * they be delivered to the main thread)
84 */
85sigset_t ThreadBlockSignal();
86
87/*
88 * Restore previous signals.
89 */
90void ThreadUnBlockSignal(sigset_t sigs);
91
92}
93}
94
95#define CREATE_THREAD(retval, fun) \
96 do { \
97 sigset_t sigs = Hdfs::Internal::ThreadBlockSignal(); \
98 try { \
99 retval = Hdfs::Internal::thread(fun); \
100 Hdfs::Internal::ThreadUnBlockSignal(sigs); \
101 } catch (...) { \
102 Hdfs::Internal::ThreadUnBlockSignal(sigs); \
103 throw; \
104 } \
105 } while(0)
106
107#endif /* _HDFS_LIBHDFS3_COMMON_THREAD_H_ */
108