split Acquire function of Semaphore

This commit is contained in:
2019-12-24 16:49:30 +08:00
parent ddf56c7016
commit e12b8a9e5c
3 changed files with 37 additions and 20 deletions

View File

@@ -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);
dispatch_time_t when=dispatch_time(DISPATCH_TIME_NOW,t*HGL_NANO_SEC_PER_SEC);
return !dispatch_semaphore_wait(ptr,when);
}
return !dispatch_semaphore_wait(ptr,when);
}
}//namespace hgl

View File

@@ -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;
struct timespec abstime;
GetWaitTime(abstime,t);
GetWaitTime(abstime,t);
return !sem_timedwait(ptr,&abstime);
}
return !sem_timedwait(ptr,&abstime);
}
}//namespace hgl

View File

@@ -41,6 +41,15 @@ namespace hgl
{
return(WaitForSingleObject(ptr,0)==WAIT_OBJECT_0);
}
/**
* 等待并获取一个信号
* @return 是否等待到了,如果超过最长时间,仍未等到即为超时,返回false
*/
bool Semaphore::Acquire()
{
return(WaitForSingleObject(ptr,INFINITE)==WAIT_OBJECT_0);
}
/**
* 等待并获取一个信号