Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
threadutils.h
Go to the documentation of this file.
1 
18 #ifndef ION_PORT_THREADUTILS_H_
19 #define ION_PORT_THREADUTILS_H_
20 
21 #if defined(ION_PLATFORM_WINDOWS)
22 #include <windows.h>
23 #else
24 #include <pthread.h>
25 #endif
26 
27 #include <functional>
28 #include <string>
29 
30 namespace ion {
31 namespace port {
32 
33 #if defined(ION_PLATFORM_WINDOWS)
34 typedef DWORD ThreadId;
37 typedef DWORD ThreadLocalStorageKey;
38 #else
39 typedef pthread_t ThreadId;
42 typedef pthread_key_t ThreadLocalStorageKey;
43 #endif
44 
46 
51 
52 
58 typedef bool (*ThreadFuncPtr)();
59 typedef std::function<bool()> ThreadStdFunc;
60 
63 #if defined(ION_PLATFORM_IOS) || defined(ION_PLATFORM_MAC) || \
64  defined(ION_PLATFORM_NACL)
65 static const ThreadId kInvalidThreadId = reinterpret_cast<ThreadId>(-1LL);
66 #else
67 static const ThreadId kInvalidThreadId = static_cast<ThreadId>(-1);
68 #endif
69 
72 static const ThreadLocalStorageKey kInvalidThreadLocalStorageKey =
73  static_cast<ThreadLocalStorageKey>(-1);
74 
76 
81 
82 
87 ION_API ThreadId SpawnThread(const ThreadFuncPtr func_ptr);
88 ION_API ThreadId SpawnThreadStd(const ThreadStdFunc* func);
89 
92 ION_API bool JoinThread(ThreadId id);
93 
96 ION_API void YieldThread();
97 
99 
104 
105 
108 ION_API bool IsThreadNamingSupported();
109 
112 ION_API size_t GetMaxThreadNameLength();
113 
118 ION_API bool SetThreadName(const std::string& name);
119 
121 
126 
127 
129 ION_API ThreadId GetCurrentThreadId();
130 
132 ION_API bool IsMainThread();
133 
139 ION_API void SetMainThreadId(ThreadId id);
140 
142 
147 
148 
152 
155 ION_API bool SetThreadLocalStorage(ThreadLocalStorageKey key, void* ptr);
156 
160 
164 
165 } // namespace port
166 } // namespace ion
167 
168 #endif // ION_PORT_THREADUTILS_H_
size_t GetMaxThreadNameLength()
Returns the maximum length of a thread name if restricted by the platform.
Definition: threadutils.cc:329
bool(* ThreadFuncPtr)()
Thread types and constants.
Definition: threadutils.h:58
ThreadLocalStorageKey CreateThreadLocalStorageKey()
Thread-local storage functions.
Definition: threadutils.cc:367
bool IsMainThread()
Returns true if the current thread is the main thread.
Definition: threadutils.cc:217
void YieldThread()
Causes the calling thread to relinquish the CPU if there are other threads waiting to execute...
Definition: threadutils.cc:359
bool JoinThread(ThreadId id)
Windows-specific public functions.
Definition: threadutils.cc:322
bool SetThreadLocalStorage(ThreadLocalStorageKey key, void *ptr)
Associates ptr with the thread-local storage area indicated by key.
Definition: threadutils.cc:374
ThreadId SpawnThreadStd(const ThreadStdFunc *func)
Definition: threadutils.cc:200
pthread_key_t ThreadLocalStorageKey
Defines a type that is used to access thread-local storage.
Definition: threadutils.h:42
std::string name
Definition: printer.cc:324
ThreadId SpawnThread(const ThreadFuncPtr func_ptr)
Platform-independent public functions.
Definition: threadutils.cc:187
std::function< bool()> ThreadStdFunc
Definition: threadutils.h:59
bool IsThreadNamingSupported()
Thread naming functions.
Definition: threadutils.cc:213
pthread_t ThreadId
Defines a type that can be used to identify a thread.
Definition: threadutils.h:40
bool SetThreadName(const std::string &name)
Sets the name of the current thread.
Definition: threadutils.cc:340
void * GetThreadLocalStorage(ThreadLocalStorageKey key)
Returns the pointer to the thread-local storage area indicated by key.
Definition: threadutils.cc:382
bool DeleteThreadLocalStorageKey(ThreadLocalStorageKey key)
Deletes a key returned by CreateThreadLocalStorageKey().
Definition: threadutils.cc:387
void SetMainThreadId(ThreadId id)
Sets the given thread ID to be considered the main thread; IsMainThread() will return true only for t...
Definition: threadutils.cc:221
ThreadId GetCurrentThreadId()
Thread ID functions.
Definition: threadutils.cc:363