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
  
435
  
436
  
437
  
438
  
439
  
440
  
441
  
442
  
443
  
444
  
445
  
446
  
447
  
448
  
449
  
450
  
451
  
452
  
453
  
454
  
455
  
456
  
457
  
458
  
459
  
460
  
461
  
462
  
463
  
464
  
465
  
466
  
467
  
468
  
469
  
470
  
471
  
472
  
473
  
474
  
475
  
476
  
477
  
478
  
479
  
480
  
481
  
482
  
483
  
484
  
485
  
486
  
#include "global.h" 
RCSID("$Id: threads.c,v 1.30 1997/09/03 04:58:17 per Exp $"); 
 
int num_threads = 1; 
int threads_disabled = 0; 
 
#ifdef _REENTRANT 
#include "threads.h" 
#include "array.h" 
#include "object.h" 
#include "pike_macros.h" 
#include "callback.h" 
#include "builtin_functions.h" 
#include "constants.h" 
#include "program.h" 
 
 
struct object *thread_id; 
static struct callback *threads_evaluator_callback=0; 
 
MUTEX_T interpreter_lock /*= PTHREAD_MUTEX_INITIALIZER*/; 
struct program *mutex_key = 0; 
struct program *thread_id_prog = 0; 
#ifdef POSIX_THREADS 
pthread_attr_t pattr; 
#endif 
 
struct thread_starter 
{ 
  struct object *id; 
  struct array *args; 
}; 
 
static void check_threads(struct callback *cb, void *arg, void * arg2) 
{ 
  THREADS_ALLOW(); 
 
  /* Allow other threads to run */ 
 
  THREADS_DISALLOW(); 
} 
 
void *new_thread_func(void * data) 
{ 
  struct thread_starter arg = *(struct thread_starter *)data; 
  JMP_BUF back; 
  INT32 tmp; 
 
  if((tmp=mt_lock( & interpreter_lock))) 
    fatal("Failed to lock interpreter, errno %d\n",tmp); 
  THREADS_FPRINTF((stderr,"THREADS_DISALLOW() Thread created...\n")); 
  init_interpreter(); 
  thread_id=arg.id; 
  SWAP_OUT_THREAD((struct thread_state *)thread_id->storage); /* Init struct */ 
  ((struct thread_state *)thread_id->storage)->swapped=0; 
 
  if(SETJMP(back)) 
  { 
    ONERROR tmp; 
    SET_ONERROR(tmp,exit_on_error,"Error in handle_error in master object!"); 
    assign_svalue_no_free(sp++, & throw_value); 
    APPLY_MASTER("handle_error", 1); 
    pop_stack(); 
    UNSET_ONERROR(tmp); 
  } else { 
    INT32 args=arg.args->size; 
    push_array_items(arg.args); 
    arg.args=0; 
    f_call_function(args); 
    pop_stack(); /* Discard the return value. /Per */ 
  } 
 
  free((char *)data); /* Moved by per, to avoid some bugs.... */ 
  UNSETJMP(back); 
 
  destruct(thread_id); 
 
  free_object(thread_id); 
  thread_id=0; 
 
  cleanup_interpret(); 
  num_threads--; 
  if(!num_threads) 
  { 
    remove_callback(threads_evaluator_callback); 
    threads_evaluator_callback=0; 
  } 
  THREADS_FPRINTF((stderr,"THREADS_ALLOW() Thread done\n")); 
  mt_unlock(& interpreter_lock); 
  th_exit(0); 
  /* NOT_REACHED, but removes a warning */ 
  return(NULL); 
} 
 
#ifdef UNIX_THREADS 
int num_lwps = 1; 
#endif 
 
