threads.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include <brl.mod/blitz.mod/blitz.h>
  2. //***** Threads *****
  3. BBThread *threads_CreateThread( BBThreadProc entry,BBObject *data ){
  4. BBThread *thread=bbThreadCreate( entry,data );
  5. bbThreadResume( thread );
  6. return thread;
  7. }
  8. void threads_DetachThread( BBThread *thread ){
  9. bbThreadDetach( thread );
  10. }
  11. BBObject *threads_WaitThread( BBThread *thread ){
  12. return bbThreadWait( thread );
  13. }
  14. //***** Mutexes *****
  15. bb_mutex_t *threads_CreateMutex(){
  16. bb_mutex_t *mutex=malloc( sizeof(bb_mutex_t) );
  17. if( bb_mutex_init( mutex ) ) return mutex;
  18. free( mutex );
  19. return 0;
  20. }
  21. void threads_CloseMutex( bb_mutex_t *mutex ){
  22. bb_mutex_destroy( mutex );
  23. free( mutex );
  24. }
  25. void threads_LockMutex( bb_mutex_t *mutex ){
  26. bb_mutex_lock( mutex );
  27. }
  28. void threads_UnlockMutex( bb_mutex_t *mutex ){
  29. bb_mutex_unlock( mutex );
  30. }
  31. int threads_TryLockMutex( bb_mutex_t *mutex ){
  32. return bb_mutex_trylock( mutex );
  33. }
  34. //***** Semaphores *****
  35. bb_sem_t *threads_CreateSemaphore( int count ){
  36. bb_sem_t *sem=malloc( sizeof(bb_sem_t) );
  37. if( bb_sem_init( sem,count ) ) return sem;
  38. free( sem );
  39. return 0;
  40. }
  41. void threads_CloseSemaphore( bb_sem_t *sem ){
  42. bb_sem_destroy( sem );
  43. free( sem );
  44. }
  45. void threads_WaitSemaphore( bb_sem_t *sem ){
  46. bb_sem_wait( sem );
  47. }
  48. void threads_PostSemaphore( bb_sem_t *sem ){
  49. bb_sem_post( sem );
  50. }
  51. //***** CondVars *****
  52. #ifdef _WIN32
  53. typedef struct BBCond BBCond;
  54. struct BBCond{
  55. int waiters;
  56. bb_sem_t sema;
  57. };
  58. BBCond *threads_CreateCond(){
  59. BBCond *cond=(BBCond*)malloc( sizeof( BBCond ) );
  60. cond->waiters=0;
  61. bb_sem_init( &cond->sema,0 );
  62. return cond;
  63. }
  64. void threads_CloseCond( BBCond *cond ){
  65. bb_sem_destroy( &cond->sema );
  66. free( cond );
  67. }
  68. void threads_WaitCond( BBCond *cond,bb_mutex_t *mutex ){
  69. bbAtomicAdd( &cond->waiters,1 );
  70. //Ok, the below is not strictly speaking 'fair'.
  71. //A context switch below the mutex_unlock and sem_wait could end up
  72. //starving this thread (I think). Possibly not a problem unless app is
  73. //really thrashing, but should be fixed none-the-less.
  74. //
  75. bb_mutex_unlock( mutex );
  76. bb_sem_wait( &cond->sema );
  77. bb_mutex_lock( mutex );
  78. }
  79. void threads_SignalCond( BBCond *cond ){
  80. int waiters;
  81. while( waiters=cond->waiters ){
  82. if( bbAtomicCAS( &cond->waiters,waiters,waiters-1 ) ){
  83. bb_sem_post( &cond->sema );
  84. return;
  85. }
  86. }
  87. }
  88. void threads_BroadcastCond( BBCond *cond ){
  89. int waiters;
  90. while( waiters=cond->waiters ){
  91. if( bbAtomicCAS( &cond->waiters,waiters,0 ) ){
  92. while( waiters-- ){
  93. bb_sem_post( &cond->sema );
  94. }
  95. return;
  96. }
  97. }
  98. }
  99. #else
  100. pthread_cond_t *threads_CreateCond(){
  101. pthread_cond_t *cond=malloc( sizeof( pthread_cond_t ) );
  102. if( pthread_cond_init( cond,0 )>=0 ) return cond;
  103. free( cond );
  104. return 0;
  105. }
  106. void threads_CloseCond( pthread_cond_t *cond ){
  107. pthread_cond_destroy( cond );
  108. free( cond );
  109. }
  110. void threads_WaitCond( pthread_cond_t *cond,bb_mutex_t *mutex ){
  111. pthread_cond_wait( cond,mutex );
  112. }
  113. void threads_SignalCond( pthread_cond_t *cond ){
  114. pthread_cond_signal( cond );
  115. }
  116. void threads_BroadcastCond( pthread_cond_t *cond ){
  117. pthread_cond_broadcast( cond );
  118. }
  119. #endif