00001
00002
00003
00004
00005
00006 #include "ParFUM.h"
00007 #include "ParFUM_internals.h"
00008
00009
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
00022 }
00023
00024 void FEM_lockN::pup(PUP::er &p) {
00025 p|owner;
00026 p|pending;
00027 p|idx;
00028 p|noreadLocks;
00029 p|nowriteLocks;
00030 }
00031
00032 void FEM_lockN::setMeshModify(femMeshModify *mod) {
00033 theMod = mod;
00034 }
00035
00036
00037
00038 void FEM_lockN::reset(int i,femMeshModify *mod) {
00039
00040 if(haslocks()) wunlock(idx);
00041 owner = -1;
00042 pending = -1;
00043 theMod = mod;
00044 idx = i;
00045 noreadLocks = 0;
00046 nowriteLocks = 0;
00047 }
00048
00049
00050
00051 int FEM_lockN::rlock() {
00052 if(nowriteLocks>0) {
00053 return -1;
00054 }
00055 else {
00056 #ifdef DEBUG_LOCKS
00057 CkPrintf("Got read lock on node %d\n", FEM_My_partition(), idx);
00058 #endif
00059 noreadLocks++;
00060 return 1;
00061 }
00062 return -1;
00063 }
00064
00065 int FEM_lockN::runlock() {
00066 CkAssert(noreadLocks>0 && nowriteLocks==0);
00067 if(noreadLocks > 0) {
00068 #ifdef DEBUG_LOCKS
00069 CkPrintf("[%d] Unlocked read lock on node %d\n", FEM_My_partition(), idx);
00070 #endif
00071 noreadLocks--;
00072 return 1;
00073 }
00074 else {
00075 return -1;
00076 }
00077 return -1;
00078 }
00079
00080 int FEM_lockN::wlock(int own) {
00081 if(nowriteLocks==0 && noreadLocks==0) {
00082 nowriteLocks++;
00083 owner = own;
00084 #ifdef DEBUG_LOCKS
00085 CkPrintf("[%d] Got write lock on node %d{%d} .\n",owner, idx, theMod->idx);
00086 #endif
00087 CkAssert(nowriteLocks==1);
00088 if(pending==own) pending=-1;
00089 return 1;
00090 } else {
00091 if((pending!=-1 && own<pending) || (pending==-1 && own<owner)) {
00092 pending = own;
00093 return -1;
00094 }
00095 return -2;
00096 }
00097 return -2;
00098 }
00099
00100 int FEM_lockN::wunlock(int own) {
00101
00102
00103
00104
00105
00106 if(nowriteLocks>0) {
00107 nowriteLocks--;
00108 CkAssert(nowriteLocks==0);
00109 #ifdef DEBUG_LOCKS
00110 CkPrintf("[%d] Unlocked write lock on node %d{%d} .\n",owner, idx, theMod->idx);
00111 #endif
00112 owner = -1;
00113 return 1;
00114 } else {
00115 return -1;
00116 }
00117 return -1;
00118 }
00119
00120
00121
00122 bool FEM_lockN::haslocks() {
00123 if(noreadLocks>0 || nowriteLocks>0) {
00124 return true;
00125 }
00126 else return false;
00127 }
00128
00129 bool FEM_lockN::verifyLock(void) {
00130 const IDXL_Rec *irec = theMod->fmMesh->node.shared.getRec(idx);
00131 if(irec) {
00132 int minchunk = theMod->idx;
00133 for(int i=0; i<irec->getShared(); i++) {
00134 int pchk = irec->getChk(i);
00135 if(pchk<minchunk) minchunk=pchk;
00136 }
00137
00138 if(minchunk!=theMod->idx) return false;
00139 }
00140 if(nowriteLocks==1) return true;
00141 else return false;
00142 }
00143
00144 int FEM_lockN::lockOwner() {
00145 if(noreadLocks>0 || nowriteLocks>0) {
00146 return owner;
00147 }
00148 else return -1;
00149 }