Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "threadsafe.h"
00035
00036 #include "common/wave_ex.h"
00037 #include "perf/perf.h"
00038 #include "threadsafe/smart_mutex.h"
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #ifdef WIN32
00049
00050 struct win32_context_t {
00051
00052 win32_context_t(void) throw() { this->clear(); }
00053 void clear(void) throw() {
00054 start_fn = NULL;
00055 real_context = NULL;
00056 }
00057
00058
00059 thread_start_fn_t start_fn;
00060 void * real_context;
00061 };
00062
00063
00064 static DWORD WINAPI
00065 win32start
00066 (
00067 IN LPVOID ctx
00068 )
00069 {
00070 win32_context_t * win32ctx = (win32_context_t *) ctx;
00071 ASSERT(win32ctx, "should always have win32 thread context");
00072 ASSERT(win32ctx->start_fn, "null thread start function");
00073
00074 win32ctx->start_fn(win32ctx->real_context);
00075
00076 return 0;
00077 }
00078
00079
00080 #endif // WIN32
00081
00082
00083
00084
00085
00086
00087
00088
00089 int
00090 createThread
00091 (
00092 IN thread_start_fn_t start_fn,
00093 IN void * context,
00094 OUT thread_id_t& threadId
00095 )
00096 {
00097 ASSERT(start_fn, "null thread start function");
00098
00099 threadId = 0;
00100
00101 #ifdef WIN32
00102
00103 ASSERT(sizeof(thread_id_t) >= sizeof(dword_t),
00104 "thread_id_t type is too small!");
00105 dword_t stackSize = 0;
00106 dword_t flags = 0;
00107 win32_context_t wctx;
00108 wctx.start_fn = start_fn;
00109 wctx.real_context = context;
00110 dword_t dwThreadId = 0;
00111 HANDLE hThread =
00112 CreateThread(NULL, stackSize, win32start, &wctx, flags,
00113 (LPDWORD) &dwThreadId);
00114 if (!hThread) {
00115 int error = GetLastError();
00116 DPRINTF("Failed to create new thread: %d", error);
00117 return (error) ? error : 1;
00118 }
00119 threadId = (thread_id_t) dwThreadId;
00120
00121 #else // WIN32
00122
00123
00124 ASSERT(sizeof(thread_id_t) >= sizeof(pthread_t),
00125 "thread_id_t type is too small!");
00126 pthread_t pid;
00127 pthread_create(&pid, NULL, start_fn, context);
00128 threadId = (thread_id_t) pid;
00129
00130 #endif // WIN32
00131
00132
00133 return 0;
00134 }
00135
00136
00137
00138 thread_id_t
00139 getCurrentThreadId
00140 (
00141 void
00142 )
00143 {
00144 #ifdef WIN32
00145 return (thread_id_t) GetCurrentThreadId();
00146 #else // WIN32
00147 return (thread_id_t) pthread_self();
00148 #endif // WIN32
00149 }
00150
00151
00152
00153 void
00154 sleepMilliseconds
00155 (
00156 IN long milliseconds
00157 )
00158 {
00159 ASSERT(milliseconds >= 0, "bad sleep milliseconds: %d",
00160 (int) milliseconds);
00161 #ifdef WIN32
00162
00163 Sleep(milliseconds);
00164 #else // WIN32
00165
00166 struct timespec ts;
00167 ts.tv_sec = 0;
00168 ts.tv_nsec = 1000 * 1000 * milliseconds;
00169 nanosleep(&ts, NULL);
00170 #endif // WIN32
00171 }
00172