void f_thread_create(INT32 args) 
{ 
  THREAD_T dummy; 
  struct thread_starter *arg; 
  int tmp; 
  arg=ALLOC_STRUCT(thread_starter); 
  arg->args=aggregate_array(args); 
  arg->id=clone_object(thread_id_prog,0); 
 
  tmp=th_create(&dummy,new_thread_func,arg); 
 
  if(!tmp) 
  { 
    num_threads++; 
 
    if(num_threads == 1 && !threads_evaluator_callback) 
    { 
      threads_evaluator_callback=add_to_callback(&evaluator_callbacks, 
                                                 check_threads, 0,0); 
    } 
#ifdef UNIX_THREADS 
    if((num_lwps==1) || num_threads/3 > num_lwps) 
      th_setconcurrency(++num_lwps); 
#endif 
    push_object(arg->id); 
    arg->id->refs++; 
  } else { 
    free_object(arg->id); 
    free_array(arg->args); 
    free((char *)arg); 
    push_int(0); 
  } 
} 
 
void f_thread_set_concurrency(INT32 args) 
{ 
  int c=1; 
  if(args) c=sp[-args].u.integer; 
  else error("No argument to thread_set_concurrency(int concurrency);\n"); 
  pop_n_elems(args); 
  num_lwps=c; 
  th_setconcurrency(c); 
} 
 
void f_this_thread(INT32 args) 
{ 
  pop_n_elems(args); 
  push_object(thread_id); 
  thread_id->refs++; 
} 
 
#define THIS_MUTEX ((struct mutex_storage *)(fp->current_storage)) 
 
 
/* Note: 
 * No reference is kept to the key object, it is destructed if the 
 * mutex is destructed. The key pointer is set to zero by the 
 * key object when the key is destructed. 
 */ 
 
struct mutex_storage 
{ 
  COND_T condition; 
  struct object *key; 
}; 
 
struct key_storage 
{ 
  struct mutex_storage *mut; 
  struct object *owner; 
  int initialized; 
}; 
 
static MUTEX_T mutex_kluge/* = PTHREAD_MUTEX_INITIALIZER*/; 
 
#define OB2KEY(X) ((struct key_storage *)((X)->storage)) 
 
void f_mutex_lock(INT32 args) 
{ 
  struct mutex_storage  *m; 
  struct object *o; 
 
  pop_n_elems(args); 
  m=THIS_MUTEX; 
  /* Needs to be cloned here, since create() 
   * might use threads. 
   */ 
  o=clone_object(mutex_key,0); 
  mt_lock(& mutex_kluge); 
  if(m->key && OB2KEY(m->key)->owner == thread_id) 
  { 
    THREADS_FPRINTF((stderr, "Recursive LOCK k:%08x, m:%08x(%08x), t:%08x\n", 
                     (unsigned int)OB2KEY(m->key), 
                     (unsigned int)m, 
                     (unsigned int)OB2KEY(m->key)->mut, 
                     (unsigned int) thread_id)); 
    mt_unlock(&mutex_kluge); 
    free_object(o); 
    error("Recursive mutex locks!\n"); 
  } 
 
  THREADS_ALLOW(); 
  while(m->key) co_wait(& m->condition, & mutex_kluge); 
  m->key=o; 
  OB2KEY(o)->mut=m; 
   
  mt_unlock(&mutex_kluge); 
  THREADS_DISALLOW(); 
  THREADS_FPRINTF((stderr, "LOCK k:%08x, m:%08x(%08x), t:%08x\n", 
                   (unsigned int)OB2KEY(o), 
                   (unsigned int)m, 
                   (unsigned int)OB2KEY(m->key)->mut, 
                   (unsigned int)thread_id)); 
  push_object(o); 
} 
 
void f_mutex_trylock(INT32 args) 
{ 
  struct mutex_storage  *m; 
  struct object *o; 
  int i=0; 
  pop_n_elems(args); 
 
  o=clone_object(mutex_key,0); 
  m=THIS_MUTEX; 
 
  mt_lock(& mutex_kluge); 
 
  /* No reason to release the interpreter lock here 
   * since we aren't calling any functions that take time. 
   */ 
 
  if(m->key && OB2KEY(m->key)->owner == thread_id) 
  { 
    mt_unlock(&mutex_kluge); 
    free_object(o); 
    error("Recursive mutex locks!\n"); 
  } 
  if(!m->key) 
  { 
    OB2KEY(o)->mut=m; 
    m->key=o; 
    i=1; 
  } 
  mt_unlock(&mutex_kluge); 
   
  if(i) 
  { 
    push_object(o); 
  } else { 
    destruct(o); 
    free_object(o); 
    push_int(0); 
  } 
} 
 
