Fix compile error and bugs

master
Kirigaya Kazuto 2017-12-27 20:20:14 +08:00
parent 89e50ae269
commit cdfb210052
1 changed files with 15 additions and 12 deletions

View File

@ -3,6 +3,7 @@
#include "SemaphoreWrapper.h" #include "SemaphoreWrapper.h"
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/file.h>
#include <fcntl.h> #include <fcntl.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
@ -94,14 +95,10 @@ ProcessSemaphore::ProcessSemaphore(int key, int action, int default_value) : _p(
if(_p->owner) if(_p->owner)
{ {
/// Unlink it (delete on close) : Only owner will delete this file.
unlink(_p->buff);
flock(_p->fd,LOCK_EX); flock(_p->fd,LOCK_EX);
LockInfo x; impl::LockInfo x;
x.total_value=default_value; x.total_value=default_value;
x.current_value=default_value; x.current_value=default_value;
x.num_waiting=0;
x.writeTo(_p->fd); x.writeTo(_p->fd);
flock(_p->fd,LOCK_UN); flock(_p->fd,LOCK_UN);
} }
@ -109,7 +106,7 @@ ProcessSemaphore::ProcessSemaphore(int key, int action, int default_value) : _p(
if(_p->status!=0&&_p->status!=2) /// Clean up if(_p->status!=0&&_p->status!=2) /// Clean up
{ {
close(_p->buff); close(_p->fd);
_p->status=0; _p->status=0;
} }
} }
@ -121,8 +118,14 @@ ProcessSemaphore::~ProcessSemaphore()
{ {
if(_p->status==2) if(_p->status==2)
{ {
if(_p->owner)
{
/// Unlink it (delete on close) : Only owner will delete this file.
unlink(_p->buff);
}
/// Close on delete. /// Close on delete.
close(_p->buff); close(_p->fd);
} }
delete _p; delete _p;
@ -137,7 +140,7 @@ bool ProcessSemaphore::isReady() const
int ProcessSemaphore::p() int ProcessSemaphore::p()
{ {
flock(_p->fd,LOCK_EX); flock(_p->fd,LOCK_EX);
LockInfo x=LockInfo::getFrom(_p->fd); impl::LockInfo x=impl::LockInfo::getFrom(_p->fd);
if(x.current_value<=0) if(x.current_value<=0)
{ {
int thisPid=getpid(); int thisPid=getpid();
@ -167,7 +170,7 @@ int ProcessSemaphore::p()
{ {
if(x.waiting[i]==thisPid) if(x.waiting[i]==thisPid)
{ {
x.waiting.erase(x.begin()+i); x.waiting.erase(x.waiting.begin()+i);
break; break;
} }
} }
@ -201,11 +204,11 @@ int ProcessSemaphore::p()
int ProcessSemaphore::v() int ProcessSemaphore::v()
{ {
flock(fd,LOCK_EX); flock(_p->fd,LOCK_EX);
LockInfo x=LockInfo::getFrom(_p->fd); impl::LockInfo x=impl::LockInfo::getFrom(_p->fd);
x.current_value=x.current_value+1; x.current_value=x.current_value+1;
x.writeTo(_p->fd); x.writeTo(_p->fd);
flock(fd,LOCK_UN); flock(_p->fd,LOCK_UN);
return 0; return 0;
} }