summaryrefslogtreecommitdiffstats
path: root/include/libtf/fiber.h
blob: ce3745bf395c7a21948ab5291d42e95b3a8bb358 (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
/* fiber.h - libtf fiber scheduler header
 *
 * 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>

/* Fiber wakeup reasons */
#define TF_WAKEUP_NONE		0
#define TF_WAKEUP_IMMEDIATE	-EAGAIN
#define TF_WAKEUP_KILL		-EINTR
#define TF_WAKEUP_TIMEOUT	-ETIME
#define TF_WAKEUP_THIS_TIMEOUT	-ETIMEDOUT
#define TF_WAKEUP_FD		-EIO

struct tf_poll_data {
	int			epoll_fd;
	int			num_waiters;
};

/* Scheduler */
struct tf_fiber;

struct tf_scheduler {
	struct tf_list_head	run_q;
	struct tf_heap_head	heap;
	struct tf_fiber *	active_fiber;
	int			num_fibers;
	tf_mtime_t		scheduler_time;
	struct tf_poll_data	poll_data;
};

struct tf_main_ctx {
	int			argc;
	char **			argv;
};

struct tf_timeout {
	tf_mtime_t	saved_timeout;
	unsigned int	timeout_change;
};

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

static inline
struct tf_fiber *tf_get_fiber(void)
{
	return tf_get_scheduler()->active_fiber;
}

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_args(tf_fiber_proc fiber_main, int argc, char **argv);
static inline int tf_main(tf_fiber_proc fiber_main)
{
	return tf_main_args(fiber_main, 0, NULL);
}

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

#define tf_timed(func, timeout)						\
	({								\
		struct tf_timeout __timeout;				\
		tf_timeout_push(&__timeout, timeout);			\
		tf_timeout_pop(&__timeout, (func));			\
	})

//* Scheduling and fiber management */
void tf_timeout_push(struct tf_timeout *timeout, tf_mtime_diff_t milliseconds);
int __tf_timeout_pop(struct tf_timeout *timeout, int err);

static inline int tf_timeout_pop(struct tf_timeout *timeout, int err)
{
	if (unlikely(timeout->timeout_change))
		return __tf_timeout_pop(timeout, err);
	return err;
}

int  tf_schedule(void);
void tf_wakeup(struct tf_fiber *fiber, int wakeup_type);
void tf_exit(void) attribute_noreturn;
void tf_kill(void *fiber);
int  tf_yield(void);

static inline
int tf_msleep(tf_mtime_diff_t milliseconds)
{
	int r;
	r = tf_timed(tf_schedule(), milliseconds);
	if (r == TF_WAKEUP_THIS_TIMEOUT)
		r = 0;
	return r;
}

#endif