summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorluwenpeng <[email protected]>2023-10-09 17:30:00 +0800
committerluwenpeng <[email protected]>2023-10-11 15:20:43 +0800
commitb6d80d130c3d63e4c6add220811883f8fd381265 (patch)
tree74b6008c7a5309d03930a4df16b7de4780b5aab4 /common
parentc1d60567156a27ebd29934026a7ae6fe2d2e2d53 (diff)
perf: 从时间缓存中获取时间戳
Diffstat (limited to 'common')
-rw-r--r--common/src/timestamp.cpp20
1 files changed, 5 insertions, 15 deletions
diff --git a/common/src/timestamp.cpp b/common/src/timestamp.cpp
index a553322..e9b9a55 100644
--- a/common/src/timestamp.cpp
+++ b/common/src/timestamp.cpp
@@ -10,7 +10,7 @@
struct timestamp
{
- struct timespec timestamp;
+ uint64_t timestamp_ms;
uint64_t update_interval_ms;
};
@@ -21,7 +21,6 @@ struct timestamp *timestamp_new(uint64_t update_interval_ms)
timestamp_update(ts);
LOG_DEBUG("%s: TIMESTAMP->update_interval_ms : %lu", LOG_TAG_TIMESTAMP, timestamp_update_interval_ms(ts));
- LOG_DEBUG("%s: TIMESTAMP->current_sec : %lu", LOG_TAG_TIMESTAMP, timestamp_get_sec(ts));
LOG_DEBUG("%s: TIMESTAMP->current_msec : %lu", LOG_TAG_TIMESTAMP, timestamp_get_msec(ts));
return ts;
@@ -40,8 +39,9 @@ void timestamp_update(struct timestamp *ts)
{
struct timespec temp;
clock_gettime(CLOCK_MONOTONIC, &temp);
- ATOMIC_SET(&(ts->timestamp.tv_sec), temp.tv_sec);
- ATOMIC_SET(&(ts->timestamp.tv_nsec), temp.tv_nsec);
+ uint64_t current_timestamp_ms = temp.tv_sec * 1000 + temp.tv_nsec / 1000000;
+
+ __atomic_store_n(&ts->timestamp_ms, current_timestamp_ms, __ATOMIC_RELEASE);
}
uint64_t timestamp_update_interval_ms(struct timestamp *ts)
@@ -49,17 +49,7 @@ uint64_t timestamp_update_interval_ms(struct timestamp *ts)
return ts->update_interval_ms;
}
-uint64_t timestamp_get_sec(struct timestamp *ts)
-{
- uint64_t sec = ATOMIC_READ(&(ts->timestamp.tv_sec));
-
- return sec;
-}
-
uint64_t timestamp_get_msec(struct timestamp *ts)
{
- uint64_t sec = ATOMIC_READ(&(ts->timestamp.tv_sec));
- uint64_t nsec = ATOMIC_READ(&(ts->timestamp.tv_nsec));
-
- return sec * 1000 + nsec / 1000000;
+ return __atomic_load_n(&ts->timestamp_ms, __ATOMIC_ACQUIRE);
}