00001
00030 #ifndef GALOIS_RUNTIME_LL_STATICINSTANCE_H
00031 #define GALOIS_RUNTIME_LL_STATICINSTANCE_H
00032
00033 #include "CompilerSpecific.h"
00034
00035 namespace Galois {
00036 namespace Runtime {
00037 namespace LL {
00038
00039
00040
00041 template<typename T>
00042 struct StaticInstance {
00043 volatile T* V;
00044 volatile int _lock;
00045
00046 inline void lock() {
00047 int oldval;
00048 do {
00049 while (_lock != 0) {
00050 asmPause();
00051 }
00052 oldval = __sync_fetch_and_or(&_lock, 1);
00053 } while (oldval & 1);
00054 }
00055
00056 inline void unlock() {
00057 compilerBarrier();
00058 _lock = 0;
00059 }
00060
00061 T* get() {
00062 volatile T* val = V;
00063 if (val)
00064 return (T*)val;
00065 lock();
00066 val = V;
00067 if (!val)
00068 V = val = new T();
00069 unlock();
00070 return (T*)val;
00071 }
00072 };
00073
00074 }
00075 }
00076 }
00077
00078 #endif