Statistics
| Branch: | Revision:

root / coroutine-gthread.c @ 0ecb72a5

History | View | Annotate | Download (3.7 kB)

1 d0e2fce5 Aneesh Kumar K.V
/*
2 d0e2fce5 Aneesh Kumar K.V
 * GThread coroutine initialization code
3 d0e2fce5 Aneesh Kumar K.V
 *
4 d0e2fce5 Aneesh Kumar K.V
 * Copyright (C) 2006  Anthony Liguori <anthony@codemonkey.ws>
5 d0e2fce5 Aneesh Kumar K.V
 * Copyright (C) 2011  Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
6 d0e2fce5 Aneesh Kumar K.V
 *
7 d0e2fce5 Aneesh Kumar K.V
 * This library is free software; you can redistribute it and/or
8 d0e2fce5 Aneesh Kumar K.V
 * modify it under the terms of the GNU Lesser General Public
9 d0e2fce5 Aneesh Kumar K.V
 * License as published by the Free Software Foundation; either
10 d0e2fce5 Aneesh Kumar K.V
 * version 2.0 of the License, or (at your option) any later version.
11 d0e2fce5 Aneesh Kumar K.V
 *
12 d0e2fce5 Aneesh Kumar K.V
 * This library is distributed in the hope that it will be useful,
13 d0e2fce5 Aneesh Kumar K.V
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 d0e2fce5 Aneesh Kumar K.V
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 d0e2fce5 Aneesh Kumar K.V
 * Lesser General Public License for more details.
16 d0e2fce5 Aneesh Kumar K.V
 *
17 d0e2fce5 Aneesh Kumar K.V
 * You should have received a copy of the GNU Lesser General Public
18 d0e2fce5 Aneesh Kumar K.V
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 d0e2fce5 Aneesh Kumar K.V
 */
