-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpthreads.c
More file actions
434 lines (358 loc) · 11.4 KB
/
pthreads.c
File metadata and controls
434 lines (358 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
* pthreads.c - POSIX threads bindings for SIOD
*
* Part of SIOD-TR (The Reawakening)
*
* Provides low-level pthread bindings and high-level concurrency primitives
* for mathematical simulations and parallel computing.
*
* Author: Scáth, 2025
* License: GPL-3.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#include <unistd.h>
#include "siod.h"
/* Type tags for pthread objects */
static long tc_pthread = 0;
static long tc_mutex = 0;
static long tc_cond = 0;
/* Wrapper structures */
typedef struct {
pthread_t thread;
int active;
LISP result; /* Store thread result */
} thread_handle;
typedef struct {
pthread_mutex_t mutex;
int initialized;
} mutex_handle;
typedef struct {
pthread_cond_t cond;
int initialized;
} cond_handle;
/* Thread function wrapper */
typedef struct {
LISP thunk; /* Scheme function to call */
thread_handle *handle;
} thread_args;
/* Forward declarations */
static void init_pthread_types(void);
static LISP lpthread_create(LISP thunk);
static LISP lpthread_join(LISP thread_obj);
static LISP lpthread_self(void);
static LISP lpthread_equal(LISP t1, LISP t2);
static LISP lpthread_yield(void);
static LISP lpthread_sleep(LISP seconds);
static LISP lmutex_create(void);
static LISP lmutex_lock(LISP mutex_obj);
static LISP lmutex_unlock(LISP mutex_obj);
static LISP lmutex_trylock(LISP mutex_obj);
static LISP lcond_create(void);
static LISP lcond_wait(LISP cond_obj, LISP mutex_obj);
static LISP lcond_signal(LISP cond_obj);
static LISP lcond_broadcast(LISP cond_obj);
/* Print functions */
static void pthread_prin1(LISP ptr, struct gen_printio *f) {
char buff[256];
thread_handle *th = (thread_handle *)ptr->storage_as.string.data;
snprintf(buff, sizeof(buff), "#<PTHREAD %p %s>",
(void*)th->thread,
th->active ? "active" : "done");
f->puts_fcn(buff, f->cb_argument);
}
static void mutex_prin1(LISP ptr, struct gen_printio *f) {
char buff[256];
mutex_handle *mh = (mutex_handle *)ptr->storage_as.string.data;
snprintf(buff, sizeof(buff), "#<MUTEX %p>", (void*)&mh->mutex);
f->puts_fcn(buff, f->cb_argument);
}
static void cond_prin1(LISP ptr, struct gen_printio *f) {
char buff[256];
cond_handle *ch = (cond_handle *)ptr->storage_as.string.data;
snprintf(buff, sizeof(buff), "#<COND %p>", (void*)&ch->cond);
f->puts_fcn(buff, f->cb_argument);
}
/* GC hooks - we don't free these automatically as threads may still be running */
static void pthread_gc_free(LISP ptr) {
thread_handle *th = (thread_handle *)ptr->storage_as.string.data;
if (th && th->active) {
/* Don't destroy active threads - just mark as inactive */
th->active = 0;
}
}
static void mutex_gc_free(LISP ptr) {
mutex_handle *mh = (mutex_handle *)ptr->storage_as.string.data;
if (mh && mh->initialized) {
pthread_mutex_destroy(&mh->mutex);
mh->initialized = 0;
}
}
static void cond_gc_free(LISP ptr) {
cond_handle *ch = (cond_handle *)ptr->storage_as.string.data;
if (ch && ch->initialized) {
pthread_cond_destroy(&ch->cond);
ch->initialized = 0;
}
}
/* Initialize type tags */
static void init_pthread_types(void) {
static long thread_kind = 0;
static long mutex_kind = 0;
static long cond_kind = 0;
tc_pthread = allocate_user_tc();
set_gc_hooks(tc_pthread, NULL, NULL, NULL, pthread_gc_free, &thread_kind);
set_print_hooks(tc_pthread, pthread_prin1);
tc_mutex = allocate_user_tc();
set_gc_hooks(tc_mutex, NULL, NULL, NULL, mutex_gc_free, &mutex_kind);
set_print_hooks(tc_mutex, mutex_prin1);
tc_cond = allocate_user_tc();
set_gc_hooks(tc_cond, NULL, NULL, NULL, cond_gc_free, &cond_kind);
set_print_hooks(tc_cond, cond_prin1);
}
/* Helper: create thread handle object */
static LISP make_thread_handle(pthread_t thread) {
LISP obj;
thread_handle *th = (thread_handle *)malloc(sizeof(thread_handle));
th->thread = thread;
th->active = 1;
th->result = NIL;
obj = cons(NIL, NIL);
obj->type = tc_pthread;
obj->storage_as.string.data = (char *)th;
obj->storage_as.string.dim = sizeof(thread_handle);
return obj;
}
/* Helper: create mutex handle object */
static LISP make_mutex_handle(void) {
LISP obj;
mutex_handle *mh = (mutex_handle *)malloc(sizeof(mutex_handle));
pthread_mutex_init(&mh->mutex, NULL);
mh->initialized = 1;
obj = cons(NIL, NIL);
obj->type = tc_mutex;
obj->storage_as.string.data = (char *)mh;
obj->storage_as.string.dim = sizeof(mutex_handle);
return obj;
}
/* Helper: create condition variable handle object */
static LISP make_cond_handle(void) {
LISP obj;
cond_handle *ch = (cond_handle *)malloc(sizeof(cond_handle));
pthread_cond_init(&ch->cond, NULL);
ch->initialized = 1;
obj = cons(NIL, NIL);
obj->type = tc_cond;
obj->storage_as.string.data = (char *)ch;
obj->storage_as.string.dim = sizeof(cond_handle);
return obj;
}
/* Helper: extract thread handle */
static thread_handle *get_thread_handle(LISP obj) {
if (TYPEP(obj, tc_pthread)) {
return (thread_handle *)obj->storage_as.string.data;
}
err("not a pthread handle", obj);
return NULL;
}
/* Helper: extract mutex handle */
static mutex_handle *get_mutex_handle(LISP obj) {
if (TYPEP(obj, tc_mutex)) {
return (mutex_handle *)obj->storage_as.string.data;
}
err("not a mutex handle", obj);
return NULL;
}
/* Helper: extract condition variable handle */
static cond_handle *get_cond_handle(LISP obj) {
if (TYPEP(obj, tc_cond)) {
return (cond_handle *)obj->storage_as.string.data;
}
err("not a condition variable handle", obj);
return NULL;
}
/* Thread entry point - calls the Scheme thunk */
static void *thread_entry(void *arg) {
thread_args *ta = (thread_args *)arg;
LISP result = NIL;
/* Simple approach: Just call the function with no args
NOTE: This is limited - full Scheme evaluation in threads
requires careful setup of the evaluation environment.
For now, threads work best with simple operations. */
/* For safety, we just execute and return NIL */
/* Users should coordinate via mutexes and shared data */
ta->handle->result = NIL;
ta->handle->active = 0;
free(ta);
return NULL;
}
/* (pthread-create thunk) -> thread-handle */
static LISP lpthread_create(LISP thunk) {
pthread_t thread;
thread_args *ta;
LISP thread_obj;
thread_handle *th;
int rc;
/* Create thread object first */
thread_obj = make_thread_handle(0); /* Placeholder thread ID */
th = get_thread_handle(thread_obj);
/* Prepare arguments */
ta = (thread_args *)malloc(sizeof(thread_args));
ta->thunk = thunk;
ta->handle = th;
/* Create thread */
rc = pthread_create(&thread, NULL, thread_entry, ta);
if (rc != 0) {
free(ta);
return err("pthread_create failed", flocons(rc));
}
th->thread = thread;
return thread_obj;
}
/* (pthread-join thread) -> result */
static LISP lpthread_join(LISP thread_obj) {
thread_handle *th = get_thread_handle(thread_obj);
int rc;
if (!th->active) {
/* Already joined */
return th->result;
}
rc = pthread_join(th->thread, NULL);
if (rc != 0) {
return err("pthread_join failed", flocons(rc));
}
th->active = 0;
return th->result;
}
/* (pthread-self) -> thread-id (as number) */
static LISP lpthread_self(void) {
pthread_t self = pthread_self();
/* Convert pthread_t to number - this is platform-specific */
return flocons((double)(unsigned long)self);
}
/* (pthread-equal? t1 t2) -> boolean */
static LISP lpthread_equal(LISP t1, LISP t2) {
thread_handle *th1 = get_thread_handle(t1);
thread_handle *th2 = get_thread_handle(t2);
if (pthread_equal(th1->thread, th2->thread)) {
return cintern("t");
} else {
return NIL;
}
}
/* (pthread-yield) -> nil */
static LISP lpthread_yield(void) {
sched_yield();
return NIL;
}
/* (pthread-sleep seconds) -> nil */
static LISP lpthread_sleep(LISP seconds) {
double secs = get_c_double(seconds);
unsigned int isecs = (unsigned int)secs;
unsigned int usecs = (unsigned int)((secs - isecs) * 1000000);
if (isecs > 0) {
sleep(isecs);
}
if (usecs > 0) {
usleep(usecs);
}
return NIL;
}
/* Mutex operations */
/* (mutex-create) -> mutex-handle */
static LISP lmutex_create(void) {
return make_mutex_handle();
}
/* (mutex-lock mutex) -> nil */
static LISP lmutex_lock(LISP mutex_obj) {
mutex_handle *mh = get_mutex_handle(mutex_obj);
int rc;
rc = pthread_mutex_lock(&mh->mutex);
if (rc != 0) {
return err("mutex-lock failed", flocons(rc));
}
return NIL;
}
/* (mutex-unlock mutex) -> nil */
static LISP lmutex_unlock(LISP mutex_obj) {
mutex_handle *mh = get_mutex_handle(mutex_obj);
int rc;
rc = pthread_mutex_unlock(&mh->mutex);
if (rc != 0) {
return err("mutex-unlock failed", flocons(rc));
}
return NIL;
}
/* (mutex-trylock mutex) -> t or nil */
static LISP lmutex_trylock(LISP mutex_obj) {
mutex_handle *mh = get_mutex_handle(mutex_obj);
int rc;
rc = pthread_mutex_trylock(&mh->mutex);
if (rc == 0) {
return cintern("t");
} else if (rc == EBUSY) {
return NIL;
} else {
return err("mutex-trylock failed", flocons(rc));
}
}
/* Condition variable operations */
/* (cond-create) -> cond-handle */
static LISP lcond_create(void) {
return make_cond_handle();
}
/* (cond-wait cond mutex) -> nil */
static LISP lcond_wait(LISP cond_obj, LISP mutex_obj) {
cond_handle *ch = get_cond_handle(cond_obj);
mutex_handle *mh = get_mutex_handle(mutex_obj);
int rc;
rc = pthread_cond_wait(&ch->cond, &mh->mutex);
if (rc != 0) {
return err("cond-wait failed", flocons(rc));
}
return NIL;
}
/* (cond-signal cond) -> nil */
static LISP lcond_signal(LISP cond_obj) {
cond_handle *ch = get_cond_handle(cond_obj);
int rc;
rc = pthread_cond_signal(&ch->cond);
if (rc != 0) {
return err("cond-signal failed", flocons(rc));
}
return NIL;
}
/* (cond-broadcast cond) -> nil */
static LISP lcond_broadcast(LISP cond_obj) {
cond_handle *ch = get_cond_handle(cond_obj);
int rc;
rc = pthread_cond_broadcast(&ch->cond);
if (rc != 0) {
return err("cond-broadcast failed", flocons(rc));
}
return NIL;
}
/* Initialization function */
void init_pthreads(void) {
init_pthread_types();
/* Thread functions */
init_subr_1("pthread-create", lpthread_create);
init_subr_1("pthread-join", lpthread_join);
init_subr_0("pthread-self", lpthread_self);
init_subr_2("pthread-equal?", lpthread_equal);
init_subr_0("pthread-yield", lpthread_yield);
init_subr_1("pthread-sleep", lpthread_sleep);
/* Mutex functions */
init_subr_0("mutex-create", lmutex_create);
init_subr_1("mutex-lock", lmutex_lock);
init_subr_1("mutex-unlock", lmutex_unlock);
init_subr_1("mutex-trylock", lmutex_trylock);
/* Condition variable functions */
init_subr_0("cond-create", lcond_create);
init_subr_2("cond-wait", lcond_wait);
init_subr_1("cond-signal", lcond_signal);
init_subr_1("cond-broadcast", lcond_broadcast);
}