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
|
/*
* GTK VNC Widget
*
* Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include <glib.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include <sys/mman.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "coroutine.h"
#ifndef MAP_ANONYMOUS
# define MAP_ANONYMOUS MAP_ANON
#endif
int coroutine_release(struct coroutine *co)
{
return cc_release(&co->cc);
}
static int _coroutine_release(struct continuation *cc)
{
struct coroutine *co = container_of(cc, struct coroutine, cc);
if (co->release) {
int ret = co->release(co);
if (ret < 0)
return ret;
}
munmap(co->cc.stack, co->cc.stack_size);
co->caller = NULL;
return 0;
}
static void coroutine_trampoline(struct continuation *cc)
{
struct coroutine *co = container_of(cc, struct coroutine, cc);
co->data = co->entry(co->data);
}
void coroutine_init(struct coroutine *co)
{
if (co->stack_size == 0)
co->stack_size = 16 << 20;
co->cc.stack_size = co->stack_size;
co->cc.stack = mmap(0, co->stack_size,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS,
-1, 0);
if (co->cc.stack == MAP_FAILED)
g_error("mmap(%" G_GSIZE_FORMAT ") failed: %s",
co->stack_size, g_strerror(errno));
co->cc.entry = coroutine_trampoline;
co->cc.release = _coroutine_release;
co->exited = 0;
cc_init(&co->cc);
}
#if 0
static __thread struct coroutine leader;
static __thread struct coroutine *current;
#else
static struct coroutine leader;
static struct coroutine *current;
#endif
struct coroutine *coroutine_self(void)
{
if (current == NULL)
current = &leader;
return current;
}
void *coroutine_swap(struct coroutine *from, struct coroutine *to, void *arg)
{
int ret;
to->data = arg;
current = to;
ret = cc_swap(&from->cc, &to->cc);
if (ret == 0)
return from->data;
else if (ret == 1) {
coroutine_release(to);
current = from;
to->exited = 1;
return to->data;
}
return NULL;
}
void *coroutine_yieldto(struct coroutine *to, void *arg)
{
g_return_val_if_fail(!to->caller, NULL);
g_return_val_if_fail(!to->exited, NULL);
to->caller = coroutine_self();
return coroutine_swap(coroutine_self(), to, arg);
}
void *coroutine_yield(void *arg)
{
struct coroutine *to = coroutine_self()->caller;
if (!to) {
fprintf(stderr, "Co-routine is yielding to no one\n");
abort();
}
coroutine_self()->caller = NULL;
return coroutine_swap(coroutine_self(), to, arg);
}
gboolean coroutine_is_main(struct coroutine *co)
{
return (co == &leader);
}
/*
* Local variables:
* c-indent-level: 8
* c-basic-offset: 8
* tab-width: 8
* End:
*/
|