20 d0e2fce5 Aneesh Kumar K.V
21 d0e2fce5 Aneesh Kumar K.V
#include <glib.h>
22 d0e2fce5 Aneesh Kumar K.V
#include "qemu-common.h"
23 d0e2fce5 Aneesh Kumar K.V
#include "qemu-coroutine-int.h"
24 d0e2fce5 Aneesh Kumar K.V
25 d0e2fce5 Aneesh Kumar K.V
typedef struct {
26 d0e2fce5 Aneesh Kumar K.V
    Coroutine base;
27 d0e2fce5 Aneesh Kumar K.V
    GThread *thread;
28 d0e2fce5 Aneesh Kumar K.V
    bool runnable;
29 d0e2fce5 Aneesh Kumar K.V
    CoroutineAction action;
30 d0e2fce5 Aneesh Kumar K.V
} CoroutineGThread;
31 d0e2fce5 Aneesh Kumar K.V
32 d0e2fce5 Aneesh Kumar K.V
static GCond *coroutine_cond;
33 d0e2fce5 Aneesh Kumar K.V
static GStaticMutex coroutine_lock = G_STATIC_MUTEX_INIT;
34 d0e2fce5 Aneesh Kumar K.V
static GStaticPrivate coroutine_key = G_STATIC_PRIVATE_INIT;
35 d0e2fce5 Aneesh Kumar K.V
36 d0e2fce5 Aneesh Kumar K.V
static void __attribute__((constructor)) coroutine_init(void)
37 d0e2fce5 Aneesh Kumar K.V
{
38 d0e2fce5 Aneesh Kumar K.V
    if (!g_thread_supported()) {
39 42ed3727 Alon Levy
#if !GLIB_CHECK_VERSION(2, 31, 0)
40 d0e2fce5 Aneesh Kumar K.V
        g_thread_init(NULL);
41 42ed3727 Alon Levy
#else
42 42ed3727 Alon Levy
        fprintf(stderr, "glib threading failed to initialize.\n");
43 42ed3727 Alon Levy
        exit(1);
44 42ed3727 Alon Levy
#endif
45 d0e2fce5 Aneesh Kumar K.V
    }
46 d0e2fce5 Aneesh Kumar K.V
47 d0e2fce5 Aneesh Kumar K.V
    coroutine_cond = g_cond_new();
48 d0e2fce5 Aneesh Kumar K.V
}
49 d0e2fce5 Aneesh Kumar K.V
50 d0e2fce5 Aneesh Kumar K.V
static void coroutine_wait_runnable_locked(CoroutineGThread *co)
51 d0e2fce5 Aneesh Kumar K.V
{
52 d0e2fce5 Aneesh Kumar K.V
    while (!co->runnable) {
53 d0e2fce5 Aneesh Kumar K.V
        g_cond_wait(coroutine_cond, g_static_mutex_get_mutex(&coroutine_lock));
54 d0e2fce5 Aneesh Kumar K.V
    }
55 d0e2fce5 Aneesh Kumar K.V
}
56 d0e2fce5 Aneesh Kumar K.V
57 d0e2fce5 Aneesh Kumar K.V
static void coroutine_wait_runnable(CoroutineGThread *co)
58 d0e2fce5 Aneesh Kumar K.V
{
59 d0e2fce5 Aneesh Kumar K.V
    g_static_mutex_lock(&coroutine_lock);
60 d0e2fce5 Aneesh Kumar K.V
    coroutine_wait_runnable_locked(co);
61 d0e2fce5 Aneesh Kumar K.V
    g_static_mutex_unlock(&coroutine_lock);
62 d0e2fce5 Aneesh Kumar K.V
}
63 d0e2fce5 Aneesh Kumar K.V
64 d0e2fce5 Aneesh Kumar K.V
static gpointer coroutine_thread(gpointer opaque)
65 d0e2fce5 Aneesh Kumar K.V
{
66 d0e2fce5 Aneesh Kumar K.V
    CoroutineGThread *co = opaque;
67 d0e2fce5 Aneesh Kumar K.V
68 d0e2fce5 Aneesh Kumar K.V
    g_static_private_set(&coroutine_key, co, NULL);
69 d0e2fce5 Aneesh Kumar K.V
    coroutine_wait_runnable(co);
70 d0e2fce5 Aneesh Kumar K.V
    co->base.entry(co->base.entry_arg);
71 d0e2fce5 Aneesh Kumar K.V
    qemu_coroutine_switch(&co->base, co->base.caller, COROUTINE_TERMINATE);
72 d0e2fce5 Aneesh Kumar K.V
    return NULL;
73 d0e2fce5 Aneesh Kumar K.V
}
74 d0e2fce5 Aneesh Kumar K.V
75 d0e2fce5 Aneesh Kumar K.V
Coroutine *qemu_coroutine_new(void)
76 d0e2fce5 Aneesh Kumar K.V
{
77 d0e2fce5 Aneesh Kumar K.V
    CoroutineGThread *co;
78 d0e2fce5 Aneesh Kumar K.V
79 7267c094 Anthony Liguori
    co = g_malloc0(sizeof(*co));
80 d0e2fce5 Aneesh Kumar K.V
    co->thread = g_thread_create_full(coroutine_thread, co, 0, TRUE, TRUE,
81 d0e2fce5 Aneesh Kumar K.V
                                      G_THREAD_PRIORITY_NORMAL, NULL);
82 d0e2fce5 Aneesh Kumar K.V
    if (!co->thread) {
83 7267c094 Anthony Liguori
        g_free(co);
84 d0e2fce5 Aneesh Kumar K.V
        return NULL;
85 d0e2fce5 Aneesh Kumar K.V
    }
86 d0e2fce5 Aneesh Kumar K.V
    return &co->base;
87 d0e2fce5 Aneesh Kumar K.V
}
88 d0e2fce5 Aneesh Kumar K.V
89 d0e2fce5 Aneesh Kumar K.V
void qemu_coroutine_delete(Coroutine *co_)
90 d0e2fce5 Aneesh Kumar K.V
{
91 d0e2fce5 Aneesh Kumar K.V
    CoroutineGThread *co = DO_UPCAST(CoroutineGThread, base, co_);
92 d0e2fce5 Aneesh Kumar K.V
93 d0e2fce5 Aneesh Kumar K.V
    g_thread_join(co->thread);
94 7267c094 Anthony Liguori
    g_free(co);
95 d0e2fce5 Aneesh Kumar K.V
}
96 d0e2fce5 Aneesh Kumar K.V
97 d0e2fce5 Aneesh Kumar K.V
CoroutineAction qemu_coroutine_switch(Coroutine *from_,
98 d0e2fce5 Aneesh Kumar K.V
                                      Coroutine *to_,
99 d0e2fce5 Aneesh Kumar K.V
                                      CoroutineAction action)
