1 // Copyright (c) 2014 Marshall A. Greenblatt. Portions copyright (c) 2011
2 // Google Inc. All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
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.
35 #ifndef CEF_INCLUDE_BASE_PLATFORM_THREAD_H_
36 #define CEF_INCLUDE_BASE_PLATFORM_THREAD_H_
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(BUILDING_CEF_SHARED)
44 // When building CEF include the Chromium header directly.
45 #include "base/threading/platform_thread.h"
46 #else // !BUILDING_CEF_SHARED
47 // The following is substantially similar to the Chromium implementation.
48 // If the Chromium implementation diverges the below implementation should be
51 #include "include/base/cef_basictypes.h"
52 #include "include/base/cef_build.h"
53 #include "include/internal/cef_thread_internal.h"
57 // Used for logging. Always an integer value.
58 typedef cef_platform_thread_id_t PlatformThreadId;
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 {
70 typedef cef_platform_thread_handle_t RefType;
76 explicit PlatformThreadRef(RefType id)
80 bool operator==(PlatformThreadRef other) const {
81 return id_ == other.id_;
84 bool is_null() const {
91 // A namespace for low-level thread functions.
92 // Chromium uses a class with static methods but CEF uses an actual namespace
93 // to avoid linker problems with the sandbox libaries on Windows.
94 namespace PlatformThread {
96 // Gets the current thread id, which may be useful for logging purposes.
97 inline PlatformThreadId CurrentId() {
98 return cef_get_current_platform_thread_id();
101 // Gets the current thread reference, which can be used to check if
102 // we're on the right thread quickly.
103 inline PlatformThreadRef CurrentRef() {
104 return PlatformThreadRef(cef_get_current_platform_thread_handle());
107 } // namespace PlatformThread
111 #endif // !BUILDING_CEF_SHARED
113 #endif // CEF_INCLUDE_BASE_PLATFORM_THREAD_H_