00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00025 #include "Lock.h"
00026
00027 utils::Lock::Lock() {
00028 pthread_mutex_init(&mut, NULL);
00029 pthread_cond_init(&cond, NULL);
00030 };
00031
00032 utils::Lock::~Lock() {
00033 pthread_mutex_destroy(&mut);
00034 pthread_cond_destroy(&cond);
00035 };
00036
00037 inline void utils::Lock::alloc(const string &res) { resources.insert(res); };
00038 inline void utils::Lock::free(const string &res) { resources.erase(res); };
00039 inline bool utils::Lock::isfree(const string &res) { return resources.count(res)==0; };
00040
00041 void utils::Lock::lock(const string &resource) {
00042 pthread_mutex_lock(&mut);
00043 while(!isfree(resource))
00044 pthread_cond_wait(&cond, &mut);
00045 alloc(resource);
00046 pthread_mutex_unlock(&mut);
00047 };
00048
00049 void utils::Lock::unlock(const string &resource) {
00050 pthread_mutex_lock(&mut);
00051 free(resource);
00052 pthread_cond_broadcast(&cond);
00053 pthread_mutex_unlock(&mut);
00054 };
00055