43#define XXH_STATIC_LINKING_ONLY
44#define XXH_ENABLE_AUTOVECTORIZE
49#define XXH_NO_LONG_LONG
51#define XXH_IMPLEMENTATION
54#if defined(__linux__) || defined(__GLIBC__)
55# include <sys/random.h>
56#elif defined(__APPLE__)
62# pragma comment(lib, "bcrypt.lib")
71 for (
int attempt = 0; attempt < 8; attempt++) {
75 auto res = getrandom(&s,
sizeof(s), 0);
77 PANIC(
"Can't get a random number");
79#elif defined(__APPLE__)
80 arc4random_buf(&s,
sizeof(s));
82 NTSTATUS st = BCryptGenRandom(
nullptr, (PUCHAR)&s,
sizeof(s), BCRYPT_USE_SYSTEM_PREFERRED_RNG);
84 PANIC(
"Couldn't allocate a seed");
88 FILE *f = fopen(
"/dev/urandom",
"rb");
90 size_t got = fread(&s,
sizeof(s), 1, f);
92 PANIC_IF(got != 1,
"Couldn't read a seed from /dev/urandom");
100 PANIC(
"Couldn't generate a nonzero seed");
107 if (engine ==
nullptr) {
131 UNUSED(signal(SIGPIPE, SIG_IGN));
134#ifdef SRN_WITH_SERENE
141#ifdef SRN_WITH_SERENE
153 *namespaces =
hmap_empty(engine->root_context);
154 atomic_init(&engine->namespaces, namespaces);
161 atomic_init(&engine->keywords, keywords);
169 engine->source_manager =
srn_sm_init(engine->root_context);
181#ifdef SRN_WITH_SERENE
189 size_t length = (data ==
nullptr) ? 0 :
len;
190 return XXH32(data, length, engine->
seed);
195 return atomic_fetch_add_explicit(&engine->
object_id_counter, 1, memory_order_relaxed);
void srn_config_validate(const srn_configuration_t *config)
A configuration with every field set to its default.
#define SRN_CONFIG_DEFAULTS
srn_context_t * srn_context_make(srn_engine_t *engine)
Make an empty context, by allocating a new memory block.
int srn_context_release(srn_context_t *ctx)
#define ALLOC_OR_PANIC(ctx, T)
void srn_engine_shutdown(srn_engine_t *engine)
srn_engine_t * srn_engine_make(srn_mm_t *mm, const srn_configuration_t *config)
Create the engine over mm, copying config (the runtime's knobs) into it.
srn_hash_t srn_hash(const srn_engine_t *engine, const void *data, size_t len)
static srn_seed_t srn_generate_seed()
srn_object_id_t srn_allocate_object_id(srn_engine_t *engine)
#define ENGINE_FIRST_OBJECT_ID
Start allocating IDs from 100, earlier IDs are reserved.
Error handling for the runtime.
AI Generated (🤦) Fiber subsystem overview.
XXH_PUBLIC_API XXH_PUREF XXH32_hash_t XXH32(const void *input, size_t length, XXH32_hash_t seed)
Calculates the 32-bit hash of input using xxHash32.
hmap_t hmap_empty(const srn_context_t *ctx)
Create, initialize and return a new hashmap pinned to ctx.
This is an implementation of Compressed Hash-Array Mapped Prefix-tree, which is a bit-partitioned,...
#define srn_mm_immortal_allocate(mm, T)
void srn_fiber_mutex_init(srn_fiber_mutex_t *m)
Initialise m to the unlocked state.
void srn_reactor_shutdown(srn_reactor_t *reactor)
Tear the reactor down, stop and join the reactor thread and release its channels.
srn_reactor_t * srn_reactor_init(srn_engine_t *engine)
Allocate the IO reactor from the engine.
void srn_sched_shutdown(srn_scheduler_t *sched)
The one stop tear down of the fiber subsystem, should be called once srn_sched_run has returned.
srn_scheduler_t * srn_sched_init(srn_engine_t *engine)
void srn_sm_shutdown(srn_sm_t *sm)
Shutdown the source manager freeing all the buffers.
srn_sm_t * srn_sm_init(srn_context_t *ctx)
Initialize the source manager, as an immortal data structure.
Source manager is what that read the source files and own the buffer that contains the data.
Every runtime knob, in one place.
Engine is a structure to own the long living and main pieces of the compiler.
srn_configuration_t config
The runtime's tunable knobs, the single source for every configurable value (see configuration....
_Atomic srn_object_id_t object_id_counter
An unsigned counter to allocate object ids atomically.
srn_scheduler_t * scheduler
The fiber scheduler, that is the entry point of the fiber subsystem.
srn_seed_t seed
We use the seed for hashing and the value will be generated at random for each new context.
srn_mm_t * mm
Memory manager.
srn_reactor_t * reactor
The I/O reactor, that is in charge of handling everything I/O.
Main memory manager structure that will own all the allocated blocks and data.
#define PANIC_IF_NULL(ptr)
#define PANIC_IF(cond, msg)