From 96e1f955a0f1de2040f47159ad94f1f883893db1 Mon Sep 17 00:00:00 2001 From: mcmartin Date: Sat, 13 Sep 2003 07:10:43 +0000 Subject: [PATCH] Control memory with a mutex instead of a semaphore Mutex locks, unlike semaphore locks, cannot fail, but they must stay within their thread. The memory lock is unlocked at all procedure boundaries; thus, it is safe to use a normal mutex. git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@1225 8092fc87-c524-0410-9efc-e669fe64eaf9 --- sc2/src/sc2code/libs/memory/w_memlib.c | 42 +++++++++++++------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/sc2/src/sc2code/libs/memory/w_memlib.c b/sc2/src/sc2code/libs/memory/w_memlib.c index 757687311..b4ac0c440 100644 --- a/sc2/src/sc2code/libs/memory/w_memlib.c +++ b/sc2/src/sc2code/libs/memory/w_memlib.c @@ -43,7 +43,7 @@ BOOLEAN leak_debug; int leak_idx = -1; int leak_size = -1; #endif -Semaphore _MemorySem; +static Mutex _MemoryLock; #define MAX_EXTENTS 100000 @@ -277,7 +277,7 @@ mem_allocate (MEM_SIZE coreSize, MEM_FLAGS flags, MEM_PRIORITY priority, { szMemoryNode *node; - SetSemaphore (_MemorySem); + LockMutex (_MemoryLock); #ifdef MEM_DEBUG CheckMemory(); @@ -307,11 +307,11 @@ mem_allocate (MEM_SIZE coreSize, MEM_FLAGS flags, MEM_PRIORITY priority, node = node; #endif /* LEAK_DEBUG */ - ClearSemaphore (_MemorySem); + UnlockMutex (_MemoryLock); return (node->handle); } - ClearSemaphore (_MemorySem); + UnlockMutex (_MemoryLock); (void) priority; /* Satisfying compiler (unused parameter) */ (void) usage; /* Satisfying compiler (unused parameter) */ return (0); @@ -342,13 +342,13 @@ mem_allocate (MEM_SIZE coreSize, MEM_FLAGS flags, MEM_PRIORITY priority, MEM_SIZE mem_get_size (MEM_HANDLE h) { - SetSemaphore (_MemorySem); + LockMutex (_MemoryLock); if (h > 0 && h <= MAX_EXTENTS && extents[h - 1].handle == h) { - ClearSemaphore (_MemorySem); + UnlockMutex (_MemoryLock); return ((int)extents[h - 1].size); } - ClearSemaphore (_MemorySem); + UnlockMutex (_MemoryLock); return (0); } @@ -382,8 +382,8 @@ mem_init (void) { int i; - _MemorySem = CreateSemaphore (1, "Memory"); - SetSemaphore (_MemorySem); + _MemoryLock = CreateMutex (); + LockMutex (_MemoryLock); freeListHead = &extents[0]; for (i=0; i= MAX_EXTENTS) @@ -501,11 +501,11 @@ mem_release(MEM_HANDLE h) extents[h].handle = -1; extents[h].next = freeListHead; freeListHead = &extents[h]; - ClearSemaphore (_MemorySem); + UnlockMutex (_MemoryLock); return TRUE; } - ClearSemaphore (_MemorySem); + UnlockMutex (_MemoryLock); return FALSE; } @@ -535,16 +535,16 @@ mem_release(MEM_HANDLE h) void * mem_simple_access(MEM_HANDLE h) { - SetSemaphore (_MemorySem); + LockMutex (_MemoryLock); if (h > 0 && h <= MAX_EXTENTS && extents[h - 1].handle == h) { ++extents[h - 1].refcount; - ClearSemaphore (_MemorySem); + UnlockMutex (_MemoryLock); return (extents[h - 1].memory); } - ClearSemaphore (_MemorySem); + UnlockMutex (_MemoryLock); return (0); } @@ -574,16 +574,16 @@ mem_simple_access(MEM_HANDLE h) MEM_BOOL mem_simple_unaccess(MEM_HANDLE h) { - SetSemaphore (_MemorySem); + LockMutex (_MemoryLock); if (h > 0 && h <= MAX_EXTENTS && extents[h - 1].handle == h) { if (extents[h - 1].refcount) --extents[h - 1].refcount; - ClearSemaphore (_MemorySem); + UnlockMutex (_MemoryLock); return (1); } - ClearSemaphore (_MemorySem); + UnlockMutex (_MemoryLock); return (0); }