summaryrefslogtreecommitdiffstats
path: root/firmware/asm/thread-win32.c
blob: 9125819ade4017946f2174484b361988b7ac2189 (plain)
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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2010 by Thomas Martitz
 *
 * Generic ARM threading support
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/


#include <windows.h>
#include "system.h"

#define INIT_MAIN_THREAD

#define THREAD_STARTUP_INIT(core, thread, function) \
    ({ (thread)->context.stack_size = (thread)->stack_size, \
       (thread)->context.stack = (uintptr_t)(thread)->stack; \
       (thread)->context.start = function; })

static void init_main_thread(void *addr)
{
    struct regs *context = (struct regs*)addr;
    /* we must convert the current main thread to a fiber to be able to
     * schedule other fibers */
    context->uc = ConvertThreadToFiber(NULL);
    context->stack_size = 0;
}

static inline void store_context(void* addr)
{
    (void)addr;
    /* nothing to do here, Fibers continue after the SwitchToFiber call */
}

static void start_thread(void)
{
    void (*func)(void) = GetFiberData();
    func();
    /* go out if thread function returns */
    thread_exit();
}

/*
 * Load context and run it
 *
 * Resume execution from the last load_context call for the thread
 */

static inline void load_context(const void* addr)
{
    struct regs *context = (struct regs*)addr;
    if (UNLIKELY(context->start))
    {   /* need setup before switching to it */
        context->uc = CreateFiber(context->stack_size,
                        (LPFIBER_START_ROUTINE)start_thread, context->start);
        /* can't assign stack pointer, only stack size */
        context->stack_size = 0;
        context->start = NULL;
    }
    SwitchToFiber(context->uc);
}