more progress on the rewrite

This commit is contained in:
IonutParau 2026-02-02 15:40:49 +01:00
parent ab22feec69
commit 89f3527147
10 changed files with 640 additions and 1869 deletions

28
rewrite/main.c Normal file
View File

@ -0,0 +1,28 @@
// The main file of the test emulator
// This is not a serious emulator intended for practical use,
// it is simply just to test stuff and showcase the API.
#include "neonucleus.h"
int main() {
nn_Context ctx;
nn_initContext(&ctx);
// create the universe
nn_Universe *u = nn_createUniverse(&ctx);
nn_ComponentMethod sandboxMethods[] = {
{"log", "log(msg: string) - Log to stdout", true},
NULL,
};
nn_ComponentType *ctype = nn_createComponentType(u, "sandbox", NULL, sandboxMethods, NULL);
nn_Computer *c = nn_createComputer(u, NULL, "computer0", 8 * NN_MiB, 256, 256);
cleanup:;
nn_destroyComponentType(ctype);
nn_destroyComputer(c);
// rip the universe
nn_destroyUniverse(u);
return 0;
}

591
rewrite/neonucleus.c Normal file
View File

@ -0,0 +1,591 @@
// all in 1 C file for convenience of distribution.
// as long as it can include the header, all is fine.
// this should be very easy to include in any modern build system, as it is a single C file.
// When compiled, you can define:
// - NN_BAREMETAL, to remove any runtime dependency on libc and use minimal headers
// - NN_NO_C11_LOCKS, to not use C11 mutexes, instead using pthread mutexes for POSIX systems or Windows locks.
// Most of the time, you only depend on libc.
// However, if pthread locks are used, you will need to link in -lpthread.
// we need the header.
#include "neonucleus.h"
// Based off https://stackoverflow.com/questions/5919996/how-to-detect-reliably-mac-os-x-ios-linux-windows-in-c-preprocessor
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
//define something for Windows (32-bit and 64-bit, this part is common)
#ifdef _WIN64
#define NN_WINDOWS
#else
#error "Windows 32-bit is not supported"
#endif
#elif __APPLE__
#include <TargetConditionals.h>
#if TARGET_IPHONE_SIMULATOR
#error "iPhone Emulators are not supported"
#elif TARGET_OS_MACCATALYST
// I guess?
#define NN_MACOS
#elif TARGET_OS_IPHONE
#error "iPhone are not supported"
#elif TARGET_OS_MAC
#define NN_MACOS
#else
#error "Unknown Apple platform"
#endif
#elif __ANDROID__
#error "Android is not supported"
#elif __linux__
#define NN_LINUX
#endif
#if __unix__ // all unices not caught above
// Unix
#define NN_UNIX
#define NN_POSIX
#elif defined(_POSIX_VERSION)
// POSIX
#define NN_POSIX
#endif
typedef struct nn_Lock nn_Lock;
// the special includes
#ifndef NN_BAREMETAL
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#if defined(__STDC_NO_THREADS__) || defined(NN_NO_C11_LOCKS) || defined(NN_WINDOWS) // fuck you Windows
#ifdef NN_POSIX
#define NN_THREAD_PTHREAD
#endif
#ifdef NN_WINDOWS
#define NN_THREAD_WINDOWS
#endif
#else
#include <threads.h>
#define NN_THREAD_C11
#endif
#ifdef NN_POSIX
#include <sys/time.h>
#include <pthread.h>
#endif
#ifdef NN_WINDOWS
#include <Windows.h>
#endif
#endif
void *nn_alloc(nn_Context *ctx, size_t size) {
if(size == 0) return ctx->alloc;
return ctx->alloc(ctx->state, NULL, 0, size);
}
void nn_free(nn_Context *ctx, void *memory, size_t size) {
if(memory == NULL) return;
if(memory == ctx->alloc) return;
ctx->alloc(ctx->state, memory, size, 0);
}
void *nn_realloc(nn_Context *ctx, void *memory, size_t oldSize, size_t newSize) {
if(memory == NULL) return nn_alloc(memory, newSize);
if(memory == ctx->alloc) return nn_alloc(memory, newSize);
if(newSize == 0) {
nn_free(ctx, memory, oldSize);
return ctx->alloc;
}
return ctx->alloc(ctx->state, memory, oldSize, newSize);
}
typedef struct nn_ArenaBlock {
// we should make each block be 1 allocation instead of 2
// TODO: make it 1 alloc instead of 2
void *memory;
size_t used;
size_t cap;
struct nn_ArenaBlock *next;
} nn_ArenaBlock;
typedef struct nn_Arena {
nn_Context ctx;
nn_ArenaBlock *block;
size_t nextCap;
} nn_Arena;
void nn_arinit(nn_Arena *arena, nn_Context *ctx) {
arena->ctx = *ctx;
arena->block = NULL;
arena->nextCap = 1024;
}
void nn_ardestroy(nn_Arena *arena) {
nn_ArenaBlock *b = arena->block;
while(b != NULL) {
nn_ArenaBlock *cur = b;
b = b->next;
nn_free(&arena->ctx, cur->memory, cur->cap);
nn_free(&arena->ctx, cur, sizeof(*cur));
}
}
nn_ArenaBlock *nn_arallocblock(nn_Context *ctx, size_t cap) {
void *memory = nn_alloc(ctx, cap);
if(memory == NULL) return NULL;
nn_ArenaBlock *block = nn_alloc(ctx, sizeof(*block));
if(block == NULL) {
nn_free(ctx, memory, cap);
return NULL;
}
block->memory = memory;
block->cap = cap;
block->used = 0;
block->next = NULL;
return block;
}
void *nn_aralloc(nn_Arena *arena, size_t size) {
if((size % NN_ALLOC_ALIGN) != 0) {
size_t over = size % NN_ALLOC_ALIGN;
size += NN_ALLOC_ALIGN - over;
}
if(arena->block == NULL) {
}
nn_ArenaBlock *block = arena->block;
while(block != NULL) {
nn_ArenaBlock *cur = block;
block = block->next;
size_t free = cur->cap - cur->used;
if(free >= size) {
void *mem = (void *)((size_t)cur->memory + cur->used);
cur->used += size;
return mem;
}
}
while(arena->nextCap < size) arena->nextCap *= 2;
nn_ArenaBlock *newBlock = nn_arallocblock(&arena->ctx, arena->nextCap);
if(newBlock == NULL) {
return NULL;
}
newBlock->next = arena->block;
newBlock->used = size;
arena->block = newBlock;
return newBlock->memory;
}
size_t nn_strlen(const char *s) {
size_t l = 0;
while(*(s++) != '\0') l++;
return l;
}
void nn_memcpy(void *dest, const void *src, size_t len) {
char *out = (char *)dest;
const char *in = (const char *)src;
for(size_t i = 0; i < len; i++) out[i] = in[i];
}
char *nn_strdup(nn_Context *ctx, const char *s) {
size_t l = nn_strlen(s);
char *buf = nn_alloc(ctx, sizeof(char) * (l+1));
if(buf == NULL) return NULL;
nn_memcpy(buf, s, sizeof(char) * l);
buf[l] = '\0';
return buf;
}
const char *nn_arstrdup(nn_Arena *arena, const char *s) {
size_t len = nn_strlen(s);
char *buf = nn_aralloc(arena, sizeof(char) * (len+1));
nn_memcpy(buf, s, sizeof(char) * len);
buf[len] = '\0';
return buf;
}
void nn_strfree(nn_Context *ctx, char *s) {
size_t l = nn_strlen(s);
nn_free(ctx, s, sizeof(char) * (l+1));
}
void nn_memset(void *dest, int x, size_t len) {
char *out = (char *)dest;
for(size_t i = 0; i < len; i++) out[i] = (char)x;
}
nn_Lock *nn_createLock(nn_Context *ctx) {
nn_LockRequest req;
req.lock = NULL;
req.action = NN_LOCK_CREATE;
ctx->lock(ctx->state, &req);
return req.lock;
}
void nn_destroyLock(nn_Context *ctx, nn_Lock *lock) {
if(lock == NULL) return;
nn_LockRequest req;
req.lock = lock;
req.action = NN_LOCK_DESTROY;
ctx->lock(ctx->state, &req);
}
void nn_lock(nn_Context *ctx, nn_Lock *lock) {
nn_LockRequest req;
req.lock = lock;
req.action = NN_LOCK_LOCK;
ctx->lock(ctx->state, &req);
}
void nn_unlock(nn_Context *ctx, nn_Lock *lock) {
nn_LockRequest req;
req.lock = lock;
req.action = NN_LOCK_UNLOCK;
ctx->lock(ctx->state, &req);
}
double nn_currentTime(nn_Context *ctx) {
return ctx->time(ctx->state);
}
size_t nn_rand(nn_Context *ctx) {
return ctx->rng(ctx->state);
}
double nn_randf(nn_Context *ctx) {
double n = (double)nn_rand(ctx);
return n / (double)(ctx->rngMaximum + 1);
}
double nn_randfi(nn_Context *ctx) {
double n = (double)nn_rand(ctx);
return n / (double)ctx->rngMaximum;
}
static void *nn_defaultAlloc(void *_, void *memory, size_t oldSize, size_t newSize) {
#ifndef NN_BAREMETAL
if(newSize == 0) {
free(memory);
return NULL;
}
return realloc(memory, newSize);
#else
// 0 memory available
return NULL;
#endif
}
static double nn_defaultTime(void *_) {
#ifndef NN_BAREMETAL
// time does not exist... yet!
return 0;
#else
// time does not exist
return 0;
#endif
}
static size_t nn_defaultRng(void *_) {
#ifndef NN_BAREMETAL
return rand();
#else
// insane levels of RNG
return 1;
#endif
}
static void nn_defaultLock(void *state, nn_LockRequest *req) {
(void)state;
#ifndef NN_BAREMETAL
#if defined(NN_THREAD_C11)
switch(req->action) {
case NN_LOCK_CREATE:;
mtx_t *mem = malloc(sizeof(mtx_t));
req->lock = mem;
if(mem == NULL) return;
if(mtx_init(mem, mtx_plain) != thrd_success) {
free(mem);
req->lock = NULL;
return;
}
return;
case NN_LOCK_DESTROY:;
mtx_destroy(req->lock);
free(req->lock);
return;
case NN_LOCK_LOCK:;
mtx_lock(req->lock);
return;
case NN_LOCK_UNLOCK:;
mtx_unlock(req->lock);
return;
}
#elif defined(NN_THREAD_PTHREAD)
switch(req->action) {
case NN_LOCK_CREATE:;
pthread_mutex_t *mem = malloc(sizeof(pthread_mutex_t));
req->lock = mem;
if(mem == NULL) return;
if(pthread_mutex_init(mem, NULL) != 0) {
free(mem);
req->lock = NULL;
return;
}
return;
case NN_LOCK_DESTROY:;
pthread_mutex_destroy(req->lock);
free(req->lock);
return;
case NN_LOCK_LOCK:;
pthread_mutex_lock(req->lock);
return;
case NN_LOCK_UNLOCK:;
pthread_mutex_unlock(req->lock);
return;
}
#elif defined(NN_THREAD_WINDOWS)
#error "Windows locks are not supported yet"
#endif
#endif
}
void nn_initContext(nn_Context *ctx) {
ctx->state = NULL;
ctx->alloc = nn_defaultAlloc;
ctx->time = nn_defaultTime;
#ifndef NN_BAREMETAL
// someone pointed out that running this multiple times
// in 1 second can cause the RNG to loop.
// However, if you call this function multiple times at all,
// that's on you.
srand(time(NULL));
ctx->rngMaximum = RAND_MAX;
#else
ctx->rngMaximum = 1;
#endif
ctx->rng = nn_defaultRng;
ctx->lock = nn_defaultLock;
}
typedef enum nn_BuiltinComponent {
NN_BUILTIN_GPU = 0,
NN_BUILTIN_SCREEN,
NN_BUILTIN_KEYBOARD,
NN_BUILTIN_FILESYSTEM,
// to determine array size
NN_BUILTIN_COUNT,
} nn_BuiltinComponent;
typedef struct nn_ComponentType {
nn_Universe *universe;
nn_Arena arena;
const char *name;
// NULL-terminated
nn_ComponentMethod *methods;
size_t methodCount;
} nn_ComponentType;
typedef struct nn_Universe {
nn_Context ctx;
nn_ComponentType *types[NN_BUILTIN_COUNT];
} nn_Universe;
typedef struct nn_Component {
char *address;
nn_ComponentType *ctype;
size_t slot;
void *userdata;
} nn_Component;
// the values
typedef enum nn_ValueType {
NN_VAL_NULL,
NN_VAL_BOOL,
NN_VAL_NUM,
NN_VAL_STR,
NN_VAL_USERDATA,
NN_VAL_TABLE,
} nn_ValueType;
typedef struct nn_String {
nn_Context ctx;
size_t refc;
size_t len;
char data[];
} nn_String;
typedef struct nn_Value {
nn_ValueType type;
union {
bool boolean;
double number;
nn_String *string;
size_t userdataIdx;
struct nn_Table *table;
};
} nn_Value;
typedef struct nn_Table {
nn_Context ctx;
size_t refc;
size_t len;
nn_Value vals[];
} nn_Table;
typedef struct nn_Computer {
nn_ComputerState state;
nn_Universe *universe;
void *userdata;
char *address;
const nn_Architecture *arch;
const nn_Architecture *desiredArch;
size_t componentCap;
size_t componentLen;
nn_Component *components;
size_t deviceInfoCap;
size_t deviceInfoLen;
nn_DeviceInfo *deviceInfo;
double totalEnergy;
double energy;
size_t totalMemory;
double creationTimestamp;
size_t stackSize;
size_t archCount;
nn_Value callstack[NN_MAX_STACK];
char errorBuffer[NN_MAX_ERROR_SIZE];
nn_Architecture archs[NN_MAX_ARCHITECTURES];
} nn_Computer;
nn_Universe *nn_createUniverse(nn_Context *ctx) {
nn_Universe *u = nn_alloc(ctx, sizeof(nn_Universe));
if(u == NULL) return NULL;
u->ctx = *ctx;
for(size_t i = 0; i < NN_BUILTIN_COUNT; i++) u->types[i] = NULL;
return u;
}
void nn_destroyUniverse(nn_Universe *universe) {
nn_Context ctx = universe->ctx;
for(size_t i = 0; i < NN_BUILTIN_COUNT; i++) nn_destroyComponentType(universe->types[i]);
nn_free(&ctx, universe, sizeof(nn_Universe));
}
nn_ComponentType *nn_createComponentType(nn_Universe *universe, const char *name, void *userdata, const nn_ComponentMethod methods[], nn_ComponentHandler *handler) {
nn_Context *ctx = &universe->ctx;
nn_ComponentType *ctype = nn_alloc(ctx, sizeof(nn_ComponentType));
if(ctype == NULL) return NULL;
ctype->universe = universe;
nn_Arena *arena = &ctype->arena;
nn_arinit(arena, ctx);
const char *namecpy = nn_arstrdup(arena, name);
if(namecpy == NULL) goto fail;
size_t methodCount = 0;
while(methods[methodCount].name != NULL) methodCount++;
nn_ComponentMethod *methodscpy = nn_aralloc(arena, methodCount * sizeof(nn_ComponentMethod));
if(methodscpy == NULL) goto fail;
ctype->methods = methodscpy;
ctype->methodCount = methodCount;
for(size_t i = 0; i < methodCount; i++) {
nn_ComponentMethod cpy;
cpy.direct = methods[i].direct;
cpy.name = nn_arstrdup(arena, methods[i].name);
if(cpy.name == NULL) goto fail;
cpy.docString = nn_arstrdup(arena, methods[i].docString);
if(cpy.docString == NULL) goto fail;
ctype->methods[i] = cpy;
}
return ctype;
fail:;
// yes, because of arenas, we support freeing a "partially initialized state"
nn_destroyComponentType(ctype);
return NULL;
}
void nn_destroyComponentType(nn_ComponentType *ctype) {
if(ctype == NULL) return;
nn_Context *ctx = &ctype->universe->ctx;
nn_ardestroy(&ctype->arena);
nn_free(ctx, ctype, sizeof(nn_ComponentType));
}
nn_Computer *nn_createComputer(nn_Universe *universe, void *userdata, const char *address, size_t totalMemory, size_t maxComponents, size_t maxDevices) {
nn_Context *ctx = &universe->ctx;
nn_Computer *c = nn_alloc(ctx, sizeof(nn_Computer));
if(c == NULL) return NULL;
c->state = NN_BOOTUP;
c->universe = universe;
c->userdata = userdata;
c->address = nn_strdup(ctx, address);
if(c->address == NULL) {
nn_free(ctx, c, sizeof(nn_Computer));
return NULL;
}
c->arch = NULL;
c->desiredArch = NULL;
c->componentCap = maxComponents;
c->componentLen = 0;
c->components = nn_alloc(ctx, sizeof(nn_Component) * maxComponents);
if(c->components == NULL) {
nn_strfree(ctx, c->address);
nn_free(ctx, c, sizeof(nn_Computer));
return NULL;
}
c->deviceInfoCap = maxDevices;
c->deviceInfoLen = 0;
c->deviceInfo = nn_alloc(ctx, sizeof(nn_DeviceInfo) * maxDevices);
if(c->deviceInfo == NULL) {
nn_free(ctx, c->components, sizeof(nn_Component) * maxComponents);
nn_strfree(ctx, c->address);
nn_free(ctx, c, sizeof(nn_Computer));
return NULL;
}
c->totalEnergy = 500;
c->energy = 500;
c->totalMemory = totalMemory;
c->creationTimestamp = nn_currentTime(ctx);
c->stackSize = 0;
c->archCount = 0;
// set to empty string
c->errorBuffer[0] = '\0';
return c;
}
void nn_destroyComputer(nn_Computer *computer) {
nn_Context *ctx = &computer->universe->ctx;
nn_free(ctx, computer->components, sizeof(nn_Component) * computer->componentCap);
nn_free(ctx, computer->deviceInfo, sizeof(nn_DeviceInfo) * computer->deviceInfoCap);
nn_strfree(ctx, computer->address);
nn_free(ctx, computer, sizeof(nn_Computer));
}
void *nn_getComputerUserdata(nn_Computer *computer) {
return computer->userdata;
}
const char *nn_getComputerAddress(nn_Computer *computer) {
return computer->address;
}

