-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuthread_ctx.c
62 lines (45 loc) · 1.35 KB
/
uthread_ctx.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
/*
* FILE: uthread_ctx_solaris.c
* AUTHOR: Peter Demoreuille
* DESCR: linux/solaris userland thread ctx swill
* DATE: Sat Sep 8 11:26:15 2001
*
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "uthread.h"
#include "uthread_ctx.h"
#include "uthread_sched.h"
static void thread_start(void (*func)(), long arg1, void *arg2);
/* -------------------- public code -- */
void uthread_makecontext(uthread_ctx_t *ctx, char *stack, int stacksz,
void (*func)(), long arg1, void *arg2) {
int rv;
assert(ctx != NULL);
assert(stack != NULL && stacksz > 0);
/* initialize the context */
rv = getcontext(ctx);
assert(rv != -1);
/* set the new stack */
ctx->uc_stack.ss_sp = (void*)stack;
ctx->uc_stack.ss_size = stacksz;
ctx->uc_stack.ss_flags = 0;
ctx->uc_link = NULL;
/* makecontext */
makecontext(ctx, (void(*)())thread_start, 3, func, arg1, arg2);
}
void uthread_setcontext(uthread_ctx_t *ctx) {
assert(ctx != NULL);
setcontext(ctx);
}
void uthread_swapcontext(uthread_ctx_t *oldctx, uthread_ctx_t *newctx) {
assert(oldctx != NULL && newctx != NULL);
swapcontext(oldctx, newctx);
}
/* ------------------- private code -- */
static void thread_start(void (*func)(), long arg1, void *arg2) {
assert(func != NULL);
(func)(arg1, arg2);
uthread_exit(0);
}