Threads.solaris.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. sigdelset( &new, SIGUSR1 );
  61. thr_sigsetmask( SIG_SETMASK, &new, &orig );
  62. pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, NULL );
  63. pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, NULL );
  64. body();
  65. thr_exit( 0 );
  66. }
  67. o_thr_t o_thrStart( oberon_proc p, int len ) {
  68. o_thr_t id;
  69. int err;
  70. if ((len != 0) && (len < 16*1024)) {
  71. len = 16*1024;
  72. }
  73. err = thr_create( NULL, len, starter, p, THR_BOUND|THR_DETACHED, &id );
  74. if (err != 0)
  75. return 0;
  76. return id;
  77. }
  78. o_thr_t o_thrThis(int dummy) {
  79. return thr_self();
  80. }
  81. void o_thrSleep(int ms) {
  82. struct timespec sltime, rem;
  83. sltime.tv_sec = ms/1000;
  84. sltime.tv_nsec = 1000000*(ms%1000);
  85. nanosleep( &sltime, &rem );
  86. }
  87. void o_thrYield(int dummy) {
  88. thr_yield( );
  89. }
  90. void o_thrExit(int dummy) {
  91. thr_exit( 0 );
  92. }
  93. void o_thrSuspend(o_thr_t thr) {
  94. thr_suspend( thr );
  95. }
  96. void o_thrResume(o_thr_t thr) {
  97. thr_continue( thr );
  98. }
  99. void o_thrSetprio(o_thr_t thr, int prio) {
  100. thr_setprio( thr, prio );
  101. }
  102. int o_thrGetprio(o_thr_t thr) {
  103. int prio;
  104. thr_getprio( thr, &prio );
  105. return ( prio );
  106. }
  107. void o_thrKill(o_thr_t thr, int sig) {
  108. thr_kill( thr, sig );
  109. /* if (thr != mainthread) {
  110. if (thr == thr_self())
  111. thr_exit( 0 );
  112. else
  113. pthread_cancel( thr );
  114. } */
  115. }
  116. /* thr_initialize returns 0 (FALSE) if the program has
  117. been compiled without threads suport. If the program
  118. has no suid root privilleges, priorities are disabled
  119. and low and high both return 0. */
  120. int o_thrInitialize( int *low, int* high ) {
  121. int pl, ph, ret;
  122. struct sched_param p;
  123. pid_t pid;
  124. pid = getpid();
  125. sched_getparam( pid, &p );
  126. ret = sched_setscheduler( pid, SCHED_OTHER, &p );
  127. /*
  128. pl = sched_get_priority_min( SCHED_OTHER );
  129. ph = sched_get_priority_max( SCHED_OTHER );
  130. */
  131. mainthread = thr_self();
  132. *low = 0; *high = 100;
  133. return 1;
  134. }