Mercurial > repos > rhope
view vis_threading.h @ 151:b75ec364cecc
Fix conflict between name of proviling variable and time function in C stdlib
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 05 Dec 2010 18:04:19 -0500 |
parents | 76568becd6d6 |
children |
line wrap: on
line source
#ifndef _VIS_THREADING_H_ #define _VIS_THREADING_H_ #ifndef COMPILE_THREADS #ifdef NO_THREADS #define COMPILE_THREADS 1 #else #define COMPILE_THREADS 4 #endif #endif #if COMPILE_THREADS > 1 #ifdef WIN32 #include "windows.h" /* __inline void VIS_EnterCriticalSectionImpl(short * cs) { int tmp = (int)cs; __asm { mov ebx, tmp CriticalLoop: bts [ebx], 0 jnc End } Sleep(0); __asm { jmp CriticalLoop } End: return; }*/ extern int lock_fail_counter; extern int lock_counter; extern int spin_counter; volatile __inline void VIS_EnterCriticalSectionImpl(short * cs) { int tmp = (int)cs; //int spins = 0; __asm { mov ebx, tmp CriticalLoop: mov ax, 1 xchg ax, [ebx] test ax, ax jz End } //++spins; Sleep(0); __asm { jmp CriticalLoop } End: /* if(spins > 0) { tmp = (int)(&lock_fail_counter); __asm { mov ebx, tmp lock inc dword ptr [ebx] } tmp = (int)(&spin_counter); __asm { mov ebx, tmp mov eax, spins lock xadd dword ptr [ebx], eax } } tmp = (int)(&lock_counter); __asm { mov ebx, tmp lock inc dword ptr [ebx] }*/ return; } volatile __inline void VIS_LeaveCriticalSectionImpl(short * cs) { int tmp = (int)cs; __asm { mov ebx, tmp mov ax, 0 xchg ax, [ebx] } } #else /* #include <unistd.h> volatile inline void VIS_EnterCriticalSectionImpl(short * cs) { int code; VIS_EnterStart: asm( "movw $1, %%ax\n\t" "xchg %%ax, (%1)\n\t" "test %%ax, %%ax\n\t" "jz VIS_EnterEnd\n\t" "movl $1, %0\n\t" "jmp VIS_EnterCont\n" "VIS_EnterEnd:\n\t" "movl $0, %0\n" "VIS_EnterCont:": "=r"(code): "r"(cs): "%ax"); if(!code) return; sleep(0); goto VIS_EnterStart; } volatile inline void VIS_LeaveCriticalSectionImpl(short * cs) { asm( "movw $0, %%ax\n\t" "mfence\n\t" "xchg %%ax, (%0)":: "r"(cs): "%ax"); }*/ #ifdef CPLUSPLUS extern "C" { #endif volatile void VIS_EnterCriticalSectionImpl(short * cs); volatile void VIS_LeaveCriticalSectionImpl(short * cs); #ifdef CPLUSPLUS } #endif #endif //Win32 #ifdef USE_OS_PRIMITIVES #ifdef WIN32 //TODO fill this in #else #ifdef SYLLABLE //TODO fill this in #else #include <pthread.h> #define VIS_CRITICAL_SECTION(cs) pthread_mutex_t cs; #define VIS_EXTERN_CRITICAL_SECTION(cs) extern pthread_mutex_t cs; #define VIS_InitializeCriticalSection(cs) pthread_mutex_init(&(cs), NULL) #define VIS_EnterCriticalSection(cs) pthread_mutex_lock(&(cs)) #define VIS_LeaveCriticalSection(cs) pthread_mutex_unlock(&(cs)) #define VIS_DeleteCriticalSection(cs) pthread_mutex_destroy(&(cs)) #endif //SYLLABLE #endif //WIN32 #else #define VIS_CRITICAL_SECTION(cs) short cs;//CRITICAL_SECTION cs;// #define VIS_EXTERN_CRITICAL_SECTION(cs) extern short cs; #define VIS_InitializeCriticalSection(cs) cs = 0 #define VIS_EnterCriticalSection(cs) VIS_EnterCriticalSectionImpl(&(cs)) #define VIS_LeaveCriticalSection(cs) cs = 0//VIS_LeaveCriticalSectionImpl(&(cs)) #define VIS_DeleteCriticalSection(cs) #endif/* #define VIS_InitializeCriticalSection(cs) InitializeCriticalSectionAndSpinCount(&(cs),0x400); #define VIS_EnterCriticalSection(cs) EnterCriticalSection(&(cs)) #define VIS_LeaveCriticalSection(cs) LeaveCriticalSection(&(cs)) #define VIS_DeleteCriticalSection(cs) DeleteCriticalSection(&(cs))*/ #ifdef WIN32 #define VIS_Event(evt) HANDLE evt; #define VIS_CreateEvent(evt) evt = CreateEvent(NULL, TRUE, FALSE, NULL) #define VIS_DestroyEvent(evt) CloseHandle(evt) #define VIS_SetEvent(evt) SetEvent(evt) #define VIS_ResetEvent(evt) ResetEvent(evt) #define VIS_WaitEvent(evt) WaitForSingleObject(evt, INFINITE) #else #ifdef SYLLABLE //TODO fill this in #define VIS_Event(evt) #define VIS_CreateEvent(evt) #define VIS_DestroyEvent(evt) #define VIS_SetEvent(evt) #define VIS_ResetEvent(evt) #define VIS_WaitEvent(evt) sleep(0) #else #define VIS_Event(evt) pthread_cond_t evt; pthread_mutex_t evt##_mutex; #define VIS_CreateEvent(evt) pthread_cond_init(&(evt), NULL); pthread_mutex_init(&(evt##_mutex),NULL) #define VIS_DestroyEvent(evt) pthread_cond_destroy(&(evt)); pthread_mutex_destroy(&(evt##_mutex)) #define VIS_SetEvent(evt) pthread_mutex_lock(&(evt##_mutex)); pthread_cond_signal(&(evt)); pthread_mutex_unlock(&evt##_mutex); #define VIS_ResetEvent(evt) #define VIS_WaitEvent(evt) pthread_mutex_lock(&(evt##_mutex)); pthread_cond_wait(&(evt), &(evt##_mutex)); pthread_mutex_unlock(&evt##_mutex) #endif //SYLLABLE #endif //WIN32 #ifdef WIN32 #define VIS_NewThread(func, data) CreateThread(NULL, 0, func, data, 0, NULL) #else #ifdef SYLLABLE #define VIS_NewThread(func, data) resume_thread(spawn_thread("vis_worker", func, 1, 0, data)); #else #include <pthread.h> pthread_t pid; #define VIS_NewThread(func, data) pthread_create(&pid, NULL, func, data); #endif #endif #else #define VIS_CRITICAL_SECTION(cs) #define VIS_EXTERN_CRITICAL_SECTION(cs) #define VIS_InitializeCriticalSection(cs) #define VIS_EnterCriticalSection(cs) #define VIS_LeaveCriticalSection(cs) #define VIS_DeleteCriticalSection(cs) #define VIS_Event(evt) #define VIS_CreateEvent(evt) #define VIS_DestroyEvent(evt) #define VIS_SetEvent(evt) #define VIS_WaitEvent(evt) #define VIS_ResetEvent(evt) #define VIS_NewThread(func, data) #endif //COMPILE_THREADS > 1 #endif //_VIS_THREADING_H_