This repository has been archived by the owner on Jun 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmisc.c
130 lines (107 loc) · 2.89 KB
/
misc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// $Id: misc.c,v 1.27 2018/08/04 22:18:49 karn Exp $
// Miscellaneous low-level routines, mostly time-related
// Copyright 2018, Phil Karn, KA9Q
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#ifndef NULL
#define NULL ((void *)0)
#endif
#include "misc.h"
// Fill buffer from pipe
// Needed because reads from a pipe can be partial
int pipefill(const int fd,void *buffer,const int cnt){
int i;
unsigned char *bp = buffer;
for(i=0;i<cnt;){
int n = read(fd,bp+i,cnt-i);
if(n < 0)
return n;
if(n == 0)
break;
i += n;
}
return i;
}
// Remove return or newline, if any, from end of string
void chomp(char *s){
if(s == NULL)
return;
char *cp;
if((cp = strchr(s,'\r')) != NULL)
*cp = '\0';
if((cp = strchr(s,'\n')) != NULL)
*cp = '\0';
}
char *Days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
char *Months[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
// Format a timestamp expressed as nanoseconds from the GPS epoch
char *lltime(long long t){
struct tm tm;
time_t utime;
int t_usec;
static char result[100];
utime = (t / 1000000000LL) - GPS_UTC_OFFSET + UNIX_EPOCH;
t_usec = (t % 1000000000LL) / 1000;
if(t_usec < 0){
t_usec += 1000000;
utime -= 1;
}
gmtime_r(&utime,&tm);
// Mon Feb 26 14:40:08.123456 UTC 2018
snprintf(result,sizeof(result),"%s %s %d %02d:%02d:%02d.%06d UTC %4d",
Days[tm.tm_wday],Months[tm.tm_mon],tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec,t_usec,tm.tm_year+1900);
return result;
}
#if __APPLE__
// OSX doesn't have pthread_barrier_*
// Taken from https://stackoverflow.com/questions/3640853/performance-test-sem-t-v-s-dispatch-semaphore-t-and-pthread-once-t-v-s-dispat
// Apparently by David Cairns
#include <errno.h>
int pthread_barrier_init(pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned int count)
{
if(count == 0)
{
errno = EINVAL;
return -1;
}
if(pthread_mutex_init(&barrier->mutex, 0) < 0)
{
return -1;
}
if(pthread_cond_init(&barrier->cond, 0) < 0)
{
pthread_mutex_destroy(&barrier->mutex);
return -1;
}
barrier->tripCount = count;
barrier->count = 0;
return 0;
}
int pthread_barrier_destroy(pthread_barrier_t *barrier)
{
pthread_cond_destroy(&barrier->cond);
pthread_mutex_destroy(&barrier->mutex);
return 0;
}
int pthread_barrier_wait(pthread_barrier_t *barrier)
{
pthread_mutex_lock(&barrier->mutex);
++(barrier->count);
if(barrier->count >= barrier->tripCount)
{
barrier->count = 0;
pthread_cond_broadcast(&barrier->cond);
pthread_mutex_unlock(&barrier->mutex);
return 1;
}
else
{
pthread_cond_wait(&barrier->cond, &(barrier->mutex));
pthread_mutex_unlock(&barrier->mutex);
return 0;
}
}
#endif // __APPLE__