20#if defined(XRT_OS_LINUX) || defined(XRT_ENV_MINGW)
24#define OS_THREAD_HAVE_SETNAME
25#elif defined(XRT_OS_WINDOWS)
30#define OS_THREAD_HAVE_SETNAME
32#error "OS not supported"
56 pthread_mutex_t mutex;
72 assert(!om->initialized);
74 om->initialized =
true;
75 om->recursive =
false;
77 return pthread_mutex_init(&om->mutex, NULL);
88 assert(om->initialized);
89 pthread_mutex_lock(&om->mutex);
100 assert(om->initialized);
101 return pthread_mutex_trylock(&om->mutex);
112 assert(om->initialized);
113 pthread_mutex_unlock(&om->mutex);
124 assert(om->initialized);
125 assert(!om->recursive);
127 pthread_mutex_destroy(&om->mutex);
130 om->initialized =
false;
131 om->recursive =
false;
143 assert(!om->initialized);
146 om->initialized =
true;
147 om->recursive =
true;
150 pthread_mutexattr_t attr;
151 pthread_mutexattr_init(&attr);
152 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
153 int ret = pthread_mutex_init(&om->mutex, &attr);
154 pthread_mutexattr_destroy(&attr);
167 assert(om->initialized);
168 assert(om->recursive);
170 pthread_mutex_destroy(&om->mutex);
173 om->initialized =
false;
174 om->recursive =
false;
204 assert(!oc->initialized);
206 oc->initialized =
true;
208 return pthread_cond_init(&oc->cond, NULL);
219 assert(oc->initialized);
220 pthread_cond_signal(&oc->cond);
231 assert(oc->initialized);
232 return pthread_cond_broadcast(&oc->cond);
253 assert(oc->initialized);
254 pthread_cond_wait(&oc->cond, &om->mutex);
265 assert(oc->initialized);
266 pthread_cond_destroy(&oc->cond);
268 oc->initialized =
false;
293typedef void *(*os_run_func_t)(
void *);
314 return pthread_create(&ost->thread, NULL, func, ptr);
327 pthread_join(ost->thread, &retval);
348#ifdef OS_THREAD_HAVE_SETNAME
349 pthread_setname_np(ost->thread, name);
378 return sem_init(&os->sem, 0, count);
402#if defined(XRT_OS_WINDOWS) && !defined(XRT_ENV_MINGW)
403 struct timespec relative;
405 pthread_win32_getabstime_np(ts, &relative);
409 if (clock_gettime(CLOCK_REALTIME, &now) < 0) {
414 uint64_t when_ns = timeout_ns + now_ns;
429 if (timeout_ns == 0) {
434 struct timespec abs_timeout;
439 sem_timedwait(&os->sem, &abs_timeout);
450 sem_destroy(&os->sem);
467 pthread_mutex_t mutex;
484 int ret = pthread_mutex_init(&oth->mutex, NULL);
489 ret = pthread_cond_init(&oth->cond, NULL);
491 pthread_mutex_destroy(&oth->mutex);
494 oth->initialized =
true;
507 pthread_mutex_lock(&oth->mutex);
509 assert(oth->initialized);
511 pthread_mutex_unlock(&oth->mutex);
515 int ret = pthread_create(&oth->thread, NULL, func, ptr);
517 pthread_mutex_unlock(&oth->mutex);
523 pthread_mutex_unlock(&oth->mutex);
539 pthread_mutex_lock(&oth->mutex);
540 assert(oth->initialized);
543 oth->running =
false;
546 pthread_cond_signal(&oth->cond);
549 pthread_mutex_unlock(&oth->mutex);
567 pthread_mutex_lock(&oth->mutex);
568 assert(oth->initialized);
572 pthread_mutex_unlock(&oth->mutex);
577 oth->running =
false;
580 pthread_cond_signal(&oth->cond);
583 pthread_mutex_unlock(&oth->mutex);
586 pthread_join(oth->thread, &retval);
601 assert(oth->initialized);
606 pthread_mutex_destroy(&oth->mutex);
607 pthread_cond_destroy(&oth->cond);
608 oth->initialized =
false;
619 pthread_mutex_lock(&oth->mutex);
630 pthread_mutex_unlock(&oth->mutex);
645 assert(oth->initialized);
646 bool ret = oth->running;
684 pthread_cond_wait(&oth->cond, &oth->mutex);
697 pthread_cond_signal(&oth->cond);
708#ifdef OS_THREAD_HAVE_SETNAME
709 pthread_setname_np(oth->thread, name);
727namespace xrt::auxiliary::os {
737 os_mutex_init(&inner_);
742 os_mutex_destroy(&inner_);
749 os_mutex_lock(&inner_);
756 return 0 == os_mutex_trylock(&inner_);
763 os_mutex_unlock(&inner_);
774 Mutex(Mutex
const &) =
delete;
775 Mutex(Mutex &&) =
delete;
777 operator=(Mutex
const &) =
delete;
779 operator=(Mutex &&) =
delete;
static void os_mutex_recursive_destroy(struct os_mutex *om)
Clean up.
Definition: os_threading.h:165
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:661
static int os_cond_broadcast(struct os_cond *oc)
Broadcast (signal to multiple threads).
Definition: os_threading.h:229
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:505
static int os_mutex_init(struct os_mutex *om)
Init.
Definition: os_threading.h:70
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:400
static void os_thread_helper_signal_locked(struct os_thread_helper *oth)
Signal a waiting thread to wake up.
Definition: os_threading.h:695
static void os_mutex_lock(struct os_mutex *om)
Lock.
Definition: os_threading.h:86
static void os_cond_wait(struct os_cond *oc, struct os_mutex *om)
Wait.
Definition: os_threading.h:251
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:562
static void os_thread_destroy(struct os_thread *ost)
Destruction.
Definition: os_threading.h:337
static void os_semaphore_release(struct os_semaphore *os)
Release.
Definition: os_threading.h:387
static void os_thread_join(struct os_thread *ost)
Join.
Definition: os_threading.h:323
static int os_thread_start(struct os_thread *ost, os_run_func_t func, void *ptr)
Start thread.
Definition: os_threading.h:312
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:536
static int os_mutex_recursive_init(struct os_mutex *om)
Init.
Definition: os_threading.h:141
static void os_thread_name(struct os_thread *ost, const char *name)
Make a best effort to name our thread.
Definition: os_threading.h:346
static void os_thread_helper_wait_locked(struct os_thread_helper *oth)
Wait for a signal.
Definition: os_threading.h:682
static int os_semaphore_init(struct os_semaphore *os, int count)
Init.
Definition: os_threading.h:376
static int os_thread_init(struct os_thread *ost)
Init.
Definition: os_threading.h:301
static void os_cond_signal(struct os_cond *oc)
Signal.
Definition: os_threading.h:217
static int os_mutex_trylock(struct os_mutex *om)
Try to lock, but do not block.
Definition: os_threading.h:98
static void os_thread_helper_unlock(struct os_thread_helper *oth)
Unlock the helper.
Definition: os_threading.h:628
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:706
static int os_thread_helper_init(struct os_thread_helper *oth)
Initialize the thread helper.
Definition: os_threading.h:480
static int os_cond_init(struct os_cond *oc)
Init.
Definition: os_threading.h:202
static void os_thread_helper_destroy(struct os_thread_helper *oth)
Destroy the thread helper, externally synchronizable.
Definition: os_threading.h:599
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:642
static void os_mutex_unlock(struct os_mutex *om)
Unlock.
Definition: os_threading.h:110
static void os_mutex_destroy(struct os_mutex *om)
Clean up.
Definition: os_threading.h:122
void *(* os_run_func_t)(void *)
Run function.
Definition: os_threading.h:293
static void os_semaphore_destroy(struct os_semaphore *os)
Clean up.
Definition: os_threading.h:448
static void os_thread_helper_lock(struct os_thread_helper *oth)
Lock the helper.
Definition: os_threading.h:617
static void os_cond_destroy(struct os_cond *oc)
Clean up.
Definition: os_threading.h:263
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:427
#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:189
A wrapper around a native mutex.
Definition: os_threading.h:55
A wrapper around a native semaphore.
Definition: os_threading.h:366
All in one helper that handles locking, waiting for change and starting a thread.
Definition: os_threading.h:465
A wrapper around a native thread.
Definition: os_threading.h:284
Definition: u_worker.c:37
Header holding common defines.
Auto detect OS and certain features.