📄 timer.c
字号:
#include <stdio.h>#include <stdlib.h>#include <stdint.h>#include <sys/time.h>#include <sys/types.h>#include <unistd.h>#include "timer.h"/* Initialize timer */TIMER *timer_init(){ TIMER *timer = (TIMER *)calloc(1, sizeof(TIMER)); if(timer) { gettimeofday(&(timer->tv), NULL); timer->start_sec = timer->tv.tv_sec; timer->start_usec = timer->tv.tv_sec * 1000000llu + timer->tv.tv_usec * 1llu; timer->last_sec = timer->start_sec; timer->last_usec = timer->start_usec; timer->sample = timer_sample; timer->reset = timer_reset; timer->check = timer_check; timer->clean = timer_clean; pthread_mutex_init(&(timer->mutex), NULL); } return timer;}/* Reset timer */void timer_reset(TIMER *timer){ if(timer) { pthread_mutex_lock(&(timer->mutex)); gettimeofday(&(timer->tv), NULL); timer->start_sec = timer->tv.tv_sec; timer->start_usec = timer->tv.tv_sec * 1000000llu + timer->tv.tv_usec * 1llu; timer->last_sec = timer->start_sec; timer->last_usec = timer->start_usec; pthread_mutex_unlock(&(timer->mutex)); }}/* TIMER sample */void timer_sample(TIMER *timer){ if(timer) { pthread_mutex_lock(&(timer->mutex)); gettimeofday(&(timer->tv), NULL); timer->last_sec_used = timer->tv.tv_sec - timer->last_sec; timer->last_usec_used = timer->tv.tv_sec * 1000000llu + timer->tv.tv_usec - timer->last_usec; timer->last_sec = timer->tv.tv_sec; timer->last_usec = timer->tv.tv_sec * 1000000llu + timer->tv.tv_usec; timer->sec_used = timer->tv.tv_sec - timer->start_sec; timer->usec_used = timer->last_usec - timer->start_usec; pthread_mutex_unlock(&(timer->mutex)); }}/* Check timer and Run callback */void timer_check(TIMER *timer, uint32_t interval){ uint64_t n = 0llu; if(timer) { pthread_mutex_lock(&(timer->mutex)); gettimeofday(&(timer->tv), NULL); n = (timer->tv.tv_sec * 1000000llu + timer->tv.tv_usec); if( (n - timer->last_usec) >= interval && timer->callback) { timer->callback(); timer->last_sec_used = timer->tv.tv_sec - timer->last_sec; timer->last_usec_used = n - timer->last_usec; timer->last_sec = timer->tv.tv_sec; timer->last_usec = n; timer->sec_used = timer->tv.tv_sec - timer->start_sec; timer->usec_used = timer->last_usec - timer->start_usec; } pthread_mutex_unlock(&(timer->mutex)); }}/* Clean Timer */void timer_clean(TIMER **timer){ if((*timer)) free((*timer)); (*timer) = NULL;}/* Heartbeat */void heartbeat(){ printf("Heartbeat Testing \n");}/* Timer testing */#ifdef _DEBUG_TIMERint main(){ int i = 10000; uint32_t interval = 10000000u; TIMER *timer = timer_init(); timer->callback = &heartbeat; while (i--) { timer->check(timer, interval); usleep(1000); } timer->clean(&timer);}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -