Skip to content

Commit

Permalink
Merge pull request #52 from baresip/tmr_jiffies_usec
Browse files Browse the repository at this point in the history
tmr: add tmr_jiffies_usec() - get accurate microseconds
  • Loading branch information
sreimers authored Dec 8, 2020
2 parents c6ba3a9 + bbf7e55 commit 2bbcfb5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/re_tmr.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct tmr {


void tmr_poll(struct list *tmrl);
uint64_t tmr_jiffies_usec(void);
uint64_t tmr_jiffies(void);
uint64_t tmr_next_timeout(struct list *tmrl);
void tmr_debug(void);
Expand Down
39 changes: 39 additions & 0 deletions src/tmr/tmr.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*
* Copyright (C) 2010 Creytiv.com
*/
#define _DEFAULT_SOURCE 1

#include <string.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
Expand Down Expand Up @@ -112,6 +114,43 @@ void tmr_poll(struct list *tmrl)
}


/**
* Get the timer jiffies in microseconds
*
* @return Jiffies in [us]
*/
uint64_t tmr_jiffies_usec(void)
{
uint64_t jfs;

#if defined(WIN32)
LARGE_INTEGER li;
static LARGE_INTEGER freq;

if (!freq.QuadPart)
QueryPerformanceFrequency(&freq);

QueryPerformanceCounter(&li);
li.QuadPart *= 1000000;
li.QuadPart /= freq.QuadPart;

jfs = li.QuadPart;
#else
struct timespec now;

if (0 != clock_gettime(CLOCK_MONOTONIC_RAW, &now)) {
DEBUG_WARNING("jiffies: clock_gettime() failed (%m)\n", errno);
return 0;
}

jfs = (long)now.tv_sec * (uint64_t)1000000;
jfs += now.tv_nsec/1000;
#endif

return jfs;
}


/**
* Get the timer jiffies in milliseconds
*
Expand Down

0 comments on commit 2bbcfb5

Please sign in to comment.