logog
logger optimized for games
thread.hpp
Go to the documentation of this file.
1 
5 #ifndef __LOGOG_THREAD_HPP__
6 #define __LOGOG_THREAD_HPP__
7 
9 #ifndef LOGOG_THREAD
10 #if defined(LOGOG_FLAVOR_WINDOWS)
11 
12 #include <process.h>
13 
14 typedef unsigned int (WINAPI * __logog_pThreadFn)(void *);
15 
16 /* 64-bit versions of Windows use 32-bit handles for interoperability. When
17 * sharing a handle between 32-bit and 64-bit applications, only the lower
18 * 32 bits are significant, so it is safe to truncate the handle (when passing
19 * it from 64-bit to 32-bit) or sign-extend the handle (when passing it from
20 * 32-bit to 64-bit)."
21 * source: https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx
22 */
23 
24 #define LOGOG_THREAD HANDLE
25 
26 #define LOGOG_THREAD_CREATE(handle, attr, pStartFn, arg) \
27  (int)((*handle=(HANDLE) _beginthreadex (NULL, /* security */ \
28  0, /* stack size */ \
29  (__logog_pThreadFn)pStartFn, /* start address */ \
30  arg, /* pv argument */ \
31  0, /* init flag */ \
32  NULL /*thread addr */ ))==NULL)
33 
34 #define LOGOG_THREAD_JOIN( thread ) \
35  ( (WaitForSingleObject(( thread ),INFINITE)!=WAIT_OBJECT_0) \
36  || !CloseHandle(thread) \
37  )
38 #define LOGOG_THREAD_SELF (LOGOG_THREAD)((size_t)GetCurrentThreadId())
39 
40 #endif // defined(LOGOG_FLAVOR_WINDOWS)
41 
42 #if defined(LOGOG_FLAVOR_POSIX)
43 
44 #define LOGOG_THREAD \
45  pthread_t
46 
47 #define LOGOG_THREAD_CREATE(handle, attr, pStartFn, arg) \
48  pthread_create(handle, attr, pStartFn, arg)
49 
50 #define LOGOG_THREAD_JOIN(thread) \
51  pthread_join(thread, NULL)
52 
53 #define LOGOG_THREAD_SELF \
54  pthread_self()
55 
56 #endif
57 
58 #endif // LOGOG_THREAD
59 
60 #ifndef LOGOG_THREAD
61 #error You need to define mutex macros for your platform; please see mutex.hpp
62 #endif
63 
65 
66 namespace logog
67 {
68 
70 class Thread
71 {
72 public:
73 
75  typedef void* (*ThreadStartLocationType)(void *);
76 
78  Thread(ThreadStartLocationType fnThreadStart, void* pvParams)
79  {
80  m_pFnThreadStart = fnThreadStart;
81  m_pvThreadParams = pvParams;
82  }
83 
85  int Start()
86  {
87  return LOGOG_THREAD_CREATE(&m_Thread, NULL, m_pFnThreadStart, m_pvThreadParams);
88  }
89 
93  static int WaitFor(const Thread& thread)
94  {
95  return LOGOG_THREAD_JOIN(thread.m_Thread);
96  }
97 
98 #ifdef LOGOG_THREAD_JOIN_SUPPORT
99  static void Join(const std::vector<Thread*>& threads)
100  {
101  for(size_t i=0; i<threads.size(); i++)
102  WaitFor(*threads.at(i));
103  }
104 
105  static void Delete(std::vector<Thread*>& threads)
106  {
107  for(size_t i=0; i<threads.size(); i++)
108  delete threads.at(i);
109  threads.clear();
110  }
111 #endif // LOGOG_THREAD_JOIN_SUPPORT
112 
114  static LOGOG_THREAD GetCurrent()
115  {
116  return (LOGOG_THREAD) LOGOG_THREAD_SELF ;
117  }
118 
119 private:
121  LOGOG_THREAD m_Thread;
123  ThreadStartLocationType m_pFnThreadStart;
125  void* m_pvThreadParams;
126 };
127 }
128 
129 #endif // __LOGOG_THREAD_HPP_
[Thread]
Definition: api.hpp:8
Definition: thread.hpp:70
static LOGOG_THREAD GetCurrent()
Definition: thread.hpp:114
void *(* ThreadStartLocationType)(void *)
Definition: thread.hpp:75
int Start()
Definition: thread.hpp:85
Thread(ThreadStartLocationType fnThreadStart, void *pvParams)
Definition: thread.hpp:78
static int WaitFor(const Thread &thread)
Definition: thread.hpp:93