void init_mutex_obj(struct object *o) 
{ 
  co_init(& THIS_MUTEX->condition); 
  THIS_MUTEX->key=0; 
} 
 
void exit_mutex_obj(struct object *o) 
{ 
  if(THIS_MUTEX->key) destruct(THIS_MUTEX->key); 
  co_destroy(& THIS_MUTEX->condition); 
} 
 
#define THIS_KEY ((struct key_storage *)(fp->current_storage)) 
void init_mutex_key_obj(struct object *o) 
{ 
  THREADS_FPRINTF((stderr, "KEY k:%08x, o:%08x\n", 
                   (unsigned int)THIS_KEY, (unsigned int)thread_id)); 
  THIS_KEY->mut=0; 
  THIS_KEY->owner=thread_id; 
  thread_id->refs++; 
  THIS_KEY->initialized=1; 
} 
 
void exit_mutex_key_obj(struct object *o) 
{ 
  THREADS_FPRINTF((stderr, "UNLOCK k:%08x m:(%08x) t:%08x o:%08x\n", 
                   (unsigned int)THIS_KEY, 
                   (unsigned int)THIS_KEY->mut, 
                   (unsigned int)thread_id, 
                   (unsigned int)THIS_KEY->owner)); 
  mt_lock(& mutex_kluge); 
  if(THIS_KEY->mut) 
  { 
#ifdef DEBUG 
    if(THIS_KEY->mut->key != o) 
      fatal("Mutex unlock from wrong key %p != %p!\n",THIS_KEY->mut->key,o); 
#endif 
    THIS_KEY->mut->key=0; 
    if (THIS_KEY->owner) { 
      free_object(THIS_KEY->owner); 
      THIS_KEY->owner=0; 
    } 
    co_signal(& THIS_KEY->mut->condition); 
    THIS_KEY->mut=0; 
    THIS_KEY->initialized=0; 
  } 
  mt_unlock(& mutex_kluge); 
} 
 
#define THIS_COND ((COND_T *)(fp->current_storage)) 
void f_cond_wait(INT32 args) 
{ 
  COND_T *c; 
  struct object *key; 
 
  if(args > 1) pop_n_elems(args - 1); 
 
  c=THIS_COND; 
 
  if(args > 0) 
  { 
    struct mutex_storage *mut; 
 
    if(sp[-1].type != T_OBJECT) 
      error("Bad argument 1 to condition->wait()\n"); 
     
    key=sp[-1].u.object; 
     
    if(key->prog != mutex_key) 
      error("Bad argument 1 to condition->wait()\n"); 
     
    mt_lock(&mutex_kluge); 
    mut=OB2KEY(key)->mut; 
    if(!mut) 
      error("Bad argument 1 to condition->wait()\n"); 
 
    THREADS_ALLOW(); 
 
    /* Unlock mutex */ 
    mut->key=0; 
    OB2KEY(key)->mut=0; 
    co_signal(& mut->condition); 
 
    /* Wait and allow mutex operations */ 
    co_wait(c,&mutex_kluge); 
 
    if(OB2KEY(key)->initialized) 
    { 
      /* Lock mutex */ 
      while(mut->key) co_wait(& mut->condition, & mutex_kluge); 
      mut->key=key; 
      OB2KEY(key)->mut=mut; 
    } 
    mt_unlock(&mutex_kluge); 
    THREADS_DISALLOW(); 
    pop_stack(); 
  } else { 
    mt_lock(&mutex_kluge); 
    THREADS_ALLOW(); 
    co_wait(c, &mutex_kluge); 
    mt_unlock(&mutex_kluge); 
    THREADS_DISALLOW(); 
  } 
} 
 
void f_cond_signal(INT32 args) { pop_n_elems(args); co_signal(THIS_COND); } 
void f_cond_broadcast(INT32 args) { pop_n_elems(args); co_broadcast(THIS_COND); } 
void init_cond_obj(struct object *o) { co_init(THIS_COND); } 
void exit_cond_obj(struct object *o) { co_destroy(THIS_COND); } 
 
void f_thread_backtrace(INT32 args) 
{ 
  struct thread_state *foo = (struct thread_state *)fp->current_object->storage; 
  struct thread_state *bar = (struct thread_state *)thread_id->storage; 
  struct svalue *osp = sp; 
  pop_n_elems(args); 
  if(foo->sp) 
  { 
    SWAP_OUT_THREAD(bar); 
    SWAP_IN_THREAD(foo); 
    sp=osp; 
    f_backtrace(0); 
    osp=sp; 
    sp=foo->sp; 
    SWAP_OUT_THREAD(foo); 
    SWAP_IN_THREAD(bar); 
    sp=osp; 
  } else { 
    push_int(0); 
    f_allocate(1); 
  } 
} 
 
void init_thread_obj(struct object *o) 
{ 
  MEMSET(o->storage, 0, sizeof(struct thread_state)); 
} 
 
void th_init(void) 
{ 
  struct program *tmp; 
  INT32 mutex_key_offset; 
 
#ifdef SGI_SPROC_THREADS 
#error /* Need to specify a filename */ 
  us_cookie = usinit(""); 
#endif /* SGI_SPROC_THREADS */ 
 
  mt_lock( & interpreter_lock); 
#ifdef POSIX_THREADS 
  pthread_attr_init(&pattr); 
#ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE 
  pthread_attr_setstacksize(&pattr, 2 * 1024 * 1204); 
#endif 
  pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_DETACHED); 
#endif 
 
  add_efun("thread_create",f_thread_create,"function(mixed ...:object)", 
           OPT_SIDE_EFFECT); 
#ifdef UNIX_THREADS 
  add_efun("thread_set_concurrency",f_thread_set_concurrency, 
           "function(int:void)", OPT_SIDE_EFFECT); 
#endif 
  add_efun("this_thread",f_this_thread,"function(:object)", 
           OPT_EXTERNAL_DEPEND); 
 
  start_new_program(); 
  add_storage(sizeof(struct mutex_storage)); 
  add_function("lock",f_mutex_lock,"function(:object)",0); 
  add_function("trylock",f_mutex_trylock,"function(:object)",0); 
  set_init_callback(init_mutex_obj); 
  set_exit_callback(exit_mutex_obj); 
  end_class("mutex", 0); 
 
  start_new_program(); 
  mutex_key_offset = add_storage(sizeof(struct key_storage)); 
  /* This is needed to allow the gc to find the possible circular reference. 
   * It also allows a process to take over ownership of a key. 
   */ 
  map_variable("_owner", "object", 0, 
               mutex_key_offset + OFFSETOF(key_storage, owner), T_OBJECT); 
  set_init_callback(init_mutex_key_obj); 
  set_exit_callback(exit_mutex_key_obj); 
  mutex_key=end_program(); 
  if(!mutex_key) 
    fatal("Failed to initialize mutex_key program!\n"); 
 
  start_new_program(); 
  add_storage(sizeof(COND_T)); 
  add_function("wait",f_cond_wait,"function(void|object:void)",0); 
  add_function("signal",f_cond_signal,"function(:void)",0); 
  add_function("broadcast",f_cond_broadcast,"function(:void)",0); 
  set_init_callback(init_cond_obj); 
  set_exit_callback(exit_cond_obj); 
  end_class("condition", 0); 
 
  start_new_program(); 
  add_storage(sizeof(struct thread_state)); 
  add_function("backtrace",f_thread_backtrace,"function(:array)",0); 
  set_init_callback(init_thread_obj); 
  thread_id_prog=end_program(); 
  if(!mutex_key) 
    fatal("Failed to initialize thread program!\n"); 
 
  thread_id=clone_object(thread_id_prog,0); 
} 
 
void th_cleanup(void) 
{ 
  if(mutex_key) 
  { 
    free_program(mutex_key); 
    mutex_key=0; 
  } 
 
  if(thread_id_prog) 
  { 
    free_program(thread_id_prog); 
    thread_id_prog=0; 
  } 
 
  if(thread_id) 
  { 
    destruct(thread_id); 
    free_object(thread_id); 
    thread_id=0; 
  } 
} 
 
#endif