| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* process.c - managing the process environment(s) |
| 6 |
* |
| 7 |
* Copyright (c) 2003 Juan Jose Garcia Ripoll |
| 8 |
* Copyright (c) 2023 Daniel Kochmański |
| 9 |
* |
| 10 |
* See file 'LICENSE' for the copyright details. |
| 11 |
* |
| 12 |
*/ |
| 13 |
|
| 14 |
#define ECL_INCLUDE_MATH_H |
| 15 |
#include <ecl/ecl.h> /* includes ECL_WINDOWS_THREADS */ |
| 16 |
#include <ecl/internal.h> |
| 17 |
#include <ecl/ecl-inl.h> |
| 18 |
|
| 19 |
#include <errno.h> |
| 20 |
#include <time.h> |
| 21 |
#include <signal.h> |
| 22 |
#include <string.h> |
| 23 |
#ifdef ECL_WINDOWS_THREADS |
| 24 |
# include <windows.h> |
| 25 |
#else |
| 26 |
# include <pthread.h> |
| 27 |
#endif |
| 28 |
#ifdef HAVE_GETTIMEOFDAY |
| 29 |
# include <sys/time.h> |
| 30 |
#endif |
| 31 |
#ifdef HAVE_SCHED_H |
| 32 |
# include <sched.h> |
| 33 |
#endif |
| 34 |
|
| 35 |
/* -- Thread-local variables ------------------------------------------------ */ |
| 36 |
|
| 37 |
#ifdef ECL_THREADS |
| 38 |
# ifdef ECL_WINDOWS_THREADS |
| 39 |
# define ecl_process_key_t DWORD |
| 40 |
# define ecl_process_key_create(key) key = TlsAlloc() |
| 41 |
# define ecl_process_get_tls(key) TlsGetValue(key) |
| 42 |
# define ecl_process_set_tls(key,val) (TlsSetValue(key,val)!=0) |
| 43 |
# else |
| 44 |
# define ecl_process_key_t static pthread_key_t |
| 45 |
# define ecl_process_key_create(key) pthread_key_create(&key, NULL) |
| 46 |
# define ecl_process_get_tls(key) pthread_getspecific(key) |
| 47 |
# define ecl_process_set_tls(key,val) (pthread_setspecific(key,val)==0) |
| 48 |
# endif /* ECL_WINDOWS_THREADS */ |
| 49 |
|
| 50 |
/* Accessing a thread-local variable representing the environment. */ |
| 51 |
|
| 52 |
ecl_process_key_t cl_env_key; |
| 53 |
|
| 54 |
cl_env_ptr |
| 55 |
ecl_process_env_unsafe(void) |
| 56 |
{ |
| 57 |
return ecl_process_get_tls(cl_env_key); |
| 58 |
} |
| 59 |
|
| 60 |
cl_env_ptr |
| 61 |
ecl_process_env(void) |
| 62 |
{ |
| 63 |
cl_env_ptr rv = ecl_process_get_tls(cl_env_key); |
| 64 |
if(!rv) { |
| 65 |
ecl_thread_internal_error("pthread_getspecific() failed."); |
| 66 |
} |
| 67 |
return rv; |
| 68 |
} |
| 69 |
|
| 70 |
void |
| 71 |
ecl_set_process_env(cl_env_ptr env) |
| 72 |
{ |
| 73 |
if(!ecl_process_set_tls(cl_env_key, env)) { |
| 74 |
ecl_thread_internal_error("pthread_setspecific() failed."); |
| 75 |
} |
| 76 |
} |
| 77 |
#else |
| 78 |
/* The current global environment for single-threaded builds. */ |
| 79 |
cl_env_ptr cl_env_p = NULL; |
| 80 |
#endif /* ECL_THREADS */ |
| 81 |
|
| 82 |
/* -- Initialiation --------------------------------------------------------- */ |
| 83 |
|
| 84 |
void |
| 85 |
init_process(void) |
| 86 |
{ |
| 87 |
cl_env_ptr env = cl_core.first_env; |
| 88 |
#ifdef ECL_THREADS |
| 89 |
ecl_process_key_create(cl_env_key); |
| 90 |
ecl_mutex_init(&cl_core.processes_lock, 1); |
| 91 |
ecl_mutex_init(&cl_core.global_lock, 1); |
| 92 |
ecl_mutex_init(&cl_core.error_lock, 1); |
| 93 |
ecl_rwlock_init(&cl_core.global_env_lock); |
| 94 |
#endif |
| 95 |
ecl_set_process_env(env); |
| 96 |
env->default_sigmask = NULL; |
| 97 |
env->method_cache = NULL; |
| 98 |
env->slot_cache = NULL; |
| 99 |
env->interrupt_struct = NULL; |
| 100 |
env->disable_interrupts = 1; |
| 101 |
} |