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:
@@ -39,7 +39,7 @@ blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr)
|
|||||||
|
|
||||||
img = (TFB_Image *) ((BYTE *) SrcFramePtr + SrcFramePtr->DataOffs);
|
img = (TFB_Image *) ((BYTE *) SrcFramePtr + SrcFramePtr->DataOffs);
|
||||||
|
|
||||||
SDL_mutexP (img->mutex);
|
LockMutex (img->mutex);
|
||||||
|
|
||||||
if (TYPE_GET (_CurFramePtr->TypeIndexAndFlags) == SCREEN_DRAWABLE)
|
if (TYPE_GET (_CurFramePtr->TypeIndexAndFlags) == SCREEN_DRAWABLE)
|
||||||
{
|
{
|
||||||
@@ -172,7 +172,7 @@ blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr)
|
|||||||
dst_img = ((TFB_Image *) ((BYTE *) _CurFramePtr +
|
dst_img = ((TFB_Image *) ((BYTE *) _CurFramePtr +
|
||||||
_CurFramePtr->DataOffs));
|
_CurFramePtr->DataOffs));
|
||||||
|
|
||||||
SDL_mutexP (dst_img->mutex);
|
LockMutex (dst_img->mutex);
|
||||||
|
|
||||||
SDL_SrcRect.x = (short) img->NormalImg->clip_rect.x;
|
SDL_SrcRect.x = (short) img->NormalImg->clip_rect.x;
|
||||||
SDL_SrcRect.y = (short) img->NormalImg->clip_rect.y;
|
SDL_SrcRect.y = (short) img->NormalImg->clip_rect.y;
|
||||||
@@ -191,10 +191,10 @@ blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr)
|
|||||||
&SDL_DstRect
|
&SDL_DstRect
|
||||||
);
|
);
|
||||||
|
|
||||||
SDL_mutexV (dst_img->mutex);
|
UnlockMutex (dst_img->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_mutexV (img->mutex);
|
UnlockMutex (img->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -244,7 +244,7 @@ fillrect_blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr)
|
|||||||
img = ((TFB_Image *) ((BYTE *) _CurFramePtr +
|
img = ((TFB_Image *) ((BYTE *) _CurFramePtr +
|
||||||
_CurFramePtr->DataOffs));
|
_CurFramePtr->DataOffs));
|
||||||
|
|
||||||
SDL_mutexP (img->mutex);
|
LockMutex (img->mutex);
|
||||||
|
|
||||||
SDLRect.x = (short) (pClipRect->corner.x -
|
SDLRect.x = (short) (pClipRect->corner.x -
|
||||||
GetFrameHotX (_CurFramePtr));
|
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_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;
|
TFB_Image *myImage;
|
||||||
|
|
||||||
myImage = (TFB_Image*) HMalloc (sizeof (TFB_Image));
|
myImage = (TFB_Image*) HMalloc (sizeof (TFB_Image));
|
||||||
myImage->mutex = SDL_CreateMutex();
|
myImage->mutex = CreateMutex();
|
||||||
myImage->ScaledImg = NULL;
|
myImage->ScaledImg = NULL;
|
||||||
myImage->Palette = NULL;
|
myImage->Palette = NULL;
|
||||||
|
|
||||||
@@ -373,7 +373,7 @@ TFB_FlushGraphics () // Only call from main thread!!
|
|||||||
|
|
||||||
DC_image = (TFB_Image*) DC.image;
|
DC_image = (TFB_Image*) DC.image;
|
||||||
if (DC_image)
|
if (DC_image)
|
||||||
SDL_mutexP (DC_image->mutex);
|
LockMutex (DC_image->mutex);
|
||||||
|
|
||||||
switch (DC.Type)
|
switch (DC.Type)
|
||||||
{
|
{
|
||||||
@@ -522,8 +522,8 @@ TFB_FlushGraphics () // Only call from main thread!!
|
|||||||
if (DC_image->Palette)
|
if (DC_image->Palette)
|
||||||
HFree (DC_image->Palette);
|
HFree (DC_image->Palette);
|
||||||
|
|
||||||
SDL_mutexV (DC_image->mutex);
|
UnlockMutex (DC_image->mutex);
|
||||||
SDL_DestroyMutex (DC_image->mutex);
|
DestroyMutex (DC_image->mutex);
|
||||||
|
|
||||||
HFree (DC_image);
|
HFree (DC_image);
|
||||||
DC_image = 0;
|
DC_image = 0;
|
||||||
@@ -531,7 +531,7 @@ TFB_FlushGraphics () // Only call from main thread!!
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (DC_image)
|
if (DC_image)
|
||||||
SDL_mutexV (DC_image->mutex);
|
UnlockMutex (DC_image->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (livelock_deterrence)
|
if (livelock_deterrence)
|
||||||
|
|||||||
@@ -20,13 +20,13 @@
|
|||||||
#define SDL_COMMON_H
|
#define SDL_COMMON_H
|
||||||
|
|
||||||
#include "SDL.h"
|
#include "SDL.h"
|
||||||
#include "SDL_thread.h"
|
|
||||||
#include "SDL_image.h"
|
#include "SDL_image.h"
|
||||||
|
|
||||||
#include "libs/graphics/gfxintrn.h"
|
#include "libs/graphics/gfxintrn.h"
|
||||||
#include "libs/input/inpintrn.h"
|
#include "libs/input/inpintrn.h"
|
||||||
#include "libs/graphics/gfx_common.h"
|
#include "libs/graphics/gfx_common.h"
|
||||||
#include "libs/input/sdl/input.h"
|
#include "libs/input/sdl/input.h"
|
||||||
|
#include "libs/threadlib.h"
|
||||||
|
|
||||||
extern SDL_Surface *SDL_Video;
|
extern SDL_Surface *SDL_Video;
|
||||||
extern SDL_Surface *SDL_Screen;
|
extern SDL_Surface *SDL_Screen;
|
||||||
@@ -45,9 +45,9 @@ typedef struct tfb_image
|
|||||||
SDL_Surface *ScaledImg;
|
SDL_Surface *ScaledImg;
|
||||||
SDL_Color *Palette;
|
SDL_Color *Palette;
|
||||||
int scale;
|
int scale;
|
||||||
SDL_mutex *mutex;
|
Mutex mutex;
|
||||||
UBYTE pad[sizeof(TFB_ImageStruct)-sizeof(SDL_Surface*)-sizeof(SDL_Surface*)-
|
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_Image *TFB_LoadImage (SDL_Surface *img);
|
TFB_Image *TFB_LoadImage (SDL_Surface *img);
|
||||||
|
|||||||
@@ -53,6 +53,10 @@
|
|||||||
# endif
|
# endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
extern void InitThreadSystem (void);
|
||||||
|
extern void UnInitThreadSystem (void);
|
||||||
|
|
||||||
|
|
||||||
typedef int (*ThreadFunction) (void *);
|
typedef int (*ThreadFunction) (void *);
|
||||||
|
|
||||||
typedef struct Thread {
|
typedef struct Thread {
|
||||||
@@ -68,11 +72,6 @@ typedef struct Thread {
|
|||||||
#endif
|
#endif
|
||||||
} *Thread;
|
} *Thread;
|
||||||
|
|
||||||
typedef void *Semaphore;
|
|
||||||
|
|
||||||
extern void InitThreadSystem (void);
|
|
||||||
extern void UnInitThreadSystem (void);
|
|
||||||
|
|
||||||
#ifdef THREAD_NAMES
|
#ifdef THREAD_NAMES
|
||||||
extern Thread CreateThreadAux (ThreadFunction func, void *data,
|
extern Thread CreateThreadAux (ThreadFunction func, void *data,
|
||||||
SDWORD stackSize, const char *name);
|
SDWORD stackSize, const char *name);
|
||||||
@@ -90,6 +89,7 @@ extern void SleepThreadUntil (TimeCount wakeTime);
|
|||||||
extern void TaskSwitch (void);
|
extern void TaskSwitch (void);
|
||||||
extern void WaitThread (Thread thread, int *status);
|
extern void WaitThread (Thread thread, int *status);
|
||||||
|
|
||||||
|
typedef void *Semaphore;
|
||||||
extern Semaphore CreateSemaphore (DWORD initial);
|
extern Semaphore CreateSemaphore (DWORD initial);
|
||||||
extern void DestroySemaphore (Semaphore sem);
|
extern void DestroySemaphore (Semaphore sem);
|
||||||
extern int SetSemaphore (Semaphore sem);
|
extern int SetSemaphore (Semaphore sem);
|
||||||
@@ -100,5 +100,12 @@ extern void ClearSemaphore (Semaphore sem);
|
|||||||
extern void PrintThreadsStats (void);
|
extern void PrintThreadsStats (void);
|
||||||
#endif /* PROFILE_THREADS */
|
#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 */
|
#endif /* _THREADLIB_H */
|
||||||
|
|
||||||
|
|||||||
@@ -67,5 +67,15 @@ extern int SDLWrapper_TimeoutSetSemaphore (Semaphore sem,
|
|||||||
#define NativeClearSemaphore(sem) \
|
#define NativeClearSemaphore(sem) \
|
||||||
SDL_SemPost ((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 */
|
#endif /* _SDLTHREAD_H */
|
||||||
|
|
||||||
|
|||||||
@@ -222,10 +222,11 @@ CreateThreadAux (ThreadFunction func, void *data, SDWORD stackSize
|
|||||||
/* 17 Sep: Added a TFB_BatchReset call. If a thread is killed while
|
/* 17 Sep: Added a TFB_BatchReset call. If a thread is killed while
|
||||||
* batching stuff, we don't want this to freeze the game.
|
* batching stuff, we don't want this to freeze the game.
|
||||||
* Better safe than sorry! --Michael
|
* Better safe than sorry! --Michael
|
||||||
|
* TEMPORARY (TODO: handle decently)
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
KillThread (Thread thread)
|
KillThread (Thread thread)
|
||||||
{
|
{
|
||||||
TFB_BatchReset ();
|
TFB_BatchReset ();
|
||||||
NativeKillThread (thread->native);
|
NativeKillThread (thread->native);
|
||||||
#ifdef DEBUG_THREADS
|
#ifdef DEBUG_THREADS
|
||||||
@@ -306,9 +307,7 @@ DestroySemaphore (Semaphore sem)
|
|||||||
int
|
int
|
||||||
SetSemaphore (Semaphore sem)
|
SetSemaphore (Semaphore sem)
|
||||||
{
|
{
|
||||||
int result;
|
return NativeSetSemaphore ((NativeSemaphore) sem);
|
||||||
result = NativeSetSemaphore ((NativeSemaphore) sem);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@@ -330,4 +329,28 @@ ClearSemaphore (Semaphore sem)
|
|||||||
NativeClearSemaphore ((NativeSemaphore) 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user