summaryrefslogtreecommitdiffstats
path: root/src/fiber.c
blob: b678842d4e9e035eb4f49302cacf6a8bb74636bc (plain)
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
/* fiber.c - fiber management and scheduling
 *
 * Copyright (C) 2009 Timo Teräs <timo.teras@iki.fi>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 or later as
 * published by the Free Software Foundation.
 *
 * See http://www.gnu.org/ for details.
 */

#include <time.h>
#include <errno.h>
#include <unistd.h>
#include <libtf/fiber.h>
#include <libtf/io.h>

struct tf_fiber {
	unsigned int		ref_count;
	int			wakeup_type;
	struct tf_list_node	queue_node;
	struct tf_heap_node	heap_node;
	char			data[TF_EMPTY_ARRAY];
};

#include TF_UCTX_H

/* FIXME: should be in thread local storage */
struct tf_scheduler *__tf_scheduler;

void *tf_fiber_create(tf_fiber_proc fiber_main, int private_size)
{
	struct tf_scheduler *sched = tf_get_scheduler();
	struct tf_fiber *fiber;

	if (tf_heap_prealloc(&sched->heap, sched->num_fibers + 1) < 0)
		return NULL;

	fiber = tf_uctx_create(fiber_main, private_size);
	if (fiber == NULL)
		return NULL;

	/* The initial references for caller and scheduler */
	*fiber = (struct tf_fiber) {
		.ref_count = 2,
		.queue_node = TF_LIST_INITIALIZER(fiber->queue_node),
	};

	tf_list_add_tail(&fiber->queue_node, &sched->run_q);
	sched->num_fibers++;

	return fiber->data;
}

static void __tf_fiber_destroy(struct tf_fiber *fiber)
{
	tf_heap_delete(&fiber->heap_node, &tf_get_scheduler()->heap);
	tf_uctx_destroy(fiber);
}

void *tf_fiber_get(void *data)
{
	struct tf_fiber *fiber = container_of(data, struct tf_fiber, data);
	tf_atomic_inc(fiber->ref_count);
	return data;
}

void tf_fiber_put(void *data)
{
	struct tf_fiber *fiber = container_of(data, struct tf_fiber, data);
	if (tf_atomic_dec(fiber->ref_count) == 0)
		__tf_fiber_destroy(fiber);
}

static void update_time(struct tf_scheduler *sched)
{
	struct timespec ts;

	clock_gettime(CLOCK_MONOTONIC, &ts);
	sched->scheduler_time = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}

static void run_fiber(struct tf_scheduler *sched, struct tf_fiber *f)
{
	struct tf_fiber *schedf = container_of((void*) tf_get_scheduler(), struct tf_fiber, data);

	sched->active_fiber = f;
	switch (tf_uctx_transfer(schedf, f, f->wakeup_type)) {
	case TF_WAKEUP_KILL:
		tf_fiber_put(f->data);
		sched->num_fibers--;
		break;
	case TF_WAKEUP_IMMEDIATE:
		break;
	default:
		TF_BUG_ON("bad scheduler call from fiber");
	}
}

static void process_heap(struct tf_scheduler *sched)
{
	struct tf_heap_node *node;
	struct tf_fiber *f;
	tf_mtime_t now = tf_mtime();

	while (!tf_heap_empty(&sched->heap) &&
	       tf_mtime_diff(now, tf_heap_get_value(&sched->heap)) >= 0) {
		node = tf_heap_get_node(&sched->heap);
		f = container_of(node, struct tf_fiber, heap_node);
		run_fiber(sched, f);
	}
}

static void process_runq(struct tf_scheduler *sched)
{
	struct tf_fiber *f;

	while (!tf_list_empty(&sched->run_q)) {
		f = tf_list_first(&sched->run_q, struct tf_fiber, queue_node);
		tf_list_del(&f->queue_node);
		run_fiber(sched, f);
	}
}

int tf_main_args(tf_fiber_proc main_fiber, int argc, char **argv)
{
	struct tf_uctx *ctx = alloca(sizeof(struct tf_uctx) + sizeof(struct tf_scheduler));
	struct tf_scheduler *sched = (struct tf_scheduler*) ctx->fiber.data;
	struct tf_main_ctx *mainctx;
	int stack_guard = STACK_GUARD;

	ctx->stack_guard = &stack_guard;
	*sched = (struct tf_scheduler){
		.run_q = TF_LIST_HEAD_INITIALIZER(sched->run_q),
	};

	__tf_scheduler = sched;
	tf_poll_init();
	update_time(sched);

	mainctx = tf_fiber_create(main_fiber, sizeof(struct tf_main_ctx));
	mainctx->argc = argc;
	mainctx->argv = argv;
	tf_fiber_put(mainctx);

	do {
		tf_mtime_diff_t timeout;

		update_time(sched);
		if (!tf_list_empty(&sched->run_q)) {
			timeout = 0;
		} else if (!tf_heap_empty(&sched->heap)) {
			timeout = tf_mtime_diff(tf_heap_get_value(&sched->heap),
						tf_mtime());
			if (timeout < 0)
				timeout = 0;
		} else
			timeout = -1;

		if (tf_poll(timeout) == TF_WAKEUP_TIMEOUT && timeout >= 0) {
			sched->scheduler_time += timeout;
			process_heap(sched);
		}
		process_runq(sched);
	} while (likely(sched->num_fibers));
	tf_poll_close();
	__tf_scheduler = NULL;

	return 0;
}

int tf_schedule(tf_mtime_diff_t milliseconds)
{
	struct tf_scheduler *sched = tf_get_scheduler();
	struct tf_fiber *schedf = container_of((void*) sched, struct tf_fiber, data);
	struct tf_fiber *f = sched->active_fiber;

	f->wakeup_type = TF_WAKEUP_NONE;
	if (milliseconds == TF_NO_TIMEOUT)
		tf_heap_delete(&f->heap_node, &sched->heap);
	else if (milliseconds != TF_NO_TIMEOUT_CHANGE)
		tf_heap_change(&f->heap_node, &sched->heap,
			       tf_mtime() + milliseconds);

	return tf_uctx_transfer(f, schedf, TF_WAKEUP_IMMEDIATE);
}

void tf_wakeup(struct tf_fiber *fiber, int wakeup_type)
{
	struct tf_scheduler *sched = tf_get_scheduler();

	if (fiber->wakeup_type == TF_WAKEUP_NONE) {
		fiber->wakeup_type = wakeup_type;
		tf_list_add_tail(&fiber->queue_node, &sched->run_q);
	}
}

void tf_exit(void)
{
	struct tf_scheduler *sched = tf_get_scheduler();
	struct tf_fiber *f = sched->active_fiber;
	struct tf_fiber *schedf = container_of((void*) sched, struct tf_fiber, data);

	tf_heap_delete(&f->heap_node, &sched->heap);
	tf_uctx_transfer(f, schedf, TF_WAKEUP_KILL);
	TF_BUG_ON(1);
}

void tf_kill(void *fiber)
{
}

int tf_yield(void)
{
	struct tf_scheduler *sched = tf_get_scheduler();
	struct tf_fiber *f = sched->active_fiber;

	tf_list_add_tail(&f->queue_node, &sched->run_q);
	return tf_schedule(TF_NO_TIMEOUT);
}