package = utils
compilerOpts.mingw_x64 = -DMINGW
compilerOpts.ios_x64 = -DIOS
compilerOpts.ios_arm64 = -DIOS
compilerOpts.ios_arm32 = -DIOS
compilerOpts.macos_x64 = -DMACOS
compilerOpts.linux_x64 = -DLINUX
---
#include <pthread.h>
#include <time.h>
#include <stdio.h>

/* Mutex */
typedef struct ktor_mutex {
    pthread_mutex_t mutex;
} ktor_mutex_t;


static inline int ktor_mutex_create(ktor_mutex_t *mutex) {
    pthread_mutexattr_t attributes;
    pthread_mutexattr_init(&attributes);
    pthread_mutexattr_settype(&attributes, PTHREAD_MUTEX_RECURSIVE);
    return pthread_mutex_init(&mutex->mutex, &attributes);
}

static inline int ktor_mutex_lock(ktor_mutex_t* mutex) {
    return pthread_mutex_lock(&mutex->mutex);
}

static inline int ktor_mutex_unlock(ktor_mutex_t* mutex) {
    return pthread_mutex_unlock(&mutex->mutex);
}

static inline int ktor_mutex_destroy(ktor_mutex_t* mutex) {
    return pthread_mutex_destroy(&mutex->mutex);
}

/* Read-Write lock */
typedef struct ktor_rwlock {
    pthread_rwlock_t lock;
} ktor_rwlock;

static inline int ktor_rwlock_create(ktor_rwlock* lock) {
    pthread_mutexattr_t attributes;
    pthread_mutexattr_init(&attributes);
    pthread_mutexattr_settype(&attributes, PTHREAD_MUTEX_RECURSIVE);
    return pthread_rwlock_init(&lock->lock, &attributes);
}

static inline int ktor_rwlock_read(ktor_rwlock* lock) {
    return pthread_rwlock_rdlock(&lock->lock);
}

static inline int ktor_rwlock_write(ktor_rwlock* lock) {
    return pthread_rwlock_wrlock(&lock->lock);
}

static inline int ktor_rwlock_unlock(ktor_rwlock* lock) {
    return pthread_rwlock_unlock(&lock->lock);
}

static inline int ktor_rwlock_destroy(ktor_rwlock* lock) {
    return pthread_rwlock_destroy(&lock->lock);
}

/* Time */
static inline time_t ktor_time(struct tm* tm) {
#ifndef MINGW
    return timegm(tm);
#else
    return _mkgmtime(tm);
#endif
}