100 d0e2fce5 Aneesh Kumar K.V
{
101 d0e2fce5 Aneesh Kumar K.V
    CoroutineGThread *from = DO_UPCAST(CoroutineGThread, base, from_);
102 d0e2fce5 Aneesh Kumar K.V
    CoroutineGThread *to = DO_UPCAST(CoroutineGThread, base, to_);
103 d0e2fce5 Aneesh Kumar K.V
104 d0e2fce5 Aneesh Kumar K.V
    g_static_mutex_lock(&coroutine_lock);
105 d0e2fce5 Aneesh Kumar K.V
    from->runnable = false;
106 d0e2fce5 Aneesh Kumar K.V
    from->action = action;
107 d0e2fce5 Aneesh Kumar K.V
    to->runnable = true;
108 d0e2fce5 Aneesh Kumar K.V
    to->action = action;
109 d0e2fce5 Aneesh Kumar K.V
    g_cond_broadcast(coroutine_cond);
110 d0e2fce5 Aneesh Kumar K.V
111 d0e2fce5 Aneesh Kumar K.V
    if (action != COROUTINE_TERMINATE) {
112 d0e2fce5 Aneesh Kumar K.V
        coroutine_wait_runnable_locked(from);
113 d0e2fce5 Aneesh Kumar K.V
    }
114 d0e2fce5 Aneesh Kumar K.V
    g_static_mutex_unlock(&coroutine_lock);
115 d0e2fce5 Aneesh Kumar K.V
    return from->action;
116 d0e2fce5 Aneesh Kumar K.V
}
117 d0e2fce5 Aneesh Kumar K.V
118 d0e2fce5 Aneesh Kumar K.V
Coroutine *qemu_coroutine_self(void)
119 d0e2fce5 Aneesh Kumar K.V
{
120 d0e2fce5 Aneesh Kumar K.V
    CoroutineGThread *co = g_static_private_get(&coroutine_key);
121 d0e2fce5 Aneesh Kumar K.V
122 d0e2fce5 Aneesh Kumar K.V
    if (!co) {
123 7267c094 Anthony Liguori
        co = g_malloc0(sizeof(*co));
124 d0e2fce5 Aneesh Kumar K.V
        co->runnable = true;
125 58a06675 Blue Swirl
        g_static_private_set(&coroutine_key, co, (GDestroyNotify)g_free);
126 d0e2fce5 Aneesh Kumar K.V
    }
127 d0e2fce5 Aneesh Kumar K.V
128 d0e2fce5 Aneesh Kumar K.V
    return &co->base;
129 d0e2fce5 Aneesh Kumar K.V
}
130 d0e2fce5 Aneesh Kumar K.V
131 d0e2fce5 Aneesh Kumar K.V
bool qemu_in_coroutine(void)
132 d0e2fce5 Aneesh Kumar K.V
{
133 d0e2fce5 Aneesh Kumar K.V
    CoroutineGThread *co = g_static_private_get(&coroutine_key);
134 d0e2fce5 Aneesh Kumar K.V
135 d0e2fce5 Aneesh Kumar K.V
    return co && co->base.caller;
136 d0e2fce5 Aneesh Kumar K.V
}