home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Examples / ThreadedApp-1.0.1 / CJRLock.m < prev    next >
Encoding:
Text File  |  1997-04-02  |  2.7 KB  |  168 lines

  1. static char rcsid[] = "$Id: CJRLock.m,v 1.1 1997/04/02 18:28:22 croehrig Exp $";
  2. /*
  3.  *  CJRLock  -- Foundation Kit-less NSLock, NSConditionLock
  4.  *
  5.  *  (c) 1997 Chris Roehrig <croehrig@House.ORG>
  6.  *
  7.  */
  8. #import "CJRLock.h"
  9. #import "ThreadedApp.h"
  10.  
  11. /***************    CJRLock        ***************/
  12.  
  13. extern cthread_t mainThread;
  14.  
  15.  
  16. @implementation CJRLock:Object
  17.  
  18.     
  19. - init
  20. {
  21.     [super init];
  22.     mutex = mutex_alloc();
  23.     return self;
  24. }
  25.  
  26. - free
  27. {
  28.     mutex_free(mutex);
  29.     return [super free];
  30. }
  31.  
  32. - (void)lock
  33. {
  34.     if( cthread_self() != mainThread ){
  35.     // ok to block...
  36.     mutex_lock(mutex);
  37.     } else {
  38.     // Don't block main event loop; use ThreadedApp's lock
  39.     [NXApp lock:mutex];
  40.     }
  41.  
  42. }
  43.  
  44. - (void)unlock
  45. {
  46.     mutex_unlock(mutex);
  47.     if( cthread_self() != mainThread ){
  48.     // notify main thread that an unlock has occurred (see ThreadedApp)
  49.     [NXApp threadDidUnlock:self];
  50.     }
  51. }
  52.  
  53. - (BOOL)tryLock
  54. {
  55.     return mutex_try_lock(mutex);
  56. }
  57.  
  58. @end
  59.  
  60.  
  61. /***************    CJRConditionLock        ***************/
  62.  
  63.  
  64. @implementation CJRConditionLock:Object
  65.  
  66.  
  67. - initWithCondition:(int)condition
  68. {
  69.     [super init];
  70.     mutex = mutex_alloc();
  71.     cond = condition_alloc();
  72.     data = condition;
  73.  
  74.     return self;
  75. }
  76.  
  77. - init
  78. {
  79.     return [self initWithCondition:0];
  80. }
  81.  
  82. - free
  83. {
  84.     condition_free(cond);
  85.     mutex_free(mutex);
  86.     return [super free];
  87. }
  88.  
  89. - (void)lock
  90. {
  91.     if( cthread_self() != mainThread ){
  92.     // ok to block...
  93.     mutex_lock(mutex);
  94.     } else {
  95.     // Don't block main event loop; use ThreadedApp's lock
  96.     [NXApp lock:mutex];
  97.     }
  98. }
  99.  
  100. - (void)unlock
  101. {
  102.     mutex_unlock(mutex);
  103.  
  104.     // wake up other threads waiting for the lock...
  105.     condition_broadcast(cond);
  106.  
  107.     if( cthread_self() != mainThread ){
  108.     // notify main thread that an unlock has occurred (see ThreadedApp)
  109.     [NXApp threadDidUnlock:self];
  110.     }
  111. }
  112.  
  113. - (int)condition
  114. {
  115.     return data;
  116. }
  117.  
  118. - (BOOL)tryLock
  119. {
  120.     return mutex_try_lock(mutex);
  121. }
  122.  
  123. - (void)lockWhenCondition:(int)condition
  124. {
  125.     if( cthread_self() != mainThread ){
  126.     // blocking is ok...
  127.     mutex_lock(mutex);
  128.     while( data != condition )
  129.         condition_wait(cond,mutex);
  130.     } else {
  131.     // Don't block the main thread...
  132.     [NXApp lock:mutex whenCondition:cond withData:&data is:condition];
  133.     }
  134. }
  135.     
  136.  
  137. - (BOOL)tryLockWhenCondition:(int)condition
  138. {
  139.     if( mutex_try_lock(mutex) ){
  140.     if( condition==data ){
  141.         return YES;
  142.     } else {
  143.         mutex_unlock(mutex);
  144.         return NO;
  145.     }
  146.     } else {
  147.     return NO;
  148.     }
  149. }
  150.     
  151. - (void)unlockWithCondition:(int)condition
  152. {
  153.     data = condition;
  154.     mutex_unlock(mutex);
  155.  
  156.     // wake up other threads waiting for the lock...
  157.     condition_broadcast(cond);
  158.  
  159.     // notify (wake up) the main thread 
  160.     if( cthread_self() != mainThread ){
  161.     [NXApp threadDidUnlock:self];
  162.     }
  163.  
  164.  
  165. }
  166.     
  167. @end
  168.