blob: 5f851c4d4202b8420ecd9a80ffad176a4124d5c6 [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
32// Real implementation of ThreadChecker, for use in debug mode, or
33// for temporary use in release mode (e.g. to CHECK on a threading issue
34// seen only in the wild).
35//
36// Note: You should almost always use the ThreadChecker class to get the
37// right version for your build configuration.
38class ThreadCheckerImpl {
39 public:
40 ThreadCheckerImpl();
41 ~ThreadCheckerImpl();
42
43 bool CalledOnValidThread() const;
44
45 // Changes the thread that is checked for in CalledOnValidThread. This may
46 // be useful when an object may be created on one thread and then used
47 // exclusively on another thread.
48 void DetachFromThread();
49
50 private:
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000051 mutable CriticalSection lock_;
52 // This is mutable so that CalledOnValidThread can set it.
53 // It's guarded by |lock_|.
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000054 mutable PlatformThreadId valid_thread_;
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000055};
56
57} // namespace rtc
58
59#endif // WEBRTC_BASE_THREAD_CHECKER_IMPL_H_