View File

@ -16,6 +16,13 @@ extern "C" {
// Internal limits or constants // Internal limits or constants
#define NN_KiB (1024)
#define NN_MiB (1024 * NN_KiB)
#define NN_GiB (1024 * NN_MiB)
#define NN_TiB (1024 * NN_TiB)
// the alignment an allocation should have
#define NN_ALLOC_ALIGN 16
// the maximum amount of items the callstack can have. // the maximum amount of items the callstack can have.
#define NN_MAX_STACK 256 #define NN_MAX_STACK 256
// the maximum size a path is allowed to have // the maximum size a path is allowed to have
@ -68,22 +75,28 @@ typedef double nn_TimeProc(void *state);
typedef size_t nn_RngProc(void *state); typedef size_t nn_RngProc(void *state);
typedef enum nn_LockAction { typedef enum nn_LockAction {
// init any necessary state // create the mutex
NN_LOCK_INIT, NN_LOCK_CREATE,
// cleanup any necessary state // destroy the mutex
NN_LOCK_DEINIT, NN_LOCK_DESTROY,
// lock the mutex // lock the mutex
NN_LOCK_LOCK, NN_LOCK_LOCK,
// unlock the mutex // unlock the mutex
NN_LOCK_UNLOCK, NN_LOCK_UNLOCK,
} nn_LockAction; } nn_LockAction;
typedef struct nn_LockRequest {
// mutate it for NN_LOCK_INIT
void *lock;
nn_LockAction action;
} nn_LockRequest;
// intended for a plain mutex. // intended for a plain mutex.
// This is used for synchronization. OpenComputers achieves synchronization // This is used for synchronization. OpenComputers achieves synchronization
// between the worker threads by sending them as requests to a central thread (indirect methods). // between the worker threads by sending them as requests to a central thread (indirect methods).
// In NeoNucleus, we simply use a lock. This technically makes all methods direct, however // In NeoNucleus, we simply use a lock. This technically makes all methods direct, however
// we consider methods to be indirect if they require locks. // we consider methods to be indirect if they require locks.
typedef void nn_LockProc(void *state, void *lock, nn_LockAction action); typedef void nn_LockProc(void *state, nn_LockRequest *req);
// The *context* NeoNucleus is operating in. // The *context* NeoNucleus is operating in.
// This determines: // This determines:
@ -100,8 +113,6 @@ typedef struct nn_Context {
// so rngMaximum+1 MUST NOT OVERFLOW. // so rngMaximum+1 MUST NOT OVERFLOW.
size_t rngMaximum; size_t rngMaximum;
nn_RngProc *rng; nn_RngProc *rng;
// the size of 1 lock. This is used to dynamically allocate enough space for the locks.
size_t lockSize;
nn_LockProc *lock; nn_LockProc *lock;
} nn_Context; } nn_Context;
@ -209,16 +220,19 @@ const char *nn_getComputerAddress(nn_Computer *computer);
// Sets the computer's architecture. // Sets the computer's architecture.
// The architecture determines everything from how the computer runs, to how it turns off. // The architecture determines everything from how the computer runs, to how it turns off.
// Everything is limited by the architecture. // Everything is limited by the architecture.
// The architecture is not copied, it must be valid for as long as the computer is.
void nn_setArchitecture(nn_Computer *computer, const nn_Architecture *arch); void nn_setArchitecture(nn_Computer *computer, const nn_Architecture *arch);
// Gets the current architecture. // Gets the current architecture.
const nn_Architecture *nn_getArchitecture(nn_Computer *computer); const nn_Architecture *nn_getArchitecture(nn_Computer *computer);
// Sets the computer's desired architecture. // Sets the computer's desired architecture.
// The desired architecture indicates, when the computer state is CHARCH, what the new architecture should be. // The desired architecture indicates, when the computer state is CHARCH, what the new architecture should be.
// This is set even if it is not in the supported architecture list, *you must check if it is in that list first.* // This is set even if it is not in the supported architecture list, *you must check if it is in that list first.*
// The architecture is not copied, it must be valid for as long as the computer is.
void nn_setDesiredArchitecture(nn_Computer *computer, const nn_Architecture *arch); void nn_setDesiredArchitecture(nn_Computer *computer, const nn_Architecture *arch);
// Gets the desired architecture. This is the architecture the computer should use after changing architectures. // Gets the desired architecture. This is the architecture the computer should use after changing architectures.
const nn_Architecture *nn_getDesiredArchitecture(nn_Computer *computer); const nn_Architecture *nn_getDesiredArchitecture(nn_Computer *computer);
// Adds a new supported architecture, which indicates to the code running on this computer that it is possible to switch to that architecture. // Adds a new supported architecture, which indicates to the code running on this computer that it is possible to switch to that architecture.
// The architecture is copied, it can be freed after this is called.
void nn_addSupportedArchitecture(nn_Computer *computer, const nn_Architecture *arch); void nn_addSupportedArchitecture(nn_Computer *computer, const nn_Architecture *arch);
// Returns the array of supported architectures, as well as the length. // Returns the array of supported architectures, as well as the length.
const nn_Architecture *nn_getSupportedArchitecture(nn_Computer *computer, size_t *len); const nn_Architecture *nn_getSupportedArchitecture(nn_Computer *computer, size_t *len);

