1 // Copyright (c) 2014 Marshall A. Greenblatt. Portions copyright (c) 2011
2 // Google Inc. All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the name Chromium Embedded
15 // Framework nor the names of its contributors may be used to endorse
16 // or promote products derived from this software without specific prior
17 // written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 // WARNING: You should *NOT* be using this class directly. PlatformThread is
32 // the low-level platform-specific abstraction to the OS's threading interface.
33 // You should instead be using a message-loop driven Thread, see thread.h.
34
35 #ifndef CEF_INCLUDE_BASE_PLATFORM_THREAD_H_
36 #define CEF_INCLUDE_BASE_PLATFORM_THREAD_H_
37
38 #if defined(BASE_THREADING_PLATFORM_THREAD_H_)
39 // Do nothing if the Chromium header has already been included.
40 // This can happen in cases where Chromium code is used directly by the
41 // client application. When using Chromium code directly always include
42 // the Chromium header first to avoid type conflicts.
43 #elif defined(USING_CHROMIUM_INCLUDES)
44 // When building CEF include the Chromium header directly.
45 #include "base/threading/platform_thread.h"
46 #else // !USING_CHROMIUM_INCLUDES
47 // The following is substantially similar to the Chromium implementation.
48 // If the Chromium implementation diverges the below implementation should be
49 // updated to match.
50
51 #include "include/base/cef_basictypes.h"
52 #include "include/base/cef_build.h"
53 #include "include/internal/cef_thread_internal.h"
54
55 namespace base {
56
57 // Used for logging. Always an integer value.
58 typedef cef_platform_thread_id_t PlatformThreadId;
59
60 // Used for thread checking and debugging.
61 // Meant to be as fast as possible.
62 // These are produced by PlatformThread::CurrentRef(), and used to later
63 // check if we are on the same thread or not by using ==. These are safe
64 // to copy between threads, but can't be copied to another process as they
65 // have no meaning there. Also, the internal identifier can be re-used
66 // after a thread dies, so a PlatformThreadRef cannot be reliably used
67 // to distinguish a new thread from an old, dead thread.
68 class PlatformThreadRef {
69 public:
70 typedef cef_platform_thread_handle_t RefType;
71
PlatformThreadRef()72 PlatformThreadRef() : id_(0) {}
73
PlatformThreadRef(RefType id)74 explicit PlatformThreadRef(RefType id) : id_(id) {}
75
76 bool operator==(PlatformThreadRef other) const { return id_ == other.id_; }
77
is_null()78 bool is_null() const { return id_ == 0; }
79
80 private:
81 RefType id_;
82 };
83
84 // A namespace for low-level thread functions.
85 // Chromium uses a class with static methods but CEF uses an actual namespace
86 // to avoid linker problems with the sandbox libaries on Windows.
87 namespace PlatformThread {
88
89 // Gets the current thread id, which may be useful for logging purposes.
CurrentId()90 inline PlatformThreadId CurrentId() {
91 return cef_get_current_platform_thread_id();
92 }
93
94 // Gets the current thread reference, which can be used to check if
95 // we're on the right thread quickly.
CurrentRef()96 inline PlatformThreadRef CurrentRef() {
97 return PlatformThreadRef(cef_get_current_platform_thread_handle());
98 }
99
100 } // namespace PlatformThread
101
102 } // namespace base
103
104 #endif // !USING_CHROMIUM_INCLUDES
105
106 #endif // CEF_INCLUDE_BASE_PLATFORM_THREAD_H_
107