diff --git a/src/Apple/Semaphore.cpp b/src/Apple/Semaphore.cpp index d4301aa..2161e6d 100644 --- a/src/Apple/Semaphore.cpp +++ b/src/Apple/Semaphore.cpp @@ -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 diff --git a/src/UNIX/Semaphore.cpp b/src/UNIX/Semaphore.cpp index 399acd7..424bfbb 100644 --- a/src/UNIX/Semaphore.cpp +++ b/src/UNIX/Semaphore.cpp @@ -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 diff --git a/src/Win/Semaphore.cpp b/src/Win/Semaphore.cpp index e0d33ca..876571d 100644 --- a/src/Win/Semaphore.cpp +++ b/src/Win/Semaphore.cpp @@ -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); + } /** * 等待并获取一个信号