2 * win32-ucontext: Unix ucontext_t operations on Windows platforms
\r
3 * Copyright(C) 2007 Panagiotis E. Hadjidoukas
\r
5 * Contact Email: phadjido@cs.uoi.gr, xdoukas@ceid.upatras.gr
\r
7 * win32-ucontext is free software; you can redistribute it and/or
\r
8 * modify it under the terms of the GNU Lesser General Public
\r
9 * License as published by the Free Software Foundation; either
\r
10 * version 2 of the License, or (at your option) any later version.
\r
12 * win32-ucontext is distributed in the hope that it will be useful,
\r
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
15 * Lesser General Public License for more details.
\r
17 * You should have received a copy of the GNU Lesser General Public
\r
18 * License along with SimGrid in the file LICENSE-LGPL-2.1;
\r
19 * if not, write to the Free Software Foundation, Inc.,
\r
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
\r
23 #include "win32_ucontext.h"
\r
24 int getcontext(ucontext_t * ucp)
\r
28 /* Retrieve the full machine context */
\r
29 ucp->uc_mcontext.ContextFlags = CONTEXT_FULL;
\r
30 ret = GetThreadContext(GetCurrentThread(), &ucp->uc_mcontext);
\r
31 return (ret == 0) ? -1 : 0;
\r
34 int setcontext(const ucontext_t * ucp)
\r
38 /* Restore the full machine context (already set) */
\r
39 ret = SetThreadContext(GetCurrentThread(), &ucp->uc_mcontext);
\r
40 return (ret == 0) ? -1 : 0;
\r
43 int makecontext(ucontext_t * ucp, void (*func) (), int argc, ...)
\r
49 /* Stack grows down */
\r
50 sp = (char *) (size_t) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size;
\r
52 /* Reserve stack space for the arguments (maximum possible: argc*(8 bytes per argument)) */
\r
54 if (sp < (char *) ucp->uc_stack.ss_sp) {
\r
56 /* errno = ENOMEM; */
\r
60 /* Set the instruction and the stack pointer */
\r
61 ucp->uc_mcontext.Eip = (unsigned long) func;
\r
62 ucp->uc_mcontext.Esp = (unsigned long) sp - 4;
\r
64 /* Save/Restore the full machine context */
\r
65 ucp->uc_mcontext.ContextFlags = CONTEXT_FULL;
\r
67 /* Copy the arguments */
\r
69 for (i = 0; i < argc; i++) {
\r
78 int swapcontext(ucontext_t * oucp, const ucontext_t * ucp)
\r
81 if ((oucp == NULL) || (ucp == NULL)) {
\r
83 /*errno = EINVAL; */
\r
86 ret = getcontext(oucp);
\r
88 ret = setcontext(ucp);
\r