00001 00002 #ifndef __PPC_ATOMIC_MUTEX__ 00003 #define __PPC_ATOMIC_MUTEX__ 00004 00005 #include <pthread.h> 00006 #include <stdio.h> 00007 #include <assert.h> 00008 #include <stdint.h> 00009 00010 #if CMK_PPC_ATOMIC_DEFAULT_IMPL 00011 #include "default_ppcq.h" 00012 #else 00013 //define new ppc atomics in the pami instance directory 00014 #include "ppc_atomicq_impl.h" 00015 #endif 00016 00017 typedef struct _ppc_atomic_mutex_t 00018 { 00019 volatile ppc_atomic_t counter; 00020 volatile ppc_atomic_t bound; 00021 } PPCAtomicMutex; 00022 00023 PPCAtomicMutex *PPCAtomicMutexInit (void * atomic_mem, 00024 size_t atomic_size) 00025 { 00026 //Verify counter array is 64-byte aligned 00027 assert( (((uintptr_t) atomic_mem) & (0x0F)) == 0 ); 00028 assert (sizeof(PPCAtomicMutex) <= atomic_size); 00029 00030 PPCAtomicMutex *mutex = (PPCAtomicMutex*) atomic_mem; 00031 PPC_AtomicStore(&mutex->counter, 0); 00032 PPC_AtomicStore(&mutex->bound, 1); 00033 00034 return mutex; 00035 } 00036 00043 static inline int PPCAtomicMutexTryAcquire (PPCAtomicMutex *mutex) 00044 { 00045 size_t rc = PPC_AtomicLoadIncrementBounded(&mutex->counter); 00046 if (rc == CMI_PPC_ATOMIC_FAIL) 00047 return 1; 00048 00049 PPC_AtomicReadFence(); 00050 return rc; 00051 } 00052 00058 static inline void PPCAtomicMutexAcquire (PPCAtomicMutex *mutex) 00059 { 00060 size_t rc = 0; 00061 do { 00062 rc = PPC_AtomicLoadIncrementBounded(&mutex->counter); 00063 } while (rc == CMI_PPC_ATOMIC_FAIL); 00064 00065 PPC_AtomicReadFence(); 00066 } 00067 00072 static inline void PPCAtomicMutexRelease(PPCAtomicMutex *mutex) 00073 { 00074 //Flush outstanding loads/stores 00075 PPC_AtomicWriteFence(); 00076 00077 /* Release the lock */ 00078 PPC_AtomicStore(&(mutex->counter), 0); 00079 } 00080 00081 00082 #endif