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 <lxsameer@lxsameer.com>
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 <stdatomic.h>
22#include <stddef.h>
23#include <stdint.h>
24
27#include "serene/utils.h"
28
29#define SRN_HASH_TYPE uint32_t
30#define SRN_SEED_TYPE uint32_t
31
32typedef struct srn_jit_t srn_jit_t;
33typedef struct srn_mm_t srn_mm_t;
34typedef struct hmap_t hmap_t;
35typedef struct srn_namespace_t srn_namespace_t;
36typedef struct srn_error_t srn_error_t;
37typedef struct srn_context_t srn_context_t;
38typedef struct srn_value_t srn_value_t;
39typedef struct srn_metadata_t srn_metadata_t;
42typedef struct srn_sm_t srn_sm_t;
44
45typedef uint64_t srn_object_id_t;
46/// Start allocating IDs from 100, earlier IDs are reserved
47#define ENGINE_FIRST_OBJECT_ID 100
48
51
52/// Engine is a structure to own the long living and main pieces of the
53/// compiler. There will be only oe instance of the engine at anytime.
54typedef struct srn_engine_t {
55 /// An unsigned counter to allocate object ids atomically. Any object in
56 /// Serene must have a unique id, this id is not guaranteed to be the same on
57 /// each run. We use this id as the routing key on the context level to
58 /// connect different pieces of information related to the same object
59 /// together. ONLY INTERACT WITH THIS VIA `allocate_object_id`.
61
62 /// The runtime's tunable knobs, the single source for every configurable
63 /// value (see configuration.h). Held by value, read-only once the engine is
64 /// up.
66
67 /// Memory manager
69
70#ifdef SRN_WITH_SERENE
71 srn_jit_t *jit;
72#endif
73
74 /// The fiber scheduler, that is the entry point of the fiber subsystem.
75 /// It's here so any fiber can reach it through its context as
76 /// fiber->ctx->engine->scheduler, which is how srn_fiber_ready works from a
77 /// thread that is not a worker.
79
80 /// The I/O reactor, that is in charge of handling everything I/O.
82
83 /// We use the seed for hashing and the value will be generated at random
84 /// for each new context. There's concern about hashmap collioson DoS attacks
85 /// but as far as I can see other compilers are doing a similar thing with
86 /// a non-cryptographic hashing function. we use xxHash but others are
87 /// using the non-cryptographic version of SipHash with a random seed as
88 /// well.
90
91#ifdef SRN_WITH_SERENE
92 /// A context owned by the engine itself, alive from engine creation to
93 /// engine shutdown. The engine wide registries below are pinned to it, so
94 /// every trie node they retain lives in its block chain rather than in
95 /// whatever transient context performed the insert. It is shared across
96 /// threads; allocate through it only while holding the lock that guards
97 /// the map being updated.
98 srn_context_t *root_context;
99
100 /// A hashmap that maps namespace names to their value. When operating on
101 /// this map, e.g. updating the `namespaces` directly, the `ns_lock` has
102 /// to be acquired.
103 /// srn_string_t -> srn_namespace_t
104 _Atomic(hmap_t *) namespaces;
105 srn_fiber_mutex_t *ns_lock;
106
107 /// Engine wide intern table for keywords. Keys are the keyword name bytes;
108 /// values are the interned srn_value_t* (with tag VKeyword). Lock before updating
109 /// the keywords itself
110 _Atomic(hmap_t *) keywords;
111 srn_fiber_mutex_t *keywords_lock;
112
113 srn_sm_t *source_manager;
114#endif
115
117
118// -----------------------------------------------------------------------------
119// Engine's LifeCycle
120// -----------------------------------------------------------------------------
121/// Create the engine over `mm`, copying `config` (the runtime's knobs) into it.
122/// Pass `nullptr` for `config` to use the defaults. `config` is read only
123/// during the call, so the caller may pass a stack value.
124[[gnu::nonnull(1)]]
126[[gnu::nonnull(1)]]
128
129// -----------------------------------------------------------------------------
130// IDs
131// -----------------------------------------------------------------------------
132[[gnu::nonnull(1)]]
134
135// -----------------------------------------------------------------------------
136// Hashing
137// -----------------------------------------------------------------------------
138[[gnu::nonnull(1)]]
139srn_hash_t srn_hash(const srn_engine_t *engine, const void *data, size_t len);
140
141// TODO(lxsameer): Re-enable keyword interning and its section on the test suits
142// -----------------------------------------------------------------------------
143// Keyword interning
144// -----------------------------------------------------------------------------
145/* #ifdef SRN_WITH_SERENE */
146/* /// Return the canonical interned keyword value for `name`. If a keyword with */
147/* /// the same name has been interned in this engine before, the same */
148/* /// srn_value_t* is returned. Otherwise the value is built, registered in the */
149/* /// engine wide keyword table, and returned. This function is thread safe, via */
150/* /// the `keywords_lock`. */
151/* [[gnu::nonnull(1, 2)]] */
152/* srn_value_t * */
153/* srn_engine_intern_keyword(srn_context_t *ctx, srn_metadata_t *metadata, const char *name); */
154/* #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:174
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:103
srn_hash_t srn_hash(const srn_engine_t *engine, const void *data, size_t len)
Definition engine.c:187
struct srn_reactor_t srn_reactor_t
The implementation is internal to the runtime.
Definition engine.h:41
uint64_t srn_object_id_t
Definition engine.h:45
struct srn_scheduler_t srn_scheduler_t
Definition engine.h:40
srn_object_id_t srn_allocate_object_id(srn_engine_t *engine)
Definition engine.c:193
struct srn_sm_t srn_sm_t
Definition engine.h:42
This is an implementation of Compressed Hash-Array Mapped Prefix-tree, which is a bit-partitioned,...
const size_t len
Every runtime knob, in one place.
Engine is a structure to own the long living and main pieces of the compiler.
Definition engine.h:54
srn_configuration_t config
The runtime's tunable knobs, the single source for every configurable value (see configuration....
Definition engine.h:65
_Atomic srn_object_id_t object_id_counter
An unsigned counter to allocate object ids atomically.
Definition engine.h:60
srn_scheduler_t * scheduler
The fiber scheduler, that is the entry point of the fiber subsystem.
Definition engine.h:78
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:89
srn_mm_t * mm
Memory manager.
Definition engine.h:68
srn_reactor_t * reactor
The I/O reactor, that is in charge of handling everything I/O.
Definition engine.h:81
A runtime error, a tag classifying the failure and a human-readable message.
Definition errors.h:173
Main memory manager structure that will own all the allocated blocks and data.
Definition interface.h:112