07513e1996-10-04Fredrik Hübinette (Hubbe) #ifndef THREADS_H #define THREADS_H #include "machine.h" #include "interpret.h" #include "error.h" #ifdef _REENTRANT
97ffe41997-01-26Per Hedbor #ifdef HAVE_THREAD_H #define UNIX_THREADS #include <thread.h> #undef HAVE_PTHREAD_H #undef HAVE_THREAD_H #endif
07513e1996-10-04Fredrik Hübinette (Hubbe) #ifdef HAVE_PTHREAD_H
97ffe41997-01-26Per Hedbor #define POSIX_THREADS
07513e1996-10-04Fredrik Hübinette (Hubbe) #include <pthread.h> #undef HAVE_PTHREAD_H #endif
5377b91997-02-01Henrik Grubbström (Grubba) #ifdef SPROC_THREADS /* Not supported yet */ #undef SPROC_THREADS #undef HAVE_SPROC #endif /* SPROC_THREADS */
97ffe41997-01-26Per Hedbor 
07513e1996-10-04Fredrik Hübinette (Hubbe) extern int num_threads;
6d1a5e1996-10-07Fredrik Hübinette (Hubbe) struct object; extern struct object *thread_id;
07513e1996-10-04Fredrik Hübinette (Hubbe) 
97ffe41997-01-26Per Hedbor #define DEFINE_MUTEX(X) MUTEX_T X #ifdef POSIX_THREADS #define THREAD_T pthread_t
07513e1996-10-04Fredrik Hübinette (Hubbe) #define MUTEX_T pthread_mutex_t #define mt_init(X) pthread_mutex_init((X),0) #define mt_lock(X) pthread_mutex_lock(X) #define mt_trylock(X) pthread_mutex_trylock(X) #define mt_unlock(X) pthread_mutex_unlock(X) #define mt_destroy(X) pthread_mutex_destroy(X)
97ffe41997-01-26Per Hedbor /* SIGH! No setconcurrency in posix threads. This is more or less * needed to make usable multi-threaded programs on solaris machines * with only one CPU. Otherwise, only systemcalls are actually * threaded. */ #define th_setconcurrency(X)
5377b91997-02-01Henrik Grubbström (Grubba) #ifdef HAVE_PTHREAD_YIELD #define th_yield() pthread_yield() #else
97ffe41997-01-26Per Hedbor #define th_yield()
5377b91997-02-01Henrik Grubbström (Grubba) #endif /* HAVE_PTHREAD_YIELD */
07513e1996-10-04Fredrik Hübinette (Hubbe)  #define th_create(ID,fun,arg) pthread_create(ID,&pattr,fun,arg) #define th_exit(foo) pthread_exit(foo)
a70bd01996-12-15Niels Möller #define th_self() pthread_self()
07513e1996-10-04Fredrik Hübinette (Hubbe) 
5377b91997-02-01Henrik Grubbström (Grubba) #ifdef HAVE_PTHREAD_COND_INIT
07513e1996-10-04Fredrik Hübinette (Hubbe) #define COND_T pthread_cond_t
5377b91997-02-01Henrik Grubbström (Grubba)  #ifdef HAVE_PTHREAD_CONDATTR_DEFAULT #define co_init(X) pthread_cond_init((X), pthread_condattr_default) #else
07513e1996-10-04Fredrik Hübinette (Hubbe) #define co_init(X) pthread_cond_init((X), 0)
5377b91997-02-01Henrik Grubbström (Grubba) #endif /* HAVE_PTHREAD_CONDATTR_DEFAULT */
07513e1996-10-04Fredrik Hübinette (Hubbe) #define co_wait(COND, MUTEX) pthread_cond_wait((COND), (MUTEX)) #define co_signal(X) pthread_cond_signal(X) #define co_broadcast(X) pthread_cond_broadcast(X) #define co_destroy(X) pthread_cond_destroy(X)
5377b91997-02-01Henrik Grubbström (Grubba) #else #error No way to make cond-vars #endif /* HAVE_PTHREAD_COND_INIT */ #endif /* POSIX_THREADS */
97ffe41997-01-26Per Hedbor  #ifdef UNIX_THREADS #define THREAD_T thread_t #define PTHREAD_MUTEX_INITIALIZER DEFAULTMUTEX #define MUTEX_T mutex_t #define mt_init(X) mutex_init((X),USYNC_THREAD,0) #define mt_lock(X) mutex_lock(X) #define mt_trylock(X) mutex_trylock(X) #define mt_unlock(X) mutex_unlock(X) #define mt_destroy(X) mutex_destroy(X) #define th_setconcurrency(X) thr_setconcurrency(X) #define th_create(ID,fun,arg) thr_create(NULL,0,fun,arg,THR_DAEMON,ID) #define th_exit(foo) thr_exit(foo) #define th_self() thr_self() #define th_yield() thr_yield() #define COND_T cond_t #define co_init(X) cond_init((X),USYNC_THREAD,0) #define co_wait(COND, MUTEX) cond_wait((COND), (MUTEX)) #define co_signal(X) cond_signal(X) #define co_broadcast(X) cond_broadcast(X) #define co_destroy(X) cond_destroy(X)
5377b91997-02-01Henrik Grubbström (Grubba)  #endif /* UNIX_THREADS */ #ifdef SPROC_THREADS /* * Not fully supported yet */ #define THREAD_T int #define MUTEX_T ulock_t #define mt_init(X) (usinitlock(((*X) = usnewlock(/*********/)))) #define mt_lock(X) ussetlock(*X) #define mt_unlock(X) usunsetlock(*X) #define mt_destroy(X) usfreelock((*X), /*******/) #define th_setconcurrency(X) /*******/ #define PIKE_SPROC_FLAGS (PR_SADDR|PR_SFDS|PR_SDIR|PS_SETEXITSIG) #define th_create(ID, fun, arg) (((*(ID)) = sproc(fun, PIKE_SPROC_FLAGS, arg)) == -1) #define th_exit(X) exit(X) #define th_self() getpid() #define th_yield() sginap(0) /* * No cond_vars yet */ #endif /* SPROC_THREADS */
97ffe41997-01-26Per Hedbor  extern MUTEX_T interpreter_lock;
07513e1996-10-04Fredrik Hübinette (Hubbe)  struct svalue; struct frame; struct thread_state { int swapped; struct svalue *sp,*evaluator_stack; struct svalue **mark_sp,**mark_stack; struct frame *fp; int evaluator_stack_malloced; int mark_stack_malloced; JMP_BUF *recoveries;
6d1a5e1996-10-07Fredrik Hübinette (Hubbe)  struct object * thread_id;
07513e1996-10-04Fredrik Hübinette (Hubbe) }; #define THREADS_ALLOW() \ do {\ struct thread_state _tmp; \ _tmp.swapped=0; \ if(num_threads > 1 && !threads_disabled) { \ _tmp.swapped=1; \ _tmp.sp=sp; \ _tmp.evaluator_stack=evaluator_stack; \ _tmp.mark_sp=mark_sp; \ _tmp.mark_stack=mark_stack; \ _tmp.fp=fp; \ _tmp.recoveries=recoveries; \ _tmp.evaluator_stack_malloced=evaluator_stack_malloced; \ _tmp.mark_stack_malloced=mark_stack_malloced; \
6d1a5e1996-10-07Fredrik Hübinette (Hubbe)  _tmp.thread_id = thread_id; \
07513e1996-10-04Fredrik Hübinette (Hubbe)  mt_unlock(& interpreter_lock); \
97ffe41997-01-26Per Hedbor  /*th_yield();*/\
07513e1996-10-04Fredrik Hübinette (Hubbe)  } #define THREADS_DISALLOW() \ if(_tmp.swapped) { \ mt_lock(& interpreter_lock); \ sp=_tmp.sp; \ evaluator_stack=_tmp.evaluator_stack; \ mark_sp=_tmp.mark_sp; \ mark_stack=_tmp.mark_stack; \ fp=_tmp.fp; \ recoveries=_tmp.recoveries; \ evaluator_stack_malloced=_tmp.evaluator_stack_malloced; \ mark_stack_malloced=_tmp.mark_stack_malloced; \
6d1a5e1996-10-07Fredrik Hübinette (Hubbe)  thread_id = _tmp.thread_id; \
07513e1996-10-04Fredrik Hübinette (Hubbe)  } \ } while(0) /* Prototypes begin here */
6d1a5e1996-10-07Fredrik Hübinette (Hubbe) struct thread_starter;
07513e1996-10-04Fredrik Hübinette (Hubbe) void *new_thread_func(void * data); void f_thread_create(INT32 args);
a29e021996-10-15Fredrik Hübinette (Hubbe) void f_this_thread(INT32 args);
07513e1996-10-04Fredrik Hübinette (Hubbe) void th_init();
6d1a5e1996-10-07Fredrik Hübinette (Hubbe) struct mutex_storage; struct key_storage;
07513e1996-10-04Fredrik Hübinette (Hubbe) void f_mutex_lock(INT32 args); void f_mutex_trylock(INT32 args); void init_mutex_obj(struct object *o); void exit_mutex_obj(struct object *o); void init_mutex_key_obj(struct object *o); void exit_mutex_key_obj(struct object *o); void f_cond_wait(INT32 args); void f_cond_signal(INT32 args); void f_cond_broadcast(INT32 args); void init_cond_obj(struct object *o); void exit_cond_obj(struct object *o); void th_init_programs();
6d1a5e1996-10-07Fredrik Hübinette (Hubbe) void th_cleanup();
07513e1996-10-04Fredrik Hübinette (Hubbe) /* Prototypes end here */ #else
97ffe41997-01-26Per Hedbor #define th_setconcurrency(X)
419ede1996-11-13Fredrik Hübinette (Hubbe) #define DEFINE_MUTEX(X)
0516881996-10-04David Hedbor #define mt_init(X) #define mt_lock(X) #define mt_unlock(X)
419ede1996-11-13Fredrik Hübinette (Hubbe) #define mt_destroy(X)
07513e1996-10-04Fredrik Hübinette (Hubbe) #define THREADS_ALLOW() #define THREADS_DISALLOW() #define th_init()
6d1a5e1996-10-07Fredrik Hübinette (Hubbe) #define th_cleanup()
07513e1996-10-04Fredrik Hübinette (Hubbe) #define th_init_programs()
5377b91997-02-01Henrik Grubbström (Grubba) #endif /* _REENTRANT */
07513e1996-10-04Fredrik Hübinette (Hubbe) 
0516881996-10-04David Hedbor 
07513e1996-10-04Fredrik Hübinette (Hubbe) extern int threads_disabled;
5377b91997-02-01Henrik Grubbström (Grubba)  #endif /* THREADS_H */