aboutsummaryrefslogtreecommitdiffstats
path: root/src/zix
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-01-16 19:35:21 +0100
committerDavid Robillard <d@drobilla.net>2021-01-16 19:35:21 +0100
commit8952dde02d9d6761ae0cae033600f4a220c8e075 (patch)
tree5715888d4322daea9d29fc2f174d2a623d6085eb /src/zix
parent9ab6e66c6ea7230f716b74d62c03fc5d19f56abe (diff)
downloadjalv-8952dde02d9d6761ae0cae033600f4a220c8e075.tar.gz
jalv-8952dde02d9d6761ae0cae033600f4a220c8e075.tar.bz2
jalv-8952dde02d9d6761ae0cae033600f4a220c8e075.zip
Update zix
Diffstat (limited to 'src/zix')
-rw-r--r--src/zix/common.h109
-rw-r--r--src/zix/ring.c225
-rw-r--r--src/zix/ring.h6
-rw-r--r--src/zix/sem.h89
-rw-r--r--src/zix/thread.h73
5 files changed, 257 insertions, 245 deletions
diff --git a/src/zix/common.h b/src/zix/common.h
index cf07451..d47586c 100644
--- a/src/zix/common.h
+++ b/src/zix/common.h
@@ -1,5 +1,5 @@
/*
- Copyright 2016 David Robillard <d@drobilla.net>
+ Copyright 2016-2020 David Robillard <d@drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
@@ -25,41 +25,37 @@
*/
/** @cond */
-#ifdef ZIX_SHARED
-# ifdef _WIN32
-# define ZIX_LIB_IMPORT __declspec(dllimport)
-# define ZIX_LIB_EXPORT __declspec(dllexport)
-# else
-# define ZIX_LIB_IMPORT __attribute__((visibility("default")))
-# define ZIX_LIB_EXPORT __attribute__((visibility("default")))
-# endif
-# ifdef ZIX_INTERNAL
-# define ZIX_API ZIX_LIB_EXPORT
-# else
-# define ZIX_API ZIX_LIB_IMPORT
-# endif
-# define ZIX_PRIVATE static
-#elif defined(ZIX_INLINE)
-# define ZIX_API static inline
-# define ZIX_PRIVATE static inline
+#if defined(_WIN32) && !defined(ZIX_STATIC) && defined(ZIX_INTERNAL)
+# define ZIX_API __declspec(dllexport)
+#elif defined(_WIN32) && !defined(ZIX_STATIC)
+# define ZIX_API __declspec(dllimport)
+#elif defined(__GNUC__)
+# define ZIX_API __attribute__((visibility("default")))
#else
-# define ZIX_API
-# define ZIX_PRIVATE static
+# define ZIX_API
#endif
#ifdef __GNUC__
-# define ZIX_PURE_FUNC __attribute__((pure))
-# define ZIX_CONST_FUNC __attribute__((const))
-# define ZIX_MALLOC_FUNC __attribute__((malloc))
+# define ZIX_PURE_FUNC __attribute__((pure))
+# define ZIX_CONST_FUNC __attribute__((const))
+# define ZIX_MALLOC_FUNC __attribute__((malloc))
#else
-# define ZIX_PURE_FUNC
-# define ZIX_CONST_FUNC
-# define ZIX_MALLOC_FUNC
+# define ZIX_PURE_FUNC
+# define ZIX_CONST_FUNC
+# define ZIX_MALLOC_FUNC
#endif
-#define ZIX_PURE_API ZIX_API ZIX_PURE_FUNC
-#define ZIX_CONST_API ZIX_API ZIX_CONST_FUNC
-#define ZIX_MALLOC_API ZIX_API ZIX_MALLOC_FUNC
+#define ZIX_PURE_API \
+ ZIX_API \
+ ZIX_PURE_FUNC
+
+#define ZIX_CONST_API \
+ ZIX_API \
+ ZIX_CONST_FUNC
+
+#define ZIX_MALLOC_API \
+ ZIX_API \
+ ZIX_MALLOC_FUNC
/** @endcond */
@@ -68,43 +64,50 @@ extern "C" {
#endif
#ifdef __GNUC__
-#define ZIX_LOG_FUNC(fmt, arg1) __attribute__((format(printf, fmt, arg1)))
+# define ZIX_LOG_FUNC(fmt, arg1) __attribute__((format(printf, fmt, arg1)))
#else
-#define ZIX_LOG_FUNC(fmt, arg1)
+# define ZIX_LOG_FUNC(fmt, arg1)
#endif
// Unused parameter macro to suppresses warnings and make it impossible to use
#if defined(__cplusplus)
-# define ZIX_UNUSED(name)
+# define ZIX_UNUSED(name)
#elif defined(__GNUC__)
-# define ZIX_UNUSED(name) name##_unused __attribute__((__unused__))
+# define ZIX_UNUSED(name) name##_unused __attribute__((__unused__))
#else
-# define ZIX_UNUSED(name) name
+# define ZIX_UNUSED(name) name
#endif
typedef enum {
- ZIX_STATUS_SUCCESS,
- ZIX_STATUS_ERROR,
- ZIX_STATUS_NO_MEM,
- ZIX_STATUS_NOT_FOUND,
- ZIX_STATUS_EXISTS,
- ZIX_STATUS_BAD_ARG,
- ZIX_STATUS_BAD_PERMS
+ ZIX_STATUS_SUCCESS,
+ ZIX_STATUS_ERROR,
+ ZIX_STATUS_NO_MEM,
+ ZIX_STATUS_NOT_FOUND,
+ ZIX_STATUS_EXISTS,
+ ZIX_STATUS_BAD_ARG,
+ ZIX_STATUS_BAD_PERMS
} ZixStatus;
static inline const char*
zix_strerror(const ZixStatus status)
{
- switch (status) {
- case ZIX_STATUS_SUCCESS: return "Success";
- case ZIX_STATUS_ERROR: return "Unknown error";
- case ZIX_STATUS_NO_MEM: return "Out of memory";
- case ZIX_STATUS_NOT_FOUND: return "Not found";
- case ZIX_STATUS_EXISTS: return "Exists";
- case ZIX_STATUS_BAD_ARG: return "Bad argument";
- case ZIX_STATUS_BAD_PERMS: return "Bad permissions";
- }
- return "Unknown error";
+ switch (status) {
+ case ZIX_STATUS_SUCCESS:
+ return "Success";
+ case ZIX_STATUS_ERROR:
+ return "Unknown error";
+ case ZIX_STATUS_NO_MEM:
+ return "Out of memory";
+ case ZIX_STATUS_NOT_FOUND:
+ return "Not found";
+ case ZIX_STATUS_EXISTS:
+ return "Exists";
+ case ZIX_STATUS_BAD_ARG:
+ return "Bad argument";
+ case ZIX_STATUS_BAD_PERMS:
+ return "Bad permissions";
+ }
+ return "Unknown error";
}
/**
@@ -129,7 +132,7 @@ typedef void (*ZixDestroyFunc)(void* ptr);
*/
#ifdef __cplusplus
-} /* extern "C" */
+} /* extern "C" */
#endif
-#endif /* ZIX_COMMON_H */
+#endif /* ZIX_COMMON_H */
diff --git a/src/zix/ring.c b/src/zix/ring.c
index e728a3f..4a4692f 100644
--- a/src/zix/ring.c
+++ b/src/zix/ring.c
@@ -1,5 +1,5 @@
/*
- Copyright 2011 David Robillard <d@drobilla.net>
+ Copyright 2011-2020 David Robillard <d@drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
@@ -20,198 +20,205 @@
#include <string.h>
#ifdef HAVE_MLOCK
-# include <sys/mman.h>
-# define ZIX_MLOCK(ptr, size) mlock((ptr), (size))
+# include <sys/mman.h>
+# define ZIX_MLOCK(ptr, size) mlock((ptr), (size))
#elif defined(_WIN32)
-# include <windows.h>
-# define ZIX_MLOCK(ptr, size) VirtualLock((ptr), (size))
+# include <windows.h>
+# define ZIX_MLOCK(ptr, size) VirtualLock((ptr), (size))
#else
-# pragma message("warning: No memory locking, possible RT violations")
-# define ZIX_MLOCK(ptr, size)
+# pragma message("warning: No memory locking, possible RT violations")
+# define ZIX_MLOCK(ptr, size)
#endif
#if defined(_MSC_VER)
-# include <windows.h>
-# define ZIX_READ_BARRIER() MemoryBarrier()
-# define ZIX_WRITE_BARRIER() MemoryBarrier()
+# include <windows.h>
+# define ZIX_READ_BARRIER() MemoryBarrier()
+# define ZIX_WRITE_BARRIER() MemoryBarrier()
#elif defined(__GNUC__)
-# define ZIX_READ_BARRIER() __atomic_thread_fence(__ATOMIC_ACQUIRE)
-# define ZIX_WRITE_BARRIER() __atomic_thread_fence(__ATOMIC_RELEASE)
+# define ZIX_READ_BARRIER() __atomic_thread_fence(__ATOMIC_ACQUIRE)
+# define ZIX_WRITE_BARRIER() __atomic_thread_fence(__ATOMIC_RELEASE)
#else
-# pragma message("warning: No memory barriers, possible SMP bugs")
-# define ZIX_READ_BARRIER()
-# define ZIX_WRITE_BARRIER()
+# pragma message("warning: No memory barriers, possible SMP bugs")
+# define ZIX_READ_BARRIER()
+# define ZIX_WRITE_BARRIER()
#endif
struct ZixRingImpl {
- uint32_t write_head; ///< Read index into buf
- uint32_t read_head; ///< Write index into buf
- uint32_t size; ///< Size (capacity) in bytes
- uint32_t size_mask; ///< Mask for fast modulo
- char* buf; ///< Contents
+ uint32_t write_head; ///< Read index into buf
+ uint32_t read_head; ///< Write index into buf
+ uint32_t size; ///< Size (capacity) in bytes
+ uint32_t size_mask; ///< Mask for fast modulo
+ char* buf; ///< Contents
};
static inline uint32_t
next_power_of_two(uint32_t size)
{
- // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
- size--;
- size |= size >> 1u;
- size |= size >> 2u;
- size |= size >> 4u;
- size |= size >> 8u;
- size |= size >> 16u;
- size++;
- return size;
+ // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
+ size--;
+ size |= size >> 1u;
+ size |= size >> 2u;
+ size |= size >> 4u;
+ size |= size >> 8u;
+ size |= size >> 16u;
+ size++;
+ return size;
}
ZixRing*
zix_ring_new(uint32_t size)
{
- ZixRing* ring = (ZixRing*)malloc(sizeof(ZixRing));
- ring->write_head = 0;
- ring->read_head = 0;
- ring->size = next_power_of_two(size);
- ring->size_mask = ring->size - 1;
- ring->buf = (char*)malloc(ring->size);
- return ring;
+ ZixRing* ring = (ZixRing*)malloc(sizeof(ZixRing));
+ ring->write_head = 0;
+ ring->read_head = 0;
+ ring->size = next_power_of_two(size);
+ ring->size_mask = ring->size - 1;
+ ring->buf = (char*)malloc(ring->size);
+ return ring;
}
void
zix_ring_free(ZixRing* ring)
{
- free(ring->buf);
- free(ring);
+ if (ring) {
+ free(ring->buf);
+ free(ring);
+ }
}
void
zix_ring_mlock(ZixRing* ring)
{
- ZIX_MLOCK(ring, sizeof(ZixRing));
- ZIX_MLOCK(ring->buf, ring->size);
+ ZIX_MLOCK(ring, sizeof(ZixRing));
+ ZIX_MLOCK(ring->buf, ring->size);
}
void
zix_ring_reset(ZixRing* ring)
{
- ring->write_head = 0;
- ring->read_head = 0;
+ ring->write_head = 0;
+ ring->read_head = 0;
}
static inline uint32_t
read_space_internal(const ZixRing* ring, uint32_t r, uint32_t w)
{
- if (r < w) {
- return w - r;
- } else {
- return (w - r + ring->size) & ring->size_mask;
- }
+ if (r < w) {
+ return w - r;
+ }
+
+ return (w - r + ring->size) & ring->size_mask;
}
uint32_t
zix_ring_read_space(const ZixRing* ring)
{
- return read_space_internal(ring, ring->read_head, ring->write_head);
+ return read_space_internal(ring, ring->read_head, ring->write_head);
}
static inline uint32_t
write_space_internal(const ZixRing* ring, uint32_t r, uint32_t w)
{
- if (r == w) {
- return ring->size - 1;
- } else if (r < w) {
- return ((r - w + ring->size) & ring->size_mask) - 1;
- } else {
- return (r - w) - 1;
- }
+ if (r == w) {
+ return ring->size - 1;
+ }
+
+ if (r < w) {
+ return ((r - w + ring->size) & ring->size_mask) - 1;
+ }
+
+ return (r - w) - 1;
}
uint32_t
zix_ring_write_space(const ZixRing* ring)
{
- return write_space_internal(ring, ring->read_head, ring->write_head);
+ return write_space_internal(ring, ring->read_head, ring->write_head);
}
uint32_t
zix_ring_capacity(const ZixRing* ring)
{
- return ring->size - 1;
+ return ring->size - 1;
}
static inline uint32_t
-peek_internal(const ZixRing* ring, uint32_t r, uint32_t w,
- uint32_t size, void* dst)
+peek_internal(const ZixRing* ring,
+ uint32_t r,
+ uint32_t w,
+ uint32_t size,
+ void* dst)
{
- if (read_space_internal(ring, r, w) < size) {
- return 0;
- }
-
- if (r + size < ring->size) {
- memcpy(dst, &ring->buf[r], size);
- } else {
- const uint32_t first_size = ring->size - r;
- memcpy(dst, &ring->buf[r], first_size);
- memcpy((char*)dst + first_size, &ring->buf[0], size - first_size);
- }
-
- return size;
+ if (read_space_internal(ring, r, w) < size) {
+ return 0;
+ }
+
+ if (r + size < ring->size) {
+ memcpy(dst, &ring->buf[r], size);
+ } else {
+ const uint32_t first_size = ring->size - r;
+ memcpy(dst, &ring->buf[r], first_size);
+ memcpy((char*)dst + first_size, &ring->buf[0], size - first_size);
+ }
+
+ return size;
}
uint32_t
zix_ring_peek(ZixRing* ring, void* dst, uint32_t size)
{
- return peek_internal(ring, ring->read_head, ring->write_head, size, dst);
+ return peek_internal(ring, ring->read_head, ring->write_head, size, dst);
}
uint32_t
zix_ring_read(ZixRing* ring, void* dst, uint32_t size)
{
- const uint32_t r = ring->read_head;
- const uint32_t w = ring->write_head;
+ const uint32_t r = ring->read_head;
+ const uint32_t w = ring->write_head;
- if (peek_internal(ring, r, w, size, dst)) {
- ZIX_READ_BARRIER();
- ring->read_head = (r + size) & ring->size_mask;
- return size;
- }
+ if (peek_internal(ring, r, w, size, dst)) {
+ ZIX_READ_BARRIER();
+ ring->read_head = (r + size) & ring->size_mask;
+ return size;
+ }
- return 0;
+ return 0;
}
uint32_t
zix_ring_skip(ZixRing* ring, uint32_t size)
{
- const uint32_t r = ring->read_head;
- const uint32_t w = ring->write_head;
- if (read_space_internal(ring, r, w) < size) {
- return 0;
- }
-
- ZIX_READ_BARRIER();
- ring->read_head = (r + size) & ring->size_mask;
- return size;
+ const uint32_t r = ring->read_head;
+ const uint32_t w = ring->write_head;
+ if (read_space_internal(ring, r, w) < size) {
+ return 0;
+ }
+
+ ZIX_READ_BARRIER();
+ ring->read_head = (r + size) & ring->size_mask;
+ return size;
}
uint32_t
zix_ring_write(ZixRing* ring, const void* src, uint32_t size)
{
- const uint32_t r = ring->read_head;
- const uint32_t w = ring->write_head;
- if (write_space_internal(ring, r, w) < size) {
- return 0;
- }
-
- if (w + size <= ring->size) {
- memcpy(&ring->buf[w], src, size);
- ZIX_WRITE_BARRIER();
- ring->write_head = (w + size) & ring->size_mask;
- } else {
- const uint32_t this_size = ring->size - w;
- memcpy(&ring->buf[w], src, this_size);
- memcpy(&ring->buf[0], (const char*)src + this_size, size - this_size);
- ZIX_WRITE_BARRIER();
- ring->write_head = size - this_size;
- }
-
- return size;
+ const uint32_t r = ring->read_head;
+ const uint32_t w = ring->write_head;
+ if (write_space_internal(ring, r, w) < size) {
+ return 0;
+ }
+
+ if (w + size <= ring->size) {
+ memcpy(&ring->buf[w], src, size);
+ ZIX_WRITE_BARRIER();
+ ring->write_head = (w + size) & ring->size_mask;
+ } else {
+ const uint32_t this_size = ring->size - w;
+ memcpy(&ring->buf[w], src, this_size);
+ memcpy(&ring->buf[0], (const char*)src + this_size, size - this_size);
+ ZIX_WRITE_BARRIER();
+ ring->write_head = size - this_size;
+ }
+
+ return size;
}
diff --git a/src/zix/ring.h b/src/zix/ring.h
index 99437f8..a375cac 100644
--- a/src/zix/ring.h
+++ b/src/zix/ring.h
@@ -1,5 +1,5 @@
/*
- Copyright 2011-2014 David Robillard <d@drobilla.net>
+ Copyright 2011-2020 David Robillard <d@drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
@@ -135,7 +135,7 @@ zix_ring_write(ZixRing* ring, const void* src, uint32_t size);
*/
#ifdef __cplusplus
-} /* extern "C" */
+} /* extern "C" */
#endif
-#endif /* ZIX_RING_H */
+#endif /* ZIX_RING_H */
diff --git a/src/zix/sem.h b/src/zix/sem.h
index 7ced8dc..d42fd08 100644
--- a/src/zix/sem.h
+++ b/src/zix/sem.h
@@ -1,5 +1,5 @@
/*
- Copyright 2012-2014 David Robillard <d@drobilla.net>
+ Copyright 2012-2020 David Robillard <d@drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
@@ -20,13 +20,13 @@
#include "zix/common.h"
#ifdef __APPLE__
-# include <mach/mach.h>
+# include <mach/mach.h>
#elif defined(_WIN32)
-# include <limits.h>
-# include <windows.h>
+# include <limits.h>
+# include <windows.h>
#else
-# include <errno.h>
-# include <semaphore.h>
+# include <errno.h>
+# include <semaphore.h>
#endif
#ifdef __cplusplus
@@ -104,126 +104,127 @@ zix_sem_try_wait(ZixSem* sem);
#ifdef __APPLE__
struct ZixSemImpl {
- semaphore_t sem;
+ semaphore_t sem;
};
static inline ZixStatus
zix_sem_init(ZixSem* sem, unsigned val)
{
- return semaphore_create(mach_task_self(), &sem->sem, SYNC_POLICY_FIFO, val)
- ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS;
+ return semaphore_create(mach_task_self(), &sem->sem, SYNC_POLICY_FIFO, val)
+ ? ZIX_STATUS_ERROR
+ : ZIX_STATUS_SUCCESS;
}
static inline void
zix_sem_destroy(ZixSem* sem)
{
- semaphore_destroy(mach_task_self(), sem->sem);
+ semaphore_destroy(mach_task_self(), sem->sem);
}
static inline void
zix_sem_post(ZixSem* sem)
{
- semaphore_signal(sem->sem);
+ semaphore_signal(sem->sem);
}
static inline ZixStatus
zix_sem_wait(ZixSem* sem)
{
- if (semaphore_wait(sem->sem) != KERN_SUCCESS) {
- return ZIX_STATUS_ERROR;
- }
- return ZIX_STATUS_SUCCESS;
+ if (semaphore_wait(sem->sem) != KERN_SUCCESS) {
+ return ZIX_STATUS_ERROR;
+ }
+ return ZIX_STATUS_SUCCESS;
}
static inline bool
zix_sem_try_wait(ZixSem* sem)
{
- const mach_timespec_t zero = { 0, 0 };
- return semaphore_timedwait(sem->sem, zero) == KERN_SUCCESS;
+ const mach_timespec_t zero = {0, 0};
+ return semaphore_timedwait(sem->sem, zero) == KERN_SUCCESS;
}
#elif defined(_WIN32)
struct ZixSemImpl {
- HANDLE sem;
+ HANDLE sem;
};
static inline ZixStatus
zix_sem_init(ZixSem* sem, unsigned initial)
{
- sem->sem = CreateSemaphore(NULL, initial, LONG_MAX, NULL);
- return (sem->sem) ? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR;
+ sem->sem = CreateSemaphore(NULL, initial, LONG_MAX, NULL);
+ return (sem->sem) ? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR;
}
static inline void
zix_sem_destroy(ZixSem* sem)
{
- CloseHandle(sem->sem);
+ CloseHandle(sem->sem);
}
static inline void
zix_sem_post(ZixSem* sem)
{
- ReleaseSemaphore(sem->sem, 1, NULL);
+ ReleaseSemaphore(sem->sem, 1, NULL);
}
static inline ZixStatus
zix_sem_wait(ZixSem* sem)
{
- if (WaitForSingleObject(sem->sem, INFINITE) != WAIT_OBJECT_0) {
- return ZIX_STATUS_ERROR;
- }
- return ZIX_STATUS_SUCCESS;
+ if (WaitForSingleObject(sem->sem, INFINITE) != WAIT_OBJECT_0) {
+ return ZIX_STATUS_ERROR;
+ }
+ return ZIX_STATUS_SUCCESS;
}
static inline bool
zix_sem_try_wait(ZixSem* sem)
{
- return WaitForSingleObject(sem->sem, 0) == WAIT_OBJECT_0;
+ return WaitForSingleObject(sem->sem, 0) == WAIT_OBJECT_0;
}
-#else /* !defined(__APPLE__) && !defined(_WIN32) */
+#else /* !defined(__APPLE__) && !defined(_WIN32) */
struct ZixSemImpl {
- sem_t sem;
+ sem_t sem;
};
static inline ZixStatus
zix_sem_init(ZixSem* sem, unsigned initial)
{
- return sem_init(&sem->sem, 0, initial)
- ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS;
+ return sem_init(&sem->sem, 0, initial) ? ZIX_STATUS_ERROR
+ : ZIX_STATUS_SUCCESS;
}
static inline void
zix_sem_destroy(ZixSem* sem)
{
- sem_destroy(&sem->sem);
+ sem_destroy(&sem->sem);
}
static inline void
zix_sem_post(ZixSem* sem)
{
- sem_post(&sem->sem);
+ sem_post(&sem->sem);
}
static inline ZixStatus
zix_sem_wait(ZixSem* sem)
{
- while (sem_wait(&sem->sem)) {
- if (errno != EINTR) {
- return ZIX_STATUS_ERROR;
- }
- /* Otherwise, interrupted, so try again. */
- }
-
- return ZIX_STATUS_SUCCESS;
+ while (sem_wait(&sem->sem)) {
+ if (errno != EINTR) {
+ return ZIX_STATUS_ERROR;
+ }
+ /* Otherwise, interrupted, so try again. */
+ }
+
+ return ZIX_STATUS_SUCCESS;
}
static inline bool
zix_sem_try_wait(ZixSem* sem)
{
- return (sem_trywait(&sem->sem) == 0);
+ return (sem_trywait(&sem->sem) == 0);
}
#endif
@@ -235,7 +236,7 @@ zix_sem_try_wait(ZixSem* sem)
*/
#ifdef __cplusplus
-} /* extern "C" */
+} /* extern "C" */
#endif
-#endif /* ZIX_SEM_H */
+#endif /* ZIX_SEM_H */
diff --git a/src/zix/thread.h b/src/zix/thread.h
index dbfdf36..77eeb1b 100644
--- a/src/zix/thread.h
+++ b/src/zix/thread.h
@@ -1,5 +1,5 @@
/*
- Copyright 2012-2014 David Robillard <d@drobilla.net>
+ Copyright 2012-2020 David Robillard <d@drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
@@ -20,10 +20,10 @@
#include "zix/common.h"
#ifdef _WIN32
-# include <windows.h>
+# include <windows.h>
#else
-# include <errno.h>
-# include <pthread.h>
+# include <errno.h>
+# include <pthread.h>
#endif
#include <stddef.h>
@@ -54,8 +54,8 @@ typedef pthread_t ZixThread;
static inline ZixStatus
zix_thread_create(ZixThread* thread,
size_t stack_size,
- void* (*function)(void*),
- void* arg);
+ void* (*function)(void*),
+ void* arg);
/**
Join `thread` (block until `thread` exits).
@@ -68,53 +68,54 @@ zix_thread_join(ZixThread thread, void** retval);
static inline ZixStatus
zix_thread_create(ZixThread* thread,
size_t stack_size,
- void* (*function)(void*),
- void* arg)
+ void* (*function)(void*),
+ void* arg)
{
- *thread = CreateThread(NULL, stack_size,
- (LPTHREAD_START_ROUTINE)function, arg,
- 0, NULL);
- return *thread ? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR;
+ *thread = CreateThread(
+ NULL, stack_size, (LPTHREAD_START_ROUTINE)function, arg, 0, NULL);
+ return *thread ? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR;
}
static inline ZixStatus
zix_thread_join(ZixThread thread, void** retval)
{
- (void)retval;
+ (void)retval;
- return WaitForSingleObject(thread, INFINITE)
- ? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR;
+ return WaitForSingleObject(thread, INFINITE) ? ZIX_STATUS_SUCCESS
+ : ZIX_STATUS_ERROR;
}
-#else /* !defined(_WIN32) */
+#else /* !defined(_WIN32) */
static inline ZixStatus
zix_thread_create(ZixThread* thread,
size_t stack_size,
- void* (*function)(void*),
- void* arg)
+ void* (*function)(void*),
+ void* arg)
{
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- pthread_attr_setstacksize(&attr, stack_size);
-
- const int ret = pthread_create(thread, NULL, function, arg);
- pthread_attr_destroy(&attr);
-
- switch (ret) {
- case EAGAIN: return ZIX_STATUS_NO_MEM;
- case EINVAL: return ZIX_STATUS_BAD_ARG;
- case EPERM: return ZIX_STATUS_BAD_PERMS;
- }
-
- return ret ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS;
+ pthread_attr_t attr;
+ pthread_attr_init(&attr);
+ pthread_attr_setstacksize(&attr, stack_size);
+
+ const int ret = pthread_create(thread, NULL, function, arg);
+ pthread_attr_destroy(&attr);
+
+ switch (ret) {
+ case EAGAIN:
+ return ZIX_STATUS_NO_MEM;
+ case EINVAL:
+ return ZIX_STATUS_BAD_ARG;
+ case EPERM:
+ return ZIX_STATUS_BAD_PERMS;
+ }
+
+ return ret ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS;
}
static inline ZixStatus
zix_thread_join(ZixThread thread, void** retval)
{
- return pthread_join(thread, retval)
- ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS;
+ return pthread_join(thread, retval) ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS;
}
#endif
@@ -125,7 +126,7 @@ zix_thread_join(ZixThread thread, void** retval)
*/
#ifdef __cplusplus
-} /* extern "C" */
+} /* extern "C" */
#endif
-#endif /* ZIX_THREAD_H */
+#endif /* ZIX_THREAD_H */