Update Files
This commit is contained in:
103
Kha/Kinc/Backends/System/POSIX/Sources/kinc/backend/atomic.h
Normal file
103
Kha/Kinc/Backends/System/POSIX/Sources/kinc/backend/atomic.h
Normal file
@ -0,0 +1,103 @@
|
||||
#pragma once
|
||||
|
||||
#include <kinc/global.h>
|
||||
|
||||
#if defined(KINC_MACOS) || defined(KINC_IOS)
|
||||
|
||||
#include <libkern/OSAtomic.h>
|
||||
|
||||
static inline bool kinc_atomic_compare_exchange(volatile int32_t *pointer, int32_t old_value, int32_t new_value) {
|
||||
return OSAtomicCompareAndSwap32Barrier(old_value, new_value, pointer);
|
||||
}
|
||||
|
||||
static inline bool kinc_atomic_compare_exchange_pointer(void *volatile *pointer, void *old_value, void *new_value) {
|
||||
return OSAtomicCompareAndSwapPtrBarrier(old_value, new_value, pointer);
|
||||
}
|
||||
|
||||
static inline int32_t kinc_atomic_increment(volatile int32_t *pointer) {
|
||||
return OSAtomicIncrement32Barrier(pointer) - 1;
|
||||
}
|
||||
|
||||
static inline int32_t kinc_atomic_decrement(volatile int32_t *pointer) {
|
||||
return OSAtomicDecrement32Barrier(pointer) + 1;
|
||||
}
|
||||
|
||||
static inline void kinc_atomic_exchange(volatile int32_t *pointer, int32_t value) {
|
||||
__sync_swap(pointer, value);
|
||||
}
|
||||
|
||||
static inline void kinc_atomic_exchange_float(volatile float *pointer, float value) {
|
||||
__sync_swap((volatile int32_t *)pointer, *(int32_t *)&value);
|
||||
}
|
||||
|
||||
static inline void kinc_atomic_exchange_double(volatile double *pointer, double value) {
|
||||
__sync_swap((volatile int64_t *)pointer, *(int64_t *)&value);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
// clang/gcc intrinsics
|
||||
|
||||
static inline bool kinc_atomic_compare_exchange(volatile int32_t *pointer, int32_t old_value, int32_t new_value) {
|
||||
return __sync_val_compare_and_swap(pointer, old_value, new_value) == old_value;
|
||||
}
|
||||
|
||||
static inline bool kinc_atomic_compare_exchange_pointer(void *volatile *pointer, void *old_value, void *new_value) {
|
||||
return __sync_val_compare_and_swap(pointer, old_value, new_value) == old_value;
|
||||
}
|
||||
|
||||
static inline int32_t kinc_atomic_increment(volatile int32_t *pointer) {
|
||||
return __sync_fetch_and_add(pointer, 1);
|
||||
}
|
||||
|
||||
static inline int32_t kinc_atomic_decrement(volatile int32_t *pointer) {
|
||||
return __sync_fetch_and_sub(pointer, 1);
|
||||
}
|
||||
|
||||
#ifdef __clang__
|
||||
|
||||
static inline void kinc_atomic_exchange(volatile int32_t *pointer, int32_t value) {
|
||||
__sync_swap(pointer, value);
|
||||
}
|
||||
|
||||
static inline void kinc_atomic_exchange_float(volatile float *pointer, float value) {
|
||||
__sync_swap((volatile int32_t *)pointer, *(int32_t *)&value);
|
||||
}
|
||||
|
||||
static inline void kinc_atomic_exchange_double(volatile double *pointer, double value) {
|
||||
__sync_swap((volatile int64_t *)pointer, *(int64_t *)&value);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
// Beware, __sync_lock_test_and_set is not a full barrier and can have platform-specific weirdness
|
||||
|
||||
static inline void kinc_atomic_exchange(volatile int32_t *pointer, int32_t value) {
|
||||
__sync_lock_test_and_set(pointer, value);
|
||||
}
|
||||
|
||||
static inline void kinc_atomic_exchange_float(volatile float *pointer, float value) {
|
||||
__sync_lock_test_and_set((volatile int32_t *)pointer, *(int32_t *)&value);
|
||||
}
|
||||
|
||||
static inline void kinc_atomic_exchange_double(volatile double *pointer, double value) {
|
||||
__sync_lock_test_and_set((volatile int64_t *)pointer, *(int64_t *)&value);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#define KINC_ATOMIC_COMPARE_EXCHANGE(pointer, oldValue, newValue) (kinc_atomic_compare_exchange(pointer, oldValue, newValue))
|
||||
|
||||
#define KINC_ATOMIC_COMPARE_EXCHANGE_POINTER(pointer, oldValue, newValue) (kinc_atomic_compare_exchange_pointer(pointer, oldValue, newValue))
|
||||
|
||||
#define KINC_ATOMIC_INCREMENT(pointer) (kinc_atomic_increment(pointer))
|
||||
|
||||
#define KINC_ATOMIC_DECREMENT(pointer) (kinc_atomic_decrement(pointer))
|
||||
|
||||
#define KINC_ATOMIC_EXCHANGE_32(pointer, value) (kinc_atomic_exchange(pointer, value))
|
||||
|
||||
#define KINC_ATOMIC_EXCHANGE_FLOAT(pointer, value) (kinc_atomic_exchange_float(pointer, value))
|
||||
|
||||
#define KINC_ATOMIC_EXCHANGE_DOUBLE(pointer, value) (kinc_atomic_exchange_double(pointer, value))
|
@ -0,0 +1,78 @@
|
||||
#include <kinc/threads/event.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
void kinc_event_init(kinc_event_t *event, bool auto_reset) {
|
||||
event->impl.auto_reset = auto_reset;
|
||||
event->impl.set = false;
|
||||
|
||||
pthread_cond_init(&event->impl.event, NULL);
|
||||
|
||||
// pthread_mutexattr_t attr;
|
||||
// pthread_mutexattr_init(&attr);
|
||||
// pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
||||
pthread_mutex_init(&event->impl.mutex, NULL); //&attr);
|
||||
}
|
||||
|
||||
void kinc_event_destroy(kinc_event_t *event) {
|
||||
pthread_cond_destroy(&event->impl.event);
|
||||
pthread_mutex_destroy(&event->impl.mutex);
|
||||
}
|
||||
|
||||
void kinc_event_signal(kinc_event_t *event) {
|
||||
pthread_mutex_lock(&event->impl.mutex);
|
||||
if (!event->impl.set) {
|
||||
event->impl.set = true;
|
||||
pthread_cond_signal(&event->impl.event);
|
||||
}
|
||||
pthread_mutex_unlock(&event->impl.mutex);
|
||||
}
|
||||
|
||||
void kinc_event_wait(kinc_event_t *event) {
|
||||
pthread_mutex_lock(&event->impl.mutex);
|
||||
while (!event->impl.set) {
|
||||
pthread_cond_wait(&event->impl.event, &event->impl.mutex);
|
||||
}
|
||||
if (event->impl.auto_reset) {
|
||||
event->impl.set = false;
|
||||
}
|
||||
pthread_mutex_unlock(&event->impl.mutex);
|
||||
}
|
||||
|
||||
bool kinc_event_try_to_wait(kinc_event_t *event, double seconds) {
|
||||
pthread_mutex_lock(&event->impl.mutex);
|
||||
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, 0);
|
||||
|
||||
int isec = (int)seconds;
|
||||
int usec = (int)((seconds - isec) * 1000000.0);
|
||||
struct timespec spec;
|
||||
spec.tv_nsec = (tv.tv_usec + usec) * 1000;
|
||||
if (spec.tv_nsec > 1000000000) {
|
||||
spec.tv_nsec -= 1000000000;
|
||||
isec += 1;
|
||||
}
|
||||
spec.tv_sec = tv.tv_sec + isec;
|
||||
|
||||
while (!event->impl.set) {
|
||||
int result = pthread_cond_timedwait(&event->impl.event, &event->impl.mutex, &spec);
|
||||
if (result == 0 || result == ETIMEDOUT) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
bool result = event->impl.set;
|
||||
if (event->impl.auto_reset) {
|
||||
event->impl.set = false;
|
||||
}
|
||||
pthread_mutex_unlock(&event->impl.mutex);
|
||||
return result;
|
||||
}
|
||||
|
||||
void kinc_event_reset(kinc_event_t *event) {
|
||||
pthread_mutex_lock(&event->impl.mutex);
|
||||
event->impl.set = false;
|
||||
pthread_mutex_unlock(&event->impl.mutex);
|
||||
}
|
18
Kha/Kinc/Backends/System/POSIX/Sources/kinc/backend/event.h
Normal file
18
Kha/Kinc/Backends/System/POSIX/Sources/kinc/backend/event.h
Normal file
@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
pthread_cond_t event;
|
||||
pthread_mutex_t mutex;
|
||||
volatile bool set;
|
||||
bool auto_reset;
|
||||
} kinc_event_impl_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
@ -0,0 +1,40 @@
|
||||
#include <kinc/threads/mutex.h>
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
void kinc_mutex_init(kinc_mutex_t *mutex) {
|
||||
pthread_mutexattr_t attr;
|
||||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
||||
pthread_mutex_init(&mutex->impl.mutex, &attr);
|
||||
}
|
||||
|
||||
void kinc_mutex_destroy(kinc_mutex_t *mutex) {
|
||||
pthread_mutex_destroy(&mutex->impl.mutex);
|
||||
}
|
||||
|
||||
bool kinc_mutex_try_to_lock(kinc_mutex_t *mutex) {
|
||||
return pthread_mutex_trylock(&mutex->impl.mutex) == 0;
|
||||
}
|
||||
|
||||
void kinc_mutex_lock(kinc_mutex_t *mutex) {
|
||||
pthread_mutex_lock(&mutex->impl.mutex);
|
||||
}
|
||||
|
||||
void kinc_mutex_unlock(kinc_mutex_t *mutex) {
|
||||
pthread_mutex_unlock(&mutex->impl.mutex);
|
||||
}
|
||||
|
||||
bool kinc_uber_mutex_init(kinc_uber_mutex_t *mutex, const char *name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void kinc_uber_mutex_destroy(kinc_uber_mutex_t *mutex) {}
|
||||
|
||||
void kinc_uber_mutex_lock(kinc_uber_mutex_t *mutex) {
|
||||
assert(false);
|
||||
}
|
||||
|
||||
void kinc_uber_mutex_unlock(kinc_uber_mutex_t *mutex) {
|
||||
assert(false);
|
||||
}
|
19
Kha/Kinc/Backends/System/POSIX/Sources/kinc/backend/mutex.h
Normal file
19
Kha/Kinc/Backends/System/POSIX/Sources/kinc/backend/mutex.h
Normal file
@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
pthread_mutex_t mutex;
|
||||
} kinc_mutex_impl_t;
|
||||
|
||||
typedef struct {
|
||||
int nothing;
|
||||
} kinc_uber_mutex_impl_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
@ -0,0 +1,5 @@
|
||||
#include "event.c.h"
|
||||
#include "mutex.c.h"
|
||||
#include "semaphore.c.h"
|
||||
#include "thread.c.h"
|
||||
#include "threadlocal.c.h"
|
@ -0,0 +1,56 @@
|
||||
#include <kinc/system.h>
|
||||
#include <kinc/threads/semaphore.h>
|
||||
|
||||
#ifdef __APPLE__
|
||||
|
||||
void kinc_semaphore_init(kinc_semaphore_t *semaphore, int current, int max) {
|
||||
semaphore->impl.semaphore = dispatch_semaphore_create(current);
|
||||
}
|
||||
|
||||
void kinc_semaphore_destroy(kinc_semaphore_t *semaphore) {}
|
||||
|
||||
void kinc_semaphore_release(kinc_semaphore_t *semaphore, int count) {
|
||||
for (int i = 0; i < count; ++i) {
|
||||
dispatch_semaphore_signal(semaphore->impl.semaphore);
|
||||
}
|
||||
}
|
||||
|
||||
void kinc_semaphore_acquire(kinc_semaphore_t *semaphore) {
|
||||
dispatch_semaphore_wait(semaphore->impl.semaphore, DISPATCH_TIME_FOREVER);
|
||||
}
|
||||
|
||||
bool kinc_semaphore_try_to_acquire(kinc_semaphore_t *semaphore, double seconds) {
|
||||
return dispatch_semaphore_wait(semaphore->impl.semaphore, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(seconds * 1000 * 1000 * 1000))) == 0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
void kinc_semaphore_init(kinc_semaphore_t *semaphore, int current, int max) {
|
||||
sem_init(&semaphore->impl.semaphore, 0, current);
|
||||
}
|
||||
|
||||
void kinc_semaphore_destroy(kinc_semaphore_t *semaphore) {
|
||||
sem_destroy(&semaphore->impl.semaphore);
|
||||
}
|
||||
|
||||
void kinc_semaphore_release(kinc_semaphore_t *semaphore, int count) {
|
||||
for (int i = 0; i < count; ++i) {
|
||||
sem_post(&semaphore->impl.semaphore);
|
||||
}
|
||||
}
|
||||
|
||||
void kinc_semaphore_acquire(kinc_semaphore_t *semaphore) {
|
||||
sem_wait(&semaphore->impl.semaphore);
|
||||
}
|
||||
|
||||
bool kinc_semaphore_try_to_acquire(kinc_semaphore_t *semaphore, double seconds) {
|
||||
double now = kinc_time();
|
||||
do {
|
||||
if (sem_trywait(&semaphore->impl.semaphore) == 0) {
|
||||
return true;
|
||||
}
|
||||
} while (kinc_time() < now + seconds);
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __APPLE__
|
||||
#include <dispatch/dispatch.h>
|
||||
#else
|
||||
#include <semaphore.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
#ifdef __APPLE__
|
||||
dispatch_semaphore_t semaphore;
|
||||
#else
|
||||
sem_t semaphore;
|
||||
#endif
|
||||
} kinc_semaphore_impl_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
@ -0,0 +1,82 @@
|
||||
#include <kinc/threads/thread.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <wchar.h>
|
||||
|
||||
#if !defined(KINC_IOS) && !defined(KINC_MACOS)
|
||||
|
||||
struct thread_start {
|
||||
void (*thread)(void *param);
|
||||
void *param;
|
||||
};
|
||||
|
||||
#define THREAD_STARTS 64
|
||||
static struct thread_start starts[THREAD_STARTS];
|
||||
static int thread_start_index = 0;
|
||||
|
||||
static void *ThreadProc(void *arg) {
|
||||
intptr_t start_index = (intptr_t)arg;
|
||||
starts[start_index].thread(starts[start_index].param);
|
||||
pthread_exit(NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void kinc_thread_init(kinc_thread_t *t, void (*thread)(void *param), void *param) {
|
||||
t->impl.param = param;
|
||||
t->impl.thread = thread;
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
// pthread_attr_setstacksize(&attr, 1024 * 64);
|
||||
struct sched_param sp;
|
||||
memset(&sp, 0, sizeof(sp));
|
||||
sp.sched_priority = 0;
|
||||
pthread_attr_setschedparam(&attr, &sp);
|
||||
intptr_t start_index = thread_start_index++;
|
||||
if (thread_start_index >= THREAD_STARTS) {
|
||||
thread_start_index = 0;
|
||||
}
|
||||
starts[start_index].thread = thread;
|
||||
starts[start_index].param = param;
|
||||
int ret = pthread_create(&t->impl.pthread, &attr, &ThreadProc, (void *)start_index);
|
||||
assert(ret == 0);
|
||||
pthread_attr_destroy(&attr);
|
||||
}
|
||||
|
||||
void kinc_thread_wait_and_destroy(kinc_thread_t *thread) {
|
||||
int ret;
|
||||
do {
|
||||
ret = pthread_join(thread->impl.pthread, NULL);
|
||||
} while (ret != 0);
|
||||
}
|
||||
|
||||
bool kinc_thread_try_to_destroy(kinc_thread_t *thread) {
|
||||
return pthread_join(thread->impl.pthread, NULL) == 0;
|
||||
}
|
||||
|
||||
void kinc_threads_init() {}
|
||||
|
||||
void kinc_threads_quit() {}
|
||||
|
||||
#endif
|
||||
|
||||
#if !defined(KINC_IOS) && !defined(KINC_MACOS)
|
||||
// Alternatively _GNU_SOURCE can be defined to make
|
||||
// the headers declare it but let's not make it too
|
||||
// easy to write Linux-specific POSIX-code
|
||||
int pthread_setname_np(pthread_t thread, const char *name);
|
||||
#endif
|
||||
|
||||
void kinc_thread_set_name(const char *name) {
|
||||
#if !defined(KINC_IOS) && !defined(KINC_MACOS)
|
||||
pthread_setname_np(pthread_self(), name);
|
||||
#else
|
||||
pthread_setname_np(name);
|
||||
#endif
|
||||
}
|
||||
|
||||
void kinc_thread_sleep(int milliseconds) {
|
||||
usleep(1000 * (useconds_t)milliseconds);
|
||||
}
|
17
Kha/Kinc/Backends/System/POSIX/Sources/kinc/backend/thread.h
Normal file
17
Kha/Kinc/Backends/System/POSIX/Sources/kinc/backend/thread.h
Normal file
@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
void *param;
|
||||
void (*thread)(void *param);
|
||||
pthread_t pthread;
|
||||
} kinc_thread_impl_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
@ -0,0 +1,17 @@
|
||||
#include <kinc/threads/threadlocal.h>
|
||||
|
||||
void kinc_thread_local_init(kinc_thread_local_t *local) {
|
||||
pthread_key_create(&local->impl.key, NULL);
|
||||
}
|
||||
|
||||
void kinc_thread_local_destroy(kinc_thread_local_t *local) {
|
||||
pthread_key_delete(local->impl.key);
|
||||
}
|
||||
|
||||
void *kinc_thread_local_get(kinc_thread_local_t *local) {
|
||||
return pthread_getspecific(local->impl.key);
|
||||
}
|
||||
|
||||
void kinc_thread_local_set(kinc_thread_local_t *local, void *data) {
|
||||
pthread_setspecific(local->impl.key, data);
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
pthread_key_t key;
|
||||
} kinc_thread_local_impl_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
Reference in New Issue
Block a user