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 | |
39 | namespace Hdfs { |
40 | namespace Internal { |
41 | |
42 | using boost::thread; |
43 | using boost::mutex; |
44 | using boost::lock_guard; |
45 | using boost::unique_lock; |
46 | using boost::condition_variable; |
47 | using boost::defer_lock_t; |
48 | using boost::once_flag; |
49 | using boost::call_once; |
50 | using namespace boost::this_thread; |
51 | |
52 | } |
53 | } |
54 | |
55 | #else |
56 | |
57 | #include <thread> |
58 | #include <mutex> |
59 | #include <condition_variable> |
60 | |
61 | namespace Hdfs { |
62 | namespace Internal { |
63 | |
64 | using std::thread; |
65 | using std::mutex; |
66 | using std::lock_guard; |
67 | using std::unique_lock; |
68 | using std::condition_variable; |
69 | using std::defer_lock_t; |
70 | using std::once_flag; |
71 | using std::call_once; |
72 | using namespace std::this_thread; |
73 | |
74 | } |
75 | } |
76 | #endif |
77 | |
78 | namespace Hdfs { |
79 | namespace Internal { |
80 | |
81 | /* |
82 | * make the background thread ignore these signals (which should allow that |
83 | * they be delivered to the main thread) |
84 | */ |
85 | sigset_t ThreadBlockSignal(); |
86 | |
87 | /* |
88 | * Restore previous signals. |
89 | */ |
90 | void 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 | |