-
Notifications
You must be signed in to change notification settings - Fork 19
/
nuse-fiber.c
235 lines (195 loc) · 4.84 KB
/
nuse-fiber.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* userspace context primitive for NUSE
* Copyright (c) 2015 Hajime Tazaki
*
* Author: Hajime Tazaki <[email protected]>
*/
#include <stdio.h>
#define __USE_GNU 1
#include <pthread.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/socket.h>
#include <stdint.h>
#include <sys/prctl.h>
#include <stdlib.h>
#include <assert.h>
#include "nuse.h"
/* FIXME */
extern int (*host_pthread_create)(pthread_t *, const pthread_attr_t *,
void *(*)(void *), void *);
struct NuseFiber {
pthread_t pthread;
pthread_mutex_t mutex;
pthread_cond_t condvar;
void * (*func)(void *);
void *context;
const char *name;
int canceled;
timer_t timerid;
};
#include <sched.h>
/* FIXME: more precise affinity shoudl be required. */
void
nuse_set_affinity(void)
{
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(0, &cpuset);
sched_setaffinity(getpid(), sizeof(cpu_set_t), &cpuset);
}
void *nuse_fiber_new_from_caller(uint32_t stackSize, const char *name)
{
struct NuseFiber *fiber = malloc(sizeof(struct NuseFiber));
fiber->func = NULL;
fiber->context = NULL;
fiber->pthread = pthread_self();
fiber->name = name;
fiber->canceled = 0;
fiber->timerid = NULL;
return fiber;
}
void *nuse_fiber_new(void * (*callback)(void *),
void *context,
uint32_t stackSize,
const char *name)
{
struct NuseFiber *fiber = nuse_fiber_new_from_caller(stackSize, name);
fiber->func = callback;
fiber->context = context;
return fiber;
}
void nuse_fiber_start(void *handler)
{
struct NuseFiber *fiber = handler;
int error;
error = pthread_mutex_init(&fiber->mutex, NULL);
assert(error == 0);
error = pthread_cond_init(&fiber->condvar, NULL);
assert(error == 0);
error = host_pthread_create(&fiber->pthread, NULL,
fiber->func, fiber->context);
assert(error == 0);
prctl(PR_SET_NAME, fiber->name, 0, 0, 0);
}
int
nuse_fiber_isself(void *handler)
{
struct NuseFiber *fiber = handler;
return fiber->pthread == pthread_self();
}
void
nuse_fiber_wait(void *handler)
{
struct NuseFiber *fiber = handler;
pthread_mutex_lock(&fiber->mutex);
pthread_cond_wait(&fiber->condvar, &fiber->mutex);
pthread_mutex_unlock(&fiber->mutex);
}
int
nuse_fiber_wakeup(void *handler)
{
struct NuseFiber *fiber = handler;
int ret;
pthread_mutex_lock(&fiber->mutex);
ret = pthread_cond_signal(&fiber->condvar);
pthread_mutex_unlock(&fiber->mutex);
return ret == 0 ? 1 : 0;
}
void nuse_fiber_stop(void *handler)
{
struct NuseFiber *fiber = handler;
fiber->canceled = 0;
if (fiber->timerid)
timer_delete(fiber->timerid);
fiber->timerid = NULL;
}
int nuse_fiber_is_stopped(void *handler)
{
struct NuseFiber *fiber = handler;
return fiber->canceled;
}
void nuse_fiber_free(void *handler)
{
struct NuseFiber *fiber = handler;
pthread_mutex_destroy(&fiber->mutex);
pthread_cond_destroy(&fiber->condvar);
/* pthread_join (fiber->pthread, 0); */
free(fiber);
}
/* hijacked functions */
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg)
{
int ret = host_pthread_create(thread, attr, start_routine, arg);
struct NuseFiber *fiber;
int error;
if (ret)
return ret;
fiber = malloc(sizeof(struct NuseFiber));
fiber->func = NULL;
fiber->context = NULL;
fiber->pthread = *thread;
fiber->name = "app_thread";
fiber->canceled = 0;
prctl(PR_SET_NAME, fiber->name, 0, 0, 0);
error = pthread_mutex_init(&fiber->mutex, NULL);
assert(error == 0);
error = pthread_cond_init(&fiber->condvar, NULL);
assert(error == 0);
nuse_task_add(fiber);
return ret;
}
struct NuseTimerTrampolineContext {
void *(*callback)(void *arg);
void *context;
timer_t timerid;
};
static void
nuse_timer_trampoline(sigval_t context)
{
struct NuseTimerTrampolineContext *ctx = context.sival_ptr;
ctx->callback(ctx->context);
if (ctx->timerid)
timer_delete(ctx->timerid);
ctx->timerid = NULL;
free(ctx);
}
void
nuse_add_timer(unsigned long ns,
void *(*func)(void *arg),
void *arg,
void *handler)
{
struct sigevent se;
struct itimerspec new_value;
struct NuseFiber *fiber = handler;
int ret;
new_value.it_value.tv_sec = ns / (1000 * 1000 * 1000);
new_value.it_value.tv_nsec = ns % (1000 * 1000 * 1000);
new_value.it_interval.tv_sec = 0;
new_value.it_interval.tv_nsec = 0;
struct NuseTimerTrampolineContext *ctx =
malloc(sizeof(struct NuseTimerTrampolineContext));
if (!ctx)
return;
ctx->callback = func;
ctx->context = arg;
memset(&se, 0, sizeof(se));
se.sigev_value.sival_ptr = ctx;
se.sigev_notify = SIGEV_THREAD;
se.sigev_notify_function = nuse_timer_trampoline;
se.sigev_notify_attributes = NULL;
ret = timer_create(CLOCK_REALTIME, &se, &ctx->timerid);
if (ret)
perror("timer_create");
ret = timer_settime(ctx->timerid, 0, &new_value, NULL);
if (ret)
perror("timer_settime");
fiber->timerid = ctx->timerid;
}