Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
engine.h
Go to the documentation of this file.
1/* -*- C -*-
2 * Serene programming language
3 * Copyright (C) 2019-2026 Sameer Rahmani <[email protected]>
4 *
5 * This library is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21#include <stddef.h>
22#include <stdint.h>
23
26#include "serene/utils.h"
27
28#define SRN_HASH_TYPE uint32_t
29#define SRN_SEED_TYPE uint32_t
30
31typedef struct srn_jit_t srn_jit_t;
32typedef struct srn_mm_t srn_mm_t;
33typedef struct hmap_t hmap_t;
34typedef struct srn_namespace_t srn_namespace_t;
35typedef struct srn_error_t srn_error_t;
36typedef struct srn_context_t srn_context_t;
37typedef struct srn_value_t srn_value_t;
38typedef struct srn_metadata_t srn_metadata_t;
41
42typedef uint64_t srn_object_id_t;
43/// Start allocating IDs from 100, earlier IDs are reserved
44#define ENGINE_FIRST_OBJECT_ID 100
45
48
49/// Engine is a structure to own the long living and main pieces of the
50/// compiler. There will be only oe instance of the engine at anytime.
51typedef struct srn_engine_t {
52 /// An unsigned counter to allocate object ids atomically. Any object in
53 /// Serene must have a unique id, this id is not guaranteed to be the same on
54 /// each run. We use this id as the routing key on the context level to
55 /// connect different pieces of information related to the same object
56 /// together. ONLY INTERACT WITH THIS VIA `allocate_object_id`.
58
59 /// The runtime's tunable knobs, the single source for every configurable
60 /// value (see configuration.h). Held by value, read-only once the engine is
61 /// up.
63
64 /// Memory manager
66
67#ifdef SRN_WITH_SERENE
68 srn_jit_t *jit;
69#endif
70
71 /// The fiber scheduler, that is the entry point of the fiber subsystem.
72 /// It's here so any fiber can reach it through its context as
73 /// fiber->ctx->engine->scheduler, which is how srn_fiber_ready works from a
74 /// thread that is not a worker.
76
77 /// The I/O reactor, that is in charge of handling everything I/O.
79
80 /// We use the seed for hashing and the value will be generated at random
81 /// for each new context. There's concern about hashmap collioson DoS attacks
82 /// but as far as I can see other compilers are doing a similar thing with
83 /// a non-cryptographic hashing function. we use xxHash but others are
84 /// using the non-cryptographic version of SipHash with a random seed as
85 /// well.
87
88#ifdef SRN_WITH_SERENE
89 /// A context owned by the engine itself, alive from engine creation to
90 /// engine shutdown. The engine wide registries below are pinned to it, so
91 /// every trie node they retain lives in its block chain rather than in
92 /// whatever transient context performed the insert. It is shared across
93 /// threads; allocate through it only while holding the lock that guards
94 /// the map being updated.
95 srn_context_t *root_context;
96
97 /// A hashmap that maps namespace names to their value. When operating on
98 /// this map, e.g. inserting a namespace which will lead to a new `hmap_t`
99 /// make sure to lock the spinlock.
100 hmap_t namespaces;
101 srn_spinlock_t ns_lock;
102
103 /// Engine wide intern table for keywords. Keys are the keyword name bytes;
104 /// values are the interned srn_value_t* (with tag VKeyword). Lock before
105 /// any lookup-then-insert.
106 hmap_t keywords;
107 srn_spinlock_t keywords_lock;
108#endif
109
111
112// -----------------------------------------------------------------------------
113// Engine's LifeCycle
114// -----------------------------------------------------------------------------
115/// Create the engine over `mm`, copying `config` (the runtime's knobs) into it.
116/// Pass `nullptr` for `config` to use the defaults. `config` is read only
117/// during the call, so the caller may pass a stack value.
118[[gnu::nonnull(1)]]
120[[gnu::nonnull(1)]]
122
123// -----------------------------------------------------------------------------
124// IDs
125// -----------------------------------------------------------------------------
126[[gnu::nonnull(1)]]
128
129// -----------------------------------------------------------------------------
130// Hashing
131// -----------------------------------------------------------------------------
132[[gnu::nonnull(1)]]
133srn_hash_t srn_hash(const srn_engine_t *engine, const void *data, size_t len);
134
135// -----------------------------------------------------------------------------
136// Keyword interning
137// -----------------------------------------------------------------------------
138#ifdef SRN_WITH_SERENE
139/// Return the canonical interned keyword value for `name`. If a keyword with
140/// the same name has been interned in this engine before, the same
141/// srn_value_t* is returned. Otherwise the value is built, registered in the
142/// engine wide keyword table, and returned. This function is thread safe, via
143/// the `keywords_lock`.
144[[gnu::nonnull(1, 2)]]
146srn_engine_intern_keyword(srn_context_t *ctx, srn_metadata_t *metadata, const char *name);
147#endif
The single place that holds every runtime knob.
#define SRN_SEED_TYPE
Definition context.h:42
SRN_HASH_TYPE srn_hash_t
Definition context.h:44
SRN_SEED_TYPE srn_seed_t
Definition context.h:45
#define SRN_HASH_TYPE
Definition context.h:41
void srn_engine_shutdown(srn_engine_t *engine)
Definition engine.c:154
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.
Definition engine.c:101
srn_hash_t srn_hash(const srn_engine_t *engine, const void *data, size_t len)
Definition engine.c:166
struct srn_reactor_t srn_reactor_t
The implementation is internal to the runtime.
Definition engine.h:40
uint64_t srn_object_id_t
Definition engine.h:42
struct hmap_t hmap_t
Definition engine.h:33
struct srn_scheduler_t srn_scheduler_t
Definition engine.h:39
srn_object_id_t srn_allocate_object_id(srn_engine_t *engine)
Definition engine.c:172
This is an implementation of Compressed Hash-Array Mapped Prefix-tree, which is a bit-partitioned,...
Every runtime knob, in one place.
Engine is a structure to own the long living and main pieces of the compiler.
Definition engine.h:51
srn_configuration_t config
The runtime's tunable knobs, the single source for every configurable value (see configuration....
Definition engine.h:62
_Atomic srn_object_id_t object_id_counter
An unsigned counter to allocate object ids atomically.
Definition engine.h:57
srn_scheduler_t * scheduler
The fiber scheduler, that is the entry point of the fiber subsystem.
Definition engine.h:75
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:86
srn_mm_t * mm
Memory manager.
Definition engine.h:65
srn_reactor_t * reactor
The I/O reactor, that is in charge of handling everything I/O.
Definition engine.h:78
A runtime error, a tag classifying the failure and a human-readable message.
Definition errors.h:125
Main memory manager structure that will own all the allocated blocks and data.
Definition interface.h:107