1 /*
2 
3   Copyright (c) 2015 Martin Sustrik
4 
5   Permission is hereby granted, free of charge, to any person obtaining a copy
6   of this software and associated documentation files (the "Software"),
7   to deal in the Software without restriction, including without limitation
8   the rights to use, copy, modify, merge, publish, distribute, sublicense,
9   and/or sell copies of the Software, and to permit persons to whom
10   the Software is furnished to do so, subject to the following conditions:
11 
12   The above copyright notice and this permission notice shall be included
13   in all copies or substantial portions of the Software.
14 
15   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21   IN THE SOFTWARE.
22 
23 */
24 
25 #ifndef MILL_CR_INCLUDED
26 #define MILL_CR_INCLUDED
27 
28 #include <setjmp.h>
29 #include <stdint.h>
30 
31 #include "chan.h"
32 #include "debug.h"
33 #include "list.h"
34 #include "slist.h"
35 #include "timer.h"
36 #include "utils.h"
37 
38 enum mill_state {
39     MILL_READY,
40     MILL_MSLEEP,
41     MILL_FDWAIT,
42     MILL_CHR,
43     MILL_CHS,
44     MILL_CHOOSE
45 };
46 
47 /* The coroutine. The memory layout looks like this:
48 
49    +----------------------------------------------------+--------+---------+
50    |                                              stack | valbuf | mill_cr |
51    +----------------------------------------------------+--------+---------+
52 
53    - mill_cr contains generic book-keeping info about the coroutine
54    - valbuf is a buffer for temporarily storing values received from channels
55    - stack is a standard C stack; it grows downwards (at the moment libmill
56      doesn't support microarchitectures where stack grows upwards)
57 
58 */
59 struct mill_cr {
60     /* Status of the coroutine. Used for debugging purposes. */
61     enum mill_state state;
62 
63     /* The coroutine is stored in this list if it is not blocked and it is
64        waiting to be executed. In such case 'is_ready' is set to 1, otherwise
65        it's set to 0. */
66     int is_ready;
67     struct mill_slist_item ready;
68 
69     /* If the coroutine is waiting for a deadline, it uses this timer. */
70     struct mill_timer timer;
71 
72     /* The file descriptor for which the coroutine waits for an event
73        when blocked in fdwait(). -1 when it isn't waiting for any event. */
74     int fd;
75 
76     /* When the coroutine is blocked in fdwait(), the events that the function
77        waits for. This is used only for debugging purposes. */
78     int events;
79 
80     /* This structure is used when the coroutine is executing a choose
81        statement. */
82     struct mill_choosedata choosedata;
83 
84     /* Stored coroutine context while it is not executing. */
85 #if defined(__x86_64__)
86     uint64_t ctx[10];
87 #else
88     sigjmp_buf ctx;
89 #endif
90 
91     /* Argument to resume() call being passed to the blocked suspend() call. */
92     int result;
93 
94     /* If size of the valbuf needs to be larger than mill_valbuf size it is
95        allocated dyncamically and the pointer, along with the size of the buffer
96        is stored here. */
97     void *valbuf;
98     size_t valbuf_sz;
99 
100     /* Coroutine-local storage. */
101     void *clsval;
102 
103 #if defined MILL_VALGRIND
104     /* Valgrind stack identifier. */
105     int sid;
106 #endif
107 
108     /* Debugging info. */
109     struct mill_debug_cr debug;
110 };
111 
112 /* Fake coroutine corresponding to the main thread of execution. */
113 extern struct mill_cr mill_main;
114 
115 /* The coroutine that is running at the moment. */
116 extern struct mill_cr *mill_running;
117 
118 /* Suspend running coroutine. Move to executing different coroutines. Once
119    someone resumes this coroutine using mill_resume function 'result' argument
120    of that function will be returned. */
121 int mill_suspend(void);
122 
123 /* Schedules preiously suspended coroutine for execution. Keep in mind that
124    it doesn't immediately run it, just puts it into the queue of ready
125    coroutines. */
126 void mill_resume(struct mill_cr *cr, int result);
127 
128 /* Returns pointer to the value buffer. The returned buffer is guaranteed
129    to be at least 'size' bytes long. */
130 void *mill_valbuf(struct mill_cr *cr, size_t size);
131 
132 /* Called in the child process after fork to stop all the coroutines
133    inherited from the parent. */
134 void mill_cr_postfork(void);
135 
136 #endif
137