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
This commit is contained in:
meep-eep
2002-09-20 23:26:06 +00:00
parent 5bf3ca6cd0
commit 95372702ab
6 changed files with 63 additions and 23 deletions
+6 -6
View File
@@ -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);
}
}
@@ -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)
@@ -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);
+12 -5
View File
@@ -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 */
@@ -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 */
+27 -4
View File
@@ -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);
}