libclod
C library for interacting with NBTs, region files, LOD data and other things.
Loading...
Searching...
No Matches
thread.h
Go to the documentation of this file.
1
8#ifndef LIBCLOD_THREAD_H
9#define LIBCLOD_THREAD_H
10
11#ifdef __GNUC__
12 #define clod_atomic_load(ptr) __atomic_load_n(ptr, __ATOMIC_SEQ_CST)
13 #define clod_atomic_cas(ptr, expected, desired) __atomic_compare_exchange_n(\
14 ptr, expected, desired,\
15 false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST\
16 )
17 #define clod_atomic_store(ptr, val) __atomic_store_n(ptr, val, __ATOMIC_SEQ_CST)
18 #define clod_atomic_add(ptr, val) __atomic_add_fetch(ptr, val, __ATOMIC_SEQ_CST)
19#endif
20
21#include <clod/lib.h>
22#include <stddef.h>
23#include <stdint.h>
24
65
66enum clod_process_result {
67 CLOD_PROCESS_OK = 0,
68 CLOD_PROCESS_INVALID = 1,
69 CLOD_PROCESS_NO_MEMORY = 2,
70 CLOD_PROCESS_UNSUPPORTED = 3
71};
72
73typedef int clod_process_main(int argc, char **argv);
74
78
81 size_t stack_size;
82
84 clod_process_main *main;
85
88
91 char **arg_vector;
92
95 size_t *arg_sizes;
96
99 const char *name;
100};
101
102typedef uintptr_t clod_process;
103
110CLOD_API CLOD_NONNULL(1)
111enum clod_process_result
112clod_process_start(struct clod_process_opts *opts, clod_process *process_out);
113
117CLOD_API
118enum clod_process_result
119clod_process_join(clod_process process);
120
121#define CLOD_SPINLOCK_INIT 0
122
124typedef char clod_spinlock;
125
128CLOD_API CLOD_NONNULL(1)
129void clod_spinlock_lock(clod_spinlock *spinlock);
130
133CLOD_API CLOD_NONNULL(1)
134void clod_spinlock_unlock(clod_spinlock *spinlock);
135
136#define CLOD_ONCE_INIT 0
137
138#define CLOD_ONCE(once) for (bool _do_once = clod_once_do(once); _do_once; clod_once_done(once), _do_once = false)
139
141typedef char clod_once;
142
147CLOD_API CLOD_NONNULL(1)
148bool clod_once_do(clod_once *once);
149
152CLOD_API CLOD_NONNULL(1)
153void clod_once_done(clod_once *once);
154
182CLOD_API
183int64_t clod_timer(int64_t *time, int64_t duration_us);
184
188typedef int clod_mutex;
189#define CLOD_MUTEX_INIT 0
190
196CLOD_API CLOD_NONNULL(1)
197void clod_mutex_lock(clod_mutex *mutex);
198
201CLOD_API CLOD_NONNULL(1)
202void clod_mutex_unlock(clod_mutex *mutex);
203
205#endif
int64_t clod_timer(int64_t *time, int64_t duration_us)
Definition timer.c:214
char clod_once
Definition thread.h:141
enum clod_process_result clod_process_join(clod_process process)
Definition thread.c:39
void clod_once_done(clod_once *once)
Definition once.c:38
void clod_mutex_unlock(clod_mutex *mutex)
Definition mutex.c:32
char clod_spinlock
Definition thread.h:124
void clod_spinlock_lock(clod_spinlock *spinlock)
Definition spinlock.c:5
void clod_spinlock_unlock(clod_spinlock *spinlock)
Definition spinlock.c:26
bool clod_once_do(clod_once *once)
Definition once.c:10
void clod_mutex_lock(clod_mutex *mutex)
Definition mutex.c:12
enum clod_process_result clod_process_start(struct clod_process_opts *opts, clod_process *process_out)
Definition thread.c:10
int clod_mutex
Definition thread.h:188
clod_process_type
Definition thread.h:25
@ CLOD_THREAD_BACKGROUND
Definition thread.h:44
@ CLOD_THREAD
Definition thread.h:32
@ CLOD_DAEMON
Definition thread.h:63
clod_process_main * main
Process entry point.
Definition thread.h:84
int arg_count
Number of arguments passed to main.
Definition thread.h:87
char ** arg_vector
Definition thread.h:91
size_t * arg_sizes
Definition thread.h:95
size_t stack_size
Definition thread.h:81
const char * name
Definition thread.h:99
enum clod_process_type type
Type of process to create.
Definition thread.h:77