Commit 471ae88b authored by Joanne Hugé's avatar Joanne Hugé

Fix syntax error and improve comments

parent 51e3fe90
...@@ -36,56 +36,17 @@ typedef struct main_param { ...@@ -36,56 +36,17 @@ typedef struct main_param {
int refresh_rate; int refresh_rate;
} main_param_t; } main_param_t;
static inline int64_t diff_ns(struct timespec t1, struct timespec t2) { static inline int64_t diff_ns(struct timespec t1, struct timespec t2);
int64_t diff; static inline int64_t max(int64_t a, int64_t b);
static inline int64_t min(int64_t a, int64_t b);
diff = NSECS_PER_SECOND * (t1.tv_sec - t2.tv_sec); static inline struct timespec add_ns(struct timespec t, int64_t ns);
diff += ((int64_t)t1.tv_nsec) - ((int64_t)t2.tv_nsec); static inline struct timespec sub_ns(struct timespec t, int64_t ns);
if( diff < 0 )
diff = -diff;
return diff;
}
static inline int64_t max(int64_t a, int64_t b) {
return a > b ? a : b;
}
static inline int64_t min(int64_t a, int64_t b) {
return a < b ? a : b;
}
static inline struct timespec add_ns(struct timespec t, int64_t ns) {
struct timespec ret;
ret.tv_nsec = t.tv_nsec + ns;
ret.tv_sec = t.tv_sec;
if(ret.tv_nsec >= NSECS_PER_SECOND) {
ret.tv_sec++;
ret.tv_nsec -= NSECS_PER_SECOND;
}
return ret;
}
static inline struct timespec sub_ns(struct timespec t, int64_t ns) {
struct timespec ret;
ret.tv_nsec = t.tv_nsec - ns;
ret.tv_sec = t.tv_sec;
if(ret.tv_nsec < 0) {
ret.tv_sec--;
ret.tv_nsec += NSECS_PER_SECOND;
}
return ret;
}
static void sleep_poll(struct timespec * next, struct timespec * current) { static void sleep_poll(struct timespec * next, struct timespec * current) {
struct timespec pre_next; struct timespec pre_next;
pre_next = sub_ns(next, 3000000); pre_next = sub_ns(next, 3000000);
clock_nanosleep(CLOCK_ID, TIMER_ABSTIME, clock_nanosleep(CLOCK_ID, TIMER_ABSTIME, pre_next);
for(;;) { for(;;) {
clock_gettime(CLOCK_ID, current); clock_gettime(CLOCK_ID, current);
if( current->tv_sec > next->tv_sec || (current->tv_sec == next->tv_sec && current->tv_nsec >= next->tv_nsec) ) if( current->tv_sec > next->tv_sec || (current->tv_sec == next->tv_sec && current->tv_nsec >= next->tv_nsec) )
...@@ -99,7 +60,6 @@ static void sleep_nanosleep(struct timespec * next, struct timespec * current) { ...@@ -99,7 +60,6 @@ static void sleep_nanosleep(struct timespec * next, struct timespec * current) {
} }
// Real-time thread // Real-time thread
// Sends packets at a regular intervall
static void *timerthread(void *p) { static void *timerthread(void *p) {
struct timespec next; struct timespec next;
...@@ -240,3 +200,48 @@ int main(int argc, char *argv[]) { ...@@ -240,3 +200,48 @@ int main(int argc, char *argv[]) {
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
static inline int64_t diff_ns(struct timespec t1, struct timespec t2) {
int64_t diff;
diff = NSECS_PER_SECOND * (t1.tv_sec - t2.tv_sec);
diff += ((int64_t)t1.tv_nsec) - ((int64_t)t2.tv_nsec);
if( diff < 0 )
diff = -diff;
return diff;
}
static inline int64_t max(int64_t a, int64_t b) {
return a > b ? a : b;
}
static inline int64_t min(int64_t a, int64_t b) {
return a < b ? a : b;
}
static inline struct timespec add_ns(struct timespec t, int64_t ns) {
struct timespec ret;
ret.tv_nsec = t.tv_nsec + ns;
ret.tv_sec = t.tv_sec;
if(ret.tv_nsec >= NSECS_PER_SECOND) {
ret.tv_sec++;
ret.tv_nsec -= NSECS_PER_SECOND;
}
return ret;
}
static inline struct timespec sub_ns(struct timespec t, int64_t ns) {
struct timespec ret;
ret.tv_nsec = t.tv_nsec - ns;
ret.tv_sec = t.tv_sec;
if(ret.tv_nsec < 0) {
ret.tv_sec--;
ret.tv_nsec += NSECS_PER_SECOND;
}
return ret;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment