8 #ifndef INCL_evo_systhread_h 9 #define INCL_evo_systhread_h 19 #if defined(__linux) && !defined(__CYGWIN__) 20 #include <sys/syscall.h> 21 #include <linux/version.h> 22 #if defined(LINUX_VERSION_CODE) && LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) 23 #define EVO_LINUX_NPTL 1 // Linux 2.6+ uses native pthreads (NPTL) 32 #define EVO_THREAD_RUN_DEFINE(NAME,PARAM_NAME) DWORD WINAPI NAME(LPVOID PARAM_NAME) 33 #define EVO_THREAD_RUN_RETURN return 0 40 #define EVO_THREAD_RUN_DEFINE(NAME,PARAM_NAME) void* NAME(void* PARAM_NAME) 45 #define EVO_THREAD_RUN_RETURN return NULL 57 typedef DWORD (WINAPI *
RunFunc)(LPVOID);
64 handle = CreateThread(NULL, 0, run_func, run_ptr, 0, NULL);
79 const bool done = (WaitForSingleObject(
handle, INFINITE) == WAIT_OBJECT_0);
92 {
return (ulong)GetCurrentThreadId(); }
96 typedef void* (*RunFunc)(
void*);
101 memset(&
handle, 0,
sizeof(Handle));
106 if (pthread_create(&
handle, NULL, run_func, run_ptr) == 0) {
116 memset(&
handle, 0,
sizeof(Handle));
130 #if defined(__APPLE__) 132 #elif defined(__CYGWIN__) 140 #if defined(SYS_gettid) && defined(EVO_LINUX_NPTL) 142 return (ulong)syscall(SYS_gettid);
145 return (ulong)pthread_self();
168 typedef CRITICAL_SECTION
Handle;
171 { InitializeCriticalSection(&
handle); }
174 { DeleteCriticalSection(&
handle); }
177 {
return (TryEnterCriticalSection(&
handle) != 0); }
179 bool trylock(ulong timeout_ms) {
180 if (TryEnterCriticalSection(&
handle) != 0)
187 if (TryEnterCriticalSection(&
handle) != 0)
190 if (ts.
compare(timeout_ts) >= 0)
197 { EnterCriticalSection(&
handle); }
200 { LeaveCriticalSection(&
handle); }
208 { pthread_mutex_init(&
handle, NULL); }
214 { pthread_mutex_destroy(&
handle); }
224 const int result = pthread_mutex_trylock(&
handle);
227 assert( result == EBUSY );
242 #if defined(__APPLE__) 243 int result = pthread_mutex_trylock(&
handle);
244 if (result == EBUSY) {
246 struct timespec timeout_ts;
250 result = pthread_mutex_trylock(&
handle);
260 #if defined(_POSIX_TIMERS) && defined(CLOCK_REALTIME) && !defined(EVO_USE_GETTIMEOFDAY) 261 ::clock_gettime(CLOCK_REALTIME, &ts);
265 ::gettimeofday(&tv, NULL);
270 const int result = pthread_mutex_timedlock(&
handle, &ts);
274 assert( result == ETIMEDOUT );
283 { pthread_mutex_lock(&
handle); }
289 { pthread_mutex_unlock(&
handle); }
int compare(const SysTimestamp &oth) const
Compare to another timestamp.
Definition: systime.h:498
static ulong id()
Definition: systhread.h:139
Evo system time implementation helpers.
Definition: systhread.h:165
pthread_mutex_t Handle
Mutex handle type.
Definition: systhread.h:204
void set_wall_timer()
Set as current real (wall clock) time for use by timers.
Definition: systime.h:392
Evo implementation detail for system portability – this is included by most Evo headers, include this via: include <evo/type.h>.
void add_msec(ulong new_msec)
Add milliseconds to current time.
Definition: systime.h:467
Error start(RunFunc run_func, void *run_ptr)
Definition: systhread.h:104
Handle handle
Definition: systhread.h:155
bool join()
Definition: systhread.h:121
void *(* RunFunc)(void *)
Definition: systhread.h:96
No error.
Definition: sys.h:1115
~SysMutex()
Destructor.
Definition: systhread.h:213
Error
General Evo error code stored in exceptions, or used directly when exceptions are disabled...
Definition: sys.h:1113
~SysThread()
Definition: systhread.h:152
static void yield()
Definition: systhread.h:129
SysThread()
Definition: systhread.h:99
void detach()
Definition: systhread.h:113
static void set_timespec_tv(struct timespec &tm, struct timeval &tv)
Definition: sys.h:1533
static int compare_timespec(const struct timespec &a, const struct timespec &b)
Definition: sys.h:1576
static void add_timespec_ms(struct timespec &tm, ulong ms)
Definition: sys.h:1546
Unknown or unspecified error.
Definition: sys.h:1120
void lock()
Lock mutex.
Definition: systhread.h:282
Evo C++ Library namespace.
Definition: alg.h:11
pthread_t Handle
Definition: systhread.h:97
void unlock()
Unlock mutex.
Definition: systhread.h:288
bool trylock()
Try to lock mutex without blocking.
Definition: systhread.h:223
SysMutex()
Constructor.
Definition: systhread.h:207
bool trylock(ulong timeout_ms)
Try to lock mutex with a timeout.
Definition: systhread.h:240
static void set_timespec_now(struct timespec &tm)
Definition: sys.h:1560
Holds a system timestamp for storing date/time and measuring elapsed time.
Definition: systime.h:269
Definition: systhread.h:54
bool attached
Definition: systhread.h:149