summaryrefslogtreecommitdiffstats
path: root/include/libtf/fiber.h
blob: 09d5ef10d882b68f57e88394b342be1d0090f41c (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
/* tf.h - libtf main include
 *
 * 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.
 */

#ifndef TF_FIBER_H
#define TF_FIBER_H

#include <errno.h>
#include <libtf/defines.h>
#include <libtf/atomic.h>
#include <libtf/list.h>
#include <libtf/heap.h>

/* Scheduler */
struct tf_scheduler {
	struct tf_list_head	run_q;
	struct tf_list_head	sleep_q;
	struct tf_heap_head	heap;

	struct tf_fiber *	active_fiber;
	int			num_fibers;

	tf_mtime_t		scheduler_time;
};

static inline
struct tf_scheduler *tf_get_scheduler(void)
{
	extern struct tf_scheduler *__tf_scheduler;
	return __tf_scheduler;
}

static inline
tf_mtime_t tf_mtime(void)
{
	return tf_get_scheduler()->scheduler_time;
}

/* Fiber creation */
typedef void (*tf_fiber_proc)(void *fiber);
int tf_main(tf_fiber_proc fiber_main);

void *tf_fiber_create(tf_fiber_proc fiber_main, int private_size);
void *tf_fiber_get(void *data);
void tf_fiber_put(void *data);

/* Scheduling and fiber management */
int tf_schedule(int err);
int tf_msleep(int milliseconds);
void tf_kill(void *fiber);

static inline int tf_yield(void)
{
	return tf_schedule(EAGAIN);
}

static inline int tf_exit(void)
{
	return tf_schedule(EFAULT);
}

#endif