henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | // Borrowed from Chromium's src/base/threading/thread_checker_impl.h. |
| 12 | |
| 13 | #ifndef WEBRTC_BASE_THREAD_CHECKER_IMPL_H_ |
| 14 | #define WEBRTC_BASE_THREAD_CHECKER_IMPL_H_ |
| 15 | |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 16 | #if defined(WEBRTC_POSIX) |
| 17 | #include <pthread.h> |
| 18 | #include <unistd.h> |
| 19 | #endif |
| 20 | |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 21 | #include "webrtc/base/criticalsection.h" |
| 22 | |
| 23 | namespace rtc { |
| 24 | |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 25 | // Used for identifying the current thread. Always an integer value. |
| 26 | #if defined(WEBRTC_WIN) |
| 27 | typedef DWORD PlatformThreadId; |
| 28 | #elif defined(WEBRTC_POSIX) |
| 29 | typedef pid_t PlatformThreadId; |
| 30 | #endif |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 31 | |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 32 | // TODO(tommi): This+PlatformThreadId belongs in a common thread related header. |
| 33 | PlatformThreadId CurrentThreadId(); |
| 34 | |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 35 | // Real implementation of ThreadChecker, for use in debug mode, or |
| 36 | // for temporary use in release mode (e.g. to CHECK on a threading issue |
| 37 | // seen only in the wild). |
| 38 | // |
| 39 | // Note: You should almost always use the ThreadChecker class to get the |
| 40 | // right version for your build configuration. |
| 41 | class ThreadCheckerImpl { |
| 42 | public: |
| 43 | ThreadCheckerImpl(); |
| 44 | ~ThreadCheckerImpl(); |
| 45 | |
| 46 | bool CalledOnValidThread() const; |
| 47 | |
| 48 | // Changes the thread that is checked for in CalledOnValidThread. This may |
| 49 | // be useful when an object may be created on one thread and then used |
| 50 | // exclusively on another thread. |
| 51 | void DetachFromThread(); |
| 52 | |
| 53 | private: |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 54 | mutable CriticalSection lock_; |
| 55 | // This is mutable so that CalledOnValidThread can set it. |
| 56 | // It's guarded by |lock_|. |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 57 | mutable PlatformThreadId valid_thread_; |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | } // namespace rtc |
| 61 | |
| 62 | #endif // WEBRTC_BASE_THREAD_CHECKER_IMPL_H_ |