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);
241 assert(oc->initialized);
242 pthread_cond_wait(&oc->cond, &om->mutex);
253 assert(oc->initialized);
254 pthread_cond_destroy(&oc->cond);
256 oc->initialized =
false;
281typedef void *(*os_run_func_t)(
void *);
302 return pthread_create(&ost->thread, NULL, func, ptr);
315 pthread_join(ost->thread, &retval);
336#ifdef OS_THREAD_HAVE_SETNAME
337 pthread_setname_np(ost->thread, name);
366 return sem_init(&os->sem, 0, count);
390#if defined(XRT_OS_WINDOWS) && !defined(XRT_ENV_MINGW)
391 struct timespec relative;
393 pthread_win32_getabstime_np(ts, &relative);
397 if (clock_gettime(CLOCK_REALTIME, &now) < 0) {
402 uint64_t when_ns = timeout_ns + now_ns;
417 if (timeout_ns == 0) {
422 struct timespec abs_timeout;
427 sem_timedwait(&os->sem, &abs_timeout);
438 sem_destroy(&os->sem);
455 pthread_mutex_t mutex;
472 int ret = pthread_mutex_init(&oth->mutex, NULL);
477 ret = pthread_cond_init(&oth->cond, NULL);
479 pthread_mutex_destroy(&oth->mutex);
482 oth->initialized =
true;
495 pthread_mutex_lock(&oth->mutex);
497 assert(oth->initialized);
499 pthread_mutex_unlock(&oth->mutex);
503 int ret = pthread_create(&oth->thread, NULL, func, ptr);
505 pthread_mutex_unlock(&oth->mutex);
511 pthread_mutex_unlock(&oth->mutex);
527 pthread_mutex_lock(&oth->mutex);
528 assert(oth->initialized);
531 oth->running =
false;
534 pthread_cond_signal(&oth->cond);
537 pthread_mutex_unlock(&oth->mutex);
555 pthread_mutex_lock(&oth->mutex);
556 assert(oth->initialized);
560 pthread_mutex_unlock(&oth->mutex);
565 oth->running =
false;
568 pthread_cond_signal(&oth->cond);
571 pthread_mutex_unlock(&oth->mutex);
574 pthread_join(oth->thread, &retval);
589 assert(oth->initialized);
594 pthread_mutex_destroy(&oth->mutex);
595 pthread_cond_destroy(&oth->cond);
596 oth->initialized =
false;
607 pthread_mutex_lock(&oth->mutex);
618 pthread_mutex_unlock(&oth->mutex);
633 assert(oth->initialized);
634 bool ret = oth->running;
672 pthread_cond_wait(&oth->cond, &oth->mutex);
685 pthread_cond_signal(&oth->cond);
696#ifdef OS_THREAD_HAVE_SETNAME
697 pthread_setname_np(oth->thread, name);
715namespace xrt::auxiliary::os {
725 os_mutex_init(&inner_);
730 os_mutex_destroy(&inner_);
737 os_mutex_lock(&inner_);
744 return 0 == os_mutex_trylock(&inner_);
751 os_mutex_unlock(&inner_);
762 Mutex(Mutex
const &) =
delete;
763 Mutex(Mutex &&) =
delete;
765 operator=(Mutex
const &) =
delete;
767 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:649
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:493
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:388
static void os_thread_helper_signal_locked(struct os_thread_helper *oth)
Signal a waiting thread to wake up.
Definition: os_threading.h:683
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:239
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:550
static void os_thread_destroy(struct os_thread *ost)
Destruction.
Definition: os_threading.h:325
static void os_semaphore_release(struct os_semaphore *os)
Release.
Definition: os_threading.h:375
static void os_thread_join(struct os_thread *ost)
Join.
Definition: os_threading.h:311
static int os_thread_start(struct os_thread *ost, os_run_func_t func, void *ptr)
Start thread.
Definition: os_threading.h:300
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:524
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:334
static void os_thread_helper_wait_locked(struct os_thread_helper *oth)
Wait for a signal.
Definition: os_threading.h:670
static int os_semaphore_init(struct os_semaphore *os, int count)
Init.
Definition: os_threading.h:364
static int os_thread_init(struct os_thread *ost)
Init.
Definition: os_threading.h:289
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:616
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:694
static int os_thread_helper_init(struct os_thread_helper *oth)
Initialize the thread helper.
Definition: os_threading.h:468
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:587
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:630
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:281
static void os_semaphore_destroy(struct os_semaphore *os)
Clean up.
Definition: os_threading.h:436
static void os_thread_helper_lock(struct os_thread_helper *oth)
Lock the helper.
Definition: os_threading.h:605
static void os_cond_destroy(struct os_cond *oc)
Clean up.
Definition: os_threading.h:251
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:415
#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:354
All in one helper that handles locking, waiting for change and starting a thread.
Definition: os_threading.h:453
A wrapper around a native thread.
Definition: os_threading.h:272
Definition: u_worker.c:37
Header holding common defines.
Auto detect OS and certain features.