Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
same-thread-guard.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Contact: Piotr Bartosiewicz <p.bartosiewi@partner.samsung.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License
17  */
18 
25 #ifndef COMMON_UTILS_SAME_THREAD_GUARD_HPP
26 #define COMMON_UTILS_SAME_THREAD_GUARD_HPP
27 
28 #ifndef NDEBUG
29 #define ENABLE_SAME_THREAD_GUARD
30 #endif
31 
32 #ifdef ENABLE_SAME_THREAD_GUARD
33 #include <atomic>
34 #include <cassert>
35 #endif
36 
37 namespace utils {
38 
49 public:
50 #ifdef ENABLE_SAME_THREAD_GUARD
51 # define ASSERT_SAME_THREAD(g) assert(g.check())
53 
58  bool check();
59 
63  void reset();
64 
65 private:
66  std::atomic<unsigned int> mThreadId;
67 
68 #else // ENABLE_SAME_THREAD_GUARD
69 # define ASSERT_SAME_THREAD(g)
70  static bool check() {return true;}
71  static void reset() {}
72 #endif // ENABLE_SAME_THREAD_GUARD
73 };
74 
75 } // namespace utils
76 
77 
78 #endif // COMMON_UTILS_SAME_THREAD_GUARD_HPP
Same thread guard.
Definition: same-thread-guard.hpp:48
std::atomic< unsigned int > mThreadId
Definition: same-thread-guard.hpp:66
void reset()
Reset thread id.
bool check()
On the first call it remembers the current thread id.