Statistics
| Branch: | Revision:

root / coroutine-win32.c @ f53ec699

History | View | Annotate | Download (2.6 kB)

1 00dccaf1 Kevin Wolf
/*
2 00dccaf1 Kevin Wolf
 * Win32 coroutine initialization code
3 00dccaf1 Kevin Wolf
 *
4 00dccaf1 Kevin Wolf
 * Copyright (c) 2011 Kevin Wolf <kwolf@redhat.com>
5 00dccaf1 Kevin Wolf
 *
6 00dccaf1 Kevin Wolf
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 00dccaf1 Kevin Wolf
 * of this software and associated documentation files (the "Software"), to deal
8 00dccaf1 Kevin Wolf
 * in the Software without restriction, including without limitation the rights
9 00dccaf1 Kevin Wolf
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 00dccaf1 Kevin Wolf
 * copies of the Software, and to permit persons to whom the Software is
11 00dccaf1 Kevin Wolf
 * furnished to do so, subject to the following conditions:
12 00dccaf1 Kevin Wolf
 *
13 00dccaf1 Kevin Wolf
 * The above copyright notice and this permission notice shall be included in
14 00dccaf1 Kevin Wolf
 * all copies or substantial portions of the Software.
15 00dccaf1 Kevin Wolf
 *
16 00dccaf1 Kevin Wolf
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 00dccaf1 Kevin Wolf
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 00dccaf1 Kevin Wolf
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 00dccaf1 Kevin Wolf
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 00dccaf1 Kevin Wolf
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 00dccaf1 Kevin Wolf
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 00dccaf1 Kevin Wolf
 * THE SOFTWARE.
23 00dccaf1 Kevin Wolf
 */
24 00dccaf1 Kevin Wolf
25 00dccaf1 Kevin Wolf
#include "qemu-common.h"
26 737e150e Paolo Bonzini
#include "block/coroutine_int.h"
27 00dccaf1 Kevin Wolf
28 00dccaf1 Kevin Wolf
typedef struct
29 00dccaf1 Kevin Wolf
{
30 00dccaf1 Kevin Wolf
    Coroutine base;
31 00dccaf1 Kevin Wolf
32 00dccaf1 Kevin Wolf
    LPVOID fiber;
33 00dccaf1 Kevin Wolf
    CoroutineAction action;
34 00dccaf1 Kevin Wolf
} CoroutineWin32;
35 00dccaf1 Kevin Wolf
36 00dccaf1 Kevin Wolf
static __thread CoroutineWin32 leader;
37 00dccaf1 Kevin Wolf
static __thread Coroutine *current;
38 00dccaf1 Kevin Wolf
39 00dccaf1 Kevin Wolf
CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
40 00dccaf1 Kevin Wolf
                                      CoroutineAction action)
41 00dccaf1 Kevin Wolf
{
42 00dccaf1 Kevin Wolf
    CoroutineWin32 *from = DO_UPCAST(CoroutineWin32, base, from_);
43 00dccaf1 Kevin Wolf
    CoroutineWin32 *to = DO_UPCAST(CoroutineWin32, base, to_);
44 00dccaf1 Kevin Wolf
45 00dccaf1 Kevin Wolf
    current = to_;
46 00dccaf1 Kevin Wolf
47 00dccaf1 Kevin Wolf
    to->action = action;
48 00dccaf1 Kevin Wolf
    SwitchToFiber(to->fiber);
49 00dccaf1 Kevin Wolf
    return from->action;
50 00dccaf1 Kevin Wolf
}
51 00dccaf1 Kevin Wolf
52 00dccaf1 Kevin Wolf
static void CALLBACK coroutine_trampoline(void *co_)
53 00dccaf1 Kevin Wolf
{
54 00dccaf1 Kevin Wolf
    Coroutine *co = co_;
55 00dccaf1 Kevin Wolf
56 00dccaf1 Kevin Wolf
    while (true) {
57 00dccaf1 Kevin Wolf
        co->entry(co->entry_arg);
58 00dccaf1 Kevin Wolf
        qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE);
59 00dccaf1 Kevin Wolf
    }
60 00dccaf1 Kevin Wolf
}
61 00dccaf1 Kevin Wolf
62 00dccaf1 Kevin Wolf
Coroutine *qemu_coroutine_new(void)
63 00dccaf1 Kevin Wolf
{
64 00dccaf1 Kevin Wolf
    const size_t stack_size = 1 << 20;
65 00dccaf1 Kevin Wolf
    CoroutineWin32 *co;
66 00dccaf1 Kevin Wolf
67 7267c094 Anthony Liguori
    co = g_malloc0(sizeof(*co));
68 00dccaf1 Kevin Wolf
    co->fiber = CreateFiber(stack_size, coroutine_trampoline, &co->base);
69 00dccaf1 Kevin Wolf
    return &co->base;
70 00dccaf1 Kevin Wolf
}
71 00dccaf1 Kevin Wolf
72 00dccaf1 Kevin Wolf
void qemu_coroutine_delete(Coroutine *co_)
73 00dccaf1 Kevin Wolf
{
74 00dccaf1 Kevin Wolf
    CoroutineWin32 *co = DO_UPCAST(CoroutineWin32, base, co_);
75 00dccaf1 Kevin Wolf
76 00dccaf1 Kevin Wolf
    DeleteFiber(co->fiber);
77 7267c094 Anthony Liguori
    g_free(co);
78 00dccaf1 Kevin Wolf
}
79 00dccaf1 Kevin Wolf
80 00dccaf1 Kevin Wolf
Coroutine *qemu_coroutine_self(void)
81 00dccaf1 Kevin Wolf
{
82 00dccaf1 Kevin Wolf
    if (!current) {
83 00dccaf1 Kevin Wolf
        current = &leader.base;
84 00dccaf1 Kevin Wolf
        leader.fiber = ConvertThreadToFiber(NULL);
85 00dccaf1 Kevin Wolf
    }
86 00dccaf1 Kevin Wolf
    return current;
87 00dccaf1 Kevin Wolf
}
88 00dccaf1 Kevin Wolf
89 00dccaf1 Kevin Wolf
bool qemu_in_coroutine(void)
90 00dccaf1 Kevin Wolf
{
91 00dccaf1 Kevin Wolf
    return current && current->caller;
92 00dccaf1 Kevin Wolf
}