1 /*
2  * GTK VNC Widget
3  *
4  * Copyright (C) 2006  Anthony Liguori <anthony@codemonkey.ws>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.0 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20 
21 #include <config.h>
22 
23 #include "coroutine.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 
27 static GCond *run_cond;
28 static GMutex *run_lock;
29 static struct coroutine *current;
30 static struct coroutine leader;
31 
32 #if 0
33 #define CO_DEBUG(OP) fprintf(stderr, "%s %p %s %d\n", OP, g_thread_self(), __FUNCTION__, __LINE__)
34 #else
35 #define CO_DEBUG(OP)
36 #endif
37 
coroutine_system_init(void)38 static void coroutine_system_init(void)
39 {
40     if (!g_thread_supported()) {
41         CO_DEBUG("INIT");
42         g_thread_init(NULL);
43     }
44 
45 
46     run_cond = g_cond_new();
47     run_lock = g_mutex_new();
48     CO_DEBUG("LOCK");
49     g_mutex_lock(run_lock);
50 
51     /* The thread that creates the first coroutine is the system coroutine
52      * so let's fill out a structure for it */
53     leader.entry = NULL;
54     leader.release = NULL;
55     leader.stack_size = 0;
56     leader.exited = 0;
57     leader.thread = g_thread_self();
58     leader.runnable = TRUE; /* we're the one running right now */
59     leader.caller = NULL;
60     leader.data = NULL;
61 
62     current = &leader;
63 }
64 
coroutine_thread(gpointer opaque)65 static gpointer coroutine_thread(gpointer opaque)
66 {
67     struct coroutine *co = opaque;
68     CO_DEBUG("LOCK");
69     g_mutex_lock(run_lock);
70     while (!co->runnable) {
71         CO_DEBUG("WAIT");
72         g_cond_wait(run_cond, run_lock);
73     }
74 
75     CO_DEBUG("RUNNABLE");
76     current = co;
77     co->data = co->entry(co->data);
78     co->exited = 1;
79 
80     co->caller->runnable = TRUE;
81     CO_DEBUG("BROADCAST");
82     g_cond_broadcast(run_cond);
83     CO_DEBUG("UNLOCK");
84     g_mutex_unlock(run_lock);
85 
86     return NULL;
87 }
88 
coroutine_init(struct coroutine * co)89 int coroutine_init(struct coroutine *co)
90 {
91     if (run_cond == NULL)
92         coroutine_system_init();
93 
94     CO_DEBUG("NEW");
95     co->thread = g_thread_create_full(coroutine_thread, co, co->stack_size,
96                                       FALSE, TRUE,
97                                       G_THREAD_PRIORITY_NORMAL,
98                                       NULL);
99     if (co->thread == NULL)
100         return -1;
101 
102     co->exited = 0;
103     co->runnable = FALSE;
104     co->caller = NULL;
105 
106     return 0;
107 }
108 
coroutine_release(struct coroutine * co G_GNUC_UNUSED)109 int coroutine_release(struct coroutine *co G_GNUC_UNUSED)
110 {
111     return 0;
112 }
113 
coroutine_swap(struct coroutine * from,struct coroutine * to,void * arg)114 void *coroutine_swap(struct coroutine *from, struct coroutine *to, void *arg)
115 {
116     from->runnable = FALSE;
117     to->runnable = TRUE;
118     to->data = arg;
119     to->caller = from;
120     CO_DEBUG("BROADCAST");
121     g_cond_broadcast(run_cond);
122     CO_DEBUG("UNLOCK");
123     g_mutex_unlock(run_lock);
124     CO_DEBUG("LOCK");
125     g_mutex_lock(run_lock);
126     while (!from->runnable) {
127         CO_DEBUG("WAIT");
128         g_cond_wait(run_cond, run_lock);
129     }
130     current = from;
131 
132     CO_DEBUG("SWAPPED");
133     return from->data;
134 }
135 
coroutine_self(void)136 struct coroutine *coroutine_self(void)
137 {
138     return current;
139 }
140 
coroutine_yieldto(struct coroutine * to,void * arg)141 void *coroutine_yieldto(struct coroutine *to, void *arg)
142 {
143     if (to->caller) {
144         fprintf(stderr, "Co-routine is re-entering itself\n");
145         abort();
146     }
147     CO_DEBUG("SWAP");
148     return coroutine_swap(coroutine_self(), to, arg);
149 }
150 
coroutine_yield(void * arg)151 void *coroutine_yield(void *arg)
152 {
153     struct coroutine *to = coroutine_self()->caller;
154     if (!to) {
155         fprintf(stderr, "Co-routine is yielding to no one\n");
156         abort();
157     }
158 
159     CO_DEBUG("SWAP");
160     coroutine_self()->caller = NULL;
161     return coroutine_swap(coroutine_self(), to, arg);
162 }
163 /*
164  * Local variables:
165  *  c-indent-level: 4
166  *  c-basic-offset: 4
167  *  indent-tabs-mode: nil
168  * End:
169  */
170