View File

@ -1,11 +0,0 @@
// Includes all the C files to compile the entire library in one Compilation Unit
// this is better than LTO, but it requires more RAM and compute.
#include "nn_utils.h"
#if defined(__STDC_NO_THREADS__) || defined(NN_WINDOWS)
#include "tinycthread.c"
#endif
#include "nn_utils.c"
#include "nn_model.c"

View File

@ -1,100 +0,0 @@
#include "neonucleus.h"
#include "nn_model.h"
#include "nn_utils.h"
nn_Universe *nn_createUniverse(nn_Context *ctx) {
nn_Universe *u = nn_alloc(ctx, sizeof(nn_Universe));
if(u == NULL) return NULL;
u->ctx = *ctx;
for(size_t i = 0; i < NN_BUILTIN_COUNT; i++) u->types[i] = NULL;
return u;
}
void nn_destroyUniverse(nn_Universe *universe) {
nn_Context ctx = universe->ctx;
for(size_t i = 0; i < NN_BUILTIN_COUNT; i++) nn_destroyComponentType(universe->types[i]);
nn_free(&ctx, universe, sizeof(nn_Universe));
}
nn_ComponentType *nn_createComponentType(nn_Universe *universe, const char *name, void *userdata, const nn_ComponentMethod methods[], nn_ComponentHandler *handler) {
nn_Context *ctx = &universe->ctx;
char *namecpy = nn_strdup(ctx, name);
if(namecpy == NULL) return NULL;
size_t methodCount = 0;
while(methods[methodCount].name != NULL) methodCount++;
// include the terminator!
methodCount++;
size_t methodSize = sizeof(nn_ComponentMethod) * methodCount;
nn_ComponentMethod *methodscpy = nn_alloc(ctx, methodSize);
if(methodscpy == NULL) {
nn_strfree(ctx, namecpy);
return NULL;
}
{
// in an ideal world, I'd just implement arenas.
// I should just implement arenas ngl
// TODO: just use arenas so memory management is ultra free
size_t methodIdx = 0;
while(methodIdx < methodCount) {
if(methods[methodIdx].name == NULL) {
methodscpy[methodIdx].name = NULL;
methodscpy[methodIdx].docString = NULL;
methodscpy[methodIdx].direct = false;
continue;
}
char *namecpy = nn_strdup(ctx, methods[methodIdx].name);
if(namecpy == NULL) goto methodOom;
char *doc = nn_strdup(ctx, methods[methodIdx].docString);
if(doc == NULL) {
nn_strfree(ctx, namecpy);
goto methodOom;
}
methodscpy[methodIdx].name = namecpy;
methodscpy[methodIdx].docString = doc;
methodscpy[methodIdx].direct = methods[methodIdx].direct;
methodIdx++;
}
goto normalExec;
methodOom:
for(size_t i = 0; i < methodIdx; i++) {
nn_strfree(ctx, (char *)methodscpy[i].name);
nn_strfree(ctx, (char *)methodscpy[i].docString);
}
return NULL;
}
normalExec:;
nn_ComponentType *ctype = nn_alloc(ctx, sizeof(nn_ComponentType));
if(ctype == NULL) {
nn_strfree(ctx, namecpy);
nn_free(ctx, methodscpy, methodSize);
return NULL;
}
ctype->name = namecpy;
ctype->methods = methodscpy;
ctype->universe = universe;
return ctype;
}
void nn_destroyComponentType(nn_ComponentType *ctype) {
nn_Context *ctx = &ctype->universe->ctx;
nn_strfree(ctx, ctype->name);
size_t methodCount = 0;
while(ctype->methods[methodCount].name != NULL) {
nn_strfree(ctx, (char *)ctype->methods[methodCount].name);
nn_strfree(ctx, (char *)ctype->methods[methodCount].docString);
methodCount++;
}
// include the terminator!
methodCount++;
size_t methodSize = sizeof(nn_ComponentMethod) * methodCount;
nn_free(ctx, ctype->methods, methodSize);
nn_free(ctx, ctype, sizeof(nn_ComponentType));
}

View File

@ -1,93 +0,0 @@
#ifndef NN_MODEL_H
#define NN_MODEL_H
#include "neonucleus.h"
typedef enum nn_BuiltinComponent {
NN_BUILTIN_GPU = 0,
NN_BUILTIN_SCREEN,
NN_BUILTIN_KEYBOARD,
NN_BUILTIN_FILESYSTEM,
// to determine array size
NN_BUILTIN_COUNT,
} nn_BuiltinComponent;
typedef struct nn_ComponentType {
nn_Universe *universe;
char *name;
// NULL-terminated
nn_ComponentMethod *methods;
} nn_ComponentType;
typedef struct nn_Universe {
nn_Context ctx;
nn_ComponentType *types[NN_BUILTIN_COUNT];
} nn_Universe;
typedef struct nn_Component {
char *address;
nn_ComponentType *ctype;
size_t slot;
void *userdata;
} nn_Component;
// the values
typedef enum nn_ValueType {
NN_VAL_NULL,
NN_VAL_BOOL,
NN_VAL_NUM,
NN_VAL_STR,
NN_VAL_USERDATA,
NN_VAL_TABLE,
} nn_ValueType;
typedef struct nn_String {
nn_Context ctx;
size_t refc;
size_t len;
char data[];
} nn_String;
typedef struct nn_Value {
nn_ValueType type;
union {
bool boolean;
double number;
nn_String *string;
size_t userdataIdx;
struct nn_Table *table;
};
} nn_Value;
typedef struct nn_Table {
nn_Context ctx;
size_t refc;
size_t len;
nn_Value vals[];
} nn_Table;
typedef struct nn_Computer {
nn_ComputerState state;
nn_Universe *universe;
char *address;
nn_Architecture *arch;
nn_Architecture *desiredArch;
size_t componentCap;
size_t componentLen;
nn_Component *components;
size_t deviceInfoCap;
size_t deviceInfoLen;
nn_DeviceInfo *deviceInfo;
double totalEnergy;
double energy;
size_t totalMemory;
double creationTimestamp;
size_t stackSize;
size_t archCount;
nn_Value callstack[NN_MAX_STACK];
char errorBuffer[NN_MAX_ERROR_SIZE];
nn_Architecture archs[NN_MAX_ARCHITECTURES];
} nn_Computer;
#endif

