00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __loaded__lock_cpp__
00021 #define __loaded__lock_cpp__
00022 using namespace std;
00023 #line 1 "lock.c++"
00024 #include <string>
00025 #include <pthread.h>
00026 #include <stdlib.h>
00027 #include <assert.h>
00028 #include <vector>
00029 #include <queue>
00030 #include <time.h>
00031 #include <iostream>
00032 #include "lock.h"
00033
00034 bool Lock::try_lock(string w)
00035 {
00036 assert(!deleted);
00037 if (locked) return false;
00038
00039 void * before = (void*)locked;
00040 if (cmpxchg(&locked,locked,this)==before)
00041 {
00042 who=w;
00043 #ifdef DEBUG_LOCKING
00044 cerr << "Locked " << locked << " by " << w << "\n";
00045 #endif
00046 return true;
00047 }
00048 #ifdef DEBUG_LOCKING
00049 cerr << "Tried locked of " << locked << " by " << w <<
00050 " could not due to " << who << "\n";
00051 #endif
00052 return false;
00053 }
00054
00055 void Lock::wait_lock(string w)
00056 {
00057 assert(!deleted);
00058 time_t start_time=time(NULL);
00059 while (!try_lock(w))
00060 if ((time(NULL)-start_time)>60)
00061 {
00062 cerr << "Cannot obtain lock for " << w << "\n"
00063 << "because " << who << " holds it excessively long\n";
00064 cerr.flush();
00065 assert(0);
00066 _exit(24);
00067 }
00068 }
00069
00070 void Lock::unlock()
00071 {
00072 assert(!deleted);
00073 assert(locked);
00074 who="";
00075 locked=NULL;
00076 }
00077
00078 #endif // __loaded__lock_cpp__