Serene Runtime 1.0.0
C runtime for the Serene programming language
Loading...
Searching...
No Matches
engine.c File Reference
#include "serene/rt/engine.h"
#include "serene/rt/context.h"
#include "serene/rt/fiber.h"
#include "serene/rt/impl/hashmap.h"
#include "serene/rt/mm/interface.h"
#include "serene/utils.h"
#include <assert.h>
#include <string.h>
#include "third_party/xxhash.h"
Include dependency graph for engine.c:

Go to the source code of this file.

Macros

#define XXH_INLINE_ALL
 
#define XXH_STATIC_LINKING_ONLY
 
#define XXH_ENABLE_AUTOVECTORIZE
 
#define XXH_NO_STDLIB
 
#define XXH_NO_XXH3
 
#define XXH_NO_LONG_LONG
 
#define XXH_NO_STREAM
 
#define XXH_IMPLEMENTATION
 

Functions

static srn_seed_t srn_generate_seed ()
 
srn_engine_tsrn_engine_make (srn_mm_t *mm)
 
void srn_engine_shutdown (srn_engine_t *engine)
 
srn_hash_t srn_hash (const srn_engine_t *engine, const void *data, size_t len)
 
srn_object_id_t srn_allocate_object_id (srn_engine_t *engine)
 

Macro Definition Documentation

◆ XXH_ENABLE_AUTOVECTORIZE

#define XXH_ENABLE_AUTOVECTORIZE

Definition at line 40 of file engine.c.

◆ XXH_IMPLEMENTATION

#define XXH_IMPLEMENTATION

Definition at line 47 of file engine.c.

◆ XXH_INLINE_ALL

#define XXH_INLINE_ALL

Definition at line 38 of file engine.c.

◆ XXH_NO_LONG_LONG

#define XXH_NO_LONG_LONG

Definition at line 45 of file engine.c.

◆ XXH_NO_STDLIB

#define XXH_NO_STDLIB

Definition at line 42 of file engine.c.

◆ XXH_NO_STREAM

#define XXH_NO_STREAM

Definition at line 46 of file engine.c.

◆ XXH_NO_XXH3

#define XXH_NO_XXH3

Definition at line 44 of file engine.c.

◆ XXH_STATIC_LINKING_ONLY

#define XXH_STATIC_LINKING_ONLY

Definition at line 39 of file engine.c.

Function Documentation

◆ srn_allocate_object_id()

srn_object_id_t srn_allocate_object_id ( srn_engine_t * engine)

Definition at line 137 of file engine.c.

137 {
138 PANIC_IF_NULL(engine);
139 return atomic_fetch_add_explicit(&engine->object_id_counter, 1, memory_order_relaxed);
140}
_Atomic srn_object_id_t object_id_counter
An unsigned counter to allocate object ids atomically.
Definition engine.h:55
#define PANIC_IF_NULL(ptr)
Definition utils.h:64
Here is the caller graph for this function:

◆ srn_engine_make()

srn_engine_t * srn_engine_make ( srn_mm_t * mm)

Definition at line 92 of file engine.c.

92 {
93 PANIC_IF_NULL(mm);
95
96 if (engine == nullptr) {
97 return nullptr;
98 }
99
100 engine->seed = srn_generate_seed();
101 // Just reserving 0..100 for any unpredicted needs
102 engine->mm = mm;
103
104#ifdef SRN_WITH_SERENE
105 engine->jit = srn_jit_make(mm);
106#endif
107 engine->scheduler = srn_sched_init(engine);
108
109#ifdef SRN_WITH_SERENE
110 // Both registries start empty. An empty hmap_t is just {.len = 0,
111 // .root = nullptr} and matches what hmap_empty would return, so we can
112 // initialize them inline without needing a context here.
113 engine->namespaces = (hmap_t){.len = 0, .root = nullptr};
114 srn_spinlock_init(&engine->ns_lock);
115 engine->keywords = (hmap_t){.len = 0, .root = nullptr};
116 srn_spinlock_init(&engine->keywords_lock);
117#endif
118 atomic_init(&engine->object_id_counter, ENGINE_FIRST_OBJECT_ID);
119
120 return engine;
121}
static srn_seed_t srn_generate_seed()
Definition engine.c:63
#define ENGINE_FIRST_OBJECT_ID
Start allocating IDs from 100, earlier IDs are reserved.
Definition engine.h:42
#define srn_mm_immortal_allocate(mm, T)
Definition interface.h:169
srn_jit_t * srn_jit_make(srn_mm_t *mm)
Definition jit.c:44
srn_scheduler_t * srn_sched_init(srn_engine_t *engine)
Definition scheduler.c:246
Engine is a structure to own the long living and main pieces of the compiler.
Definition engine.h:49
srn_scheduler_t * scheduler
The fiber scheduler, set by srn_sched_init.
Definition engine.h:67
srn_seed_t seed
We use the seed for hashing and the value will be generated at random for each new context.
Definition engine.h:75
srn_mm_t * mm
Memory manager.
Definition engine.h:58
static void srn_spinlock_init(srn_spinlock_t *lock)
Definition utils.h:281
Here is the call graph for this function:
Here is the caller graph for this function:

◆ srn_engine_shutdown()

void srn_engine_shutdown ( srn_engine_t * engine)

Definition at line 123 of file engine.c.

123 {
124 PANIC_IF_NULL(engine);
126#ifdef SRN_WITH_SERENE
127 srn_jit_shutdown(engine->jit);
128#endif
129}
int srn_jit_shutdown(srn_jit_t *jit)
Definition jit.c:75
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.
Definition scheduler.c:314
Here is the call graph for this function:
Here is the caller graph for this function:

◆ srn_generate_seed()

static srn_seed_t srn_generate_seed ( )
inlinestatic

Definition at line 63 of file engine.c.

63 {
64 srn_seed_t s = 0;
65
66#if defined(__linux__)
67 auto res = getrandom(&s, sizeof(s), 0);
68 if (res < 0) {
69 PANIC("Can't get a random number");
70 }
71#elif defined(__APPLE__)
72 arc4random_buf(&s, sizeof(s));
73#elif defined(_WIN32)
74 NTSTATUS st = BCryptGenRandom(nullptr, (PUCHAR)&s, sizeof(s), BCRYPT_USE_SYSTEM_PREFERRED_RNG);
75 if (st != 0) {
76 PANIC("Couldn't allocate a seed");
77 }
78#else
79 // fallback: /dev/urandom
80 FILE *f = fopen("/dev/urandom", "rb");
81 fread(&s, sizeof(s), 1, f);
82 fclose(f);
83#endif
84
85 // Avoiding zero seed
86 if (s == 0) {
87 PANIC("Couldn't allocate a seed");
88 }
89 return s;
90}
SRN_SEED_TYPE srn_seed_t
Definition context.h:45
#define PANIC(msg)
Definition utils.h:51
Here is the caller graph for this function:

◆ srn_hash()

srn_hash_t srn_hash ( const srn_engine_t * engine,
const void * data,
size_t len )

Definition at line 131 of file engine.c.

131 {
132 // NULL pointers are only valid if the length is zero
133 size_t length = (data == nullptr) ? 0 : len;
134 return XXH32(data, length, engine->seed);
135}
Here is the caller graph for this function: