comparison Python/thread.c @ 1418:b1bb74a2b593 legacy-trunk

Port to Solaris 2.3.
author Sjoerd Mullender <sjoerd@acm.org>
date Fri, 03 Dec 1993 16:54:45 +0000
parents d9cb55ef34e2
children b724599401ff
comparison
equal deleted inserted replaced
1417:f7edfa6d0dcb 1418:b1bb74a2b593
24 24
25 #include "thread.h" 25 #include "thread.h"
26 26
27 #ifdef DEBUG 27 #ifdef DEBUG
28 static int thread_debug = 0; 28 static int thread_debug = 0;
29 #define dprintf(args) (thread_debug && printf args) 29 #define dprintf(args) ((thread_debug & 1) && printf args)
30 #define d2printf(args) ((thread_debug & 8) && printf args)
30 #else 31 #else
31 #define dprintf(args) 32 #define dprintf(args)
33 #define d2printf(args)
32 #endif 34 #endif
33 35
34 #ifdef __sgi 36 #ifdef __sgi
35 #include <stdlib.h> 37 #include <stdlib.h>
36 #include <stdio.h> 38 #include <stdio.h>
41 #include <errno.h> 43 #include <errno.h>
42 44
43 #define HDR_SIZE 2680 /* sizeof(ushdr_t) */ 45 #define HDR_SIZE 2680 /* sizeof(ushdr_t) */
44 #define MAXPROC 100 /* max # of threads that can be started */ 46 #define MAXPROC 100 /* max # of threads that can be started */
45 47
46 /*static*/ usptr_t *shared_arena; 48 static usptr_t *shared_arena;
47 static ulock_t count_lock; /* protection for some variables */ 49 static ulock_t count_lock; /* protection for some variables */
48 static ulock_t wait_lock; /* lock used to wait for other threads */ 50 static ulock_t wait_lock; /* lock used to wait for other threads */
49 static int waiting_for_threads; /* protected by count_lock */ 51 static int waiting_for_threads; /* protected by count_lock */
50 static int nthreads; /* protected by count_lock */ 52 static int nthreads; /* protected by count_lock */
51 static int exit_status; 53 static int exit_status;
52 static int do_exit; /* indicates that the program is to exit */ 54 static int do_exit; /* indicates that the program is to exit */
53 static int exiting; /* we're already exiting (for maybe_exit) */ 55 static int exiting; /* we're already exiting (for maybe_exit) */
54 static pid_t my_pid; /* PID of main thread */ 56 static pid_t my_pid; /* PID of main thread */
55 static pid_t pidlist[MAXPROC]; /* PIDs of other threads */ 57 static pid_t pidlist[MAXPROC]; /* PIDs of other threads */
56 static int maxpidindex; /* # of PIDs in pidlist */ 58 static int maxpidindex; /* # of PIDs in pidlist */
59 #endif /* __sgi */
60 #ifdef SOLARIS
61 #include <stdlib.h>
62 #include <stdio.h>
63 #include <unistd.h>
64 #include </usr/include/thread.h>
65 #undef sun
57 #endif 66 #endif
58 #ifdef sun 67 #ifdef sun
59 #include <lwp/lwp.h> 68 #include <lwp/lwp.h>
60 #include <lwp/stackdep.h> 69 #include <lwp/stackdep.h>
61 70
65 struct lock { 74 struct lock {
66 int lock_locked; 75 int lock_locked;
67 cv_t lock_condvar; 76 cv_t lock_condvar;
68 mon_t lock_monitor; 77 mon_t lock_monitor;
69 }; 78 };
70 #endif 79 #endif /* sun */
71 #ifdef C_THREADS 80 #ifdef C_THREADS
72 #include <cthreads.h> 81 #include <cthreads.h>
73 #endif 82 #endif /* C_THREADS */
83 #ifdef _POSIX_THREADS
84 #include <pthread.h>
85 #endif /* _POSIX_THREADS */
74 86
75 #ifdef __STDC__ 87 #ifdef __STDC__
76 #define _P(args) args 88 #define _P(args) args
77 #define _P0() (void) 89 #define _P0() (void)
78 #define _P1(v,t) (t) 90 #define _P1(v,t) (t)
80 #else 92 #else
81 #define _P(args) () 93 #define _P(args) ()
82 #define _P0() () 94 #define _P0() ()
83 #define _P1(v,t) (v) t; 95 #define _P1(v,t) (v) t;
84 #define _P2(v1,t1,v2,t2) (v1,v2) t1; t2; 96 #define _P2(v1,t1,v2,t2) (v1,v2) t1; t2;
85 #endif 97 #endif /* __STDC__ */
86 98
87 static int initialized; 99 static int initialized;
88 100
89 #ifdef __sgi 101 #ifdef __sgi
90 /* 102 /*
92 * exits. When that happens, we must see whether we have to exit as 104 * exits. When that happens, we must see whether we have to exit as
93 * well (because of an exit_prog()) or whether we should continue on. 105 * well (because of an exit_prog()) or whether we should continue on.
94 */ 106 */
95 static void exit_sig _P0() 107 static void exit_sig _P0()
96 { 108 {
97 dprintf(("exit_sig called\n")); 109 d2printf(("exit_sig called\n"));
98 if (exiting && getpid() == my_pid) { 110 if (exiting && getpid() == my_pid) {
99 dprintf(("already exiting\n")); 111 d2printf(("already exiting\n"));
100 return; 112 return;
101 } 113 }
102 if (do_exit) { 114 if (do_exit) {
103 dprintf(("exiting in exit_sig\n")); 115 d2printf(("exiting in exit_sig\n"));
116 #ifdef DEBUG
117 if ((thread_debug & 8) == 0)
118 thread_debug &= ~1; /* don't produce debug messages */
119 #endif
104 exit_thread(); 120 exit_thread();
105 } 121 }
106 } 122 }
107 123
108 /* 124 /*
116 dprintf(("already exiting\n")); 132 dprintf(("already exiting\n"));
117 return; 133 return;
118 } 134 }
119 exit_prog(0); 135 exit_prog(0);
120 } 136 }
121 #endif 137 #endif /* __sgi */
122 138
123 /* 139 /*
124 * Initialization. 140 * Initialization.
125 */ 141 */
126 void init_thread _P0() 142 void init_thread _P0()
127 { 143 {
128 #ifdef __sgi 144 #ifdef __sgi
129 struct sigaction s; 145 struct sigaction s;
130 #ifdef USE_DL 146 #ifdef USE_DL
131 long addr, size; 147 long addr, size;
132 #endif 148 #endif /* USE_DL */
133 #endif 149 #endif /* __sgi */
134 150
135 #ifdef DEBUG 151 #ifdef DEBUG
136 thread_debug = getenv("THREADDEBUG") != 0; 152 char *p = getenv("THREADDEBUG");
137 #endif 153
154 if (p) {
155 if (*p)
156 thread_debug = atoi(p);
157 else
158 thread_debug = 1;
159 }
160 #endif /* DEBUG */
138 if (initialized) 161 if (initialized)
139 return; 162 return;
140 initialized = 1; 163 initialized = 1;
141 dprintf(("init_thread called\n")); 164 dprintf(("init_thread called\n"));
142 165
149 addr = (long) dl_getrange(size + HDR_SIZE); 172 addr = (long) dl_getrange(size + HDR_SIZE);
150 dprintf(("trying to use addr %lx-%lx for shared arena\n", addr, addr+size)); 173 dprintf(("trying to use addr %lx-%lx for shared arena\n", addr, addr+size));
151 errno = 0; 174 errno = 0;
152 if ((addr = usconfig(CONF_ATTACHADDR, addr)) < 0 && errno != 0) 175 if ((addr = usconfig(CONF_ATTACHADDR, addr)) < 0 && errno != 0)
153 perror("usconfig - CONF_ATTACHADDR (set)"); 176 perror("usconfig - CONF_ATTACHADDR (set)");
154 #endif 177 #endif /* USE_DL */
155 if (usconfig(CONF_INITUSERS, 16) < 0) 178 if (usconfig(CONF_INITUSERS, 16) < 0)
156 perror("usconfig - CONF_INITUSERS"); 179 perror("usconfig - CONF_INITUSERS");
157 my_pid = getpid(); /* so that we know which is the main thread */ 180 my_pid = getpid(); /* so that we know which is the main thread */
158 atexit(maybe_exit); 181 atexit(maybe_exit);
159 s.sa_handler = exit_sig; 182 s.sa_handler = exit_sig;
163 sigaction(SIGUSR1, &s, 0); 186 sigaction(SIGUSR1, &s, 0);
164 if (prctl(PR_SETEXITSIG, SIGUSR1) < 0) 187 if (prctl(PR_SETEXITSIG, SIGUSR1) < 0)
165 perror("prctl - PR_SETEXITSIG"); 188 perror("prctl - PR_SETEXITSIG");
166 if (usconfig(CONF_ARENATYPE, US_SHAREDONLY) < 0) 189 if (usconfig(CONF_ARENATYPE, US_SHAREDONLY) < 0)
167 perror("usconfig - CONF_ARENATYPE"); 190 perror("usconfig - CONF_ARENATYPE");
168 /*usconfig(CONF_LOCKTYPE, US_DEBUGPLUS);*/ 191 #ifdef DEBUG
192 if (thread_debug & 4)
193 usconfig(CONF_LOCKTYPE, US_DEBUGPLUS);
194 else if (thread_debug & 2)
195 usconfig(CONF_LOCKTYPE, US_DEBUG);
196 #endif /* DEBUG */
169 if ((shared_arena = usinit(tmpnam(0))) == 0) 197 if ((shared_arena = usinit(tmpnam(0))) == 0)
170 perror("usinit"); 198 perror("usinit");
171 #ifdef USE_DL 199 #ifdef USE_DL
172 if (usconfig(CONF_ATTACHADDR, addr) < 0) /* reset address */ 200 if (usconfig(CONF_ATTACHADDR, addr) < 0) /* reset address */
173 perror("usconfig - CONF_ATTACHADDR (reset)"); 201 perror("usconfig - CONF_ATTACHADDR (reset)");
174 #endif 202 #endif /* USE_DL */
175 if ((count_lock = usnewlock(shared_arena)) == NULL) 203 if ((count_lock = usnewlock(shared_arena)) == NULL)
176 perror("usnewlock (count_lock)"); 204 perror("usnewlock (count_lock)");
177 (void) usinitlock(count_lock); 205 (void) usinitlock(count_lock);
178 if ((wait_lock = usnewlock(shared_arena)) == NULL) 206 if ((wait_lock = usnewlock(shared_arena)) == NULL)
179 perror("usnewlock (wait_lock)"); 207 perror("usnewlock (wait_lock)");
180 dprintf(("arena start: %lx, arena size: %ld\n", (long) shared_arena, (long) usconfig(CONF_GETSIZE, shared_arena))); 208 dprintf(("arena start: %lx, arena size: %ld\n", (long) shared_arena, (long) usconfig(CONF_GETSIZE, shared_arena)));
209 #endif /* __sgi */
210 #ifdef SOLARIS
211 /* nothing */
181 #endif 212 #endif
182 #ifdef sun 213 #ifdef sun
183 lwp_setstkcache(STACKSIZE, NSTACKS); 214 lwp_setstkcache(STACKSIZE, NSTACKS);
184 #endif 215 #endif /* sun */
185 #ifdef C_THREADS 216 #ifdef C_THREADS
186 cthread_init(); 217 cthread_init();
187 #endif 218 #endif /* C_THREADS */
188 } 219 }
189 220
190 /* 221 /*
191 * Thread support. 222 * Thread support.
192 */ 223 */
224 #ifdef SOLARIS
225 struct func_arg {
226 void (*func) _P((void *));
227 void *arg;
228 };
229
230 static void *new_func _P1(funcarg, void *funcarg)
231 {
232 void (*func) _P((void *));
233 void *arg;
234
235 func = ((struct func_arg *) funcarg)->func;
236 arg = ((struct func_arg *) funcarg)->arg;
237 free(funcarg);
238 (*func)(arg);
239 return 0;
240 }
241 #endif /* SOLARIS */
242
193 int start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg) 243 int start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg)
194 { 244 {
245 #ifdef SOLARIS
246 struct func_arg *funcarg;
247 #endif
195 #ifdef sun 248 #ifdef sun
196 thread_t tid; 249 thread_t tid;
197 #endif 250 #endif /* sun */
198 #if defined(__sgi) && defined(USE_DL) 251 #if defined(__sgi) && defined(USE_DL)
199 long addr, size; 252 long addr, size;
200 static int local_initialized = 0; 253 static int local_initialized = 0;
254 #endif /* __sgi and USE_DL */
255 #ifdef _POSIX_THREADS
256 pthread_t th;
201 #endif 257 #endif
202 int success = 0; /* init not needed when SOLARIS and */ 258 int success = 0; /* init not needed when SOLARIS and */
203 /* C_THREADS implemented properly */ 259 /* C_THREADS implemented properly */
204 260
205 dprintf(("start_new_thread called\n")); 261 dprintf(("start_new_thread called\n"));
223 dprintf(("trying to use addr %lx-%lx for sproc\n", addr, addr+size)); 279 dprintf(("trying to use addr %lx-%lx for sproc\n", addr, addr+size));
224 errno = 0; 280 errno = 0;
225 if ((addr = usconfig(CONF_ATTACHADDR, addr)) < 0 && errno != 0) 281 if ((addr = usconfig(CONF_ATTACHADDR, addr)) < 0 && errno != 0)
226 perror("usconfig - CONF_ATTACHADDR (set)"); 282 perror("usconfig - CONF_ATTACHADDR (set)");
227 } 283 }
228 #endif 284 #endif /* USE_DL */
229 if ((success = sproc(func, PR_SALL, arg)) < 0) 285 if ((success = sproc(func, PR_SALL, arg)) < 0)
230 perror("sproc"); 286 perror("sproc");
231 #ifdef USE_DL 287 #ifdef USE_DL
232 if (!local_initialized) { 288 if (!local_initialized) {
233 if (usconfig(CONF_ATTACHADDR, addr) < 0) /* reset address */ 289 if (usconfig(CONF_ATTACHADDR, addr) < 0) /* reset address */
234 perror("usconfig - CONF_ATTACHADDR (reset)"); 290 perror("usconfig - CONF_ATTACHADDR (reset)");
235 local_initialized = 1; 291 local_initialized = 1;
236 } 292 }
237 #endif 293 #endif /* USE_DL */
238 if (success >= 0) { 294 if (success >= 0) {
239 nthreads++; 295 nthreads++;
240 pidlist[maxpidindex++] = success; 296 pidlist[maxpidindex++] = success;
241 } 297 }
242 } 298 }
243 if (usunsetlock(count_lock) < 0) 299 if (usunsetlock(count_lock) < 0)
244 perror("usunsetlock (count_lock)"); 300 perror("usunsetlock (count_lock)");
245 #endif 301 #endif /* __sgi */
246 #ifdef SOLARIS 302 #ifdef SOLARIS
247 (void) thread_create(0, 0, func, arg, THREAD_NEW_LWP); 303 funcarg = (struct func_arg *) malloc(sizeof(struct func_arg));
248 #endif 304 funcarg->func = func;
305 funcarg->arg = arg;
306 if (thr_create(0, 0, new_func, funcarg, THR_NEW_LWP, 0)) {
307 perror("thr_create");
308 free((void *) funcarg);
309 success = -1;
310 }
311 #endif /* SOLARIS */
249 #ifdef sun 312 #ifdef sun
250 success = lwp_create(&tid, func, MINPRIO, 0, lwp_newstk(), 1, arg); 313 success = lwp_create(&tid, func, MINPRIO, 0, lwp_newstk(), 1, arg);
251 #endif 314 #endif /* sun */
252 #ifdef C_THREADS 315 #ifdef C_THREADS
253 (void) cthread_fork(func, arg); 316 (void) cthread_fork(func, arg);
317 #endif /* C_THREADS */
318 #ifdef _POSIX_THREADS
319 pthread_create(&th, NULL, func, arg);
254 #endif 320 #endif
255 return success < 0 ? 0 : 1; 321 return success < 0 ? 0 : 1;
256 } 322 }
257 323
258 static void do_exit_thread _P1(no_cleanup, int no_cleanup) 324 static void do_exit_thread _P1(no_cleanup, int no_cleanup)
309 } else if (do_exit) 375 } else if (do_exit)
310 (void) kill(my_pid, SIGUSR1); 376 (void) kill(my_pid, SIGUSR1);
311 if (usunsetlock(count_lock) < 0) 377 if (usunsetlock(count_lock) < 0)
312 perror("usunsetlock (count_lock)"); 378 perror("usunsetlock (count_lock)");
313 _exit(0); 379 _exit(0);
314 #endif 380 #endif /* __sgi */
315 #ifdef SOLARIS 381 #ifdef SOLARIS
316 thread_exit(); 382 thr_exit(0);
317 #endif 383 #endif /* SOLARIS */
318 #ifdef sun 384 #ifdef sun
319 lwp_destroy(SELF); 385 lwp_destroy(SELF);
320 #endif 386 #endif /* sun */
321 #ifdef C_THREADS 387 #ifdef C_THREADS
322 cthread_exit(0); 388 cthread_exit(0);
323 #endif 389 #endif /* C_THREADS */
324 } 390 }
325 391
326 void exit_thread _P0() 392 void exit_thread _P0()
327 { 393 {
328 do_exit_thread(0); 394 do_exit_thread(0);
344 #ifdef __sgi 410 #ifdef __sgi
345 do_exit = 1; 411 do_exit = 1;
346 exit_status = status; 412 exit_status = status;
347 do_exit_thread(no_cleanup); 413 do_exit_thread(no_cleanup);
348 #endif 414 #endif
415 #ifdef SOLARIS
416 if (no_cleanup)
417 _exit(status);
418 else
419 exit(status);
420 #endif
349 #ifdef sun 421 #ifdef sun
350 pod_exit(status); 422 pod_exit(status);
351 #endif 423 #endif
352 } 424 }
353 425
367 type_lock allocate_lock _P0() 439 type_lock allocate_lock _P0()
368 { 440 {
369 #ifdef __sgi 441 #ifdef __sgi
370 ulock_t lock; 442 ulock_t lock;
371 #endif 443 #endif
444 #ifdef SOLARIS
445 mutex_t *lock;
446 #endif
372 #ifdef sun 447 #ifdef sun
373 struct lock *lock; 448 struct lock *lock;
374 extern char *malloc(); 449 extern char *malloc();
375 #endif 450 #endif
376 451
380 455
381 #ifdef __sgi 456 #ifdef __sgi
382 if ((lock = usnewlock(shared_arena)) == NULL) 457 if ((lock = usnewlock(shared_arena)) == NULL)
383 perror("usnewlock"); 458 perror("usnewlock");
384 (void) usinitlock(lock); 459 (void) usinitlock(lock);
385 #endif 460 #endif /* __sgi */
461 #ifdef SOLARIS
462 lock = (mutex_t *) malloc(sizeof(mutex_t));
463 if (mutex_init(lock, USYNC_THREAD, 0)) {
464 perror("mutex_init");
465 free((void *) lock);
466 lock = 0;
467 }
468 #endif /* SOLARIS */
386 #ifdef sun 469 #ifdef sun
387 lock = (struct lock *) malloc(sizeof(struct lock)); 470 lock = (struct lock *) malloc(sizeof(struct lock));
388 lock->lock_locked = 0; 471 lock->lock_locked = 0;
389 (void) mon_create(&lock->lock_monitor); 472 (void) mon_create(&lock->lock_monitor);
390 (void) cv_create(&lock->lock_condvar, lock->lock_monitor); 473 (void) cv_create(&lock->lock_condvar, lock->lock_monitor);
391 #endif 474 #endif /* sun */
392 dprintf(("allocate_lock() -> %lx\n", (long)lock)); 475 dprintf(("allocate_lock() -> %lx\n", (long)lock));
393 return (type_lock) lock; 476 return (type_lock) lock;
394 } 477 }
395 478
396 void free_lock _P1(lock, type_lock lock) 479 void free_lock _P1(lock, type_lock lock)
397 { 480 {
398 dprintf(("free_lock(%lx) called\n", (long)lock)); 481 dprintf(("free_lock(%lx) called\n", (long)lock));
399 #ifdef __sgi 482 #ifdef __sgi
400 usfreelock((ulock_t) lock, shared_arena); 483 usfreelock((ulock_t) lock, shared_arena);
484 #endif /* __sgi */
485 #ifdef SOLARIS
486 mutex_destroy((mutex_t *) lock);
487 free((void *) lock);
401 #endif 488 #endif
402 #ifdef sun 489 #ifdef sun
403 mon_destroy(((struct lock *) lock)->lock_monitor); 490 mon_destroy(((struct lock *) lock)->lock_monitor);
404 free((char *) lock); 491 free((char *) lock);
405 #endif 492 #endif /* sun */
406 } 493 }
407 494
408 int acquire_lock _P2(lock, type_lock lock, waitflag, int waitflag) 495 int acquire_lock _P2(lock, type_lock lock, waitflag, int waitflag)
409 { 496 {
410 int success; 497 int success;
416 success = ussetlock((ulock_t) lock); 503 success = ussetlock((ulock_t) lock);
417 else 504 else
418 success = uscsetlock((ulock_t) lock, 1); /* Try it once */ 505 success = uscsetlock((ulock_t) lock, 1); /* Try it once */
419 if (success < 0) 506 if (success < 0)
420 perror(waitflag ? "ussetlock" : "uscsetlock"); 507 perror(waitflag ? "ussetlock" : "uscsetlock");
421 #endif 508 #endif /* __sgi */
509 #ifdef SOLARIS
510 if (waitflag)
511 success = mutex_lock((mutex_t *) lock);
512 else
513 success = mutex_trylock((mutex_t *) lock);
514 if (success < 0)
515 perror(waitflag ? "mutex_lock" : "mutex_trylock");
516 else
517 success = !success; /* solaris does it the other way round */
518 #endif /* SOLARIS */
422 #ifdef sun 519 #ifdef sun
423 success = 0; 520 success = 0;
424 521
425 (void) mon_enter(((struct lock *) lock)->lock_monitor); 522 (void) mon_enter(((struct lock *) lock)->lock_monitor);
426 if (waitflag) 523 if (waitflag)
430 success = 1; 527 success = 1;
431 ((struct lock *) lock)->lock_locked = 1; 528 ((struct lock *) lock)->lock_locked = 1;
432 } 529 }
433 cv_broadcast(((struct lock *) lock)->lock_condvar); 530 cv_broadcast(((struct lock *) lock)->lock_condvar);
434 mon_exit(((struct lock *) lock)->lock_monitor); 531 mon_exit(((struct lock *) lock)->lock_monitor);
435 #endif 532 #endif /* sun */
436 dprintf(("acquire_lock(%lx, %d) -> %d\n", (long)lock, waitflag, success)); 533 dprintf(("acquire_lock(%lx, %d) -> %d\n", (long)lock, waitflag, success));
437 return success; 534 return success;
438 } 535 }
439 536
440 void release_lock _P1(lock, type_lock lock) 537 void release_lock _P1(lock, type_lock lock)
441 { 538 {
442 dprintf(("release_lock(%lx) called\n", (long)lock)); 539 dprintf(("release_lock(%lx) called\n", (long)lock));
443 #ifdef __sgi 540 #ifdef __sgi
444 if (usunsetlock((ulock_t) lock) < 0) 541 if (usunsetlock((ulock_t) lock) < 0)
445 perror("usunsetlock"); 542 perror("usunsetlock");
446 #endif 543 #endif /* __sgi */
544 #ifdef SOLARIS
545 if (mutex_unlock((mutex_t *) lock))
546 perror("mutex_unlock");
547 #endif /* SOLARIS */
447 #ifdef sun 548 #ifdef sun
448 (void) mon_enter(((struct lock *) lock)->lock_monitor); 549 (void) mon_enter(((struct lock *) lock)->lock_monitor);
449 ((struct lock *) lock)->lock_locked = 0; 550 ((struct lock *) lock)->lock_locked = 0;
450 cv_broadcast(((struct lock *) lock)->lock_condvar); 551 cv_broadcast(((struct lock *) lock)->lock_condvar);
451 mon_exit(((struct lock *) lock)->lock_monitor); 552 mon_exit(((struct lock *) lock)->lock_monitor);
452 #endif 553 #endif /* sun */
453 } 554 }
454 555
455 /* 556 /*
456 * Semaphore support. 557 * Semaphore support.
457 */ 558 */
458 type_sema allocate_sema _P1(value, int value) 559 type_sema allocate_sema _P1(value, int value)
459 { 560 {
460 #ifdef __sgi 561 #ifdef __sgi
461 usema_t *sema; 562 usema_t *sema;
563 #endif /* __sgi */
564 #ifdef SOLARIS
565 sema_t *sema;
462 #endif 566 #endif
463 567
464 dprintf(("allocate_sema called\n")); 568 dprintf(("allocate_sema called\n"));
465 if (!initialized) 569 if (!initialized)
466 init_thread(); 570 init_thread();
467 571
468 #ifdef __sgi 572 #ifdef __sgi
469 if ((sema = usnewsema(shared_arena, value)) == NULL) 573 if ((sema = usnewsema(shared_arena, value)) == NULL)
470 perror("usnewsema"); 574 perror("usnewsema");
575 #endif /* __sgi */
576 #ifdef SOLARIS
577 sema = (sema_t *) malloc(sizeof(sema_t));
578 if (sema_init(sema, value, USYNC_THREAD, 0)) {
579 perror("sema_init");
580 free((void *) sema);
581 sema = 0;
582 }
583 #endif /* SOLARIS */
471 dprintf(("allocate_sema() -> %lx\n", (long) sema)); 584 dprintf(("allocate_sema() -> %lx\n", (long) sema));
472 return (type_sema) sema; 585 return (type_sema) sema;
473 #endif
474 } 586 }
475 587
476 void free_sema _P1(sema, type_sema sema) 588 void free_sema _P1(sema, type_sema sema)
477 { 589 {
478 dprintf(("free_sema(%lx) called\n", (long) sema)); 590 dprintf(("free_sema(%lx) called\n", (long) sema));
479 #ifdef __sgi 591 #ifdef __sgi
480 usfreesema((usema_t *) sema, shared_arena); 592 usfreesema((usema_t *) sema, shared_arena);
481 #endif 593 #endif /* __sgi */
594 #ifdef SOLARIS
595 if (sema_destroy((sema_t *) sema))
596 perror("sema_destroy");
597 free((void *) sema);
598 #endif /* SOLARIS */
482 } 599 }
483 600
484 void down_sema _P1(sema, type_sema sema) 601 void down_sema _P1(sema, type_sema sema)
485 { 602 {
486 dprintf(("down_sema(%lx) called\n", (long) sema)); 603 dprintf(("down_sema(%lx) called\n", (long) sema));
487 #ifdef __sgi 604 #ifdef __sgi
488 if (uspsema((usema_t *) sema) < 0) 605 if (uspsema((usema_t *) sema) < 0)
489 perror("uspsema"); 606 perror("uspsema");
490 #endif 607 #endif
608 #ifdef SOLARIS
609 if (sema_wait((sema_t *) sema))
610 perror("sema_wait");
611 #endif
491 dprintf(("down_sema(%lx) return\n", (long) sema)); 612 dprintf(("down_sema(%lx) return\n", (long) sema));
492 } 613 }
493 614
494 void up_sema _P1(sema, type_sema sema) 615 void up_sema _P1(sema, type_sema sema)
495 { 616 {
496 dprintf(("up_sema(%lx)\n", (long) sema)); 617 dprintf(("up_sema(%lx)\n", (long) sema));
497 #ifdef __sgi 618 #ifdef __sgi
498 if (usvsema((usema_t *) sema) < 0) 619 if (usvsema((usema_t *) sema) < 0)
499 perror("usvsema"); 620 perror("usvsema");
500 #endif 621 #endif /* __sgi */
501 } 622 #ifdef SOLARIS
623 if (sema_post((sema_t *) sema))
624 perror("sema_post");
625 #endif
626 }