View File

@ -1,174 +0,0 @@
#include "neonucleus.h"
#include "nn_utils.h"
#ifndef NN_BAREMETAL
#include <stdlib.h>
#include <time.h>
#if defined(__STDC_NO_THREADS__) || defined(NN_WINDOWS) // fuck you Windows
#include "tinycthread.h"
#else
#include <threads.h>
#endif
#endif
void *nn_alloc(nn_Context *ctx, size_t size) {
if(size == 0) return ctx->alloc;
return ctx->alloc(ctx->state, NULL, 0, size);
}
void *nn_realloc(nn_Context *ctx, void *memory, size_t oldSize, size_t newSize) {
if(memory == ctx->alloc) return nn_alloc(memory, newSize);
if(newSize == 0) {
nn_free(ctx, memory, oldSize);
return ctx->alloc;
}
return ctx->alloc(ctx->state, memory, oldSize, newSize);
}
void nn_free(nn_Context *ctx, void *memory, size_t size) {
if(memory == ctx->alloc) return;
ctx->alloc(ctx->state, memory, size, 0);
}
size_t nn_strlen(const char *s) {
size_t l = 0;
while(*(s++) != '\0') l++;
return l;
}
char *nn_strdup(nn_Context *ctx, const char *s) {
size_t l = nn_strlen(s);
char *buf = nn_alloc(ctx, sizeof(char) * (l+1));
if(buf == NULL) return NULL;
nn_memcpy(buf, s, sizeof(char) * l);
buf[l] = '\0';
return buf;
}
void nn_strfree(nn_Context *ctx, char *s) {
size_t l = nn_strlen(s);
nn_free(ctx, s, sizeof(char) * (l+1));
}
void nn_memcpy(void *dest, const void *src, size_t len) {
char *out = (char *)dest;
const char *in = (const char *)src;
for(size_t i = 0; i < len; i++) out[i] = in[i];
}
void nn_memset(void *dest, int x, size_t len) {
char *out = (char *)dest;
for(size_t i = 0; i < len; i++) out[i] = (char)x;
}
nn_Lock *nn_createLock(nn_Context *ctx) {
void *mem = nn_alloc(ctx, ctx->lockSize);
if(mem == NULL) return NULL;
ctx->lock(ctx->state, mem, NN_LOCK_INIT);
return mem;
}
void nn_destroyLock(nn_Context *ctx, nn_Lock *lock) {
if(lock == NULL) return;
ctx->lock(ctx->state, lock, NN_LOCK_DEINIT);
nn_free(ctx, lock, ctx->lockSize);
}
void nn_lock(nn_Context *ctx, nn_Lock *lock) {
ctx->lock(ctx->state, lock, NN_LOCK_LOCK);
}
void nn_unlock(nn_Context *ctx, nn_Lock *lock) {
ctx->lock(ctx->state, lock, NN_LOCK_UNLOCK);
}
double nn_currentTime(nn_Context *ctx) {
return ctx->time(ctx->state);
}
size_t nn_rand(nn_Context *ctx) {
return ctx->rng(ctx->state);
}
double nn_randf(nn_Context *ctx) {
double n = (double)nn_rand(ctx);
return n / (double)(ctx->rngMaximum + 1);
}
double nn_randfi(nn_Context *ctx) {
double n = (double)nn_rand(ctx);
return n / (double)ctx->rngMaximum;
}
static void *nn_defaultAlloc(void *_, void *memory, size_t oldSize, size_t newSize) {
#ifndef NN_BAREMETAL
if(newSize == 0) {
free(memory);
return NULL;
}
return realloc(memory, newSize);
#else
// 0 memory available
return NULL;
#endif
}
static double nn_defaultTime(void *_) {
#ifndef NN_BAREMETAL
// time does not exist... yet!
return 0;
#else
// time does not exist
return 0;
#endif
}
static size_t nn_defaultRng(void *_) {
#ifndef NN_BAREMETAL
return rand();
#else
// insane levels of RNG
return 1;
#endif
}
static void nn_defaultLock(void *state, void *lock, nn_LockAction action) {
#ifndef NN_BAREMETAL
switch(action) {
case NN_LOCK_INIT:
mtx_init(lock, mtx_plain);
break;
case NN_LOCK_DEINIT:
mtx_destroy(lock);
break;
case NN_LOCK_LOCK:
mtx_lock(lock);
break;
case NN_LOCK_UNLOCK:
mtx_unlock(lock);
break;
}
#endif
}
void nn_initContext(nn_Context *ctx) {
ctx->state = NULL;
ctx->alloc = nn_defaultAlloc;
ctx->time = nn_defaultTime;
#ifndef NN_BAREMETAL
// someone pointed out that running this multiple times
// in 1 second can cause the RNG to loop.
// However, if you call this function multiple times at all,
// that's on you.
srand(time(NULL));
ctx->rngMaximum = RAND_MAX;
ctx->lockSize = sizeof(mtx_t);
#else
ctx->rngMaximum = 1;
ctx->lockSize = 0;
#endif
ctx->rng = nn_defaultRng;
ctx->lock = nn_defaultLock;
}

View File

@ -1,71 +0,0 @@
#ifndef NN_UTILS_H
#define NN_UTILS_H
#include "neonucleus.h"
void *nn_alloc(nn_Context *ctx, size_t size);
void *nn_realloc(nn_Context *ctx, void *memory, size_t oldSize, size_t newSize);
void nn_free(nn_Context *ctx, void *memory, size_t size);
size_t nn_strlen(const char *s);
char *nn_strdup(nn_Context *ctx, const char *s);
void nn_strfree(nn_Context *ctx, char *s);
void nn_memcpy(void *dest, const void *src, size_t len);
void nn_memset(void *dest, int x, size_t len);
typedef struct nn_Lock nn_Lock;
nn_Lock *nn_createLock(nn_Context *ctx);
void nn_destroyLock(nn_Context *ctx, nn_Lock *lock);
void nn_lock(nn_Context *ctx, nn_Lock *lock);
void nn_unlock(nn_Context *ctx, nn_Lock *lock);
double nn_currentTime(nn_Context *ctx);
size_t nn_rand(nn_Context *ctx);
// nn_rand but in the range of [0, 1)
double nn_randf(nn_Context *ctx);
// nn_rand but in the range of [0, 1] (aka both inclusive)
double nn_randfi(nn_Context *ctx);
// Based off https://stackoverflow.com/questions/5919996/how-to-detect-reliably-mac-os-x-ios-linux-windows-in-c-preprocessor
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
//define something for Windows (32-bit and 64-bit, this part is common)
#ifdef _WIN64
#define NN_WINDOWS
#else
#error "Windows 32-bit is not supported"
#endif
#elif __APPLE__
#include <TargetConditionals.h>
#if TARGET_IPHONE_SIMULATOR
#error "iPhone Emulators are not supported"
#elif TARGET_OS_MACCATALYST
// I guess?
#define NN_MACOS
#elif TARGET_OS_IPHONE
#error "iPhone are not supported"
#elif TARGET_OS_MAC
#define NN_MACOS
#else
#error "Unknown Apple platform"
#endif
#elif __ANDROID__
#error "Android is not supported"
#elif __linux__
#define NN_LINUX
#endif
#if __unix__ // all unices not caught above
// Unix
#define NN_UNIX
#define NN_POSIX
#elif defined(_POSIX_VERSION)
// POSIX
#define NN_POSIX
#endif
#endif

View File

