Threads.solaris.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*--------- threads support ------------------------- g.f. -----*/
  2. /*--------- lower half of the Oberon Threads module */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <limits.h>
  6. #include "Threads.h"
  7. #define __EXTENSIONS__ 1
  8. #include <pthread.h>
  9. #include <synch.h>
  10. #include <sched.h>
  11. extern int suid_root;
  12. static o_thr_t mainthread = 0;
  13. extern void SetSigaltstack();
  14. o_mtx_t o_mtxInit(int dummy) {
  15. o_mtx_t mtx;
  16. mtx = (o_mtx_t)malloc( sizeof(mutex_t) );
  17. mutex_init( mtx, USYNC_THREAD, NULL );
  18. return mtx;
  19. }
  20. void o_mtxDestroy(o_mtx_t mtx) {
  21. mutex_destroy( mtx );
  22. free( mtx );
  23. }
  24. void o_mtxLock(o_mtx_t mtx) {
  25. mutex_lock( mtx );
  26. }
  27. void o_mtxUnlock(o_mtx_t mtx) {
  28. mutex_unlock( mtx );
  29. }
  30. o_con_t o_conInit(int dummy) {
  31. o_con_t c;
  32. c = (o_con_t)malloc( sizeof(cond_t) );
  33. cond_init( c, USYNC_THREAD, NULL );
  34. return c;
  35. }
  36. void o_conDestroy(o_con_t c) {
  37. cond_destroy( c );
  38. free( c );
  39. }
  40. void o_conWait( o_con_t c, o_mtx_t m ) {
  41. cond_wait( c, m );
  42. }
  43. void o_conSignal( o_con_t c ) {
  44. cond_signal( c );
  45. }
  46. void* starter( void* p ) {
  47. sigset_t orig, new;
  48. oberon_proc body = (oberon_proc)p;
  49. SetSigaltstack();
  50. sigfillset( &new );
  51. sigdelset( &new, SIGILL );
  52. sigdelset( &new, SIGTRAP );
  53. sigdelset( &new, SIGEMT );
  54. sigdelset( &new, SIGFPE );
  55. sigdelset( &new, SIGBUS );
  56. sigdelset( &new, SIGSEGV );
  57. sigdelset( &new, SIGSYS );
  58. sigdelset( &new, SIGPIPE );
  59. sigdelset( &new, SIGALRM );
  60. thr_sigsetmask( SIG_SETMASK, &new, &orig );
  61. pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, NULL );
  62. pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, NULL );
  63. body();
  64. thr_exit( 0 );
  65. }
  66. o_thr_t o_thrStart( oberon_proc p, int len ) {
  67. o_thr_t id;
  68. int err;
  69. if ((len != 0) && (len < 16*1024)) {
  70. len = 16*1024;
  71. }
  72. err = thr_create( NULL, len, starter, p, THR_BOUND|THR_DETACHED, &id );
  73. if (err != 0)
  74. return 0;
  75. return id;
  76. }
  77. o_thr_t o_thrThis(int dummy) {
  78. return thr_self();
  79. }
  80. void o_thrSleep(int ms) {
  81. struct timespec sltime, rem;
  82. sltime.tv_sec = ms/1000;
  83. sltime.tv_nsec = 1000000*(ms%1000);
  84. nanosleep( &sltime, &rem );
  85. }
  86. void o_thrYield(int dummy) {
  87. thr_yield( );
  88. }
  89. void o_thrExit(int dummy) {
  90. thr_exit( 0 );
  91. }
  92. void o_thrSuspend(o_thr_t thr) {
  93. thr_suspend( thr );
  94. }
  95. void o_thrResume(o_thr_t thr) {
  96. thr_continue( thr );
  97. }
  98. void o_thrSetprio(o_thr_t thr, int prio) {
  99. thr_setprio( thr, prio );
  100. }
  101. int o_thrGetprio(o_thr_t thr) {
  102. int prio;
  103. thr_getprio( thr, &prio );
  104. return ( prio );
  105. }
  106. void o_thrKill(o_thr_t thr) {
  107. if (thr != mainthread) {
  108. if (thr == thr_self())
  109. thr_exit( 0 );
  110. else
  111. pthread_cancel( thr );
  112. }
  113. }
  114. /* thr_initialize returns 0 (FALSE) if the program has
  115. been compiled without threads suport. If the program
  116. has no suid root privilleges, priorities are disabled
  117. and low and high both return 0. */
  118. int o_thrInitialize( int *low, int* high ) {
  119. int pl, ph, ret;
  120. struct sched_param p;
  121. pid_t pid;
  122. pid = getpid();
  123. sched_getparam( pid, &p );
  124. ret = sched_setscheduler( pid, SCHED_OTHER, &p );
  125. /*
  126. pl = sched_get_priority_min( SCHED_OTHER );
  127. ph = sched_get_priority_max( SCHED_OTHER );
  128. */
  129. mainthread = thr_self();
  130. *low = 0; *high = 100;
  131. return 1;
  132. }