11 #ifndef UT_LOCKUTILS_H
12 #define UT_LOCKUTILS_H
72 : myMutex(scope.myMutex)
73 , myIsLocked(scope.myIsLocked)
75 scope.myMutex =
nullptr;
76 scope.myIsLocked =
false;
89 if (myIsLocked || !myMutex)
112 if (myIsLocked || !myMutex)
117 myIsLocked = myMutex->tryLock();
128 myIsLocked = myMutex->tryLock();
140 if (!mutex.safeLock())
150 if (!myIsLocked || !myMutex)
152 UT_ASSERT(!
"Can't unlock without locking first");
175 bool lock(
int) {
return true; }
185 template <
class Lock>
190 : myLock(lockstate, threadlock),
195 fprintf(stderr,
"Lock[%s]: %d collisions\n",
196 myName, myLock.getCollisions());
201 bool lock(
int ms) {
return myLock.lock(ms); }
219 template <
class Lock>
225 myObjs.setCapacity(capacity);
236 ut_ObjLockEntry *olock;
242 ident = getIdentifier(obj);
243 olock = myObjs[ident];
245 olock->myLock.lock();
253 ut_ObjLockEntry *olock;
257 ident = getIdentifier(obj);
258 olock = myObjs[ident];
260 if (olock->myLock.tryLock())
265 releaseIdentifier(obj, ident);
273 myObjs[ident]->myLock.unlock();
274 releaseIdentifier(obj, ident);
280 for (
exint i = 0; i < myObjs.entries(); i++)
284 int getIdentifier(
const void *obj)
288 for (
exint i = 0; i < myObjs.entries(); i++)
290 if (myObjs[i]->myObj == obj)
295 if (!myObjs[i]->myRefCount)
300 myObjs.append(
new ut_ObjLockEntry());
301 ident = myObjs.entries()-1;
305 myObjs[ident]->myObj = obj;
306 myObjs[ident]->myRefCount++;
309 void releaseIdentifier(
const void *obj,
int ident)
312 UT_ASSERT(myObjs[ident]->myRefCount > 0);
313 myObjs[ident]->myRefCount--;
314 if (!myObjs(ident)->myRefCount)
315 myObjs(ident)->myObj =
nullptr;
318 class ut_ObjLockEntry
337 template <
class Lock>
344 { myId = lock.
lock(myObj); }
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
UT_UniqueLock(UT_EmptyScope)
Construct without mutex. Use acquire() to give it a mutex.
UT_UniqueLock(Lock &mutex, bool acquire=true)
Construct with given mutex. Acquires it by default.
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
bool safeLock(Lock &mutex)
UT_DebugLockType(const char *name, int lockstate=0, bool threadlock=false)
int tryLock(const void *obj)
bool tryLock(Lock &mutex)
UT_UniqueLock(UT_UniqueLock< Lock > &&scope)
bool tryLock()
Try to lock the mutex, return immediately false if someone else owns it.
UT_ObjLockType(int capacity=256)
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
#define SYS_NO_DISCARD_RESULT
GLuint const GLchar * name
auto fprintf(std::FILE *f, const S &fmt, const T &...args) -> int
UT_Array< ut_ObjLockEntry * > myObjs
int lock(const void *obj)
UT_AutoObjLockType(UT_ObjLockType< Lock > &lock, const void *obj)
void unlock()
Unlock the mutex before this object is deleted.
void unlock(const void *obj, int ident)