From 95372702ab907f1b62f5eb940a8ce4baaafd74e0 Mon Sep 17 00:00:00 2001 From: meep-eep Date: Fri, 20 Sep 2002 23:26:06 +0000 Subject: [PATCH] Added mutexes to threadlib and changed existing code to use those. git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@53 8092fc87-c524-0410-9efc-e669fe64eaf9 --- sc2/src/sc2code/libs/graphics/sdl/3do_blt.c | 12 +++---- .../sc2code/libs/graphics/sdl/sdl_common.c | 10 +++--- .../sc2code/libs/graphics/sdl/sdl_common.h | 6 ++-- sc2/src/sc2code/libs/threadlib.h | 17 +++++++--- sc2/src/sc2code/libs/threads/sdl/sdlthreads.h | 10 ++++++ sc2/src/sc2code/libs/threads/thrcommon.c | 31 ++++++++++++++++--- 6 files changed, 63 insertions(+), 23 deletions(-) diff --git a/sc2/src/sc2code/libs/graphics/sdl/3do_blt.c b/sc2/src/sc2code/libs/graphics/sdl/3do_blt.c index 062e8b1af..17103428d 100644 --- a/sc2/src/sc2code/libs/graphics/sdl/3do_blt.c +++ b/sc2/src/sc2code/libs/graphics/sdl/3do_blt.c @@ -39,7 +39,7 @@ blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr) img = (TFB_Image *) ((BYTE *) SrcFramePtr + SrcFramePtr->DataOffs); - SDL_mutexP (img->mutex); + LockMutex (img->mutex); if (TYPE_GET (_CurFramePtr->TypeIndexAndFlags) == SCREEN_DRAWABLE) { @@ -172,7 +172,7 @@ blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr) dst_img = ((TFB_Image *) ((BYTE *) _CurFramePtr + _CurFramePtr->DataOffs)); - SDL_mutexP (dst_img->mutex); + LockMutex (dst_img->mutex); SDL_SrcRect.x = (short) img->NormalImg->clip_rect.x; SDL_SrcRect.y = (short) img->NormalImg->clip_rect.y; @@ -191,10 +191,10 @@ blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr) &SDL_DstRect ); - SDL_mutexV (dst_img->mutex); + UnlockMutex (dst_img->mutex); } - SDL_mutexV (img->mutex); + UnlockMutex (img->mutex); } static void @@ -244,7 +244,7 @@ fillrect_blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr) img = ((TFB_Image *) ((BYTE *) _CurFramePtr + _CurFramePtr->DataOffs)); - SDL_mutexP (img->mutex); + LockMutex (img->mutex); SDLRect.x = (short) (pClipRect->corner.x - GetFrameHotX (_CurFramePtr)); @@ -255,7 +255,7 @@ fillrect_blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr) SDL_FillRect (img->NormalImg, &SDLRect, SDL_MapRGB (img->NormalImg->format, r, g, b)); - SDL_mutexV (img->mutex); + UnlockMutex (img->mutex); } } diff --git a/sc2/src/sc2code/libs/graphics/sdl/sdl_common.c b/sc2/src/sc2code/libs/graphics/sdl/sdl_common.c index afea0ec30..7c409e0ba 100644 --- a/sc2/src/sc2code/libs/graphics/sdl/sdl_common.c +++ b/sc2/src/sc2code/libs/graphics/sdl/sdl_common.c @@ -132,7 +132,7 @@ TFB_LoadImage (SDL_Surface *img) TFB_Image *myImage; myImage = (TFB_Image*) HMalloc (sizeof (TFB_Image)); - myImage->mutex = SDL_CreateMutex(); + myImage->mutex = CreateMutex(); myImage->ScaledImg = NULL; myImage->Palette = NULL; @@ -373,7 +373,7 @@ TFB_FlushGraphics () // Only call from main thread!! DC_image = (TFB_Image*) DC.image; if (DC_image) - SDL_mutexP (DC_image->mutex); + LockMutex (DC_image->mutex); switch (DC.Type) { @@ -522,8 +522,8 @@ TFB_FlushGraphics () // Only call from main thread!! if (DC_image->Palette) HFree (DC_image->Palette); - SDL_mutexV (DC_image->mutex); - SDL_DestroyMutex (DC_image->mutex); + UnlockMutex (DC_image->mutex); + DestroyMutex (DC_image->mutex); HFree (DC_image); DC_image = 0; @@ -531,7 +531,7 @@ TFB_FlushGraphics () // Only call from main thread!! } if (DC_image) - SDL_mutexV (DC_image->mutex); + UnlockMutex (DC_image->mutex); } if (livelock_deterrence) diff --git a/sc2/src/sc2code/libs/graphics/sdl/sdl_common.h b/sc2/src/sc2code/libs/graphics/sdl/sdl_common.h index 725b1494a..3db82db64 100644 --- a/sc2/src/sc2code/libs/graphics/sdl/sdl_common.h +++ b/sc2/src/sc2code/libs/graphics/sdl/sdl_common.h @@ -20,13 +20,13 @@ #define SDL_COMMON_H #include "SDL.h" -#include "SDL_thread.h" #include "SDL_image.h" #include "libs/graphics/gfxintrn.h" #include "libs/input/inpintrn.h" #include "libs/graphics/gfx_common.h" #include "libs/input/sdl/input.h" +#include "libs/threadlib.h" extern SDL_Surface *SDL_Video; extern SDL_Surface *SDL_Screen; @@ -45,9 +45,9 @@ typedef struct tfb_image SDL_Surface *ScaledImg; SDL_Color *Palette; int scale; - SDL_mutex *mutex; + Mutex mutex; UBYTE pad[sizeof(TFB_ImageStruct)-sizeof(SDL_Surface*)-sizeof(SDL_Surface*)- - sizeof(SDL_Color*)-sizeof(int)-sizeof(SDL_mutex*)]; + sizeof(SDL_Color*)-sizeof(int)-sizeof(Mutex)]; } TFB_Image; TFB_Image *TFB_LoadImage (SDL_Surface *img); diff --git a/sc2/src/sc2code/libs/threadlib.h b/sc2/src/sc2code/libs/threadlib.h index 293bfa4d7..dd152d3c2 100644 --- a/sc2/src/sc2code/libs/threadlib.h +++ b/sc2/src/sc2code/libs/threadlib.h @@ -53,6 +53,10 @@ # endif #endif +extern void InitThreadSystem (void); +extern void UnInitThreadSystem (void); + + typedef int (*ThreadFunction) (void *); typedef struct Thread { @@ -68,11 +72,6 @@ typedef struct Thread { #endif } *Thread; -typedef void *Semaphore; - -extern void InitThreadSystem (void); -extern void UnInitThreadSystem (void); - #ifdef THREAD_NAMES extern Thread CreateThreadAux (ThreadFunction func, void *data, SDWORD stackSize, const char *name); @@ -90,6 +89,7 @@ extern void SleepThreadUntil (TimeCount wakeTime); extern void TaskSwitch (void); extern void WaitThread (Thread thread, int *status); +typedef void *Semaphore; extern Semaphore CreateSemaphore (DWORD initial); extern void DestroySemaphore (Semaphore sem); extern int SetSemaphore (Semaphore sem); @@ -100,5 +100,12 @@ extern void ClearSemaphore (Semaphore sem); extern void PrintThreadsStats (void); #endif /* PROFILE_THREADS */ +typedef void *Mutex; +extern Mutex CreateMutex (void); +extern void DestroyMutex (Mutex sem); +extern int LockMutex (Mutex sem); +extern void UnlockMutex (Mutex sem); + + #endif /* _THREADLIB_H */ diff --git a/sc2/src/sc2code/libs/threads/sdl/sdlthreads.h b/sc2/src/sc2code/libs/threads/sdl/sdlthreads.h index cf41c4f74..d5cb5d496 100644 --- a/sc2/src/sc2code/libs/threads/sdl/sdlthreads.h +++ b/sc2/src/sc2code/libs/threads/sdl/sdlthreads.h @@ -67,5 +67,15 @@ extern int SDLWrapper_TimeoutSetSemaphore (Semaphore sem, #define NativeClearSemaphore(sem) \ SDL_SemPost ((sem)) +typedef SDL_mutex *NativeMutex; +#define NativeCreateMutex() \ + SDL_CreateMutex () +#define NativeDestroyMutex(mutex) \ + SDL_DestroyMutex ((mutex)) +#define NativeLockMutex(mutex) \ + SDL_mutexP ((mutex)) +#define NativeUnlockMutex(mutex) \ + SDL_mutexV ((mutex)) + #endif /* _SDLTHREAD_H */ diff --git a/sc2/src/sc2code/libs/threads/thrcommon.c b/sc2/src/sc2code/libs/threads/thrcommon.c index 55ae9e152..5e8b98610 100644 --- a/sc2/src/sc2code/libs/threads/thrcommon.c +++ b/sc2/src/sc2code/libs/threads/thrcommon.c @@ -222,10 +222,11 @@ CreateThreadAux (ThreadFunction func, void *data, SDWORD stackSize /* 17 Sep: Added a TFB_BatchReset call. If a thread is killed while * batching stuff, we don't want this to freeze the game. * Better safe than sorry! --Michael + * TEMPORARY (TODO: handle decently) */ void KillThread (Thread thread) -{ +{ TFB_BatchReset (); NativeKillThread (thread->native); #ifdef DEBUG_THREADS @@ -306,9 +307,7 @@ DestroySemaphore (Semaphore sem) int SetSemaphore (Semaphore sem) { - int result; - result = NativeSetSemaphore ((NativeSemaphore) sem); - return result; + return NativeSetSemaphore ((NativeSemaphore) sem); } int @@ -330,4 +329,28 @@ ClearSemaphore (Semaphore sem) NativeClearSemaphore ((NativeSemaphore) sem); } +Mutex +CreateMutex () +{ + return (Mutex) NativeCreateMutex (); +} + +void +DestroyMutex (Mutex sem) +{ + NativeDestroyMutex ((NativeMutex) sem); +} + +int +LockMutex (Mutex sem) +{ + return NativeLockMutex ((NativeMutex) sem); +} + +void +UnlockMutex (Mutex sem) +{ + NativeUnlockMutex ((NativeMutex) sem); +} +