usermode/library/malloc-original/benchmarks/larson/parallel.h

00001 #ifndef _SPIN_LOCKS_H_
00002 #define _SPIN_LOCKS_H_
00003 
00004 /* File: parallel.h
00005  *
00006  * This package was originally used at the University of Waterloo to 
00007  * replace the inline "asm" macros for spin locks and barriers provided 
00008  * by the Sequent C compiler. The UW version relied on Gnu C and Gnu
00009  * assembler (gas). Subsequently modified to work with Microsoft C
00010  * and MASM.
00011  *
00012  * Author: Paul Larson, palarson@microsoft.com, July 1997
00013  *
00014  * 12/18/97 PAL
00015  * Deleted everything to do with barriers.
00016  * Changed to use InterlockedExchange to atomically set/test a lock 
00017  * which eliminated all assembler code. Had to change locks to LONG.
00018  * 
00019  */
00020 
00021 #define L_UNLOCKED         0
00022 #define L_LOCKED             1
00023 #define L_MAXTESTS  4000
00024 #define L_SLEEPTIME    0
00025 
00026 
00027 /*
00028  * A spin lock allows for mutual exclusion on
00029  * data structures.
00030  */
00031 
00032 typedef long    splock_t ;       /* spin lock */
00033 
00034 __inline int S_LOCK( splock_t *laddr )
00035 { int cnt ;
00036 
00037   while( InterlockedExchange( laddr, L_LOCKED ) == L_LOCKED ){
00038     cnt = L_MAXTESTS ;
00039     /* check no more than L_MAXTESTS times then yield */
00040                 while( *laddr == L_LOCKED ) { 
00041                         cnt-- ;
00042                         if( cnt < 0 ){ 
00043                                 Sleep(L_SLEEPTIME) ;
00044                                 cnt = L_MAXTESTS ;
00045                         }
00046                 }
00047   }
00048   return(0) ;
00049 }
00050 
00051 __inline int S_TRY_LOCK( splock_t *laddr )
00052 { long oldval ;
00053 
00054         oldval = InterlockedExchange( laddr, L_LOCKED ) ;
00055         if( oldval == L_UNLOCKED) return(0) ;
00056         else                      return(1) ;
00057 }
00058 
00059 
00060 __inline static void S_UNLOCK( splock_t *laddr )
00061 {
00062         *laddr = L_UNLOCKED;
00063 }
00064 
00065 __inline static void S_INIT_LOCK( splock_t *laddr )
00066 {
00067         S_UNLOCK( laddr );
00068 }
00069 
00070 
00071 #endif

Generated on Sat Apr 23 11:43:35 2011 for Mnemosyne by  doxygen 1.4.7