home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / NeXT-Icons / next-icon@gun.com / Apps / ImagePortfolio / threads.subproj / nonBusyMutex.c < prev    next >
Encoding:
Text File  |  1993-06-03  |  2.6 KB  |  109 lines

  1. // -------------------------------------------------------------------------------------
  2. // nonBusyMutex.c
  3. // Non-busy mutex_lock
  4. // -------------------------------------------------------------------------------------
  5. // Permission is granted to freely redistribute this source code, and to use fragments
  6. // of this code in your own applications if you find them to be useful.  This class/module,
  7. // along with the source code, come with no warranty of any kind, and the user assumes
  8. // all responsibility for its use.
  9. // -------------------------------------------------------------------------------------
  10. #import <stdlib.h>
  11. #import <mach/cthreads.h>
  12. #import "nonBusyMutex.h"
  13. #import "threadStdio.h"
  14.  
  15. // -------------------------------------------------------------------------------------
  16. // normal mutex mode if TRUE
  17. int    _nb_mutex_mode = 0;
  18.  
  19. // -------------------------------------------------------------------------------------
  20.  
  21. /* mutex_init() */
  22. void nb_mutex_init(nb_mutex_t n)
  23. {
  24.   mutex_init(&n->m);
  25.   condition_init(&n->c);
  26.   n->l = 0;
  27. }
  28.  
  29. /* mutex_alloc() */
  30. nb_mutex_t nb_mutex_alloc()
  31. {
  32.   nb_mutex_t    n = ((nb_mutex_t)malloc(sizeof(struct nb_mutex_s)));
  33.   nb_mutex_init(n);
  34.   return n;
  35. }
  36.  
  37. /* mutex_clear() */
  38. void nb_mutex_clear(nb_mutex_t n)
  39. {
  40.   mutex_clear(&n->m);
  41.   condition_clear(&n->c);
  42. }
  43.  
  44. /* mutex_free() */
  45. void nb_mutex_free(nb_mutex_t n)
  46. {
  47.   mutex_clear(&n->m);
  48.   condition_clear(&n->c);
  49.   free(n);
  50. }
  51.  
  52. /* mutex_try_lock() */
  53. int nb_mutex_try_lock(nb_mutex_t n)
  54. {
  55.   int            success;
  56.   if (_nb_mutex_mode) return mutex_try_lock(&n->m);
  57.   mutex_lock(&n->m);
  58.   success = n->l? 0 : (n->l = 1); 
  59.   mutex_unlock(&n->m);
  60.   return success;
  61. }
  62.  
  63. /* mutex_lock() */
  64. void nb_mutex_lock(nb_mutex_t n)
  65. {
  66.   if (_nb_mutex_mode) { mutex_lock(&n->m); return; }
  67.   mutex_lock(&n->m);
  68.   while (n->l) condition_wait(&n->c, &n->m);
  69.   n->l = 1;
  70.   mutex_unlock(&n->m);
  71. }
  72.  
  73. /* mutex_unlock() */
  74. void nb_mutex_unlock(nb_mutex_t n)
  75. {
  76.   if (_nb_mutex_mode) { mutex_unlock(&n->m); return; }
  77.   mutex_lock(&n->m);
  78.   n->l = 0;
  79.   mutex_unlock(&n->m);
  80.   condition_broadcast(&n->c);
  81. }
  82.  
  83. /* mutex_name() */
  84. char *nb_mutex_name(nb_mutex_t n)
  85. {
  86.   return mutex_name(&n->m);
  87. }
  88.  
  89. /* mutex_set_name() */
  90. void nb_mutex_set_name(nb_mutex_t n, char *name)
  91. {
  92.   mutex_set_name(&n->m, name);
  93. }
  94.  
  95. // -------------------------------------------------------------------------------------
  96. // conditions
  97.  
  98. /* condition_wait() */
  99. void nb_condition_wait(condition_t c, nb_mutex_t n)
  100. {
  101.   if (_nb_mutex_mode) { condition_wait(c, &n->m); return; }
  102.   mutex_lock(&n->m);
  103.   n->l = 0;
  104.   condition_broadcast(&n->c);
  105.   condition_wait(c, &n->m);
  106.   n->l = 1;
  107.   mutex_unlock(&n->m);
  108. }
  109.