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