IBR-DTNSuite  0.8
ibrcommon/ibrcommon/thread/ObjectLock.cpp
Go to the documentation of this file.
00001 /*
00002  * ObjectLock.cpp
00003  *
00004  *  Created on: 23.11.2010
00005  *      Author: morgenro
00006  */
00007 
00008 #include "ibrcommon/thread/ObjectLock.h"
00009 #include "ibrcommon/thread/MutexLock.h"
00010 
00011 namespace ibrcommon
00012 {
00013         ibrcommon::Conditional ObjectLock::__cond;
00014         std::set<void*> ObjectLock::__locks;
00015 
00016         ObjectLock::ObjectLock(void *obj)
00017          : _obj(obj)
00018         {
00019                 ibrcommon::MutexLock l(__cond);
00020 
00021                 // check for lock
00022                 while (__locks.find(obj) != __locks.end())
00023                 {
00024                         // wait while there is a lock
00025                         __cond.wait();
00026                 }
00027 
00028                 // add lock
00029                 __locks.insert(obj);
00030         }
00031 
00032         ObjectLock::~ObjectLock()
00033         {
00034                 ibrcommon::MutexLock l(__cond);
00035                 __locks.erase(_obj);
00036         }
00037 }