21#if defined(XRT_OS_OSX)
25#elif defined(XRT_OS_LINUX) || defined(XRT_ENV_MINGW)
29#define OS_THREAD_HAVE_SETNAME
30#define OS_THREAD_HAVE_SEMAPHORE
32#elif defined(XRT_OS_WINDOWS)
37#define OS_THREAD_HAVE_SETNAME
38#define OS_THREAD_HAVE_SEMAPHORE
42#error "OS not supported"
67 pthread_mutex_t mutex;
83 assert(!om->initialized);
85 om->initialized =
true;
86 om->recursive =
false;
88 return pthread_mutex_init(&om->mutex, NULL);
99 assert(om->initialized);
100 pthread_mutex_lock(&om->mutex);
111 assert(om->initialized);
112 return pthread_mutex_trylock(&om->mutex);
123 assert(om->initialized);
124 pthread_mutex_unlock(&om->mutex);
135 assert(om->initialized);
136 assert(!om->recursive);
138 pthread_mutex_destroy(&om->mutex);
141 om->initialized =
false;
142 om->recursive =
false;
154 assert(!om->initialized);
157 om->initialized =
true;
158 om->recursive =
true;
161 pthread_mutexattr_t attr;
162 pthread_mutexattr_init(&attr);
163 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
164 int ret = pthread_mutex_init(&om->mutex, &attr);
165 pthread_mutexattr_destroy(&attr);
178 assert(om->initialized);
179 assert(om->recursive);
181 pthread_mutex_destroy(&om->mutex);
184 om->initialized =
false;
185 om->recursive =
false;
215 assert(!oc->initialized);
217 oc->initialized =
true;
219 return pthread_cond_init(&oc->cond, NULL);
230 assert(oc->initialized);
231 pthread_cond_signal(&oc->cond);
242 assert(oc->initialized);
243 return pthread_cond_broadcast(&oc->cond);
264 assert(oc->initialized);
265 pthread_cond_wait(&oc->cond, &om->mutex);
276 assert(oc->initialized);
277 pthread_cond_destroy(&oc->cond);
279 oc->initialized =
false;
304typedef void *(*os_run_func_t)(
void *);
325 return pthread_create(&ost->thread, NULL, func, ptr);
338 pthread_join(ost->thread, &retval);
359#ifdef OS_THREAD_HAVE_SETNAME
360 pthread_setname_np(ost->thread, name);
367#ifdef OS_THREAD_HAVE_SEMAPHORE
390 return sem_init(&os->sem, 0, count);
414#if defined(XRT_OS_WINDOWS) && !defined(XRT_ENV_MINGW)
415 struct timespec relative;
417 pthread_win32_getabstime_np(ts, &relative);
421 if (clock_gettime(CLOCK_REALTIME, &now) < 0) {
426 uint64_t when_ns = timeout_ns + now_ns;
441 if (timeout_ns == 0) {
446 struct timespec abs_timeout;
451 sem_timedwait(&os->sem, &abs_timeout);
462 sem_destroy(&os->sem);
480 pthread_mutex_t mutex;
497 int ret = pthread_mutex_init(&oth->mutex, NULL);
502 ret = pthread_cond_init(&oth->cond, NULL);
504 pthread_mutex_destroy(&oth->mutex);
507 oth->initialized =
true;
520 pthread_mutex_lock(&oth->mutex);
522 assert(oth->initialized);
524 pthread_mutex_unlock(&oth->mutex);
528 int ret = pthread_create(&oth->thread, NULL, func, ptr);
530 pthread_mutex_unlock(&oth->mutex);
536 pthread_mutex_unlock(&oth->mutex);
552 pthread_mutex_lock(&oth->mutex);
553 assert(oth->initialized);
556 oth->running =
false;
559 pthread_cond_signal(&oth->cond);
562 pthread_mutex_unlock(&oth->mutex);
580 pthread_mutex_lock(&oth->mutex);
581 assert(oth->initialized);
585 pthread_mutex_unlock(&oth->mutex);
590 oth->running =
false;
593 pthread_cond_signal(&oth->cond);
596 pthread_mutex_unlock(&oth->mutex);
599 pthread_join(oth->thread, &retval);
614 assert(oth->initialized);
619 pthread_mutex_destroy(&oth->mutex);
620 pthread_cond_destroy(&oth->cond);
621 oth->initialized =
false;
632 pthread_mutex_lock(&oth->mutex);
643 pthread_mutex_unlock(&oth->mutex);
658 assert(oth->initialized);
659 bool ret = oth->running;
697 pthread_cond_wait(&oth->cond, &oth->mutex);
710 pthread_cond_signal(&oth->cond);
721#ifdef OS_THREAD_HAVE_SETNAME
722 pthread_setname_np(oth->thread, name);
740namespace xrt::auxiliary::os {
750 os_mutex_init(&inner_);
755 os_mutex_destroy(&inner_);
762 os_mutex_lock(&inner_);
769 return 0 == os_mutex_trylock(&inner_);
776 os_mutex_unlock(&inner_);
787 Mutex(Mutex
const &) =
delete;
788 Mutex(Mutex &&) =
delete;
790 operator=(Mutex
const &) =
delete;
792 operator=(Mutex &&) =
delete;
static void os_mutex_recursive_destroy(struct os_mutex *om)
Clean up.
Definition: os_threading.h:176
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:674
static int os_cond_broadcast(struct os_cond *oc)
Broadcast (signal to multiple threads).
Definition: os_threading.h:240
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:518
static int os_mutex_init(struct os_mutex *om)
Init.
Definition: os_threading.h:81
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:412
static void os_thread_helper_signal_locked(struct os_thread_helper *oth)
Signal a waiting thread to wake up.
Definition: os_threading.h:708
static void os_mutex_lock(struct os_mutex *om)
Lock.
Definition: os_threading.h:97
static void os_cond_wait(struct os_cond *oc, struct os_mutex *om)
Wait.
Definition: os_threading.h:262
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:575
static void os_thread_destroy(struct os_thread *ost)
Destruction.
Definition: os_threading.h:348
static void os_semaphore_release(struct os_semaphore *os)
Release.
Definition: os_threading.h:399
static void os_thread_join(struct os_thread *ost)
Join.
Definition: os_threading.h:334
static int os_thread_start(struct os_thread *ost, os_run_func_t func, void *ptr)
Start thread.
Definition: os_threading.h:323
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:549
static int os_mutex_recursive_init(struct os_mutex *om)
Init.
Definition: os_threading.h:152
static void os_thread_name(struct os_thread *ost, const char *name)
Make a best effort to name our thread.
Definition: os_threading.h:357
static void os_thread_helper_wait_locked(struct os_thread_helper *oth)
Wait for a signal.
Definition: os_threading.h:695
static int os_semaphore_init(struct os_semaphore *os, int count)
Init.
Definition: os_threading.h:388
static int os_thread_init(struct os_thread *ost)
Init.
Definition: os_threading.h:312
static void os_cond_signal(struct os_cond *oc)
Signal.
Definition: os_threading.h:228
static int os_mutex_trylock(struct os_mutex *om)
Try to lock, but do not block.
Definition: os_threading.h:109
static void os_thread_helper_unlock(struct os_thread_helper *oth)
Unlock the helper.
Definition: os_threading.h:641
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:719
static int os_thread_helper_init(struct os_thread_helper *oth)
Initialize the thread helper.
Definition: os_threading.h:493
static int os_cond_init(struct os_cond *oc)
Init.
Definition: os_threading.h:213
static void os_thread_helper_destroy(struct os_thread_helper *oth)
Destroy the thread helper, externally synchronizable.
Definition: os_threading.h:612
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:655
static void os_mutex_unlock(struct os_mutex *om)
Unlock.
Definition: os_threading.h:121
static void os_mutex_destroy(struct os_mutex *om)
Clean up.
Definition: os_threading.h:133
void *(* os_run_func_t)(void *)
Run function.
Definition: os_threading.h:304
static void os_semaphore_destroy(struct os_semaphore *os)
Clean up.
Definition: os_threading.h:460
static void os_thread_helper_lock(struct os_thread_helper *oth)
Lock the helper.
Definition: os_threading.h:630
static void os_cond_destroy(struct os_cond *oc)
Clean up.
Definition: os_threading.h:274
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:439
#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:200
A wrapper around a native mutex.
Definition: os_threading.h:66
A wrapper around a native semaphore.
Definition: os_threading.h:378
All in one helper that handles locking, waiting for change and starting a thread.
Definition: os_threading.h:478
A wrapper around a native thread.
Definition: os_threading.h:295
Definition: u_worker.c:38
Header holding common defines.
Auto detect OS and certain features.