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;
204 pthread_rwlock_t rwlock;
219 assert(!orwl->initialized);
221 orwl->initialized =
true;
223 return pthread_rwlock_init(&orwl->rwlock, NULL);
234 assert(orwl->initialized);
235 pthread_rwlock_rdlock(&orwl->rwlock);
246 assert(orwl->initialized);
247 pthread_rwlock_wrlock(&orwl->rwlock);
258 assert(orwl->initialized);
259 pthread_rwlock_unlock(&orwl->rwlock);
270 assert(orwl->initialized);
272 pthread_rwlock_destroy(&orwl->rwlock);
275 orwl->initialized =
false;
305 assert(!oc->initialized);
307 oc->initialized =
true;
309 return pthread_cond_init(&oc->cond, NULL);
320 assert(oc->initialized);
321 pthread_cond_signal(&oc->cond);
332 assert(oc->initialized);
333 return pthread_cond_broadcast(&oc->cond);
354 assert(oc->initialized);
355 pthread_cond_wait(&oc->cond, &om->mutex);
366 assert(oc->initialized);
367 pthread_cond_destroy(&oc->cond);
369 oc->initialized =
false;
394typedef void *(*os_run_func_t)(
void *);
415 return pthread_create(&ost->thread, NULL, func, ptr);
428 pthread_join(ost->thread, &retval);
447#if defined(XRT_OS_LINUX) || defined(XRT_OS_OSX)
448 return (int64_t)sysconf(_SC_NPROCESSORS_ONLN);
449#elif defined(XRT_OS_WINDOWS)
451 GetSystemInfo(&sysinfo);
452 return (int64_t)sysinfo.dwNumberOfProcessors;
454#error "OS not supported"
469#ifdef OS_THREAD_HAVE_SETNAME
470 pthread_setname_np(pthread_self(), name);
476#ifdef OS_THREAD_HAVE_SEMAPHORE
499 return sem_init(&os->sem, 0, count);
523#if defined(XRT_OS_WINDOWS) && !defined(XRT_ENV_MINGW)
524 struct timespec relative;
526 pthread_win32_getabstime_np(ts, &relative);
530 if (clock_gettime(CLOCK_REALTIME, &now) < 0) {
535 uint64_t when_ns = timeout_ns + now_ns;
550 if (timeout_ns == 0) {
555 struct timespec abs_timeout;
556 if (os_semaphore_get_realtime_clock(&abs_timeout, timeout_ns) == -1) {
560 sem_timedwait(&os->sem, &abs_timeout);
571 sem_destroy(&os->sem);
589 pthread_mutex_t mutex;
606 int ret = pthread_mutex_init(&oth->mutex, NULL);
611 ret = pthread_cond_init(&oth->cond, NULL);
613 pthread_mutex_destroy(&oth->mutex);
616 oth->initialized =
true;
629 pthread_mutex_lock(&oth->mutex);
631 assert(oth->initialized);
633 pthread_mutex_unlock(&oth->mutex);
637 int ret = pthread_create(&oth->thread, NULL, func, ptr);
639 pthread_mutex_unlock(&oth->mutex);
645 pthread_mutex_unlock(&oth->mutex);
661 pthread_mutex_lock(&oth->mutex);
662 assert(oth->initialized);
665 oth->running =
false;
668 pthread_cond_signal(&oth->cond);
671 pthread_mutex_unlock(&oth->mutex);
689 pthread_mutex_lock(&oth->mutex);
690 assert(oth->initialized);
694 pthread_mutex_unlock(&oth->mutex);
699 oth->running =
false;
702 pthread_cond_signal(&oth->cond);
705 pthread_mutex_unlock(&oth->mutex);
708 pthread_join(oth->thread, &retval);
723 assert(oth->initialized);
725 os_thread_helper_stop_and_wait(oth);
728 pthread_mutex_destroy(&oth->mutex);
729 pthread_cond_destroy(&oth->cond);
730 oth->initialized =
false;
741 pthread_mutex_lock(&oth->mutex);
752 pthread_mutex_unlock(&oth->mutex);
766 os_thread_helper_lock(oth);
767 assert(oth->initialized);
768 bool ret = oth->running;
769 os_thread_helper_unlock(oth);
806 pthread_cond_wait(&oth->cond, &oth->mutex);
819 pthread_cond_signal(&oth->cond);
833namespace xrt::auxiliary::os {
843 os_mutex_init(&inner_);
848 os_mutex_destroy(&inner_);
855 os_mutex_lock(&inner_);
862 return 0 == os_mutex_trylock(&inner_);
869 os_mutex_unlock(&inner_);
880 Mutex(Mutex
const &) =
delete;
881 Mutex(Mutex &&) =
delete;
883 operator=(Mutex
const &) =
delete;
885 operator=(Mutex &&) =
delete;
static void os_rwlock_destroy(struct os_rwlock *orwl)
Clean up.
Definition os_threading.h:268
static int os_rwlock_init(struct os_rwlock *orwl)
Init.
Definition os_threading.h:217
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:783
static int os_cond_broadcast(struct os_cond *oc)
Broadcast (signal to multiple threads).
Definition os_threading.h:330
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:627
static void os_rwlock_unlock(struct os_rwlock *orwl)
Unlock, from either a read or a write lock.
Definition os_threading.h:256
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:521
static void os_thread_helper_signal_locked(struct os_thread_helper *oth)
Signal a waiting thread to wake up.
Definition os_threading.h:817
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:352
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:684
static void os_thread_destroy(struct os_thread *ost)
Destruction.
Definition os_threading.h:438
static void os_semaphore_release(struct os_semaphore *os)
Release.
Definition os_threading.h:508
static void os_thread_name_self(const char *name)
Make a best effort to name the current thread.
Definition os_threading.h:467
static void os_thread_join(struct os_thread *ost)
Join.
Definition os_threading.h:424
static int os_thread_start(struct os_thread *ost, os_run_func_t func, void *ptr)
Start thread.
Definition os_threading.h:413
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:658
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:804
static int os_semaphore_init(struct os_semaphore *os, int count)
Init.
Definition os_threading.h:497
static int os_thread_init(struct os_thread *ost)
Init.
Definition os_threading.h:402
static void os_cond_signal(struct os_cond *oc)
Signal.
Definition os_threading.h:318
static int64_t os_hardware_thread_count(void)
Gets the number of hardware threads available.
Definition os_threading.h:445
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:750
static int os_thread_helper_init(struct os_thread_helper *oth)
Initialize the thread helper.
Definition os_threading.h:602
static int os_cond_init(struct os_cond *oc)
Init.
Definition os_threading.h:303
static void os_rwlock_write_lock(struct os_rwlock *orwl)
Lock for writing, waits for all readers and writers to unlock.
Definition os_threading.h:244
static void os_thread_helper_destroy(struct os_thread_helper *oth)
Destroy the thread helper, externally synchronizable.
Definition os_threading.h:721
static void os_rwlock_read_lock(struct os_rwlock *orwl)
Lock for reading, any number of readers can hold it at the same time.
Definition os_threading.h:232
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:764
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:569
static void os_thread_helper_lock(struct os_thread_helper *oth)
Lock the helper.
Definition os_threading.h:739
static void os_cond_destroy(struct os_cond *oc)
Clean up.
Definition os_threading.h:364
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:548
#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:290
A wrapper around a native mutex.
Definition os_threading.h:69
A wrapper around a native read-write lock.
Definition os_threading.h:203
A wrapper around a native semaphore.
Definition os_threading.h:487
All in one helper that handles locking, waiting for change and starting a thread.
Definition os_threading.h:587
A wrapper around a native thread.
Definition os_threading.h:385
Header holding common defines.
Auto detect OS and certain features.
A minimal way to include Windows.h.