Serene Runtime 1.0.0
C runtime for the Serene programming language
Loading...
Searching...
No Matches
thread.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/// @file
22/// `srn_thread_t`, `srn_mutex_t`, and `srn_cond_t` model the thread-level
23/// operations the runtime needs, with semantics defined here rather than
24/// borrowed from one OS API. The status enum and the operation signatures below
25/// are platform-neutral. Only the type realizations differ per platform, so
26/// each backend defines the three types over its own primitives inside the
27/// matching branch. pthreads back them on POSIX. A Windows backend realises the
28/// same operations on the Win32 API directly, so neither platform emulates the
29/// other. The types are thin wrappers so they embed inline in another struct
30/// rather than needing a separate allocation.
31///
32/// Bear in mind that, we only expose what we need. THIS IS NOT A GENERICE
33/// THREAD INTERFACE.
34
35#ifdef _WIN32
36# error "Win32 threading is not supported yet"
37#else
38# include <pthread.h>
39
40typedef struct srn_thread_t {
41 pthread_t handle;
42 /// The function the thread runs and its argument, recorded by
43 /// `srn_thread_spawn` and read once by the backend when the thread starts.
44 /// Keeping them on the thread carries them across the start boundary without
45 /// an allocation. They are internal and callers do not touch them.
46 void (*fn)(void *);
47 void *arg;
49
50typedef struct srn_mutex_t {
51 pthread_mutex_t handle;
53
54typedef struct srn_cond_t {
55 pthread_cond_t handle;
57#endif
58
59/// Result of a thread operation. Each backend maps its own error space onto
60/// these, so a caller never sees a pthread or Win32 code.
61typedef enum srn_thread_status_t {
63 /// The system lacked the resources to start the thread, such as a per-process
64 /// thread limit. Retrying after others exit may succeed.
66 /// Out of memory.
68 /// The caller lacks permission for the requested operation.
70 /// A failure the backend could not map to the above.
73
74/// Run `fn(arg)` on a new OS thread. Returns SRN_THREAD_OK, or a status
75/// describing why the thread could not be created. The caller owns `t` and must
76/// keep it alive until `srn_thread_join` returns.
78 void *arg);
79
80/// Block until the thread started for `t` returns.
82
86
87/// Release a mutex's resources. The mutex must be unlocked and have no waiters,
88/// and must not be used again without a fresh `srn_mutex_init`.
90
92
93/// Release `m`, sleep until notified, then re-acquire `m` before returning. A
94/// wait can wake spuriously, so the caller must re-check its predicate in a
95/// loop.
97
98/// Wake one waiter.
100
101/// Wake every waiter.
103
104/// Release a condition's resources. It must have no waiters, and must not be
105/// used again without a fresh `srn_cond_init`.
pthread_cond_t handle
Definition thread.h:55
pthread_mutex_t handle
Definition thread.h:51
void * arg
Definition thread.h:47
pthread_t handle
Definition thread.h:41
void(* fn)(void *)
The function the thread runs and its argument, recorded by srn_thread_spawn and read once by the back...
Definition thread.h:46
srn_thread_status_t srn_mutex_destroy(srn_mutex_t *m)
Release a mutex's resources.
srn_thread_status_t srn_mutex_init(srn_mutex_t *m)
srn_thread_status_t srn_cond_destroy(srn_cond_t *c)
Release a condition's resources.
srn_thread_status_t srn_thread_join(srn_thread_t *t)
Block until the thread started for t returns.
srn_thread_status_t srn_mutex_unlock(srn_mutex_t *m)
srn_thread_status_t
Result of a thread operation.
Definition thread.h:61
@ SRN_THREAD_AGAIN
The system lacked the resources to start the thread, such as a per-process thread limit.
Definition thread.h:65
@ SRN_THREAD_OK
Definition thread.h:62
@ SRN_THREAD_ERROR
A failure the backend could not map to the above.
Definition thread.h:71
@ SRN_THREAD_PERM
The caller lacks permission for the requested operation.
Definition thread.h:69
@ SRN_THREAD_NOMEM
Out of memory.
Definition thread.h:67
srn_thread_status_t srn_cond_wait(srn_cond_t *c, srn_mutex_t *m)
Release m, sleep until notified, then re-acquire m before returning.
srn_thread_status_t srn_mutex_lock(srn_mutex_t *m)
srn_thread_status_t srn_cond_init(srn_cond_t *c)
srn_thread_status_t srn_cond_notify_one(srn_cond_t *c)
Wake one waiter.
srn_thread_status_t srn_cond_notify_all(srn_cond_t *c)
Wake every waiter.
srn_thread_status_t srn_thread_spawn(srn_thread_t *t, void(*fn)(void *), void *arg)
Run fn(arg) on a new OS thread.