+++ /dev/null
-/********************************************************************************
- * 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" /* Current file is to be included when cross-compiling,
- but not during native builds. So this include is needed */
-
-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);
- (char*)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;
-}
+++ /dev/null
-/********************************************************************************
- * 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
-
-#ifndef S_SPLINT_S
-# include <windows.h>
-#endif
-
-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 */
+++ /dev/null
-#include "win_thread.h"
-
-#include <string.h>
-#include <stdio.h>
-
-
-static win_threads_t __win_threads = NULL;
-
-static win_threads_t
-__win_threads_instance(void);
-
-static int
-__win_threads_attach(win_thread_t thread);
-
-static int
-__win_threads_detach(win_thread_t thread);
-
-static void
-__win_threads_free(void);
-
-static void
-__win_threads_lock(void);
-
-static void
-__win_threads_unlock(void);
-
-static win_thread_t
-__win_thread_self(void);
-
-static int
-__win_thread_key_attach(win_thread_t thread,win_thread_key_t key);
-
-static int
-__win_thread_key_detach(win_thread_t thread,win_thread_key_t key);
-
-int
-win_thread_create(win_thread_t* thread,pfn_start_routine_t start_routine,void* param){
-
- *thread = xbt_new0(s_win_thread_t,1);
-
- if(!*thread)
- return 1;
-
- (*thread)->keys = xbt_new0(s_win_thread_keys_t,1);
-
- if(!((*thread)->keys)){
- xbt_free(*thread);
- return 1;
- }
-
- (*thread)->handle = CreateThread(NULL,NULL,start_routine,param,0,&((*thread)->id));
-
- if(!((*thread)->handle)){
- xbt_free(*thread);
- *thread = NULL;
- return 1;
- }
-
- __win_threads_attach(*thread);
-
- return 0;
-}
-
-int
-win_thread_exit(win_thread_t* thread,unsigned long exit_code)
-{
- win_thread_key_t cur,next;
- win_thread_t ___thread = *thread;
-
- __win_threads_detach(*thread);
-
- CloseHandle(___thread->handle);
-
- cur = ___thread->keys->front;
-
- while(cur){
-
- next = cur->next;
-
- if(cur->dstr_func){
-
- (*(cur->dstr_func))(win_thread_getspecific(cur));
- win_thread_key_delete(cur);
-
- }
-
- cur = next;
- }
-
- xbt_free(___thread->keys);
- xbt_free(___thread);
- ___thread = NULL;
-
- ExitThread(exit_code);
-
- return 0;
-}
-
-win_thread_t
-win_thread_self(void){
- return __win_thread_self();
-}
-
-int
-win_thread_mutex_init(win_thread_mutex_t* mutex)
-{
-
- *mutex = xbt_new0(s_win_thread_mutex_t,1);
-
- if(!*mutex)
- return 1;
-
- /* initialize the critical section object */
- InitializeCriticalSection(&((*mutex)->lock));
- return 0;
-}
-
-int
-win_thread_mutex_lock(win_thread_mutex_t* mutex){
-
- EnterCriticalSection(&((*mutex)->lock));
-
- return 0;
-}
-
-int
-win_thread_mutex_unlock(win_thread_mutex_t* mutex){
-
- LeaveCriticalSection (&((*mutex)->lock));
- return 0;
-}
-
-int
-win_thread_mutex_destroy(win_thread_mutex_t* mutex){
-
- DeleteCriticalSection(&((*mutex)->lock));
-
- free(*mutex);
- *mutex = NULL;
-
- return 0;
-}
-
-int
-win_thread_cond_init(win_thread_cond_t* cond){
-
- *cond = xbt_new0(s_win_thread_cond_t,1);
-
- if(!*cond)
- return 1;
-
- memset(&((*cond)->waiters_count_lock),0,sizeof(CRITICAL_SECTION));
-
- /* initialize the critical section object */
- InitializeCriticalSection(&((*cond)->waiters_count_lock));
-
- (*cond)->waiters_count = 0;
-
- /* Create an auto-reset event */
- (*cond)->events[SIGNAL] = CreateEvent (NULL, FALSE, FALSE, NULL);
-
- if(!(*cond)->events[SIGNAL]){
- DeleteCriticalSection(&((*cond)->waiters_count_lock));
- free(*cond);
- return 1;
- }
-
- /* Create a manual-reset event. */
- (*cond)->events[BROADCAST] = CreateEvent (NULL, TRUE, FALSE,NULL);
-
- if(!(*cond)->events[BROADCAST]){
-
- DeleteCriticalSection(&((*cond)->waiters_count_lock));
-
- if(!CloseHandle((*cond)->events[SIGNAL]))
- return 1;
-
- free(*cond);
- return 1;
- }
-
- return 0;
-}
-
-int
-win_thread_cond_wait(win_thread_cond_t* cond,win_thread_mutex_t* mutex){
-
- unsigned long wait_result;
- int is_last_waiter;
-
- /* lock the threads counter and increment it */
- EnterCriticalSection (&((*cond)->waiters_count_lock));
- (*cond)->waiters_count++;
- LeaveCriticalSection(&((*cond)->waiters_count_lock));
-
-
- /* unlock the mutex associate with the condition */
- LeaveCriticalSection(&(*mutex)->lock);
-
- /* wait for a signal (broadcast or no) */
- wait_result = WaitForMultipleObjects (2, (*cond)->events, FALSE, INFINITE);
-
- if(WAIT_FAILED == wait_result)
- return 1;
-
- /* we have a signal lock the condition */
- EnterCriticalSection (&((*cond)->waiters_count_lock));
- (*cond)->waiters_count--;
-
- /* it's the last waiter or it's a broadcast ? */
- is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && ((*cond)->waiters_count == 0));
-
- LeaveCriticalSection(&((*cond)->waiters_count_lock));
-
- /* yes it's the last waiter or it's a broadcast
- * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
- * by the système.
- */
- if (is_last_waiter){
-
- if(!ResetEvent ((*cond)->events[BROADCAST]))
- return 1;
- }
-
- /* relock the mutex associated with the condition in accordance with the posix thread specification */
- EnterCriticalSection (&(*mutex)->lock);
-
- return 0;
-}
-
-int
-win_thread_cond_signal(win_thread_cond_t* cond)
-{
- int have_waiters;
-
- EnterCriticalSection (&((*cond)->waiters_count_lock));
- have_waiters = (*cond)->waiters_count > 0;
- LeaveCriticalSection(&((*cond)->waiters_count_lock));
-
- if (have_waiters){
-
- if(!SetEvent((*cond)->events[SIGNAL]))
- return 1;
-
- }
-
- return 0;
-}
-
-int
-win_thread_cond_broadcast(win_thread_cond_t* cond)
-{
- int have_waiters;
-
- EnterCriticalSection (&((*cond)->waiters_count_lock));
- have_waiters = (*cond)->waiters_count > 0;
- LeaveCriticalSection(&((*cond)->waiters_count_lock));
-
- if (have_waiters)
- SetEvent((*cond)->events[BROADCAST]);
-
- return 0;
-}
-
-
-
-int
-win_thread_cond_destroy(win_thread_cond_t* cond)
-{
- int result = 0;
-
- if(!CloseHandle((*cond)->events[SIGNAL]))
- result= 1;
-
- if(!CloseHandle((*cond)->events[BROADCAST]))
- result = 1;
-
- DeleteCriticalSection(&((*cond)->waiters_count_lock));
-
- xbt_free(*cond);
- *cond = NULL;
-
- return result;
-}
-
-void
-win_thread_yield(void)
-{
- Sleep(0);
-}
-
-
-int
-win_thread_key_create(win_thread_key_t* key, pfn_key_dstr_func_t dstr_func)
-{
- *key = xbt_new0(s_win_thread_key_t,1);
-
- if(!(*key))
- return 1;
-
- if(__win_thread_key_attach(__win_thread_self(),*key){
- xbt_free(*key);
- *key = NULL;
- return 1;
- }
-
- (*key)->value = TlsAlloc();
- (*key)->dstr_func = dstr_func;
-
- return 0;
-}
-
-int
-win_thread_key_delete(win_thread_key_t key)
-{
- void* p;
- int success;
-
- if(!key)
- return 1;
-
- if(!TlsFree(key->value))
- return 1;
-
- success = __win_thread_key_detach(__win_thread_self(),key);
-
- xbt_free(key);
-
- return success;
-}
-
-int
-win_thread_setspecific(win_thread_key_t key, const void* p)
-{
- if(!key)
- return 1;
-
- if(!TlsSetValue(key->key,(void*)p))
- return 1;
-
- return 0;
-}
-
-void*
-win_thread_getspecific(win_thread_key_t key)
-{
- if(!key)
- return NULL;
-
- return TlsGetValue(key->value);
-}
-
-
-static win_threads_t
-__win_threads_instance(void){
-
- if(!__win_threads){
- __win_threads = xbt_new0(s_win_threads_t,1);
-
- if(!__win_threads)
- return NULL;
-
- InitializeCriticalSection(&(__win_threads->lock));
- }
-
- return __win_threads;
-}
-
-static int
-__win_threads_attach(win_thread_t thread)
-{
- win_thread_entry_t entry;
- win_threads_t threads;
-
- __win_threads_lock();
-
- threads = __win_threads_instance();
-
- if(!threads){
- __win_threads_unlock();
- return 1;
- }
-
-
- entry = xbt_new0(win_thread_entry_t,1);
-
- if(!entry){
- __win_threads_unlock();
- return 1;
- }
-
- entry->thread = thread;
-
- if(threads->front){
- entry->next = threads->front;
- threads->front->prev = entry;
- }
-
- threads->front = entry;
- threads->size++;
-
- __win_threads_unlock();
- return 0;
-}
-
-static int
-__win_threads_detach(win_thread_t thread)
-{
- win_thread_entry_t cur;
- win_threads_t threads ;
-
-
- __win_threads_lock();
-
- threads = __win_threads_instance();
-
- if(!threads){
- __win_threads_unlock();
- return 1;
- }
-
- cur = threads->front;
-
- while(cur){
-
- if(cur->thread == thread){
-
- if(cur->next)
- cur->next->prev = cur->prev;
-
- if(cur->prev)
- cur->prev->next = cur->next;
-
- xbt_free(cur);
-
- threads->size--;
-
- if(!(threads->size))
- __win_threads_free();
- else
- __win_threads_unlock();
-
- return 0;
- }
-
- cur = cur->next;
- }
-
- __win_threads_unlock();
- return 1;
-}
-
-static win_thread_t
-__win_thread_self(void)
-{
- win_thread_entry_t cur;
- win_threads_t threads ;
- win_thread_t thread;
- unsigned long thread_id;
-
- __win_threads_lock();
-
- thread_id = GetCurrentThreadId();
-
- threads = __win_threads_instance();
-
- if(!threads){
- __win_threads_unlock();
- return NULL;
- }
-
- cur = threads->front;
-
- while(cur){
-
- if(cur->thread->id == thread_id){
-
- thread = cur->thread;
- __win_threads_unlock();
- return thread;
- }
-
- cur = cur->next;
- }
-
- __win_threads_unlock();
- return NULL;
-}
-
-static void
-__win_threads_free(void)
-{
- CRITICAL_SECTION lock;
-
- if(__win_threads){
- lock = __win_threads->lock;
- free(__win_threads);
- __win_threads = NULL;
- LeaveCriticalSection(&lock);
- DeleteCriticalSection(&lock);
- }
-}
-
-static void
-__win_threads_lock(void)
-{
- EnterCriticalSection(&(__win_threads->lock));
-}
-
-static void
-__win_threads_unlock(void)
-{
- LeaveCriticalSection(&(__win_threads->lock));
-}
-
-static int
-__win_thread_key_attach(win_thread_t thread,win_thread_key_t key)
-{
- win_thread_keys_t keys;
-
- keys = thread->keys;
-
- if(!keys)
- return 1;
-
-
- if(keys->front){
- key->next = keys->front;
- keys->front->prev = key;
- }
-
- keys->front = key;
-
- return 0;
-}
-
-static int
-__win_thread_key_detach(win_thread_t thread,win_thread_key_t key)
-{
- win_thread_key_t cur;
- win_thread_keys_t keys ;
-
- keys = thread->keys;
-
- if(!keys)
- return 1;
-
- cur = keys->front;
-
- while(cur){
-
- if(cur == key){
-
- if(cur->next)
- cur->next->prev = cur->prev;
-
- if(cur->prev)
- cur->prev->next = cur->next;
-
- return 0;
- }
-
- cur = cur->next;
- }
-
- return 1;
-}
-
-
-
-
+++ /dev/null
-/*
- * Copyright (c) 2007 Malek CHERIER. All rights reserved.
- */
-
-/* This program is free software; you can redistribute it and/or modify it
- * under the terms of the license (GNU LGPL) which comes with this package.
- */
-
-#ifndef XBT_WIN_THREAD_H
-#define XBT_WIN_THREAD_H
-
-#include <windows.h>
-#include "xbt/sysdep.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* type of function pointeur used */
-typedef DWORD WINAPI (*pfn_start_routine_t)(void*);
-
-typedef struct s_win_thread_key s_win_thread_key_t,* win_thread_key_t;
-typedef struct s_win_thread_keys s_win_thread_keys_t,* win_thread_keys_t;
-
-/*
- * This structure represents a windows thread.
- */
-typedef struct s_win_thread
-{
- HANDLE handle; /* the win thread handle */
- unsigned long id; /* the win thread id */
- win_thread_keys_t keys;
-}s_win_thread_t,* win_thread_t;
-
-struct s_win_thread_keys
-{
- win_thread_key_t* keys;
- win_thread_key_t front;
- win_thread_key_t back;
-};
-
-typedef struct s_win_thread_entry
-{
- HANDLE handle;
- win_thread_t thread;
- win_thread_t next;
- win_thread_t prev;
-}s_win_thread_entry_t,win_thread_entry_t;
-
-typedef struct s_win_threads
-{
- win_thread_entry_t* threads;
- win_thread_entry_t front;
- win_thread_entry_t back;
- unsigned int size;
- CRITICAL_SECTION lock;
-}s_win_threads_t,* win_threads_t;
-
-
-/*
- * This structure simulates a pthread cond.
- */
-
- enum
- {
- SIGNAL = 0,
- BROADCAST = 1,
- MAX_EVENTS = 2
- };
-
-typedef struct s_win_thread_cond
-{
- HANDLE events[MAX_EVENTS];
-
- unsigned int waiters_count; /* the number of waiters */
- CRITICAL_SECTION waiters_count_lock;/* protect access to waiters_count */
-}s_win_thread_cond_t,* win_thread_cond_t;
-
-
-/* type of pointer function used by the
- * win_thread_key_create() function.
- */
-typedef void (*pfn_key_dstr_func_t) (void*);
-
-/*
- * This structure represents a windows key
- */
-struct s_win_thread_key
-{
- unsigned long value; /* the key value */
- pfn_key_dstr_func_t dstr_func; /* the pointer to the destruction function */
- win_thread_key_t next;
- win_thread_key_t prev;
-};
-
-
-/*
- * This structure represents a windows mutext
- * remark : only encapsulate the mutext handle.
- */
-typedef struct s_win_thread_mutex
-{
- CRITICAL_SECTION lock;
-}s_win_thread_mutex_t,* win_thread_mutex_t;
-
-/*
- * Windows thread connected functions.
- */
-
-/*
- * Create a win thread.
- * @param thread The address of the thread to create
- * @param start_routine The thread function.
- * @param param A optional pointer to the thread function parameters.
- * @return If successful the function returns 0. Otherwise the function
- * return 1.
- */
-int
-win_thread_create(win_thread_t* thread, pfn_start_routine_t start_routine,void* param);
-
-/*
- * Terminate the win thread.
- * @param thread An address to the thread to exit.
- * @param exit_code The exit code returned by the thread.
- * @return This function always returns 0;
- */
-int
-win_thread_exit(win_thread_t* thread,unsigned long exit_code);
-
-/*
- * Return the identifier of the current thread.
- */
-win_thread_t
-win_thread_self(void);
-
-/*
- * Windows mutex connected functions;
- */
-
-/*
- * Create a windows mutex.
- * @param mutex The address to the mutex to create.
- * @return If successful the function returns 0. Otherwise
- * the function returns 1.
- */
-int
-win_thread_mutex_init(win_thread_mutex_t* mutex);
-
-/*
- * Lock a windows mutex.
- * @param mutex The address to the mutex to lock.
- * @return If successful the function returns 0.
- * Otherwise the function return 1.
- */
-int
-win_thread_mutex_lock(win_thread_mutex_t* mutex);
-
-/*
- * Unlock a windows mutex.
- * @param mutex The address to the mutex to unlock.
- * @return If successful the function returns 0.
- * Otherwise the function return 1.
- */
-int
-win_thread_mutex_unlock(win_thread_mutex_t* mutex);
-
-/*
- * Destroy a windows mutex.
- * @param mutex The address of the mutex to destroy.
- * @return If successful the function return 0.
- * Otherwise the function returns 1.
- */
-int
-win_thread_mutex_destroy(win_thread_mutex_t* mutex);
-
-/*
- * Condition connected functions.
- */
-
-/*
- * Create a condition.
- * @param cond The address to the condition to create.
- * @return If successful the function returns 0.
- * Otherwise the function returns 1.
- */
-int
-win_thread_cond_init(win_thread_cond_t* cond);
-
-/*
- * Wait about a condition.
- * @param cond The address to the condition to wait about.
- * @param mutex The address to the mutex associated to the condition.
- * @return If successful the function returns 0.
- * Otherwise the function return 1.
- */
-int
-win_thread_cond_wait(win_thread_cond_t* cond,win_thread_mutex_t* mutex);
-
-/*
- * Signal that a condition is verified.
- * @param cond The address of the condition concerned by the signal.
- * @return If successful the function return 0.
- * Otherwise the function return 1.
- */
-int
-win_thread_cond_signal(win_thread_cond_t* cond);
-
-/*
- * Unblock all threads waiting for a condition.
- * @param cond The address of the condition to broadcast.
- * @return If successful the function return 0.
- * Otherwise the function return 1.
- */
-
-int
-win_thread_cond_broadcast(win_thread_cond_t* cond);
-
-/*
- * Destroy a condition.
- * @param The address to the condition to destroy.
- * @return If successful the function returns 0.
- * Otherwise the function return 1.
- */
-int
-win_thread_cond_destroy(win_thread_cond_t* cond);
-
-/*
- * Forces the current thread to relinquish use of its processor.
- */
-void
-win_thread_yield(void);
-
-
-/*
- * windows thread key connected functions
- */
-
-/*
- * Create a windows thread key.
- * @param key The pointer to the key to create.
- * @param dstr_func The pointer to the cleanup function.
- * @return If successful the function returns .Otherwise
- * the function returns 1.
- */
-int
-win_thread_key_create(win_thread_key_t* key, pfn_key_dstr_func_t dstr_func);
-/*
- * Destroy a key.
- * @param key The key to delete.
- * @return If successful the function returns 0.
- * Otherwise the function returns 1.
- */
-int
-win_thread_key_delete(win_thread_key_t key);
-
-/*
- * Update or set the value associated to a key.
- * @param key The key concerned by the operation.
- * @param value The value to set or update.
- * @return If successful the function returns 0.
- * Otherwise the function returns 1.
- */
-int
-win_thread_setspecific(win_thread_key_t key, const void* p);
-
-/*
- * Return the value associated to a key.
- * @param key The key concerned by the operation.
- * @return If successful the function returns the value.
- * Otherwise the function returns NULL.
- */
-void*
-win_thread_getspecific(win_thread_key_t key);
-
-
-
-
-
-#ifdef __cplusplus
-extern }
-#endif
-
-
-#endif /* !XBT_WIN_THREAD_H */
\ No newline at end of file