|
@@ -5,8 +5,10 @@
|
|
|
* NOTE: 主要用于状态通知
|
|
* NOTE: 主要用于状态通知
|
|
|
* HISTORY:
|
|
* HISTORY:
|
|
|
* 1, [2010-12-17] created by NiuJiuRu
|
|
* 1, [2010-12-17] created by NiuJiuRu
|
|
|
- * 2, [2025-11-24] 优化修改"sw_signal_wait()"函数, 使其不依赖系统时间,实现
|
|
|
|
|
- * 超时等待
|
|
|
|
|
|
|
+ * 2, [2025-11-24] 优化修改"sw_signal_wait()"函数, 避免依赖系统时间, 导致的
|
|
|
|
|
+ * 超时误差或跳变,具体修改如下:
|
|
|
|
|
+ * - 将 "gettimeofday()" 替换成 "clock_gettime()"
|
|
|
|
|
+ * - 将 "sem_timedwait()" 替换成 "sem_clockwait()"
|
|
|
***********************************************************************/
|
|
***********************************************************************/
|
|
|
#include "swapi.h"
|
|
#include "swapi.h"
|
|
|
#include "swmem.h"
|
|
#include "swmem.h"
|
|
@@ -50,10 +52,8 @@ wait_p1:
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- if(0)
|
|
|
|
|
- { // 依赖系统时间的超时等待, 内核唤醒
|
|
|
|
|
struct timespec ts, now;
|
|
struct timespec ts, now;
|
|
|
- clock_gettime(CLOCK_REALTIME, &now);
|
|
|
|
|
|
|
+ clock_gettime(CLOCK_MONOTONIC/*单调时钟, 不受系统时间影响*/, &now); // "CLOCK_REALTIME"依赖系统时间
|
|
|
ts.tv_sec = now.tv_sec + timeout/1000;
|
|
ts.tv_sec = now.tv_sec + timeout/1000;
|
|
|
ts.tv_nsec = now.tv_nsec + (timeout%1000)*1000*1000;
|
|
ts.tv_nsec = now.tv_nsec + (timeout%1000)*1000*1000;
|
|
|
if(ts.tv_nsec >= (1000*1000*1000))
|
|
if(ts.tv_nsec >= (1000*1000*1000))
|
|
@@ -62,22 +62,11 @@ wait_p1:
|
|
|
ts.tv_nsec %= (1000*1000*1000);
|
|
ts.tv_nsec %= (1000*1000*1000);
|
|
|
}
|
|
}
|
|
|
wait_p2:
|
|
wait_p2:
|
|
|
- ret = sem_timedwait(sem, &ts);
|
|
|
|
|
|
|
+ ret = sem_clockwait(sem, CLOCK_MONOTONIC, &ts); // 非POSIX标准, GNU扩展, 需定义:"_GNU_SOURCE"支持
|
|
|
|
|
+ // , 同时glic库版本需大于等于2.28
|
|
|
if(ret < 0 && errno == EINTR) goto wait_p2;
|
|
if(ret < 0 && errno == EINTR) goto wait_p2;
|
|
|
else return ret;
|
|
else return ret;
|
|
|
}
|
|
}
|
|
|
- else
|
|
|
|
|
- { // 无视系统时间的超时等待, 轮询检查
|
|
|
|
|
- struct timespec ts = {0, 1000*1000}; // 1ms
|
|
|
|
|
- while((ret = sem_trywait(sem)) < 0 && errno == EAGAIN)
|
|
|
|
|
- {
|
|
|
|
|
- if(timeout <= 0) return -1;
|
|
|
|
|
- nanosleep(&ts, NULL);
|
|
|
|
|
- timeout -= 1;
|
|
|
|
|
- }
|
|
|
|
|
- return ret;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* 点亮信号量 */
|
|
/* 点亮信号量 */
|