00001 /* File: fem_lock_node.C 00002 * Authors: Nilesh Choudhury 00003 * 00004 */ 00005 00006 #include "fem_lock_node.h" 00007 #include "fem_mesh_modify.h" 00008 00009 //#define DEBUG_LOCKS 00010 00011 FEM_lockN::FEM_lockN(int i,femMeshModify *mod) { 00012 owner = -1; 00013 pending = -1; 00014 theMod = mod; 00015 idx = i; 00016 noreadLocks = 0; 00017 nowriteLocks = 0; 00018 } 00019 00020 FEM_lockN::~FEM_lockN() { 00021 //before deleting it, ensure that it is not holding any locks 00022 } 00023 00024 void FEM_lockN::reset(int i,femMeshModify *mod) { 00025 //CkAssert(noreadLocks==0 && nowriteLocks==0); 00026 if(haslocks()) wunlock(idx); 00027 owner = -1; 00028 pending = -1; 00029 theMod = mod; 00030 idx = i; 00031 noreadLocks = 0; 00032 nowriteLocks = 0; 00033 } 00034 00035 int FEM_lockN::rlock() { 00036 if(nowriteLocks>0) { //if someone has a write lock, do not give read locks 00037 return -1; 00038 } 00039 else { 00040 //CkPrintf("Got read lock on node %d\n", idx); 00041 noreadLocks++; 00042 return 1; 00043 } 00044 return -1; //should not reach here 00045 } 00046 00047 int FEM_lockN::runlock() { 00048 CkAssert(noreadLocks>0 && nowriteLocks==0); 00049 if(noreadLocks > 0) { 00050 //CkPrintf("Unlocked read lock on node %d\n", idx); 00051 noreadLocks--; 00052 return 1; 00053 } 00054 else { 00055 return -1; 00056 } 00057 return -1; //should not reach here 00058 } 00059 00060 int FEM_lockN::wlock(int own) { 00061 if(nowriteLocks==0 && noreadLocks==0) { 00062 nowriteLocks++; 00063 owner = own; 00064 #ifdef DEBUG_LOCKS 00065 CkPrintf("[%d] Got write lock on node %d{%d} .\n",owner, idx, theMod->idx); 00066 #endif 00067 return 1; 00068 } else { 00069 /*if(pending==-1 || own<=pending) { 00070 pending = own; 00071 return -2; //keep trying 00072 }*/ 00073 return -1; //give up trying for a while 00074 } 00075 return -1; 00076 } 00077 00078 bool FEM_lockN::verifyLock(void) { 00079 const IDXL_Rec *irec = theMod->fmMesh->node.shared.getRec(idx); 00080 if(irec) { 00081 int minchunk = theMod->idx; 00082 for(int i=0; i<irec->getShared(); i++) { 00083 int pchk = irec->getChk(i); 00084 if(pchk<minchunk) minchunk=pchk; 00085 } 00086 //if someone wants to lock me, I should be on the smallest chunk 00087 if(minchunk!=theMod->idx) return false; 00088 } 00089 if(nowriteLocks==1) return true; 00090 else return false; 00091 } 00092 00093 int FEM_lockN::wunlock(int own) { 00094 /*if(!(noreadLocks==0 && nowriteLocks>0)) { 00095 CkPrintf("[%d] Error:: unlocking unacquired write lock %d{%d} .\n",owner, idx, theMod->idx); 00096 nowriteLocks=1; 00097 }*/ 00098 //CkAssert(noreadLocks==0 && nowriteLocks>0); 00099 if(nowriteLocks) { 00100 nowriteLocks--; 00101 #ifdef DEBUG_LOCKS 00102 CkPrintf("[%d] Unlocked write lock on node %d{%d} .\n",owner, idx, theMod->idx); 00103 #endif 00104 owner = -1; 00105 return 1; 00106 } else { 00107 return -1; 00108 } 00109 return -1; 00110 } 00111 00112 bool FEM_lockN::haslocks() { 00113 if(noreadLocks>0 || nowriteLocks>0) { 00114 return true; 00115 } 00116 else return false; 00117 } 00118 00119 int FEM_lockN::lockOwner() { 00120 if(noreadLocks>0 || nowriteLocks>0) { 00121 return owner; 00122 } 00123 else return -1; 00124 }