1 /********************************************************************************
2 * Copyright (c) 2003, 2004 *
3 * Panagiotis E. Hadjidoukas (peh@hpclab.ceid.upatras.gr) *
4 * HPCLab - University of Patras. All Rights Reserved. *
5 * Unix ucontext_t on Windows Operating System, May 2004 (revision 1) *
7 * The author disclaims all warranties with regard to this software including *
8 * all implied warranties of merchantability and fitness for a particular *
9 * purpose. In no event shall HPCLab be liable for any special, indirect, *
10 * or consequential damages or any damages whatsoever resulting from *
11 * loss of use, data or profits, whether in action of contract negligence, *
12 * or other tortious action, arising out of or in connection with the use *
13 * or performance of this software. *
14 ********************************************************************************/
16 /* The original author granted me (Martin Quinson) to redistribute this work
17 under the LGPL licence, what I here do. */
19 #include "context_win32.h" /* Current file is to be included when cross-compiling,
20 but not during native builds. So this include is needed */
22 int getcontext(ucontext_t *ucp)
26 /* Retrieve the full machine context */
27 ucp->uc_mcontext.ContextFlags = CONTEXT_FULL;
28 ret = GetThreadContext(GetCurrentThread(), &ucp->uc_mcontext);
30 return (ret == 0) ? -1: 0;
33 int setcontext(const ucontext_t *ucp)
37 /* Restore the full machine context (already set) */
38 ret = SetThreadContext(GetCurrentThread(), &ucp->uc_mcontext);
40 return (ret == 0) ? -1: 0;
43 int makecontext(ucontext_t *ucp, void (*func)(), int argc, ...)
49 /* Stack grows down */
50 sp = (char *) (size_t) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size;
52 /* Reserve stack space for the arguments (maximum possible: argc*(8 bytes per argument)) */
55 if ( sp < (char *)ucp->uc_stack.ss_sp) {
60 /* Set the instruction and the stack pointer */
61 ucp->uc_mcontext.Eip = (unsigned long) func;
62 ucp->uc_mcontext.Esp = (unsigned long) sp - 4;
64 /* Save/Restore the full machine context */
65 ucp->uc_mcontext.ContextFlags = CONTEXT_FULL;
67 /* Copy the arguments */
69 for (i=0; i<argc; i++) {
79 int swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
83 if ((oucp == NULL) || (ucp == NULL)) {
88 ret = getcontext(oucp);
90 ret = setcontext(ucp);