|
|
@@ -5,6 +5,8 @@
|
|
|
* NOTE: 主要用于状态通知
|
|
|
* HISTORY:
|
|
|
* 1, [2010-12-17] created by NiuJiuRu
|
|
|
+ * 2, [2025-11-24] 替换"gettimeofday()"为"clock_gettime()", 前者会依赖系统
|
|
|
+ * 时间, 后者依赖单调递增时钟, 避免了系统时间变化带来的影响
|
|
|
***********************************************************************/
|
|
|
#include "swapi.h"
|
|
|
#include "swmem.h"
|
|
|
@@ -37,8 +39,6 @@ void sw_signal_destroy(void *hSignal)
|
|
|
int sw_signal_wait(void *hSignal, int timeout)
|
|
|
{
|
|
|
sem_t *sem = (sem_t *)hSignal;
|
|
|
- struct timeval tv = { 0 };
|
|
|
- struct timespec ts = { 0 };
|
|
|
int ret;
|
|
|
|
|
|
if(timeout < 0)
|
|
|
@@ -50,11 +50,15 @@ wait_p1:
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- gettimeofday(&tv, NULL);
|
|
|
- ts.tv_sec = tv.tv_sec + timeout/1000;
|
|
|
- ts.tv_nsec = tv.tv_usec*1000 + (timeout%1000)*1000*1000;
|
|
|
- ts.tv_sec += ts.tv_nsec/(1000*1000*1000);
|
|
|
- ts.tv_nsec %= (1000*1000*1000);
|
|
|
+ struct timespec ts, now;
|
|
|
+ clock_gettime(CLOCK_MONOTONIC, &now);
|
|
|
+ ts.tv_sec = now.tv_sec + timeout/1000;
|
|
|
+ ts.tv_nsec = now.tv_nsec + (timeout%1000)*1000*1000;
|
|
|
+ if(ts.tv_nsec >= (1000*1000*1000))
|
|
|
+ {
|
|
|
+ ts.tv_sec += ts.tv_nsec/(1000*1000*1000);
|
|
|
+ ts.tv_nsec %= (1000*1000*1000);
|
|
|
+ }
|
|
|
wait_p2:
|
|
|
ret = sem_timedwait(sem, &ts);
|
|
|
if(ret < 0 && errno == EINTR) goto wait_p2;
|