@ -1,934 +0,0 @@
// Not created by NeoNucleus project authors.
// It was not modified aside from the writing of this comment.
/* -*- mode: c; tab-width: 2; indent-tabs-mode: nil; -*-
Copyright (c) 2012 Marcus Geelnard
Copyright (c) 2013-2016 Evan Nemerson
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "tinycthread.h"
#include <stdlib.h>
/* Platform specific includes */
#if defined(_TTHREAD_POSIX_)
#include <signal.h>
#include <sched.h>
#include <unistd.h>
#include <sys/time.h>
#include <errno.h>
#elif defined(_TTHREAD_WIN32_)
#include <process.h>
#include <sys/timeb.h>
#endif
/* Standard, good-to-have defines */
#ifndef NULL
#define NULL (void*)0
#endif
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifdef __cplusplus
extern "C" {
#endif
int mtx_init(mtx_t *mtx, int type)
{
#if defined(_TTHREAD_WIN32_)
mtx->mAlreadyLocked = FALSE;
mtx->mRecursive = type & mtx_recursive;
mtx->mTimed = type & mtx_timed;
if (!mtx->mTimed)
{
InitializeCriticalSection(&(mtx->mHandle.cs));
}
else
{
mtx->mHandle.mut = CreateMutex(NULL, FALSE, NULL);
if (mtx->mHandle.mut == NULL)
{
return thrd_error;
}
}
return thrd_success;
#else
int ret;
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
if (type & mtx_recursive)
{
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
}
ret = pthread_mutex_init(mtx, &attr);
pthread_mutexattr_destroy(&attr);
return ret == 0 ? thrd_success : thrd_error;
#endif
}
void mtx_destroy(mtx_t *mtx)
{
#if defined(_TTHREAD_WIN32_)
if (!mtx->mTimed)
{
DeleteCriticalSection(&(mtx->mHandle.cs));
}
else
{
CloseHandle(mtx->mHandle.mut);
}
#else
pthread_mutex_destroy(mtx);
#endif
}
int mtx_lock(mtx_t *mtx)
{
#if defined(_TTHREAD_WIN32_)
if (!mtx->mTimed)
{
EnterCriticalSection(&(mtx->mHandle.cs));
}
else
{
switch (WaitForSingleObject(mtx->mHandle.mut, INFINITE))
{
case WAIT_OBJECT_0:
break;
case WAIT_ABANDONED:
default:
return thrd_error;
}
}
if (!mtx->mRecursive)
{
while(mtx->mAlreadyLocked) Sleep(1); /* Simulate deadlock... */
mtx->mAlreadyLocked = TRUE;
}
return thrd_success;
#else
return pthread_mutex_lock(mtx) == 0 ? thrd_success : thrd_error;
#endif
}
int mtx_timedlock(mtx_t *mtx, const struct timespec *ts)
{
#if defined(_TTHREAD_WIN32_)
struct timespec current_ts;
DWORD timeoutMs;
if (!mtx->mTimed)
{
return thrd_error;
}
timespec_get(&current_ts, TIME_UTC);
if ((current_ts.tv_sec > ts->tv_sec) || ((current_ts.tv_sec == ts->tv_sec) && (current_ts.tv_nsec >= ts->tv_nsec)))
{
timeoutMs = 0;
}
else
{
timeoutMs = (DWORD)(ts->tv_sec - current_ts.tv_sec) * 1000;
timeoutMs += (ts->tv_nsec - current_ts.tv_nsec) / 1000000;
timeoutMs += 1;
}
/* TODO: the timeout for WaitForSingleObject doesn't include time
while the computer is asleep. */
switch (WaitForSingleObject(mtx->mHandle.mut, timeoutMs))
{
case WAIT_OBJECT_0:
break;
case WAIT_TIMEOUT:
return thrd_timedout;
case WAIT_ABANDONED:
default:
return thrd_error;
}
if (!mtx->mRecursive)
{
while(mtx->mAlreadyLocked) Sleep(1); /* Simulate deadlock... */
mtx->mAlreadyLocked = TRUE;
}
return thrd_success;
#elif defined(_POSIX_TIMEOUTS) && (_POSIX_TIMEOUTS >= 200112L) && defined(_POSIX_THREADS) && (_POSIX_THREADS >= 200112L)
switch (pthread_mutex_timedlock(mtx, ts)) {
case 0:
return thrd_success;
case ETIMEDOUT:
return thrd_timedout;
default:
return thrd_error;
}
#else
int rc;
struct timespec cur, dur;
/* Try to acquire the lock and, if we fail, sleep for 5ms. */
while ((rc = pthread_mutex_trylock (mtx)) == EBUSY) {
timespec_get(&cur, TIME_UTC);
if ((cur.tv_sec > ts->tv_sec) || ((cur.tv_sec == ts->tv_sec) && (cur.tv_nsec >= ts->tv_nsec)))
{
break;
}
dur.tv_sec = ts->tv_sec - cur.tv_sec;
dur.tv_nsec = ts->tv_nsec - cur.tv_nsec;
if (dur.tv_nsec < 0)
{
dur.tv_sec--;
dur.tv_nsec += 1000000000;
}
if ((dur.tv_sec != 0) || (dur.tv_nsec > 5000000))
{
dur.tv_sec = 0;
dur.tv_nsec = 5000000;
}
nanosleep(&dur, NULL);
}
switch (rc) {
case 0:
return thrd_success;
case ETIMEDOUT:
case EBUSY:
return thrd_timedout;
default:
return thrd_error;
}
#endif
}
int mtx_trylock(mtx_t *mtx)
{
#if defined(_TTHREAD_WIN32_)
int ret;
if (!mtx->mTimed)
{
ret = TryEnterCriticalSection(&(mtx->mHandle.cs)) ? thrd_success : thrd_busy;
}
else
{
ret = (WaitForSingleObject(mtx->mHandle.mut, 0) == WAIT_OBJECT_0) ? thrd_success : thrd_busy;
}
if ((!mtx->mRecursive) && (ret == thrd_success))
{
if (mtx->mAlreadyLocked)
{
LeaveCriticalSection(&(mtx->mHandle.cs));
ret = thrd_busy;
}
else
{
mtx->mAlreadyLocked = TRUE;
}
}
return ret;
#else
return (pthread_mutex_trylock(mtx) == 0) ? thrd_success : thrd_busy;
#endif
}
int mtx_unlock(mtx_t *mtx)
{
#if defined(_TTHREAD_WIN32_)
mtx->mAlreadyLocked = FALSE;
if (!mtx->mTimed)
{
LeaveCriticalSection(&(mtx->mHandle.cs));
}
else
{
if (!ReleaseMutex(mtx->mHandle.mut))
{
return thrd_error;
}
}
return thrd_success;
#else
return pthread_mutex_unlock(mtx) == 0 ? thrd_success : thrd_error;;
#endif
}
#if defined(_TTHREAD_WIN32_)
#define _CONDITION_EVENT_ONE 0
#define _CONDITION_EVENT_ALL 1
#endif
int cnd_init(cnd_t *cond)
{
#if defined(_TTHREAD_WIN32_)
cond->mWaitersCount = 0;
/* Init critical section */
InitializeCriticalSection(&cond->mWaitersCountLock);
/* Init events */
cond->mEvents[_CONDITION_EVENT_ONE] = CreateEvent(NULL, FALSE, FALSE, NULL);
if (cond->mEvents[_CONDITION_EVENT_ONE] == NULL)
{
cond->mEvents[_CONDITION_EVENT_ALL] = NULL;
return thrd_error;
}
cond->mEvents[_CONDITION_EVENT_ALL] = CreateEvent(NULL, TRUE, FALSE, NULL);
if (cond->mEvents[_CONDITION_EVENT_ALL] == NULL)
{
CloseHandle(cond->mEvents[_CONDITION_EVENT_ONE]);
cond->mEvents[_CONDITION_EVENT_ONE] = NULL;
return thrd_error;
}
return thrd_success;
#else
return pthread_cond_init(cond, NULL) == 0 ? thrd_success : thrd_error;
#endif
}
void cnd_destroy(cnd_t *cond)
{
#if defined(_TTHREAD_WIN32_)
if (cond->mEvents[_CONDITION_EVENT_ONE] != NULL)
{
CloseHandle(cond->mEvents[_CONDITION_EVENT_ONE]);
}
if (cond->mEvents[_CONDITION_EVENT_ALL] != NULL)
{
CloseHandle(cond->mEvents[_CONDITION_EVENT_ALL]);
}
DeleteCriticalSection(&cond->mWaitersCountLock);
#else
pthread_cond_destroy(cond);
#endif
}
int cnd_signal(cnd_t *cond)
{
#if defined(_TTHREAD_WIN32_)
int haveWaiters;
/* Are there any waiters? */
EnterCriticalSection(&cond->mWaitersCountLock);
haveWaiters = (cond->mWaitersCount > 0);
LeaveCriticalSection(&cond->mWaitersCountLock);
/* If we have any waiting threads, send them a signal */
if(haveWaiters)
{
if (SetEvent(cond->mEvents[_CONDITION_EVENT_ONE]) == 0)
{
return thrd_error;
}
}
return thrd_success;
#else
return pthread_cond_signal(cond) == 0 ? thrd_success : thrd_error;
#endif
}
int cnd_broadcast(cnd_t *cond)
{
#if defined(_TTHREAD_WIN32_)
int haveWaiters;
/* Are there any waiters? */
EnterCriticalSection(&cond->mWaitersCountLock);
haveWaiters = (cond->mWaitersCount > 0);
LeaveCriticalSection(&cond->mWaitersCountLock);
/* If we have any waiting threads, send them a signal */
if(haveWaiters)
{
if (SetEvent(cond->mEvents[_CONDITION_EVENT_ALL]) == 0)
{
return thrd_error;
}
}
return thrd_success;
#else
return pthread_cond_broadcast(cond) == 0 ? thrd_success : thrd_error;
#endif
}
#if defined(_TTHREAD_WIN32_)
static int _cnd_timedwait_win32(cnd_t *cond, mtx_t *mtx, DWORD timeout)
{
DWORD result;
int lastWaiter;
/* Increment number of waiters */
EnterCriticalSection(&cond->mWaitersCountLock);
++ cond->mWaitersCount;
LeaveCriticalSection(&cond->mWaitersCountLock);
/* Release the mutex while waiting for the condition (will decrease
the number of waiters when done)... */
mtx_unlock(mtx);
/* Wait for either event to become signaled due to cnd_signal() or
cnd_broadcast() being called */
result = WaitForMultipleObjects(2, cond->mEvents, FALSE, timeout);
if (result == WAIT_TIMEOUT)
{
/* The mutex is locked again before the function returns, even if an error occurred */
mtx_lock(mtx);
return thrd_timedout;
}
else if (result == WAIT_FAILED)
{
/* The mutex is locked again before the function returns, even if an error occurred */
mtx_lock(mtx);
return thrd_error;
}
/* Check if we are the last waiter */
EnterCriticalSection(&cond->mWaitersCountLock);
-- cond->mWaitersCount;
lastWaiter = (result == (WAIT_OBJECT_0 + _CONDITION_EVENT_ALL)) &&
(cond->mWaitersCount == 0);
LeaveCriticalSection(&cond->mWaitersCountLock);
/* If we are the last waiter to be notified to stop waiting, reset the event */
if (lastWaiter)
{
if (ResetEvent(cond->mEvents[_CONDITION_EVENT_ALL]) == 0)
{
/* The mutex is locked again before the function returns, even if an error occurred */
mtx_lock(mtx);
return thrd_error;
}
}
/* Re-acquire the mutex */
mtx_lock(mtx);
return thrd_success;
}
#endif
int cnd_wait(cnd_t *cond, mtx_t *mtx)
{
#if defined(_TTHREAD_WIN32_)
return _cnd_timedwait_win32(cond, mtx, INFINITE);
#else
return pthread_cond_wait(cond, mtx) == 0 ? thrd_success : thrd_error;
#endif
}
int cnd_timedwait(cnd_t *cond, mtx_t *mtx, const struct timespec *ts)
{
#if defined(_TTHREAD_WIN32_)
struct timespec now;
if (timespec_get(&now, TIME_UTC) == TIME_UTC)
{
unsigned long long nowInMilliseconds = now.tv_sec * 1000 + now.tv_nsec / 1000000;
unsigned long long tsInMilliseconds = ts->tv_sec * 1000 + ts->tv_nsec / 1000000;
DWORD delta = (tsInMilliseconds > nowInMilliseconds) ?
(DWORD)(tsInMilliseconds - nowInMilliseconds) : 0;
return _cnd_timedwait_win32(cond, mtx, delta);
}
else
return thrd_error;
#else
int ret;
ret = pthread_cond_timedwait(cond, mtx, ts);
if (ret == ETIMEDOUT)
{
return thrd_timedout;
}
return ret == 0 ? thrd_success : thrd_error;
#endif
}
#if defined(_TTHREAD_WIN32_)
struct TinyCThreadTSSData {
void* value;
tss_t key;
struct TinyCThreadTSSData* next;
};
static tss_dtor_t _tinycthread_tss_dtors[1088] = { NULL, };
static _Thread_local struct TinyCThreadTSSData* _tinycthread_tss_head = NULL;
static _Thread_local struct TinyCThreadTSSData* _tinycthread_tss_tail = NULL;
static void _tinycthread_tss_cleanup (void);
static void _tinycthread_tss_cleanup (void) {
struct TinyCThreadTSSData* data;
int iteration;
unsigned int again = 1;
void* value;
for (iteration = 0 ; iteration < TSS_DTOR_ITERATIONS && again > 0 ; iteration++)
{
again = 0;
for (data = _tinycthread_tss_head ; data != NULL ; data = data->next)
{
if (data->value != NULL)
{
value = data->value;
data->value = NULL;
if (_tinycthread_tss_dtors[data->key] != NULL)
{
again = 1;
_tinycthread_tss_dtors[data->key](value);
}
}
}
}
while (_tinycthread_tss_head != NULL) {
data = _tinycthread_tss_head->next;
free (_tinycthread_tss_head);
_tinycthread_tss_head = data;
}
_tinycthread_tss_head = NULL;
_tinycthread_tss_tail = NULL;
}
static void NTAPI _tinycthread_tss_callback(PVOID h, DWORD dwReason, PVOID pv)
{
(void)h;
(void)pv;
if (_tinycthread_tss_head != NULL && (dwReason == DLL_THREAD_DETACH || dwReason == DLL_PROCESS_DETACH))
{
_tinycthread_tss_cleanup();
}
}
#if defined(_MSC_VER)
#ifdef _M_X64
#pragma const_seg(".CRT$XLB")
#else
#pragma data_seg(".CRT$XLB")
#endif
PIMAGE_TLS_CALLBACK p_thread_callback = _tinycthread_tss_callback;
#ifdef _M_X64
#pragma data_seg()
#else
#pragma const_seg()
#endif
#else
PIMAGE_TLS_CALLBACK p_thread_callback __attribute__((section(".CRT$XLB"))) = _tinycthread_tss_callback;
#endif
#endif /* defined(_TTHREAD_WIN32_) */
/** Information to pass to the new thread (what to run). */
typedef struct {
thrd_start_t mFunction; /**< Pointer to the function to be executed. */
void * mArg; /**< Function argument for the thread function. */
} _thread_start_info;
/* Thread wrapper function. */
#if defined(_TTHREAD_WIN32_)
static DWORD WINAPI _thrd_wrapper_function(LPVOID aArg)
#elif defined(_TTHREAD_POSIX_)
static void * _thrd_wrapper_function(void * aArg)
#endif
{
thrd_start_t fun;
void *arg;
int res;
/* Get thread startup information */
_thread_start_info *ti = (_thread_start_info *) aArg;
fun = ti->mFunction;
arg = ti->mArg;
/* The thread is responsible for freeing the startup information */
free((void *)ti);
/* Call the actual client thread function */
res = fun(arg);
#if defined(_TTHREAD_WIN32_)
if (_tinycthread_tss_head != NULL)
{
_tinycthread_tss_cleanup();
}
return (DWORD)res;
#else
return (void*)(intptr_t)res;
#endif
}
int thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
{
/* Fill out the thread startup information (passed to the thread wrapper,
which will eventually free it) */
_thread_start_info* ti = (_thread_start_info*)malloc(sizeof(_thread_start_info));
if (ti == NULL)
{
return thrd_nomem;
}
ti->mFunction = func;
ti->mArg = arg;
/* Create the thread */
#if defined(_TTHREAD_WIN32_)
*thr = CreateThread(NULL, 0, _thrd_wrapper_function, (LPVOID) ti, 0, NULL);
#elif defined(_TTHREAD_POSIX_)
if(pthread_create(thr, NULL, _thrd_wrapper_function, (void *)ti) != 0)
{
*thr = 0;
}
#endif
/* Did we fail to create the thread? */
if(!*thr)
{
free(ti);
return thrd_error;
}
return thrd_success;
}
thrd_t thrd_current(void)
{
#if defined(_TTHREAD_WIN32_)
return GetCurrentThread();
#else
return pthread_self();
#endif
}
int thrd_detach(thrd_t thr)
{
#if defined(_TTHREAD_WIN32_)
/* https://stackoverflow.com/questions/12744324/how-to-detach-a-thread-on-windows-c#answer-12746081 */
return CloseHandle(thr) != 0 ? thrd_success : thrd_error;
#else
return pthread_detach(thr) == 0 ? thrd_success : thrd_error;
#endif
}
int thrd_equal(thrd_t thr0, thrd_t thr1)
{
#if defined(_TTHREAD_WIN32_)
return GetThreadId(thr0) == GetThreadId(thr1);
#else
return pthread_equal(thr0, thr1);
#endif
}
void thrd_exit(int res)
{
#if defined(_TTHREAD_WIN32_)
if (_tinycthread_tss_head != NULL)
{
_tinycthread_tss_cleanup();
}
ExitThread((DWORD)res);
#else
pthread_exit((void*)(intptr_t)res);
#endif
}
int thrd_join(thrd_t thr, int *res)
{
#if defined(_TTHREAD_WIN32_)
DWORD dwRes;
if (WaitForSingleObject(thr, INFINITE) == WAIT_FAILED)
{
return thrd_error;
}
if (res != NULL)
{
if (GetExitCodeThread(thr, &dwRes) != 0)
{
*res = (int) dwRes;
}
else
{
return thrd_error;
}
}
CloseHandle(thr);
#elif defined(_TTHREAD_POSIX_)
void *pres;
if (pthread_join(thr, &pres) != 0)
{
return thrd_error;
}
if (res != NULL)
{
*res = (int)(intptr_t)pres;
}
#endif
return thrd_success;
}
int thrd_sleep(const struct timespec *duration, struct timespec *remaining)
{
#if !defined(_TTHREAD_WIN32_)
int res = nanosleep(duration, remaining);
if (res == 0) {
return 0;
} else if (errno == EINTR) {
return -1;
} else {
return -2;
}
#else
struct timespec start;
DWORD t;
timespec_get(&start, TIME_UTC);
t = SleepEx((DWORD)(duration->tv_sec * 1000 +
duration->tv_nsec / 1000000 +
(((duration->tv_nsec % 1000000) == 0) ? 0 : 1)),
TRUE);
if (t == 0) {
return 0;
} else {
if (remaining != NULL) {
timespec_get(remaining, TIME_UTC);
remaining->tv_sec -= start.tv_sec;
remaining->tv_nsec -= start.tv_nsec;
if (remaining->tv_nsec < 0)
{
remaining->tv_nsec += 1000000000;
remaining->tv_sec -= 1;
}
}
return (t == WAIT_IO_COMPLETION) ? -1 : -2;
}
#endif
}
void thrd_yield(void)
{
#if defined(_TTHREAD_WIN32_)
Sleep(0);
#else
sched_yield();
#endif
}
int tss_create(tss_t *key, tss_dtor_t dtor)
{
#if defined(_TTHREAD_WIN32_)
*key = TlsAlloc();
if (*key == TLS_OUT_OF_INDEXES)
{
return thrd_error;
}
_tinycthread_tss_dtors[*key] = dtor;
#else
if (pthread_key_create(key, dtor) != 0)
{
return thrd_error;
}
#endif
return thrd_success;
}
void tss_delete(tss_t key)
{
#if defined(_TTHREAD_WIN32_)
struct TinyCThreadTSSData* data = (struct TinyCThreadTSSData*) TlsGetValue (key);
struct TinyCThreadTSSData* prev = NULL;
if (data != NULL)
{
if (data == _tinycthread_tss_head)
{
_tinycthread_tss_head = data->next;
}
else
{
prev = _tinycthread_tss_head;
if (prev != NULL)
{
while (prev->next != data)
{
prev = prev->next;
}
}
}
if (data == _tinycthread_tss_tail)
{
_tinycthread_tss_tail = prev;
}
free (data);
}
_tinycthread_tss_dtors[key] = NULL;
TlsFree(key);
#else
pthread_key_delete(key);
#endif
}
void *tss_get(tss_t key)
{
#if defined(_TTHREAD_WIN32_)
struct TinyCThreadTSSData* data = (struct TinyCThreadTSSData*)TlsGetValue(key);
if (data == NULL)
{
return NULL;
}
return data->value;
#else
return pthread_getspecific(key);
#endif
}
int tss_set(tss_t key, void *val)
{
#if defined(_TTHREAD_WIN32_)
struct TinyCThreadTSSData* data = (struct TinyCThreadTSSData*)TlsGetValue(key);
if (data == NULL)
{
data = (struct TinyCThreadTSSData*)malloc(sizeof(struct TinyCThreadTSSData));
if (data == NULL)
{
return thrd_error;
}
data->value = NULL;
data->key = key;
data->next = NULL;
if (_tinycthread_tss_tail != NULL)
{
_tinycthread_tss_tail->next = data;
}
else
{
_tinycthread_tss_tail = data;
}
if (_tinycthread_tss_head == NULL)
{
_tinycthread_tss_head = data;
}
if (!TlsSetValue(key, data))
{
free (data);
return thrd_error;
}
}
data->value = val;
#else
if (pthread_setspecific(key, val) != 0)
{
return thrd_error;
}
#endif
return thrd_success;
}
#if defined(_TTHREAD_EMULATE_TIMESPEC_GET_)
int _tthread_timespec_get(struct timespec *ts, int base)
{
#if defined(_TTHREAD_WIN32_)
struct _timeb tb;
#elif !defined(CLOCK_REALTIME)
struct timeval tv;
#endif
if (base != TIME_UTC)
{
return 0;
}
#if defined(_TTHREAD_WIN32_)
_ftime_s(&tb);
ts->tv_sec = (time_t)tb.time;
ts->tv_nsec = 1000000L * (long)tb.millitm;
#elif defined(CLOCK_REALTIME)
base = (clock_gettime(CLOCK_REALTIME, ts) == 0) ? base : 0;
#else
gettimeofday(&tv, NULL);
ts->tv_sec = (time_t)tv.tv_sec;
ts->tv_nsec = 1000L * (long)tv.tv_usec;
#endif
return base;
}
#endif /* _TTHREAD_EMULATE_TIMESPEC_GET_ */
#if defined(_TTHREAD_WIN32_)
void call_once(once_flag *flag, void (*func)(void))
{
/* The idea here is that we use a spin lock (via the
InterlockedCompareExchange function) to restrict access to the
critical section until we have initialized it, then we use the
critical section to block until the callback has completed
execution. */
while (flag->status < 3)
{
switch (flag->status)
{
case 0:
if (InterlockedCompareExchange (&(flag->status), 1, 0) == 0) {
InitializeCriticalSection(&(flag->lock));
EnterCriticalSection(&(flag->lock));
flag->status = 2;
func();
flag->status = 3;
LeaveCriticalSection(&(flag->lock));
return;
}
break;
case 1:
break;
case 2:
EnterCriticalSection(&(flag->lock));
LeaveCriticalSection(&(flag->lock));
break;
}
}
}
#endif /* defined(_TTHREAD_WIN32_) */
#ifdef __cplusplus
}
#endif

