Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add the ucontext reimplementation for windows from Panagiotis
authormquinson <mquinson@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Wed, 15 Dec 2004 22:48:55 +0000 (22:48 +0000)
committermquinson <mquinson@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Wed, 15 Dec 2004 22:48:55 +0000 (22:48 +0000)
git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@644 48e7efb5-ca39-0410-a469-dd3cf9ba447f

src/Makefile.am
src/xbt/context_win32.c [new file with mode: 0644]
src/xbt/context_win32.h [new file with mode: 0644]

index 15225a0..c10bcf2 100644 (file)
@@ -22,7 +22,7 @@ EXTRA_DIST= \
         xbt/dict_private.h \
         xbt/heap_private.h \
         xbt/fifo_private.h \
-        xbt/context_private.h \
+        xbt/context_private.h xbt/context_win32.h xbt/context_win32.c\
         \
        surf/maxmin_private.h \
        surf/trace_mgr_private.h \
diff --git a/src/xbt/context_win32.c b/src/xbt/context_win32.c
new file mode 100644 (file)
index 0000000..aa8b55d
--- /dev/null
@@ -0,0 +1,92 @@
+/********************************************************************************
+ * Copyright (c) 2003, 2004                                                     *
+ * Panagiotis E. Hadjidoukas   (peh@hpclab.ceid.upatras.gr)                    *
+ * HPCLab - University of Patras. All Rights Reserved.                          *
+ * Unix ucontext_t on Windows Operating System, May 2004 (revision 1)           *
+ *                                                                              *
+ * The author disclaims all warranties with regard to this software including   *
+ * all implied warranties of merchantability and fitness for a particular       *
+ * purpose. In no event shall HPCLab be liable for any special, indirect,       *
+ * or consequential damages or any damages whatsoever resulting from            *
+ * loss of use, data or profits, whether in action of contract negligence,      *
+ * or other tortious action, arising out of or in connection with the use       *
+ * or performance of this software.                                             *
+ ********************************************************************************/
+
+/* The original author granted me (Martin Quinson) to redistribute this work 
+   under the LGPL licence, what I here do. */
+
+/*#include "context_win32.h" Not needed since this file is to be included*/
+
+int getcontext(ucontext_t *ucp)
+{
+       int ret;
+
+       /* Retrieve the full machine context */
+       ucp->uc_mcontext.ContextFlags = CONTEXT_FULL;
+       ret = GetThreadContext(GetCurrentThread(), &ucp->uc_mcontext);
+
+       return (ret == 0) ? -1: 0;
+}
+
+int setcontext(const ucontext_t *ucp)
+{
+       int ret;
+       
+       /* Restore the full machine context (already set) */
+       ret = SetThreadContext(GetCurrentThread(), &ucp->uc_mcontext);
+
+       return (ret == 0) ? -1: 0;
+}
+
+int makecontext(ucontext_t *ucp, void (*func)(), int argc, ...)
+{
+       int i;
+    va_list ap;
+       char *sp;
+
+       /* Stack grows down */
+       sp = (char *) (size_t) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size;     
+
+       /* Reserve stack space for the arguments (maximum possible: argc*(8 bytes per argument)) */
+       sp -= argc*8;
+
+       if ( sp < (char *)ucp->uc_stack.ss_sp) {
+               /* errno = ENOMEM;*/
+               return -1;
+       }
+
+       /* Set the instruction and the stack pointer */
+       ucp->uc_mcontext.Eip = (unsigned long) func;
+       ucp->uc_mcontext.Esp = (unsigned long) sp - 4;
+
+       /* Save/Restore the full machine context */
+       ucp->uc_mcontext.ContextFlags = CONTEXT_FULL;
+
+       /* Copy the arguments */
+       va_start (ap, argc);
+       for (i=0; i<argc; i++) {
+               memcpy(sp, ap, 8);
+               ap +=8;
+               sp += 8;
+       }
+       va_end(ap);
+
+       return 0;
+}
+
+int swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
+{
+       int ret;
+
+       if ((oucp == NULL) || (ucp == NULL)) {
+               /*errno = EINVAL;*/
+               return -1;
+       }
+
+       ret = getcontext(oucp);
+       if (ret == 0) {
+               ret = setcontext(ucp);
+       }
+       return ret;
+}
diff --git a/src/xbt/context_win32.h b/src/xbt/context_win32.h
new file mode 100644 (file)
index 0000000..fa5fa05
--- /dev/null
@@ -0,0 +1,47 @@
+/********************************************************************************
+ * Copyright (c) 2003, 2004                                                     *
+ * Panagiotis E. Hadjidoukas   (peh@hpclab.ceid.upatras.gr)                    *
+ * HPCLab - University of Patras. All Rights Reserved.                          *
+ * Unix ucontext_t on Windows Operating System, May 2004 (revision 1)           *
+ *                                                                              *
+ * The author disclaims all warranties with regard to this software including   *
+ * all implied warranties of merchantability and fitness for a particular       *
+ * purpose. In no event shall HPCLab be liable for any special, indirect,       *
+ * or consequential damages or any damages whatsoever resulting from            *
+ * loss of use, data or profits, whether in action of contract negligence,      *
+ * or other tortious action, arising out of or in connection with the use       *
+ * or performance of this software.                                             *
+ ********************************************************************************/
+
+/* The original author granted me (Martin Quinson) to redistribute this work 
+   under the LGPL licence, what I here do. */
+
+#ifndef UCONTEXT_H
+#define UCONTEXT_H
+
+#include <windows.h>
+
+typedef struct __stack {
+       void *ss_sp;
+       size_t ss_size;
+       int ss_flags;
+} stack_t;
+
+typedef CONTEXT mcontext_t;
+typedef unsigned long __sigset_t;
+
+typedef struct __ucontext {
+       unsigned long int       uc_flags;
+       struct __ucontext       *uc_link;
+       stack_t                         uc_stack;
+       mcontext_t                      uc_mcontext; 
+       __sigset_t                      uc_sigmask;
+} ucontext_t;
+
+
+int getcontext(ucontext_t *ucp);
+int setcontext(const ucontext_t *ucp);
+int makecontext(ucontext_t *, void (*)(), int, ...);
+int swapcontext(ucontext_t *, const ucontext_t *);
+
+#endif /* UCONTEXT_H */