• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(USING_CHROMIUM_INCLUDES)
39 // When building CEF include the Chromium header directly.
40 #include "base/threading/platform_thread.h"
41 #else  // !USING_CHROMIUM_INCLUDES
42 // The following is substantially similar to the Chromium implementation.
43 // If the Chromium implementation diverges the below implementation should be
44 // updated to match.
45 
46 #include "include/base/cef_basictypes.h"
47 #include "include/base/cef_build.h"
48 #include "include/internal/cef_thread_internal.h"
49 
50 namespace base {
51 
52 // Used for logging. Always an integer value.
53 typedef cef_platform_thread_id_t PlatformThreadId;
54 
55 // Used for thread checking and debugging.
56 // Meant to be as fast as possible.
57 // These are produced by PlatformThread::CurrentRef(), and used to later
58 // check if we are on the same thread or not by using ==. These are safe
59 // to copy between threads, but can't be copied to another process as they
60 // have no meaning there. Also, the internal identifier can be re-used
61 // after a thread dies, so a PlatformThreadRef cannot be reliably used
62 // to distinguish a new thread from an old, dead thread.
63 class PlatformThreadRef {
64  public:
65   typedef cef_platform_thread_handle_t RefType;
66 
PlatformThreadRef()67   PlatformThreadRef() : id_(0) {}
68 
PlatformThreadRef(RefType id)69   explicit PlatformThreadRef(RefType id) : id_(id) {}
70 
71   bool operator==(PlatformThreadRef other) const { return id_ == other.id_; }
72 
is_null()73   bool is_null() const { return id_ == 0; }
74 
75  private:
76   RefType id_;
77 };
78 
79 // A namespace for low-level thread functions.
80 // Chromium uses a class with static methods but CEF uses an actual namespace
81 // to avoid linker problems with the sandbox libaries on Windows.
82 namespace PlatformThread {
83 
84 // Gets the current thread id, which may be useful for logging purposes.
CurrentId()85 inline PlatformThreadId CurrentId() {
86   return cef_get_current_platform_thread_id();
87 }
88 
89 // Gets the current thread reference, which can be used to check if
90 // we're on the right thread quickly.
CurrentRef()91 inline PlatformThreadRef CurrentRef() {
92   return PlatformThreadRef(cef_get_current_platform_thread_handle());
93 }
94 
95 }  // namespace PlatformThread
96 
97 }  // namespace base
98 
99 #endif  // !USING_CHROMIUM_INCLUDES
100 
101 #endif  // CEF_INCLUDE_BASE_PLATFORM_THREAD_H_
102