21#if defined(XRT_OS_OSX)
26#elif defined(XRT_OS_LINUX) || defined(XRT_ENV_MINGW)
31#define OS_THREAD_HAVE_SETNAME
32#define OS_THREAD_HAVE_SEMAPHORE
34#elif defined(XRT_OS_WINDOWS)
40#define OS_THREAD_HAVE_SETNAME
41#define OS_THREAD_HAVE_SEMAPHORE
45#error "OS not supported"
70 pthread_mutex_t mutex;
86 assert(!om->initialized);
88 om->initialized =
true;
89 om->recursive =
false;
91 return pthread_mutex_init(&om->mutex, NULL);
102 assert(om->initialized);
103 pthread_mutex_lock(&om->mutex);
114 assert(om->initialized);
115 return pthread_mutex_trylock(&om->mutex);
126 assert(om->initialized);
127 pthread_mutex_unlock(&om->mutex);
138 assert(om->initialized);
139 assert(!om->recursive);
141 pthread_mutex_destroy(&om->mutex);
144 om->initialized =
false;
145 om->recursive =
false;
157 assert(!om->initialized);
160 om->initialized =
true;
161 om->recursive =
true;
164 pthread_mutexattr_t attr;
165 pthread_mutexattr_init(&attr);
166 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
167 int ret = pthread_mutex_init(&om->mutex, &attr);
168 pthread_mutexattr_destroy(&attr);
181 assert(om->initialized);
182 assert(om->recursive);
184 pthread_mutex_destroy(&om->mutex);
187 om->initialized =
false;
188 om->recursive =
false;
218 assert(!oc->initialized);
220 oc->initialized =
true;
222 return pthread_cond_init(&oc->cond, NULL);
233 assert(oc->initialized);
234 pthread_cond_signal(&oc->cond);
245 assert(oc->initialized);
246 return pthread_cond_broadcast(&oc->cond);
267 assert(oc->initialized);
268 pthread_cond_wait(&oc->cond, &om->mutex);
279 assert(oc->initialized);
280 pthread_cond_destroy(&oc->cond);
282 oc->initialized =
false;
307typedef void *(*os_run_func_t)(
void *);
328 return pthread_create(&ost->thread, NULL, func, ptr);
341 pthread_join(ost->thread, &retval);
360#if defined(XRT_OS_LINUX) || defined(XRT_OS_OSX)
361 return (int64_t)sysconf(_SC_NPROCESSORS_ONLN);
362#elif defined(XRT_OS_WINDOWS)
363 SYSTEM_INFO sysinfo = {0};
364 GetSystemInfo(&sysinfo);
365 return (int64_t)sysinfo.dwNumberOfProcessors;
367#error "OS not supported"
380#ifdef OS_THREAD_HAVE_SETNAME
381 pthread_setname_np(ost->thread, name);
388#ifdef OS_THREAD_HAVE_SEMAPHORE
411 return sem_init(&os->sem, 0, count);
435#if defined(XRT_OS_WINDOWS) && !defined(XRT_ENV_MINGW)
436 struct timespec relative;
438 pthread_win32_getabstime_np(ts, &relative);
442 if (clock_gettime(CLOCK_REALTIME, &now) < 0) {
447 uint64_t when_ns = timeout_ns + now_ns;
462 if (timeout_ns == 0) {
467 struct timespec abs_timeout;
472 sem_timedwait(&os->sem, &abs_timeout);
483 sem_destroy(&os->sem);
501 pthread_mutex_t mutex;
518 int ret = pthread_mutex_init(&oth->mutex, NULL);
523 ret = pthread_cond_init(&oth->cond, NULL);
525 pthread_mutex_destroy(&oth->mutex);
528 oth->initialized =
true;
541 pthread_mutex_lock(&oth->mutex);
543 assert(oth->initialized);
545 pthread_mutex_unlock(&oth->mutex);
549 int ret = pthread_create(&oth->thread, NULL, func, ptr);
551 pthread_mutex_unlock(&oth->mutex);
557 pthread_mutex_unlock(&oth->mutex);
573 pthread_mutex_lock(&oth->mutex);
574 assert(oth->initialized);
577 oth->running =
false;
580 pthread_cond_signal(&oth->cond);
583 pthread_mutex_unlock(&oth->mutex);
601 pthread_mutex_lock(&oth->mutex);
602 assert(oth->initialized);
606 pthread_mutex_unlock(&oth->mutex);
611 oth->running =
false;
614 pthread_cond_signal(&oth->cond);
617 pthread_mutex_unlock(&oth->mutex);
620 pthread_join(oth->thread, &retval);
635 assert(oth->initialized);
640 pthread_mutex_destroy(&oth->mutex);
641 pthread_cond_destroy(&oth->cond);
642 oth->initialized =
false;
653 pthread_mutex_lock(&oth->mutex);
664 pthread_mutex_unlock(&oth->mutex);
679 assert(oth->initialized);
680 bool ret = oth->running;
718 pthread_cond_wait(&oth->cond, &oth->mutex);
731 pthread_cond_signal(&oth->cond);
742#ifdef OS_THREAD_HAVE_SETNAME
743 pthread_setname_np(oth->thread, name);
761namespace xrt::auxiliary::os {
771 os_mutex_init(&inner_);
776 os_mutex_destroy(&inner_);
783 os_mutex_lock(&inner_);
790 return 0 == os_mutex_trylock(&inner_);
797 os_mutex_unlock(&inner_);
808 Mutex(Mutex
const &) =
delete;
809 Mutex(Mutex &&) =
delete;
811 operator=(Mutex
const &) =
delete;
813 operator=(Mutex &&) =
delete;
static void os_mutex_recursive_destroy(struct os_mutex *om)
Clean up.
Definition: os_threading.h:179
static bool os_thread_helper_is_running_locked(struct os_thread_helper *oth)
Is the thread running, or supposed to be running.
Definition: os_threading.h:695
static int os_cond_broadcast(struct os_cond *oc)
Broadcast (signal to multiple threads).
Definition: os_threading.h:243
static int os_thread_helper_start(struct os_thread_helper *oth, os_run_func_t func, void *ptr)
Start the internal thread.
Definition: os_threading.h:539
static int os_mutex_init(struct os_mutex *om)
Init.
Definition: os_threading.h:84
static int os_semaphore_get_realtime_clock(struct timespec *ts, uint64_t timeout_ns)
Set ts to the current time, plus the timeout_ns value.
Definition: os_threading.h:433
static void os_thread_helper_signal_locked(struct os_thread_helper *oth)
Signal a waiting thread to wake up.
Definition: os_threading.h:729
static void os_mutex_lock(struct os_mutex *om)
Lock.
Definition: os_threading.h:100
static void os_cond_wait(struct os_cond *oc, struct os_mutex *om)
Wait.
Definition: os_threading.h:265
static int os_thread_helper_stop_and_wait(struct os_thread_helper *oth)
Stop the thread and wait for it to exit.
Definition: os_threading.h:596
static void os_thread_destroy(struct os_thread *ost)
Destruction.
Definition: os_threading.h:351
static void os_semaphore_release(struct os_semaphore *os)
Release.
Definition: os_threading.h:420
static void os_thread_join(struct os_thread *ost)
Join.
Definition: os_threading.h:337
static int os_thread_start(struct os_thread *ost, os_run_func_t func, void *ptr)
Start thread.
Definition: os_threading.h:326
static int os_thread_helper_signal_stop(struct os_thread_helper *oth)
Signal from within the thread that we are stopping.
Definition: os_threading.h:570
static int os_mutex_recursive_init(struct os_mutex *om)
Init.
Definition: os_threading.h:155
static void os_thread_name(struct os_thread *ost, const char *name)
Make a best effort to name our thread.
Definition: os_threading.h:378
static void os_thread_helper_wait_locked(struct os_thread_helper *oth)
Wait for a signal.
Definition: os_threading.h:716
static int os_semaphore_init(struct os_semaphore *os, int count)
Init.
Definition: os_threading.h:409
static int os_thread_init(struct os_thread *ost)
Init.
Definition: os_threading.h:315
static void os_cond_signal(struct os_cond *oc)
Signal.
Definition: os_threading.h:231
static int64_t os_hardware_thread_count(void)
Gets the number of hardware threads available.
Definition: os_threading.h:358
static int os_mutex_trylock(struct os_mutex *om)
Try to lock, but do not block.
Definition: os_threading.h:112
static void os_thread_helper_unlock(struct os_thread_helper *oth)
Unlock the helper.
Definition: os_threading.h:662
static void os_thread_helper_name(struct os_thread_helper *oth, const char *name)
Make a best effort to name our thread.
Definition: os_threading.h:740
static int os_thread_helper_init(struct os_thread_helper *oth)
Initialize the thread helper.
Definition: os_threading.h:514
static int os_cond_init(struct os_cond *oc)
Init.
Definition: os_threading.h:216
static void os_thread_helper_destroy(struct os_thread_helper *oth)
Destroy the thread helper, externally synchronizable.
Definition: os_threading.h:633
static bool os_thread_helper_is_running(struct os_thread_helper *oth)
Is the thread running, or supposed to be running.
Definition: os_threading.h:676
static void os_mutex_unlock(struct os_mutex *om)
Unlock.
Definition: os_threading.h:124
static void os_mutex_destroy(struct os_mutex *om)
Clean up.
Definition: os_threading.h:136
void *(* os_run_func_t)(void *)
Run function.
Definition: os_threading.h:307
static void os_semaphore_destroy(struct os_semaphore *os)
Clean up.
Definition: os_threading.h:481
static void os_thread_helper_lock(struct os_thread_helper *oth)
Lock the helper.
Definition: os_threading.h:651
static void os_cond_destroy(struct os_cond *oc)
Clean up.
Definition: os_threading.h:277
static void os_semaphore_wait(struct os_semaphore *os, uint64_t timeout_ns)
Wait, if timeout_ns is zero then waits forever.
Definition: os_threading.h:460
#define U_ZERO(PTR)
Zeroes the correct amount of memory based on the type pointed-to by the argument.
Definition: u_misc.h:68
Wrapper around OS native time functions.
A wrapper around a native conditional variable.
Definition: os_threading.h:203
A wrapper around a native mutex.
Definition: os_threading.h:69
A wrapper around a native semaphore.
Definition: os_threading.h:399
All in one helper that handles locking, waiting for change and starting a thread.
Definition: os_threading.h:499
A wrapper around a native thread.
Definition: os_threading.h:298
Definition: u_worker.c:38
Header holding common defines.
Auto detect OS and certain features.
A minimal way to include Windows.h.