00001
00002
00003 #include "debug.h"
00004 #include "locks.h"
00005 #include "filesystem.h"
00006 #include "utils.h"
00007
00008 int lockMutex(mutex_type **mutex) {
00009 char sem_name[1024];
00010 getResourceName(sem_name, SEM_NAME);
00011 #ifdef USE_BOOST
00012 *mutex = new boost::interprocess::named_mutex(boost::interprocess::open_or_create, sem_name);
00013 (*mutex)->lock();
00014 return 0;
00015 #else
00016 *mutex = sem_open(sem_name,O_CREAT,0644,1);
00017 if(*mutex == SEM_FAILED)
00018 {
00019 perror("unable to create semaphore");
00020 sem_unlink(SEM_NAME);
00021 return -1;
00022 } else {
00023 int v;
00024 sem_getvalue(*mutex, &v);
00025
00026 sem_wait(*mutex);
00027 sem_getvalue(*mutex, &v);
00028
00029 sem_close(*mutex);
00030 return 0;
00031 }
00032 #endif
00033 }
00034 void unlockMutex(mutex_type **mutex) {
00035 #ifdef USE_BOOST
00036 (*mutex)->unlock();
00037 delete *mutex;
00038 #else
00039 char sem_name[1024];
00040 getResourceName(sem_name, SEM_NAME);
00041 int v;
00042 *mutex = sem_open(sem_name,O_CREAT,0644,1);
00043 sem_getvalue(*mutex, &v);
00044
00045 sem_post(*mutex);
00046 sem_getvalue(*mutex, &v);
00047
00048 sem_close(*mutex);
00049 #endif
00050 }
00051
00052
00053 void resetMutex(mutex_type **mutex) {
00054 char sem_name[1024];
00055 getResourceName(sem_name, SEM_NAME);
00056 #ifdef USE_BOOST
00057 try {
00058 mutex_type *mutex0 = new boost::interprocess::named_mutex(boost::interprocess::open_only, sem_name);
00059 mutex0->unlock();
00060 delete mutex0;
00061 boost::interprocess::named_mutex::remove(sem_name);
00062 } catch (boost::interprocess::interprocess_exception e) {
00063
00064 }
00065 #else
00066 sem_unlink(sem_name);
00067 #endif
00068 }
00069
00070