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
  
#ifndef THREADS_H 
#define THREADS_H 
 
#include "machine.h" 
#include "interpret.h" 
#include "error.h" 
#ifdef HAVE_SYS_TYPES_H 
/* Needed for pthread_t on OSF/1 */ 
#include <sys/types.h> 
#endif /* HAVE_SYS_TYPES_H */ 
#ifdef _REENTRANT 
 
/* 
 * Decide which type of threads to use 
 * 
 * UNIX_THREADS : Unix international threads 
 * POSIX_THREADS : POSIX standard threads 
 * SGI_SPROC_THREADS : SGI sproc() based threads 
 */ 
 
#ifdef _UNIX_THREADS 
#ifdef HAVE_THREAD_H 
#define UNIX_THREADS 
#include <thread.h> 
#undef HAVE_PTHREAD_H 
#undef HAVE_THREAD_H 
#endif 
#endif /* _UNIX_THREADS */ 
 
#ifdef _MIT_POSIX_THREADS 
#ifdef HAVE_PTHREAD_H 
#define POSIX_THREADS 
#include <pthread.h> 
#undef HAVE_PTHREAD_H 
#endif 
#endif /* _MIT_POSIX_THREADS */ 
 
#ifdef _SGI_SPROC_THREADS 
/* Not supported yet */ 
#undef SGI_SPROC_THREADS 
#undef HAVE_SPROC 
#endif /* _SGI_SPROC_THREADS */ 
 
 
extern int num_threads; 
struct object; 
extern struct object *thread_id; 
 
#define DEFINE_MUTEX(X) MUTEX_T X 
 
 
#ifdef POSIX_THREADS 
#define THREAD_T pthread_t 
#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) 
 
/* 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)  
#ifdef HAVE_PTHREAD_YIELD 
#define th_yield()    pthread_yield() 
#else 
#define th_yield() 
#endif /* HAVE_PTHREAD_YIELD */ 
 
#define th_create(ID,fun,arg) pthread_create(ID,&pattr,fun,arg) 
#define th_exit(foo) pthread_exit(foo) 
#define th_self() pthread_self() 
 
#ifdef HAVE_PTHREAD_COND_INIT 
#define COND_T pthread_cond_t 
 
#ifdef HAVE_PTHREAD_CONDATTR_DEFAULT 
#define co_init(X) pthread_cond_init((X), pthread_condattr_default) 
#else 
#define co_init(X) pthread_cond_init((X), 0) 
#endif /* HAVE_PTHREAD_CONDATTR_DEFAULT */ 
 
#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) 
#else 
#error No way to make cond-vars 
#endif /* HAVE_PTHREAD_COND_INIT */ 
 
#endif /* POSIX_THREADS */ 
 
 
 
 
#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) 
 
 
#endif /* UNIX_THREADS */ 
 
#ifdef SGI_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 /* SGI_SPROC_THREADS */ 
 
extern MUTEX_T interpreter_lock; 
 
 
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; 
  struct object * thread_id; 
}; 
 
#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; \ 
       _tmp.thread_id = thread_id; \ 
       mt_unlock(& interpreter_lock); \ 
       /*th_yield();*/\ 
     } 
 
#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; \ 
       thread_id = _tmp.thread_id; \ 
     } \ 
   } while(0) 
 
/* Prototypes begin here */ 
struct thread_starter; 
void *new_thread_func(void * data); 
void f_thread_create(INT32 args); 
void f_this_thread(INT32 args); 
void th_init(void); 
struct mutex_storage; 
struct key_storage; 
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_cleanup(void); 
/* Prototypes end here */ 
 
#else 
#define th_setconcurrency(X) 
#define DEFINE_MUTEX(X) 
#define mt_init(X) 
#define mt_lock(X) 
#define mt_unlock(X) 
#define mt_destroy(X) 
#define THREADS_ALLOW() 
#define THREADS_DISALLOW() 
#define th_init() 
#define th_cleanup() 
#define th_init_programs() 
#endif /* _REENTRANT */ 
 
 
extern int threads_disabled; 
 
#endif /* THREADS_H */