-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcoroutines_internal.h
113 lines (95 loc) · 2.93 KB
/
coroutines_internal.h
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
/**
* coroutines_internal.h - header file for simple coroutines for SimpleMail.
* Copyright (C) 2015 Sebastian Bauer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file coroutines_internal.h
*/
#ifndef SM__COROUTINES_INTERNAL_H
#define SM__COROUTINES_INTERNAL_H
#ifndef SM__COROUTINES_H
#include "coroutines.h"
#endif
#ifndef SM__LISTS_H
#include "lists.h"
#endif
/**
* A simple coroutine.
*/
struct coroutine
{
/** Embedded node structure for adding it to lists */
struct node node;
/** The context parameter of the coroutine */
struct coroutine_basic_context *context;
/** The actual entry of the coroutine */
coroutine_entry_t entry;
};
/**
* A simple list of elements of type coroutine_t.
*/
struct coroutines_list
{
struct list list;
};
/**
* A simple scheduler for coroutines.
*/
struct coroutine_scheduler
{
/** Contains all ready coroutines. Elements are of type coroutine_t */
struct coroutines_list coroutines_ready_list;
/** Contains all waiting coroutines. Elements are of type coroutine_t */
struct coroutines_list waiting_coroutines_list;
/** Contains all finished coroutines. Elements are of type coroutine_t */
struct coroutines_list finished_coroutines_list;
/**
* Function that is invoked to wait or poll for a next event
*
* @return if there were events that potentially were blocked
*/
int (*wait_for_event)(coroutine_scheduler_t sched, int poll, void *udata);
/** User data passed to wait_for_event() */
void *wait_for_event_udata;
};
/**
* Initializes a coroutines list.
*
* @param list the list to initialize.
*/
void coroutines_list_init(struct coroutines_list *list);
/**
* Returns the first element of a coroutines list.
*
* @param list the coroutines list.
* @return the first element or NULL if the list is empty
*/
coroutine_t coroutines_list_first(struct coroutines_list *list);
/**
* Remove the first element of the given coroutine list and return it.
*
* @param list the coroutines list.
* @return the formerly first element or NULL if the list was empty.
*/
coroutine_t coroutines_list_remove_head(struct coroutines_list *list);
/**
* Returns the next coroutine within the same list.
*
* @param c the coroutine whose successor shall be determined
* @return the successor of c or NULL if c was the last element
*/
coroutine_t coroutines_next(coroutine_t c);
#endif