home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / quartz / quartz10.lha / src / presto / spinlock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-02  |  2.3 KB  |  139 lines

  1. //
  2. // spinlock.c
  3. //    non-inlined functions for spinlocks
  4. //
  5. // Modification History:
  6. //
  7. // 28-Dec-1989  JEF
  8. // Add class HC_Spinlock (for sequent symmetry only).  This variation of
  9. // a spinlock works well when there is high contention for the lock.
  10. // After original by raj.
  11. //
  12.  
  13. #include <stream.h>
  14. #include "presto.h"
  15.  
  16. void
  17. Spinlock::print(ostream& s)
  18. {
  19.     s << form("(Spinlock)this=0x%x, sl_lock=0x%x", this, this->sl_lock);
  20. }
  21.  
  22.  
  23. #ifdef vax
  24.  
  25. //
  26. // These are the functions that eventually get called for operations on
  27. // spinlocks on a VAX.
  28. //
  29.  
  30. void
  31. s_init_lock(slock_t* l)
  32. {
  33.      *l  = 0;
  34. }
  35.  
  36. /*
  37.  * s_lock, s_unlock and s_clock are defined in asm code in vax_lock.s
  38.  * XXX asm versions of s_lock and s_unlock don't return a value, should
  39.  * be void.
  40.  *
  41.  * s_clock acquires the spinlock iff it is free, else returns immediately
  42.  * without spinning.
  43.  *
  44.  * The asm versions of these functions assume that a lock is a longword.
  45.  * The low-order bit of the word is used as the lock bit.  The other bits
  46.  * are unused.
  47.  */
  48.  
  49. /*
  50.  
  51. int
  52. *s_lock(slock_t *l)
  53. {
  54.     while (*l)
  55.      ;
  56.     return(*l++);
  57. }
  58.  
  59.  
  60. int
  61. s_unlock(slock_t* l)
  62. {
  63.      *l = 0;
  64.      return(1);
  65. }
  66.  
  67.  
  68. int
  69. s_clock(slock_t* l)
  70. {
  71.      return((*l) ? 0 : (*l)++);
  72. }
  73.  
  74. */
  75.  
  76. #endif vax
  77. #ifdef sun
  78. #ifdef DO_SPINLOCK_INLINE
  79.         //
  80.         // s_lock, s_unlock and s_clock are defined in sun_lock.s
  81.         //
  82.         void s_init_lock(slock_t* l) {
  83.         *l  = 0; 
  84.     }
  85.  
  86.         void s_lock(slock_t *l) { 
  87.         while (*l) ; *l =  1; 
  88.     }
  89.         void s_unlock(slock_t* l) { 
  90.         *l = 0; 
  91.     }
  92.         int s_clock(slock_t* l) { 
  93.         return((*l) ? 0 : (*l)++);
  94.     }
  95. #else
  96.         void Spinlock::~Spinlock() {
  97.                 if (sl_lock)
  98.                         unlock();
  99.         }
  100.  
  101.         void Spinlock::unlock() {
  102.                 (void) S_UNLOCK(&sl_lock);
  103. #   ifndef NO_PREEMPT
  104.                 thisthread->releasingspinlock();
  105. #   endif  NO_PREEMPT
  106.         }
  107.  
  108.         void Spinlock::lock() {
  109. #   ifndef NO_PREEMPT
  110.                 thisthread->holdingspinlock();
  111.  
  112. #   endif NO_PREEMPT
  113.                 (void) S_LOCK(&sl_lock);
  114.         }
  115. #endif DO_SPINLOCK_INLINE
  116. #endif sun
  117.  
  118.  
  119. //
  120. // non-inlined functions for HC_Spinlocks
  121. //
  122. #ifdef i386
  123.  
  124. void
  125. HC_Spinlock::print(ostream& s)
  126. {
  127.     // XXX note: understands what a hc_slock_t looks like...
  128.     s << form("(HC_Spinlock)this=0x%x, sl_lock=0x%x", this, sl_lock.x[0][0]);
  129.     s << "\n";
  130. }
  131.  
  132. #endif i386
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.