-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathos_unix.c
198 lines (173 loc) · 4.82 KB
/
os_unix.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// os_unix
// The platform specific code for UNIX-likes.
// Mostly POSIX, but might contain some extensions.
//
// Copyright (c) 2019, Adrian "vifino" Pistol <vifino@tty.sh>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#define _GNU_SOURCE
#include "../types.h"
#include "../oscore.h"
#include "../main.h"
#include "../timers.h"
#include <sys/time.h>
#include <unistd.h>
#include <assert.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <fcntl.h>
#if defined(__FreeBSD__) || defined(__OpenBSD__)
#include <pthread_np.h>
#endif
// Main method.
int main(int argc, char** argv) {
return sled_main(argc, argv);
}
// -- event
typedef struct {
int send;
int recv;
} oscore_event_i;
int oscore_event_wait_until(oscore_event ev, oscore_time desired_usec) {
oscore_time tnow = udate();
if (tnow >= desired_usec)
return tnow;
oscore_time sleeptime = desired_usec - tnow;
oscore_event_i * oei = (oscore_event_i *) ev;
struct timeval timeout;
timeout.tv_sec = sleeptime / 1000000;
timeout.tv_usec = sleeptime % 1000000;
fd_set set;
FD_ZERO(&set);
FD_SET(oei->recv, &set);
if (select(FD_SETSIZE, &set, NULL, NULL, &timeout)) {
char buf[512];
// Non-blocking (see creation). It DOESN'T MATTER if this errors!!!
read(oei->recv, buf, 512);
return 1; // we got an interrupt
}
return 0; // timeout
}
void oscore_event_signal(oscore_event ev) {
oscore_event_i * oei = (oscore_event_i *) ev;
char discard = 0;
ssize_t ret = write(oei->send, &discard, 1);
if(ret < 0) {
perror("oscor_event_signal write error");
exit(1);
}
}
oscore_event oscore_event_new(void) {
oscore_event_i * oei = malloc(sizeof(oscore_event_i));
int fd[2];
assert(oei);
int ret = pipe(fd);
if(ret < 0) {
perror("oscor_event_new pipe error");
exit(1);
}
oei->recv = fd[0];
// Make the receiver non-blocking to avoid accidentally causing lockups in wait_until
fcntl(fd[0], F_SETFL, fcntl(fd[0], F_GETFL) | O_NONBLOCK);
oei->send = fd[1];
return oei;
}
void oscore_event_free(oscore_event ev) {
oscore_event_i * oei = (oscore_event_i *) ev;
close(oei->send);
close(oei->recv);
free(oei);
}
// Time keeping.
oscore_time oscore_udate(void) {
struct timeval tv;
if (gettimeofday(&tv, NULL) == -1) {
printf("Failed to get the time???\n");
exit(1);
}
return T_SECOND * tv.tv_sec + tv.tv_usec;
}
// Threading
oscore_task oscore_task_create(const char* name, oscore_task_function func, void* ctx) {
pthread_t* thread = calloc(1, sizeof(pthread_t));
pthread_create(thread, NULL, func, ctx);
#if defined(__linux__) || defined(__NetBSD__)
pthread_setname_np(*thread, name);
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
pthread_set_name_np(*thread, name);
#endif
return thread;
}
void oscore_task_yield(void) {
#ifdef pthread_yield
pthread_yield();
#else
#include <sched.h>
sched_yield();
#endif
}
void oscore_task_exit(void * status) {
pthread_exit(status);
}
void * oscore_task_join(oscore_task task) {
void * retval = 0;
if (pthread_join(*((pthread_t *) task), (void*) &retval)) {
free(task);
return NULL;
}
free(task);
return retval;
}
void oscore_task_setprio(oscore_task task, int prio) {
struct sched_param param;
int policy;
pthread_getschedparam(pthread_self(), &policy, ¶m);
#if defined(__linux__)
if (prio == TPRIO_LOW)
policy = SCHED_BATCH;
#endif
param.sched_priority++; // decrease priority.
pthread_setschedparam(*(pthread_t*) task, policy, ¶m);
}
int oscore_ncpus(void) {
return sysconf(_SC_NPROCESSORS_ONLN);
}
void oscore_task_pin(oscore_task task, int cpu) {
#ifdef pthread_setaffinity_np
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(cpu, &cpuset);
pthread_setaffinity_np(*((pthread_t*) task), sizeof(cpu_set_t), &cpuset);
#endif
}
// -- mutex
oscore_mutex oscore_mutex_new(void) {
pthread_mutex_t * mutex = malloc(sizeof(pthread_mutex_t));
assert(mutex);
assert(!pthread_mutex_init(mutex, NULL));
return mutex;
}
void oscore_mutex_lock(oscore_mutex m) {
pthread_mutex_t * mutex = m;
pthread_mutex_lock(mutex);
}
void oscore_mutex_unlock(oscore_mutex m) {
pthread_mutex_t * mutex = m;
pthread_mutex_unlock(mutex);
}
void oscore_mutex_free(oscore_mutex m) {
pthread_mutex_t * mutex = m;
pthread_mutex_destroy(mutex);
free(mutex);
}