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; |
tommi@webrtc.org | 2eb1660 | 2015-02-07 19:17:47 +0000 | [diff] [blame^] | 28 | typedef DWORD PlatformThreadRef; |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 29 | #elif defined(WEBRTC_POSIX) |
| 30 | typedef pid_t PlatformThreadId; |
tommi@webrtc.org | 2eb1660 | 2015-02-07 19:17:47 +0000 | [diff] [blame^] | 31 | typedef pthread_t PlatformThreadRef; |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 32 | #endif |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 33 | |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 34 | // TODO(tommi): This+PlatformThreadId belongs in a common thread related header. |
| 35 | PlatformThreadId CurrentThreadId(); |
tommi@webrtc.org | 2eb1660 | 2015-02-07 19:17:47 +0000 | [diff] [blame^] | 36 | PlatformThreadRef CurrentThreadRef(); |
| 37 | |
| 38 | // Compares two thread identifiers for equality. |
| 39 | bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b); |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 40 | |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 41 | // Real implementation of ThreadChecker, for use in debug mode, or |
| 42 | // for temporary use in release mode (e.g. to CHECK on a threading issue |
| 43 | // seen only in the wild). |
| 44 | // |
| 45 | // Note: You should almost always use the ThreadChecker class to get the |
| 46 | // right version for your build configuration. |
| 47 | class ThreadCheckerImpl { |
| 48 | public: |
| 49 | ThreadCheckerImpl(); |
| 50 | ~ThreadCheckerImpl(); |
| 51 | |
| 52 | bool CalledOnValidThread() const; |
| 53 | |
| 54 | // Changes the thread that is checked for in CalledOnValidThread. This may |
| 55 | // be useful when an object may be created on one thread and then used |
| 56 | // exclusively on another thread. |
| 57 | void DetachFromThread(); |
| 58 | |
| 59 | private: |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 60 | mutable CriticalSection lock_; |
| 61 | // This is mutable so that CalledOnValidThread can set it. |
| 62 | // It's guarded by |lock_|. |
tommi@webrtc.org | 2eb1660 | 2015-02-07 19:17:47 +0000 | [diff] [blame^] | 63 | mutable PlatformThreadRef valid_thread_; |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | } // namespace rtc |
| 67 | |
| 68 | #endif // WEBRTC_BASE_THREAD_CHECKER_IMPL_H_ |