Debian Codegraph @

src/c/threads/thread.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * thread.d - native threads
6 *
7 * Copyright (c) 2003 Juan Jose Garcia Ripoll
8 *
9 * See file 'LICENSE' for the copyright details.
10 *
11 */
12
13 #define ECL_INCLUDE_MATH_H
14 #include <ecl/ecl.h> /* includes ECL_WINDOWS_THREADS */
15 #include <ecl/internal.h>
16 #include <ecl/ecl-inl.h>
17
18 #include <errno.h>
19 #include <time.h>
20 #include <signal.h>
21 #include <string.h>
22 #ifdef ECL_WINDOWS_THREADS
23 # include <windows.h>
24 #else
25 # include <pthread.h>
26 #endif
27 #ifdef HAVE_GETTIMEOFDAY
28 # include <sys/time.h>
29 #endif
30 #ifdef HAVE_SCHED_H
31 # include <sched.h>
32 #endif
33
34 /* -- Macros -------------------------------------------------------- */
35
36 #ifdef ECL_WINDOWS_THREADS
37 # define ecl_process_eq(t1, t2) (GetThreadId(t1) == GetThreadId(t2))
38 # define ecl_set_process_self(var) \
39 { \
40 HANDLE aux = GetCurrentThread(); \
41 DuplicateHandle(GetCurrentProcess(), \
42 aux, \
43 GetCurrentProcess(), \
44 &var, \
45 0, \
46 FALSE, \
47 DUPLICATE_SAME_ACCESS); \
48 }
49 #else
50 # define ecl_process_eq(t1, t2) (t1 == t2)
51 # define ecl_set_process_self(var) (var = pthread_self())
52 #endif /* ECL_WINDOWS_THREADS */
53
54 /* -- Core ---------------------------------------------------------- */
55
56 static void
57 extend_process_vector()
58 {
59 cl_object v = cl_core.processes;
60 cl_index new_size = v->vector.dim + v->vector.dim/2;
61 cl_env_ptr the_env = ecl_process_env();
62 ECL_WITH_NATIVE_LOCK_BEGIN(the_env, &cl_core.processes_lock) {
63 cl_object other = cl_core.processes;
64 if (new_size > other->vector.dim) {
65 cl_object new = si_make_vector(ECL_T,
66 ecl_make_fixnum(new_size),
67 ecl_make_fixnum(other->vector.fillp),
68 ECL_NIL, ECL_NIL, ECL_NIL);
69 ecl_copy_subarray(new, 0, other, 0, other->vector.dim);
70 cl_core.processes = new;
71 }
72 } ECL_WITH_NATIVE_LOCK_END;
73 }
74
75 static void
76 ecl_list_process(cl_object process)
77 {
78 cl_env_ptr the_env = ecl_process_env();
79 bool ok = 0;
80 do {
81 ECL_WITH_NATIVE_LOCK_BEGIN(the_env, &cl_core.processes_lock) {
82 cl_object vector = cl_core.processes;
83 cl_index size = vector->vector.dim;
84 cl_index ndx = vector->vector.fillp;
85 if (ndx < size) {
86 vector->vector.self.t[ndx++] = process;
87 vector->vector.fillp = ndx;
88 ok = 1;
89 }
90 } ECL_WITH_NATIVE_LOCK_END;
91 if (ok) break;
92 extend_process_vector();
93 } while (1);
94 }
95
96 /* Must be called with disabled interrupts to prevent race conditions
97 * in thread_cleanup */
98 static void
99 ecl_unlist_process(cl_object process)
100 {
101 ecl_mutex_lock(&cl_core.processes_lock);
102 cl_object vector = cl_core.processes;
103 cl_index i;
104 for (i = 0; i < vector->vector.fillp; i++) {
105 if (vector->vector.self.t[i] == process) {
106 vector->vector.fillp--;
107 do {
108 vector->vector.self.t[i] =
109 vector->vector.self.t[i+1];
110 } while (++i < vector->vector.fillp);
111 break;
112 }
113 }
114 ecl_mutex_unlock(&cl_core.processes_lock);
115 }
116
117 static cl_object
118 ecl_process_list()
119 {
120 cl_env_ptr the_env = ecl_process_env();
121 cl_object output = ECL_NIL;
122 ECL_WITH_NATIVE_LOCK_BEGIN(the_env, &cl_core.processes_lock) {
123 cl_object vector = cl_core.processes;
124 cl_object *data = vector->vector.self.t;
125 cl_index i;
126 for (i = 0; i < vector->vector.fillp; i++) {
127 cl_object p = data[i];
128 if (p != ECL_NIL)
129 output = ecl_cons(p, output);
130 }
131 } ECL_WITH_NATIVE_LOCK_END;
132 return output;
133 }
134
135 /* -- Environment --------------------------------------------------- */
136
137 extern void ecl_init_env(struct cl_env_struct *env);
138
139 cl_object
140 mp_current_process(void)
141 {
142 return ecl_process_env()->own_process;
143 }
144
145 /* -- Thread object ------------------------------------------------- */
146
147 static void
148 assert_type_process(cl_object o)
149 {
150 if (ecl_t_of(o) != t_process)
151 FEwrong_type_argument(@[mp::process], o);
152 }
153
154 static void
155 thread_cleanup(void *aux)
156 {
157 /* This routine performs some cleanup before a thread is completely
158 * killed. For instance, it has to remove the associated process
159 * object from the list, an it has to dealloc some memory.
160 *
161 * NOTE: thread_cleanup() does not provide enough "protection". In
162 * order to ensure that all UNWIND-PROTECT forms are properly
163 * executed, never use pthread_cancel() to kill a process, but
164 * rather use the lisp functions mp_interrupt_process() and
165 * mp_process_kill().
166 */
167 cl_object process = (cl_object)aux;
168 cl_env_ptr env = process->process.env;
169
170 /* The following flags will disable all interrupts. */
171 if (env) {
172 ecl_disable_interrupts_env(env);
173 ecl_clear_bignum_registers(env);
174 }
175 ecl_mutex_lock(&process->process.start_stop_lock);
176 process->process.phase = ECL_PROCESS_EXITING;
177 #ifdef HAVE_SIGPROCMASK
178 /* ...but we might get stray signals. */
179 {
180 sigset_t new[1];
181 sigemptyset(new);
182 sigaddset(new, ecl_option_values[ECL_OPT_THREAD_INTERRUPT_SIGNAL]);
183 pthread_sigmask(SIG_BLOCK, new, NULL);
184 }
185 #endif
186 process->process.env = NULL;
187 ecl_unlist_process(process);
188 #ifdef ECL_WINDOWS_THREADS
189 CloseHandle(process->process.thread);
190 #endif
191 ecl_set_process_env(NULL);
192 if (env) _ecl_dealloc_env(env);
193
194 process->process.phase = ECL_PROCESS_INACTIVE;
195 ecl_cond_var_broadcast(&process->process.exit_barrier);
196 ecl_mutex_unlock(&process->process.start_stop_lock);
197 }
198
199 #ifdef ECL_WINDOWS_THREADS
200 static DWORD WINAPI
201 #else
202 static void *
203 #endif
204 thread_entry_point(void *arg)
205 {
206 cl_object process = (cl_object)arg;
207 cl_env_ptr env = process->process.env;
208
209 /*
210 * Upon entering this routine
211 * process.env = our environment for lisp
212 * process.phase = ECL_PROCESS_BOOTING
213 * signals are disabled in the environment
214 * the communication interrupt is disabled (sigmasked)
215 *
216 * This process will not receive signals that originate from
217 * other processes. Furthermore, we expect not to get any
218 * other interrupts (SIGSEGV, SIGFPE) if we do things right.
219 */
220 /* 1) Setup the environment for the execution of the thread */
221 ecl_set_process_env(env = process->process.env);
222 #ifndef ECL_WINDOWS_THREADS
223 pthread_cleanup_push(thread_cleanup, (void *)process);
224 #endif
225 ecl_cs_init(env);
226 ecl_mutex_lock(&process->process.start_stop_lock);
227
228 /* 2) Execute the code. The CATCH_ALL point is the destination
229 * provides us with an elegant way to exit the thread: we just
230 * do an unwind up to frs_top.
231 */
232 ECL_CATCH_ALL_BEGIN(env) {
233 #ifdef HAVE_SIGPROCMASK
234 {
235 sigset_t *new = (sigset_t*)env->default_sigmask;
236 pthread_sigmask(SIG_SETMASK, new, NULL);
237 }
238 #endif
239 process->process.phase = ECL_PROCESS_ACTIVE;
240 ecl_mutex_unlock(&process->process.start_stop_lock);
241 ecl_enable_interrupts_env(env);
242 si_trap_fpe(@'last', ECL_T);
243 ecl_bds_bind(env, @'mp::*current-process*', process);
244
245 ECL_RESTART_CASE_BEGIN(env, @'abort') {
246 env->values[0] = cl_apply(2, process->process.function,
247 process->process.args);
248 {
249 cl_object output = ECL_NIL;
250 int i = env->nvalues;
251 while (i--) {
252 output = CONS(env->values[i], output);
253 }
254 process->process.exit_values = output;
255 }
256 } ECL_RESTART_CASE(1,args) {
257 /* ABORT restart. */
258 process->process.exit_values = args;
259 } ECL_RESTART_CASE_END;
260 ecl_bds_unwind1(env);
261 } ECL_CATCH_ALL_END;
262
263 /* 4) If everything went right, we should be exiting the thread
264 * through this point. thread_cleanup is automatically invoked
265 * marking the process as inactive.
266 */
267 #ifdef ECL_WINDOWS_THREADS
268 thread_cleanup(process);
269 return 1;
270 #else
271 pthread_cleanup_pop(1);
272 return NULL;
273 #endif
274 }
275
276 static void
277 init_tl_bindings(cl_object process, cl_env_ptr env)
278 {
279 cl_index bindings_size;
280 cl_object *bindings;
281 if (Null(process->process.inherit_bindings_p)) {
282 cl_index idx = 0, size = 256;
283 bindings_size = size;
284 bindings = (cl_object *)ecl_malloc(size*sizeof(cl_object*));
285 for(idx=0; idx<256; idx++) {
286 bindings[idx] = ECL_NO_TL_BINDING;
287 }
288 } else {
289 cl_env_ptr parent_env = ecl_process_env();
290 bindings_size = parent_env->bds_stack.tl_bindings_size;
291 bindings = (cl_object *)ecl_malloc(bindings_size*sizeof(cl_object*));
292 ecl_copy(bindings, parent_env->bds_stack.tl_bindings, bindings_size*sizeof(cl_object*));
293 }
294 env->bds_stack.tl_bindings_size = bindings_size;
295 env->bds_stack.tl_bindings = bindings;
296 }
297
298 static cl_object
299 alloc_process(cl_object name, cl_object initial_bindings_p)
300 {
301 cl_env_ptr env = ecl_process_env();
302 cl_object process = ecl_alloc_object(t_process), array;
303 cl_index bindings_size;
304 cl_object* bindings;
305 process->process.phase = ECL_PROCESS_INACTIVE;
306 process->process.name = name;
307 process->process.function = ECL_NIL;
308 process->process.args = ECL_NIL;
309 process->process.interrupt = ECL_NIL;
310 process->process.inherit_bindings_p = Null(initial_bindings_p)? ECL_T : ECL_NIL;
311 process->process.exit_values = ECL_NIL;
312 process->process.env = NULL;
313 process->process.woken_up = ECL_NIL;
314 ecl_disable_interrupts_env(env);
315 ecl_mutex_init(&process->process.start_stop_lock, TRUE);
316 ecl_cond_var_init(&process->process.exit_barrier);
317 ecl_set_finalizer_unprotected(process, ECL_T);
318 ecl_enable_interrupts_env(env);
319 return process;
320 }
321
322 bool
323 ecl_import_current_thread(cl_object name, cl_object bindings)
324 {
325 struct cl_env_struct env_aux[1];
326 cl_object process;
327 ecl_thread_t current;
328 cl_env_ptr env;
329 int registered;
330 struct GC_stack_base stack;
331 ecl_set_process_self(current);
332 #ifdef GBC_BOEHM
333 GC_get_stack_base(&stack);
334 switch (GC_register_my_thread(&stack)) {
335 case GC_SUCCESS:
336 registered = 1;
337 break;
338 case GC_DUPLICATE:
339 /* Thread was probably created using the GC hooks for thread creation. */
340 registered = 0;
341 break;
342 default:
343 return 0;
344 }
345 #endif
346 {
347 cl_object processes = cl_core.processes;
348 cl_index i, size;
349 for (i = 0, size = processes->vector.fillp; i < size; i++) {
350 cl_object p = processes->vector.self.t[i];
351 if (!Null(p) && ecl_process_eq(p->process.thread, current)) {
352 return 0;
353 }
354 }
355 }
356 /* We need a fake env to allow for interrupts blocking and to set up
357 * frame stacks or other stuff which may be needed by alloc_process
358 * and ecl_list_process. Since the fake env is allocated on the stack,
359 * we can safely store pointers to memory allocated by the gc there. */
360 memset(env_aux, 0, sizeof(*env_aux));
361 env_aux->disable_interrupts = 1;
362 env_aux->interrupt_struct = ecl_alloc_unprotected(sizeof(*env_aux->interrupt_struct));
363 env_aux->interrupt_struct->pending_interrupt = ECL_NIL;
364 ecl_mutex_init(&env_aux->interrupt_struct->signal_queue_lock, FALSE);
365 env_aux->interrupt_struct->signal_queue = ECL_NIL;
366 ecl_set_process_env(env_aux);
367 ecl_init_env(env_aux);
368
369 /* Allocate real environment, link it together with process */
370 env = _ecl_alloc_env(0);
371 process = alloc_process(name, ECL_NIL);
372 process->process.env = env;
373 process->process.phase = ECL_PROCESS_BOOTING;
374 process->process.thread = current;
375
376 /* Copy initial bindings from process to the fake environment */
377 env_aux->cleanup = registered;
378 init_tl_bindings(process, env_aux);
379
380 /* Switch over to the real environment */
381 memcpy(env, env_aux, sizeof(*env));
382 env->own_process = process;
383 ecl_set_process_env(env);
384 ecl_list_process(process);
385 ecl_enable_interrupts_env(env);
386
387 process->process.phase = ECL_PROCESS_ACTIVE;
388
389 ecl_bds_bind(env, @'mp::*current-process*', process);
390 return 1;
391 }
392
393 void
394 ecl_release_current_thread(void)
395 {
396 cl_env_ptr env = ecl_process_env();
397
398 int cleanup = env->cleanup;
399 cl_object own_process = env->own_process;
400 thread_cleanup(own_process);
401 #ifdef GBC_BOEHM
402 if (cleanup) {
403 GC_unregister_my_thread();
404 }
405 #endif
406 }
407
408 @(defun mp::make-process (&key name ((:initial-bindings initial_bindings_p) ECL_T))
409 cl_object process;
410 @
411 process = alloc_process(name, initial_bindings_p);
412 @(return process);
413 @)
414
415 cl_object
416 mp_process_preset(cl_narg narg, cl_object process, cl_object function, ...)
417 {
418 ecl_va_list args;
419 if (narg < 2)
420 FEwrong_num_arguments(@[mp::process-preset]);
421 ecl_va_start(args, function, narg, 2);
422 assert_type_process(process);
423 process->process.function = function;
424 process->process.args = cl_grab_rest_args(args);
425 ecl_va_end(args);
426 @(return process);
427 }
428
429 cl_object
430 mp_interrupt_process(cl_object process, cl_object function)
431 {
432 cl_env_ptr env = ecl_process_env();
433 /* Make sure we don't interrupt an exiting process */
434 ECL_WITH_NATIVE_LOCK_BEGIN(env, &process->process.start_stop_lock) {
435 unlikely_if (mp_process_active_p(process) == ECL_NIL)
436 FEerror("Cannot interrupt the inactive process ~A", 1, process);
437 ecl_interrupt_process(process, function);
438 } ECL_WITH_NATIVE_LOCK_END;
439 @(return ECL_T);
440 }
441
442 cl_object
443 mp_suspend_loop()
444 {
445 cl_env_ptr env = ecl_process_env();
446 ECL_CATCH_BEGIN(env,@'mp::suspend-loop') {
447 for ( ; ; ) {
448 cl_sleep(ecl_make_fixnum(100));
449 }
450 } ECL_CATCH_END;
451 ecl_return0(env);
452 }
453
454 cl_object
455 mp_break_suspend_loop()
456 {
457 cl_env_ptr the_env = ecl_process_env();
458 if (frs_sch(@'mp::suspend-loop')) {
459 cl_throw(@'mp::suspend-loop');
460 }
461 ecl_return0(the_env);
462 }
463
464 cl_object
465 mp_process_suspend(cl_object process)
466 {
467 return mp_interrupt_process(process, @'mp::suspend-loop');
468 }
469
470 cl_object
471 mp_process_resume(cl_object process)
472 {
473 return mp_interrupt_process(process, @'mp::break-suspend-loop');
474 }
475
476 cl_object
477 mp_process_kill(cl_object process)
478 {
479 return mp_interrupt_process(process, @'mp::exit-process');
480 }
481
482 cl_object
483 mp_process_yield(void)
484 {
485 #if defined(ECL_WINDOWS_THREADS)
486 Sleep(0);
487 #elif defined(HAVE_SCHED_H)
488 sched_yield();
489 #else
490 ecl_musleep(0.0);
491 #endif
492 @(return);
493 }
494
495 cl_object
496 mp_process_enable(cl_object process)
497 {
498 /* process_env and ok are changed after the setjmp call in
499 * ECL_UNWIND_PROTECT_BEGIN, so they need to be declared volatile */
500 volatile cl_env_ptr process_env = NULL;
501 cl_env_ptr the_env = ecl_process_env();
502 volatile int ok = 1;
503 ECL_UNWIND_PROTECT_BEGIN(the_env) {
504 /* Try to gain exclusive access to the process. This prevents two
505 * concurrent calls to process-enable from different threads on
506 * the same process */
507 ecl_mutex_lock(&process->process.start_stop_lock);
508 /* Ensure that the process is inactive. */
509 if (process->process.phase != ECL_PROCESS_INACTIVE) {
510 FEerror("Cannot enable the running process ~A.", 1, process);
511 }
512 ok = 0;
513 process->process.phase = ECL_PROCESS_BOOTING;
514
515 process->process.parent = mp_current_process();
516 process->process.trap_fpe_bits =
517 process->process.parent->process.env->trap_fpe_bits;
518
519 /* Link environment and process together */
520 process_env = _ecl_alloc_env(the_env);
521 process_env->own_process = process;
522 process->process.env = process_env;
523
524 /* Immediately list the process such that its environment is
525 * marked by the gc when its contents are allocated */
526 ecl_list_process(process);
527
528 /* Now we can safely allocate memory for the environment contents
529 * and store pointers to it in the environment */
530 ecl_init_env(process_env);
531
532 process_env->trap_fpe_bits = process->process.trap_fpe_bits;
533 init_tl_bindings(process, process_env);
534
535 ecl_disable_interrupts_env(the_env);
536 #ifdef ECL_WINDOWS_THREADS
537 {
538 HANDLE code;
539 DWORD threadId;
540
541 code = (HANDLE)CreateThread(NULL, 0, thread_entry_point, process, 0, &threadId);
542 ok = (process->process.thread = code) != NULL;
543 }
544 #else
545 {
546 int code;
547 pthread_attr_t pthreadattr;
548
549 pthread_attr_init(&pthreadattr);
550 pthread_attr_setdetachstate(&pthreadattr, PTHREAD_CREATE_DETACHED);
551 /*
552 * Block all asynchronous signals until the thread is completely
553 * set up. The synchronous signals SIGSEGV and SIGBUS are needed
554 * by the gc and thus can't be blocked.
555 */
556 #ifdef HAVE_SIGPROCMASK
557 {
558 sigset_t new, previous;
559 sigfillset(&new);
560 sigdelset(&new, SIGSEGV);
561 sigdelset(&new, SIGBUS);
562 pthread_sigmask(SIG_BLOCK, &new, &previous);
563 code = pthread_create(&process->process.thread, &pthreadattr,
564 thread_entry_point, process);
565 pthread_sigmask(SIG_SETMASK, &previous, NULL);
566 }
567 #else
568 code = pthread_create(&process->process.thread, &pthreadattr,
569 thread_entry_point, process);
570 #endif
571 ok = (code == 0);
572 }
573 #endif
574 ecl_enable_interrupts_env(the_env);
575 } ECL_UNWIND_PROTECT_THREAD_SAFE_EXIT {
576 if (!ok) {
577 /* INV: interrupts are already disabled through thread safe
578 * unwind-protect */
579 ecl_unlist_process(process);
580 process->process.phase = ECL_PROCESS_INACTIVE;
581 /* Alert possible waiting processes. */
582 ecl_cond_var_broadcast(&process->process.exit_barrier);
583 process->process.env = NULL;
584 if (process_env != NULL)
585 _ecl_dealloc_env(process_env);
586 }
587 /* Unleash the thread */
588 ecl_mutex_unlock(&process->process.start_stop_lock);
589 } ECL_UNWIND_PROTECT_THREAD_SAFE_END;
590
591 @(return (ok? process : ECL_NIL));
592 }
593
594 cl_object
595 mp_exit_process(void)
596 {
597 /* We simply undo the whole of the frame stack. This brings up
598 back to the thread entry point, going through all possible
599 UNWIND-PROTECT.
600 */
601 const cl_env_ptr the_env = ecl_process_env();
602 ecl_unwind(the_env, the_env->frs_stack.org);
603 /* Never reached */
604 }
605
606 cl_object
607 mp_all_processes(void)
608 {
609 /* No race condition here because this list is never destructively
610 * modified. When we add or remove processes, we create new lists. */
611 @(return ecl_process_list());
612 }
613
614 cl_object
615 mp_process_name(cl_object process)
616 {
617 assert_type_process(process);
618 @(return process->process.name);
619 }
620
621 cl_object
622 mp_process_active_p(cl_object process)
623 {
624 assert_type_process(process);
625 @(return (process->process.phase? ECL_T : ECL_NIL));
626 }
627
628 cl_object
629 mp_process_whostate(cl_object process)
630 {
631 assert_type_process(process);
632 @(return (cl_core.null_string));
633 }
634
635 cl_object
636 mp_process_join(cl_object process)
637 {
638 cl_env_ptr the_env = ecl_process_env();
639 volatile cl_object values;
640
641 assert_type_process(process);
642 ECL_UNWIND_PROTECT_BEGIN(the_env) {
643 ecl_mutex_lock(&process->process.start_stop_lock);
644 while (process->process.phase != ECL_PROCESS_INACTIVE) {
645 ecl_cond_var_wait(&process->process.exit_barrier, &process->process.start_stop_lock);
646 }
647 values = cl_values_list(process->process.exit_values);
648 } ECL_UNWIND_PROTECT_THREAD_SAFE_EXIT {
649 ecl_mutex_unlock(&process->process.start_stop_lock);
650 } ECL_UNWIND_PROTECT_THREAD_SAFE_END;
651 return values;
652 }
653
654 cl_object
655 mp_process_run_function(cl_narg narg, cl_object name, cl_object function, ...)
656 {
657 cl_object process;
658 cl_object rest;
659 ecl_va_list args;
660 if (narg < 2)
661 FEwrong_num_arguments(@[mp::process-run-function]);
662 if (CONSP(name)) {
663 process = cl_apply(2, @'mp::make-process', name);
664 } else {
665 process = mp_make_process(2, @':name', name);
666 }
667 ecl_va_start(args, function, narg, 2);
668 rest = cl_grab_rest_args(args);
669 ecl_va_end(args);
670 cl_apply(4, @'mp::process-preset', process, function,
671 rest);
672 return mp_process_enable(process);
673 }
674
675 cl_object
676 mp_process_run_function_wait(cl_narg narg, ...)
677 {
678 cl_object process;
679 ecl_va_list args;
680 ecl_va_start(args, narg, narg, 0);
681 process = cl_apply(2, @'mp::process-run-function',
682 cl_grab_rest_args(args));
683 if (!Null(process)) {
684 while (process->process.phase < ECL_PROCESS_ACTIVE) {
685 ecl_musleep(0.001);
686 }
687 }
688 ecl_va_end(args);
689 @(return process);
690 }
691
692 /*----------------------------------------------------------------------
693 * INTERRUPTS
694 */
695
696 #ifndef ECL_WINDOWS_THREADS
697 static cl_object
698 mp_get_sigmask(void)
699 {
700 cl_object data = ecl_alloc_simple_vector(sizeof(sigset_t), ecl_aet_b8);
701 sigset_t *mask_ptr = (sigset_t*)data->vector.self.b8;
702 sigset_t no_signals;
703 sigemptyset(&no_signals);
704 if (pthread_sigmask(SIG_BLOCK, &no_signals, mask_ptr))
705 FElibc_error("MP:GET-SIGMASK failed in a call to pthread_sigmask", 0);
706 @(return data);
707 }
708
709 static cl_object
710 mp_set_sigmask(cl_object data)
711 {
712 sigset_t *mask_ptr = (sigset_t*)data->vector.self.b8;
713 if (pthread_sigmask(SIG_SETMASK, mask_ptr, NULL))
714 FElibc_error("MP:SET-SIGMASK failed in a call to pthread_sigmask", 0);
715 @(return data);
716 }
717 #endif
718
719 cl_object
720 mp_block_signals(void)
721 {
722 #ifdef ECL_WINDOWS_THREADS
723 cl_env_ptr the_env = ecl_process_env();
724 cl_object previous = ecl_cmp_symbol_value(the_env, @'ext::*interrupts-enabled*');
725 ECL_SETQ(the_env, @'ext::*interrupts-enabled*', ECL_NIL);
726 @(return previous);
727 #else
728 cl_object previous = mp_get_sigmask();
729 sigset_t all_signals;
730 sigfillset(&all_signals);
731 /* SIGSEGV or SIGBUS are needed by the gc in incremental mode and
732 * can thus never be blocked */
733 sigdelset(&all_signals, SIGSEGV);
734 sigdelset(&all_signals, SIGBUS);
735 if (pthread_sigmask(SIG_SETMASK, &all_signals, NULL))
736 FElibc_error("MP:BLOCK-SIGNALS failed in a call to pthread_sigmask",0);
737 @(return previous);
738 #endif
739 }
740
741 cl_object
742 mp_restore_signals(cl_object sigmask)
743 {
744 #ifdef ECL_WINDOWS_THREADS
745 cl_env_ptr the_env = ecl_process_env();
746 ECL_SETQ(the_env, @'ext::*interrupts-enabled*', sigmask);
747 ecl_check_pending_interrupts(the_env);
748 @(return sigmask);
749 #else
750 return mp_set_sigmask(sigmask);
751 #endif
752 }
753
754 /* -- Initialization ------------------------------------------------ */
755
756 void
757 init_threads()
758 {
759 cl_env_ptr the_env = ecl_process_env();
760 cl_object process;
761 ecl_thread_t main_thread;
762 /* We have to set the environment before any allocation takes place,
763 * so that the interrupt handling code works. */
764 ecl_cs_init(the_env);
765 ecl_set_process_self(main_thread);
766 process = ecl_alloc_object(t_process);
767 process->process.phase = ECL_PROCESS_ACTIVE;
768 process->process.name = @'si::top-level';
769 process->process.function = ECL_NIL;
770 process->process.args = ECL_NIL;
771 process->process.thread = main_thread;
772 process->process.env = the_env;
773 process->process.woken_up = ECL_NIL;
774 ecl_mutex_init(&process->process.start_stop_lock, TRUE);
775 ecl_cond_var_init(&process->process.exit_barrier);
776
777 the_env->own_process = process;
778 {
779 cl_object v = si_make_vector(ECL_T, /* Element type */
780 ecl_make_fixnum(256), /* Size */
781 ecl_make_fixnum(0), /* fill pointer */
782 ECL_NIL, ECL_NIL, ECL_NIL);
783 v->vector.self.t[0] = process;
784 v->vector.fillp = 1;
785 cl_core.processes = v;
786 }
787 }