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)
364 GetSystemInfo(&sysinfo);
365 return (int64_t)sysinfo.dwNumberOfProcessors;
367#error "OS not supported"
382#ifdef OS_THREAD_HAVE_SETNAME
383 pthread_setname_np(pthread_self(), name);
389#ifdef OS_THREAD_HAVE_SEMAPHORE
412 return sem_init(&os->sem, 0, count);
436#if defined(XRT_OS_WINDOWS) && !defined(XRT_ENV_MINGW)
437 struct timespec relative;
439 pthread_win32_getabstime_np(ts, &relative);
443 if (clock_gettime(CLOCK_REALTIME, &now) < 0) {
448 uint64_t when_ns = timeout_ns + now_ns;
463 if (timeout_ns == 0) {
468 struct timespec abs_timeout;
469 if (os_semaphore_get_realtime_clock(&abs_timeout, timeout_ns) == -1) {
473 sem_timedwait(&os->sem, &abs_timeout);
484 sem_destroy(&os->sem);
502 pthread_mutex_t mutex;
519 int ret = pthread_mutex_init(&oth->mutex, NULL);
524 ret = pthread_cond_init(&oth->cond, NULL);
526 pthread_mutex_destroy(&oth->mutex);
529 oth->initialized =
true;
542 pthread_mutex_lock(&oth->mutex);
544 assert(oth->initialized);
546 pthread_mutex_unlock(&oth->mutex);
550 int ret = pthread_create(&oth->thread, NULL, func, ptr);
552 pthread_mutex_unlock(&oth->mutex);
558 pthread_mutex_unlock(&oth->mutex);
574 pthread_mutex_lock(&oth->mutex);
575 assert(oth->initialized);
578 oth->running =
false;
581 pthread_cond_signal(&oth->cond);
584 pthread_mutex_unlock(&oth->mutex);
602 pthread_mutex_lock(&oth->mutex);
603 assert(oth->initialized);
607 pthread_mutex_unlock(&oth->mutex);
612 oth->running =
false;
615 pthread_cond_signal(&oth->cond);
618 pthread_mutex_unlock(&oth->mutex);
621 pthread_join(oth->thread, &retval);
636 assert(oth->initialized);
638 os_thread_helper_stop_and_wait(oth);
641 pthread_mutex_destroy(&oth->mutex);
642 pthread_cond_destroy(&oth->cond);
643 oth->initialized =
false;
654 pthread_mutex_lock(&oth->mutex);
665 pthread_mutex_unlock(&oth->mutex);
679 os_thread_helper_lock(oth);
680 assert(oth->initialized);
681 bool ret = oth->running;
682 os_thread_helper_unlock(oth);
719 pthread_cond_wait(&oth->cond, &oth->mutex);
732 pthread_cond_signal(&oth->cond);
746namespace xrt::auxiliary::os {
756 os_mutex_init(&inner_);
761 os_mutex_destroy(&inner_);
768 os_mutex_lock(&inner_);
775 return 0 == os_mutex_trylock(&inner_);
782 os_mutex_unlock(&inner_);
793 Mutex(Mutex
const &) =
delete;
794 Mutex(Mutex &&) =
delete;
796 operator=(Mutex
const &) =
delete;
798 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:696
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:540
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:434
static void os_thread_helper_signal_locked(struct os_thread_helper *oth)
Signal a waiting thread to wake up.
Definition os_threading.h:730
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:597
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:421
static void os_thread_name_self(const char *name)
Make a best effort to name the current thread.
Definition os_threading.h:380
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:571
static int os_mutex_recursive_init(struct os_mutex *om)
Init.
Definition os_threading.h:155
static void os_thread_helper_wait_locked(struct os_thread_helper *oth)
Wait for a signal.
Definition os_threading.h:717
static int os_semaphore_init(struct os_semaphore *os, int count)
Init.
Definition os_threading.h:410
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:663
static int os_thread_helper_init(struct os_thread_helper *oth)
Initialize the thread helper.
Definition os_threading.h:515
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:634
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:677
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
static void os_semaphore_destroy(struct os_semaphore *os)
Clean up.
Definition os_threading.h:482
static void os_thread_helper_lock(struct os_thread_helper *oth)
Lock the helper.
Definition os_threading.h:652
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:461
#define U_ZERO(PTR)
Zeroes the correct amount of memory based on the type pointed-to by the argument.
Definition u_misc.h:68
#define XRT_STRUCT_INIT
Very small default init for structs that works in both C and C++.
Definition xrt_compiler.h:311
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:400
All in one helper that handles locking, waiting for change and starting a thread.
Definition os_threading.h:500
A wrapper around a native thread.
Definition os_threading.h:298
Header holding common defines.
Auto detect OS and certain features.
A minimal way to include Windows.h.