View File

@ -1,479 +0,0 @@
/* -*- mode: c; tab-width: 2; indent-tabs-mode: nil; -*-
Copyright (c) 2012 Marcus Geelnard
Copyright (c) 2013-2016 Evan Nemerson
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#ifndef _TINYCTHREAD_H_
#define _TINYCTHREAD_H_
#ifdef __cplusplus
extern "C" {
#endif
/**
* @file
* @mainpage TinyCThread API Reference
*
* @section intro_sec Introduction
* TinyCThread is a minimal, portable implementation of basic threading
* classes for C.
*
* They closely mimic the functionality and naming of the C11 standard, and
* should be easily replaceable with the corresponding standard variants.
*
* @section port_sec Portability
* The Win32 variant uses the native Win32 API for implementing the thread
* classes, while for other systems, the POSIX threads API (pthread) is used.
*
* @section misc_sec Miscellaneous
* The following special keywords are available: #_Thread_local.
*
* For more detailed information, browse the different sections of this
* documentation. A good place to start is:
* tinycthread.h.
*/
/* Which platform are we on? */
#if !defined(_TTHREAD_PLATFORM_DEFINED_)
#if defined(_WIN32) || defined(__WIN32__) || defined(__WINDOWS__)
#define _TTHREAD_WIN32_
#else
#define _TTHREAD_POSIX_
#endif
#define _TTHREAD_PLATFORM_DEFINED_
#endif
/* Activate some POSIX functionality (e.g. clock_gettime and recursive mutexes) */
#if defined(_TTHREAD_POSIX_)
#undef _FEATURES_H
#if !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#if !defined(_POSIX_C_SOURCE) || ((_POSIX_C_SOURCE - 0) < 199309L)
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 199309L
#endif
#if !defined(_XOPEN_SOURCE) || ((_XOPEN_SOURCE - 0) < 500)
#undef _XOPEN_SOURCE
#define _XOPEN_SOURCE 500
#endif
#define _XPG6
#endif
/* Generic includes */
#include <time.h>
/* Platform specific includes */
#if defined(_TTHREAD_POSIX_)
#include <pthread.h>
#elif defined(_TTHREAD_WIN32_)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#define __UNDEF_LEAN_AND_MEAN
#endif
#include <windows.h>
#ifdef __UNDEF_LEAN_AND_MEAN
#undef WIN32_LEAN_AND_MEAN
#undef __UNDEF_LEAN_AND_MEAN
#endif
#endif
/* Compiler-specific information */
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
#define TTHREAD_NORETURN _Noreturn
#elif defined(__GNUC__)
#define TTHREAD_NORETURN __attribute__((__noreturn__))
#else
#define TTHREAD_NORETURN
#endif
/* If TIME_UTC is missing, provide it and provide a wrapper for
timespec_get. */
#ifndef TIME_UTC
#define TIME_UTC 1
#define _TTHREAD_EMULATE_TIMESPEC_GET_
#if defined(_TTHREAD_WIN32_)
struct _tthread_timespec {
time_t tv_sec;
long tv_nsec;
};
#define timespec _tthread_timespec
#endif
int _tthread_timespec_get(struct timespec *ts, int base);
#define timespec_get _tthread_timespec_get
#endif
/** TinyCThread version (major number). */
#define TINYCTHREAD_VERSION_MAJOR 1
/** TinyCThread version (minor number). */
#define TINYCTHREAD_VERSION_MINOR 2
/** TinyCThread version (full version). */
#define TINYCTHREAD_VERSION (TINYCTHREAD_VERSION_MAJOR * 100 + TINYCTHREAD_VERSION_MINOR)
/**
* @def _Thread_local
* Thread local storage keyword.
* A variable that is declared with the @c _Thread_local keyword makes the
* value of the variable local to each thread (known as thread-local storage,
* or TLS). Example usage:
* @code
* // This variable is local to each thread.
* _Thread_local int variable;
* @endcode
* @note The @c _Thread_local keyword is a macro that maps to the corresponding
* compiler directive (e.g. @c __declspec(thread)).
* @note This directive is currently not supported on Mac OS X (it will give
* a compiler error), since compile-time TLS is not supported in the Mac OS X
* executable format. Also, some older versions of MinGW (before GCC 4.x) do
* not support this directive, nor does the Tiny C Compiler.
* @hideinitializer
*/
#if !(defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201102L)) && !defined(_Thread_local)
#if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_CC) || defined(__IBMCPP__)
#define _Thread_local __thread
#else
#define _Thread_local __declspec(thread)
#endif
#elif defined(__GNUC__) && defined(__GNUC_MINOR__) && (((__GNUC__ << 8) | __GNUC_MINOR__) < ((4 << 8) | 9))
#define _Thread_local __thread
#endif
/* Macros */
#if defined(_TTHREAD_WIN32_)
#define TSS_DTOR_ITERATIONS (4)
#else
#define TSS_DTOR_ITERATIONS PTHREAD_DESTRUCTOR_ITERATIONS
#endif
/* Function return values */
#define thrd_error 0 /**< The requested operation failed */
#define thrd_success 1 /**< The requested operation succeeded */
#define thrd_timedout 2 /**< The time specified in the call was reached without acquiring the requested resource */
#define thrd_busy 3 /**< The requested operation failed because a tesource requested by a test and return function is already in use */
#define thrd_nomem 4 /**< The requested operation failed because it was unable to allocate memory */
/* Mutex types */
#define mtx_plain 0
#define mtx_timed 1
#define mtx_recursive 2
/* Mutex */
#if defined(_TTHREAD_WIN32_)
typedef struct {
union {
CRITICAL_SECTION cs; /* Critical section handle (used for non-timed mutexes) */
HANDLE mut; /* Mutex handle (used for timed mutex) */
} mHandle; /* Mutex handle */
int mAlreadyLocked; /* TRUE if the mutex is already locked */
int mRecursive; /* TRUE if the mutex is recursive */
int mTimed; /* TRUE if the mutex is timed */
} mtx_t;
#else
typedef pthread_mutex_t mtx_t;
#endif
/** Create a mutex object.
* @param mtx A mutex object.
* @param type Bit-mask that must have one of the following six values:
* @li @c mtx_plain for a simple non-recursive mutex
* @li @c mtx_timed for a non-recursive mutex that supports timeout
* @li @c mtx_plain | @c mtx_recursive (same as @c mtx_plain, but recursive)
* @li @c mtx_timed | @c mtx_recursive (same as @c mtx_timed, but recursive)
* @return @ref thrd_success on success, or @ref thrd_error if the request could
* not be honored.
*/
int mtx_init(mtx_t *mtx, int type);
/** Release any resources used by the given mutex.
* @param mtx A mutex object.
*/
void mtx_destroy(mtx_t *mtx);
/** Lock the given mutex.
* Blocks until the given mutex can be locked. If the mutex is non-recursive, and
* the calling thread already has a lock on the mutex, this call will block
* forever.
* @param mtx A mutex object.
* @return @ref thrd_success on success, or @ref thrd_error if the request could
* not be honored.
*/
int mtx_lock(mtx_t *mtx);
/** Lock the given mutex, or block until a specific point in time.
* Blocks until either the given mutex can be locked, or the specified TIME_UTC
* based time.
* @param mtx A mutex object.
* @param ts A UTC based calendar time
* @return @ref The mtx_timedlock function returns thrd_success on success, or
* thrd_timedout if the time specified was reached without acquiring the
* requested resource, or thrd_error if the request could not be honored.
*/
int mtx_timedlock(mtx_t *mtx, const struct timespec *ts);
/** Try to lock the given mutex.
* The specified mutex shall support either test and return or timeout. If the
* mutex is already locked, the function returns without blocking.
* @param mtx A mutex object.
* @return @ref thrd_success on success, or @ref thrd_busy if the resource
* requested is already in use, or @ref thrd_error if the request could not be
* honored.
*/
int mtx_trylock(mtx_t *mtx);
/** Unlock the given mutex.
* @param mtx A mutex object.
* @return @ref thrd_success on success, or @ref thrd_error if the request could
* not be honored.
*/
int mtx_unlock(mtx_t *mtx);
/* Condition variable */
#if defined(_TTHREAD_WIN32_)
typedef struct {
HANDLE mEvents[2]; /* Signal and broadcast event HANDLEs. */
unsigned int mWaitersCount; /* Count of the number of waiters. */
CRITICAL_SECTION mWaitersCountLock; /* Serialize access to mWaitersCount. */
} cnd_t;
#else
typedef pthread_cond_t cnd_t;
#endif
/** Create a condition variable object.
* @param cond A condition variable object.
* @return @ref thrd_success on success, or @ref thrd_error if the request could
* not be honored.
*/
int cnd_init(cnd_t *cond);
/** Release any resources used by the given condition variable.
* @param cond A condition variable object.
*/
void cnd_destroy(cnd_t *cond);
/** Signal a condition variable.
* Unblocks one of the threads that are blocked on the given condition variable
* at the time of the call. If no threads are blocked on the condition variable
* at the time of the call, the function does nothing and return success.
* @param cond A condition variable object.
* @return @ref thrd_success on success, or @ref thrd_error if the request could
* not be honored.
*/
int cnd_signal(cnd_t *cond);
/** Broadcast a condition variable.
* Unblocks all of the threads that are blocked on the given condition variable
* at the time of the call. If no threads are blocked on the condition variable
* at the time of the call, the function does nothing and return success.
* @param cond A condition variable object.
* @return @ref thrd_success on success, or @ref thrd_error if the request could
* not be honored.
*/
int cnd_broadcast(cnd_t *cond);
/** Wait for a condition variable to become signaled.
* The function atomically unlocks the given mutex and endeavors to block until
* the given condition variable is signaled by a call to cnd_signal or to
* cnd_broadcast. When the calling thread becomes unblocked it locks the mutex
* before it returns.
* @param cond A condition variable object.
* @param mtx A mutex object.
* @return @ref thrd_success on success, or @ref thrd_error if the request could
* not be honored.
*/
int cnd_wait(cnd_t *cond, mtx_t *mtx);
/** Wait for a condition variable to become signaled.
* The function atomically unlocks the given mutex and endeavors to block until
* the given condition variable is signaled by a call to cnd_signal or to
* cnd_broadcast, or until after the specified time. When the calling thread
* becomes unblocked it locks the mutex before it returns.
* @param cond A condition variable object.
* @param mtx A mutex object.
* @param xt A point in time at which the request will time out (absolute time).
* @return @ref thrd_success upon success, or @ref thrd_timeout if the time
* specified in the call was reached without acquiring the requested resource, or
* @ref thrd_error if the request could not be honored.
*/
int cnd_timedwait(cnd_t *cond, mtx_t *mtx, const struct timespec *ts);
/* Thread */
#if defined(_TTHREAD_WIN32_)
typedef HANDLE thrd_t;
#else
typedef pthread_t thrd_t;
#endif
/** Thread start function.
* Any thread that is started with the @ref thrd_create() function must be
* started through a function of this type.
* @param arg The thread argument (the @c arg argument of the corresponding
* @ref thrd_create() call).
* @return The thread return value, which can be obtained by another thread
* by using the @ref thrd_join() function.
*/
typedef int (*thrd_start_t)(void *arg);
/** Create a new thread.
* @param thr Identifier of the newly created thread.
* @param func A function pointer to the function that will be executed in
* the new thread.
* @param arg An argument to the thread function.
* @return @ref thrd_success on success, or @ref thrd_nomem if no memory could
* be allocated for the thread requested, or @ref thrd_error if the request
* could not be honored.
* @note A threads identifier may be reused for a different thread once the
* original thread has exited and either been detached or joined to another
* thread.
*/
int thrd_create(thrd_t *thr, thrd_start_t func, void *arg);
/** Identify the calling thread.
* @return The identifier of the calling thread.
*/
thrd_t thrd_current(void);
/** Dispose of any resources allocated to the thread when that thread exits.
* @return thrd_success, or thrd_error on error
*/
int thrd_detach(thrd_t thr);
/** Compare two thread identifiers.
* The function determines if two thread identifiers refer to the same thread.
* @return Zero if the two thread identifiers refer to different threads.
* Otherwise a nonzero value is returned.
*/
int thrd_equal(thrd_t thr0, thrd_t thr1);
/** Terminate execution of the calling thread.
* @param res Result code of the calling thread.
*/
TTHREAD_NORETURN void thrd_exit(int res);
/** Wait for a thread to terminate.
* The function joins the given thread with the current thread by blocking
* until the other thread has terminated.
* @param thr The thread to join with.
* @param res If this pointer is not NULL, the function will store the result
* code of the given thread in the integer pointed to by @c res.
* @return @ref thrd_success on success, or @ref thrd_error if the request could
* not be honored.
*/
int thrd_join(thrd_t thr, int *res);
/** Put the calling thread to sleep.
* Suspend execution of the calling thread.
* @param duration Interval to sleep for
* @param remaining If non-NULL, this parameter will hold the remaining
* time until time_point upon return. This will
* typically be zero, but if the thread was woken up
* by a signal that is not ignored before duration was
* reached @c remaining will hold a positive time.
* @return 0 (zero) on successful sleep, -1 if an interrupt occurred,
* or a negative value if the operation fails.
*/
int thrd_sleep(const struct timespec *duration, struct timespec *remaining);
/** Yield execution to another thread.
* Permit other threads to run, even if the current thread would ordinarily
* continue to run.
*/
void thrd_yield(void);
/* Thread local storage */
#if defined(_TTHREAD_WIN32_)
typedef DWORD tss_t;
#else
typedef pthread_key_t tss_t;
#endif
/** Destructor function for a thread-specific storage.
* @param val The value of the destructed thread-specific storage.
*/
typedef void (*tss_dtor_t)(void *val);
/** Create a thread-specific storage.
* @param key The unique key identifier that will be set if the function is
* successful.
* @param dtor Destructor function. This can be NULL.
* @return @ref thrd_success on success, or @ref thrd_error if the request could
* not be honored.
* @note On Windows, the @c dtor will definitely be called when
* appropriate for threads created with @ref thrd_create. It will be
* called for other threads in most cases, the possible exception being
* for DLLs loaded with LoadLibraryEx. In order to be certain, you
* should use @ref thrd_create whenever possible.
*/
int tss_create(tss_t *key, tss_dtor_t dtor);
/** Delete a thread-specific storage.
* The function releases any resources used by the given thread-specific
* storage.
* @param key The key that shall be deleted.
*/
void tss_delete(tss_t key);
/** Get the value for a thread-specific storage.
* @param key The thread-specific storage identifier.
* @return The value for the current thread held in the given thread-specific
* storage.
*/
void *tss_get(tss_t key);
/** Set the value for a thread-specific storage.
* @param key The thread-specific storage identifier.
* @param val The value of the thread-specific storage to set for the current
* thread.
* @return @ref thrd_success on success, or @ref thrd_error if the request could
* not be honored.
*/
int tss_set(tss_t key, void *val);
#if defined(_TTHREAD_WIN32_)
typedef struct {
LONG volatile status;
CRITICAL_SECTION lock;
} once_flag;
#define ONCE_FLAG_INIT {0,}
#else
#define once_flag pthread_once_t
#define ONCE_FLAG_INIT PTHREAD_ONCE_INIT
#endif
/** Invoke a callback exactly once
* @param flag Flag used to ensure the callback is invoked exactly
* once.
* @param func Callback to invoke.
*/
#if defined(_TTHREAD_WIN32_)
void call_once(once_flag *flag, void (*func)(void));
#else
#define call_once(flag,func) pthread_once(flag,func)
#endif
#ifdef __cplusplus
}
#endif
#endif /* _TINYTHREAD_H_ */