forked from sustrik/libmill
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcr.h
More file actions
124 lines (96 loc) · 3.9 KB
/
cr.h
File metadata and controls
124 lines (96 loc) · 3.9 KB
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
/*
Copyright (c) 2015 Martin Sustrik
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#ifndef MILL_CR_INCLUDED
#define MILL_CR_INCLUDED
#include <stdint.h>
#include "debug.h"
#include "list.h"
#include "slist.h"
#include "utils.h"
enum mill_state {
MILL_READY,
MILL_MSLEEP,
MILL_FDWAIT,
MILL_CHR,
MILL_CHS,
MILL_CHOOSE
};
struct mill_ready {
struct mill_slist_item item;
};
/* This structure covers fdwait and msleep operations. */
struct mill_fdwait {
/* Item in the global list of timers. */
struct mill_list_item item;
/* The timepoint when the timer expires. */
int64_t expiry;
};
/* This structure covers chr, chs and choose operations. */
struct mill_choose {
/* List of clauses in the 'choose' statement. */
struct mill_slist clauses;
/* 1 if there is 'otherwise' clause. 0 if there is not. */
int othws;
/* Number of clauses that are immediately available. */
int available;
};
/* The coroutine. The memory layout looks like this:
+----------------------------------------------------+--------+---------+
| stack | valbuf | mill_cr |
+----------------------------------------------------+--------+---------+
- mill_cr contains generic book-keeping info about the coroutine
- valbuf is a buffer for temporary storing values received from channels
- stack is a standard C stack; it grows downwards
*/
struct mill_cr {
/* Status of the coroutine. Used for debugging purposes. */
enum mill_state state;
struct mill_ready u_ready;
struct mill_fdwait u_fdwait;
struct mill_choose u_choose;
/* Stored coroutine context while it is not executing. */
struct mill_ctx ctx;
/* Argument to resume() call being passed to the blocked suspend() call. */
int result;
/* If size of the valbuf needs to be larger than mill_valbuf size it is
allocated dyncamically and the pointer, along with the size of the buffer
is stored here. */
void *valbuf;
size_t valbuf_sz;
/* Coroutine-local storage. */
void *cls;
/* Debugging info. */
struct mill_debug_cr debug;
};
/* Fake coroutine corresponding to the main thread of execution. */
extern struct mill_cr mill_main;
/* The coroutine that is running at the moment. */
extern struct mill_cr *mill_running;
/* Suspend running coroutine. Move to executing different coroutines. Once
someone resumes this coroutine using mill_resume function 'result' argument
of that function will be returned. */
int mill_suspend(void);
/* Schedules preiously suspended coroutine for execution. Keep in mind that
it doesn't immediately run it, just puts it into the queue of ready
coroutines. */
void mill_resume(struct mill_cr *cr, int result);
/* Returns pointer to the value buffer. The returned buffer is guaranteed
to be at least 'size' bytes long. */
void *mill_valbuf(struct mill_cr *cr, size_t size);
#endif