split Acquire function of Semaphore
This commit is contained in:
@@ -3,8 +3,6 @@
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
void GetWaitTime(struct timespec &,double);
|
||||
|
||||
/**
|
||||
* @param max_count 最大计数
|
||||
*/
|
||||
@@ -50,7 +48,18 @@ namespace hgl
|
||||
{
|
||||
if(!ptr)return(false);
|
||||
|
||||
return !dispatch_semaphore_wait(ptr, DISPATCH_TIME_NOW);
|
||||
return !dispatch_semaphore_wait(ptr,DISPATCH_TIME_NOW);
|
||||
}
|
||||
|
||||
/**
|
||||
* 等待并获取一个信号
|
||||
* @return 是否等待到了,如果超过最长时间,仍未等到即为超时,返回false
|
||||
*/
|
||||
bool Semaphore::Acquire()
|
||||
{
|
||||
if(!ptr)return(false);
|
||||
|
||||
return !dispatch_semaphore_wait(ptr,DISPATCH_TIME_FOREVER);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,15 +71,8 @@ namespace hgl
|
||||
{
|
||||
if(!ptr)return(false);
|
||||
|
||||
if(t<=0)
|
||||
{
|
||||
return !dispatch_semaphore_wait(ptr, DISPATCH_TIME_FOREVER);
|
||||
}
|
||||
else
|
||||
{
|
||||
dispatch_time_t when=dispatch_time(DISPATCH_TIME_NOW,t*HGL_NANO_SEC_PER_SEC);
|
||||
|
||||
return !dispatch_semaphore_wait(ptr,when);
|
||||
}
|
||||
}
|
||||
}//namespace hgl
|
||||
|
@@ -64,6 +64,17 @@ namespace hgl
|
||||
return !sem_trywait(ptr);
|
||||
}
|
||||
|
||||
/**
|
||||
* 等待并获取一个信号
|
||||
* @return 是否等待到了,如果超过最长时间,仍未等到即为超时,返回false
|
||||
*/
|
||||
bool Semaphore::Acquire()
|
||||
{
|
||||
if(!ptr)return(false);
|
||||
|
||||
return !sem_wait(ptr);
|
||||
}
|
||||
|
||||
/**
|
||||
* 等待并获取一个信号
|
||||
* @param t 等待的最长时间,使用0表示无限等待.(单位秒)
|
||||
@@ -73,15 +84,10 @@ namespace hgl
|
||||
{
|
||||
if(!ptr)return(false);
|
||||
|
||||
if(t<=0)
|
||||
return !sem_wait(ptr);
|
||||
else
|
||||
{
|
||||
struct timespec abstime;
|
||||
|
||||
GetWaitTime(abstime,t);
|
||||
|
||||
return !sem_timedwait(ptr,&abstime);
|
||||
}
|
||||
}
|
||||
}//namespace hgl
|
||||
|
@@ -42,6 +42,15 @@ namespace hgl
|
||||
return(WaitForSingleObject(ptr,0)==WAIT_OBJECT_0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 等待并获取一个信号
|
||||
* @return 是否等待到了,如果超过最长时间,仍未等到即为超时,返回false
|
||||
*/
|
||||
bool Semaphore::Acquire()
|
||||
{
|
||||
return(WaitForSingleObject(ptr,INFINITE)==WAIT_OBJECT_0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 等待并获取一个信号
|
||||
* @param time 等待的最长时间,使用0表示无限等待.(单位秒)
|
||||
|
Reference in New Issue
Block a user