blob: 5ad15bb5158a7a6997e9f1f5b1887dcc0048ebec [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;
tommi@webrtc.org2eb16602015-02-07 19:17:47 +000028typedef DWORD PlatformThreadRef;
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000029#elif defined(WEBRTC_POSIX)
30typedef pid_t PlatformThreadId;
tommi@webrtc.org2eb16602015-02-07 19:17:47 +000031typedef pthread_t PlatformThreadRef;
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000032#endif
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000033
tommi@webrtc.orgd43bdf52015-02-03 16:29:57 +000034// TODO(tommi): This+PlatformThreadId belongs in a common thread related header.
35PlatformThreadId CurrentThreadId();
tommi@webrtc.org2eb16602015-02-07 19:17:47 +000036PlatformThreadRef CurrentThreadRef();
37
38// Compares two thread identifiers for equality.
39bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b);
tommi@webrtc.orgd43bdf52015-02-03 16:29:57 +000040
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000041// 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.
47class 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.org1e3c5c22014-06-16 11:34:44 +000060 mutable CriticalSection lock_;
61 // This is mutable so that CalledOnValidThread can set it.
62 // It's guarded by |lock_|.
tommi@webrtc.org2eb16602015-02-07 19:17:47 +000063 mutable PlatformThreadRef valid_thread_;
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000064};
65
66} // namespace rtc
67
68#endif // WEBRTC_BASE_THREAD_CHECKER_IMPL_H_