blob: 67adc6ebdf62304feea7ce695227f7b89b5757e5 [file] [log] [blame]
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +00001/*
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.org04cd4662015-01-26 15:27:29 +000016#if defined(WEBRTC_POSIX)
17#include <pthread.h>
18#include <unistd.h>
19#endif
20
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000021#include "webrtc/base/criticalsection.h"
22
23namespace rtc {
24
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000025// Used for identifying the current thread. Always an integer value.
26#if defined(WEBRTC_WIN)
27typedef DWORD PlatformThreadId;
28#elif defined(WEBRTC_POSIX)
29typedef pid_t PlatformThreadId;
30#endif
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000031
tommi@webrtc.orgd43bdf52015-02-03 16:29:57 +000032// TODO(tommi): This+PlatformThreadId belongs in a common thread related header.
33PlatformThreadId CurrentThreadId();
34
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000035// 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.
41class 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.org1e3c5c22014-06-16 11:34:44 +000054 mutable CriticalSection lock_;
55 // This is mutable so that CalledOnValidThread can set it.
56 // It's guarded by |lock_|.
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000057 mutable PlatformThreadId valid_thread_;
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000058};
59
60} // namespace rtc
61
62#endif // WEBRTC_BASE_THREAD_CHECKER_IMPL_H_