|
@@ -22,25 +22,31 @@
|
|
|
|
|
|
#include "platformX86UNIX/platformX86UNIX.h"
|
|
#include "platformX86UNIX/platformX86UNIX.h"
|
|
#include "platform/threads/semaphore.h"
|
|
#include "platform/threads/semaphore.h"
|
|
-// Instead of that mess that was here before, lets use the SDL lib to deal
|
|
|
|
-// with the semaphores.
|
|
|
|
-
|
|
|
|
-#include <SDL/SDL.h>
|
|
|
|
-#include <SDL/SDL_thread.h>
|
|
|
|
|
|
+//MGT: converted SDL to OS semaphores to remove dependency on SDL in dedicated server
|
|
|
|
+#include <unistd.h>
|
|
|
|
+#include <sys/types.h>
|
|
|
|
+#include <errno.h>
|
|
|
|
+#include <semaphore.h>
|
|
|
|
+#include <time.h>
|
|
|
|
|
|
struct PlatformSemaphore
|
|
struct PlatformSemaphore
|
|
{
|
|
{
|
|
- SDL_sem *semaphore;
|
|
|
|
|
|
+ sem_t semaphore;
|
|
|
|
+ bool initialized;
|
|
|
|
|
|
PlatformSemaphore(S32 initialCount)
|
|
PlatformSemaphore(S32 initialCount)
|
|
{
|
|
{
|
|
- semaphore = SDL_CreateSemaphore(initialCount);
|
|
|
|
- AssertFatal(semaphore, "PlatformSemaphore constructor - Failed to create SDL Semaphore.");
|
|
|
|
|
|
+ initialized = true;
|
|
|
|
+ if (sem_init(&semaphore, 0, initialCount) == -1) {
|
|
|
|
+ initialized = false;
|
|
|
|
+ AssertFatal(0, "PlatformSemaphore constructor - Failed to create Semaphore.");
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
~PlatformSemaphore()
|
|
~PlatformSemaphore()
|
|
{
|
|
{
|
|
- SDL_DestroySemaphore(semaphore);
|
|
|
|
|
|
+ sem_destroy(&semaphore);
|
|
|
|
+ initialized = false;
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
@@ -57,28 +63,37 @@ Semaphore::~Semaphore()
|
|
|
|
|
|
bool Semaphore::acquire(bool block, S32 timeoutMS)
|
|
bool Semaphore::acquire(bool block, S32 timeoutMS)
|
|
{
|
|
{
|
|
- AssertFatal(mData && mData->semaphore, "Semaphore::acquire - Invalid semaphore.");
|
|
|
|
|
|
+ AssertFatal(mData && mData->initialized, "Semaphore::acquire - Invalid semaphore.");
|
|
if (block)
|
|
if (block)
|
|
{
|
|
{
|
|
|
|
+ //SDL was removed so I do not now if this still holds true or not with OS calls but my guess is they are used underneath SDL anyway
|
|
// Semaphore acquiring is different from the MacOS/Win realization because SDL_SemWaitTimeout() with "infinite" timeout can be too heavy on some platforms.
|
|
// Semaphore acquiring is different from the MacOS/Win realization because SDL_SemWaitTimeout() with "infinite" timeout can be too heavy on some platforms.
|
|
// (see "man SDL_SemWaitTimeout(3)" for more info)
|
|
// (see "man SDL_SemWaitTimeout(3)" for more info)
|
|
// "man" states to avoid the use of SDL_SemWaitTimeout at all, but at current stage this looks like a valid and working solution, so keeping it this way.
|
|
// "man" states to avoid the use of SDL_SemWaitTimeout at all, but at current stage this looks like a valid and working solution, so keeping it this way.
|
|
// [bank / Feb-2010]
|
|
// [bank / Feb-2010]
|
|
if (timeoutMS == -1)
|
|
if (timeoutMS == -1)
|
|
{
|
|
{
|
|
- if (SDL_SemWait(mData->semaphore) < 0)
|
|
|
|
- AssertFatal(false, "Semaphore::acquie - Wait failed.");
|
|
|
|
|
|
+ if (sem_wait(&mData->semaphore) < 0)
|
|
|
|
+ AssertFatal(false, "Semaphore::acquire - Wait failed.");
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- if (SDL_SemWaitTimeout(mData->semaphore, timeoutMS) < 0)
|
|
|
|
- AssertFatal(false, "Semaphore::acquie - Wait with timeout failed.");
|
|
|
|
|
|
+ //convert timeoutMS to timespec
|
|
|
|
+ timespec ts;
|
|
|
|
+ if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
|
|
|
|
+ AssertFatal(false, "Semaphore::acquire - clock_realtime failed.");
|
|
|
|
+ }
|
|
|
|
+ ts.tv_sec += timeoutMS / 1000;
|
|
|
|
+ ts.tv_nsec += (timeoutMS % 1000) * 1000;
|
|
|
|
+
|
|
|
|
+ if (sem_timedwait(&mData->semaphore, &ts) < 0)
|
|
|
|
+ AssertFatal(false, "Semaphore::acquire - Wait with timeout failed.");
|
|
}
|
|
}
|
|
return (true);
|
|
return (true);
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- int res = SDL_SemTryWait(mData->semaphore);
|
|
|
|
|
|
+ int res = sem_trywait(&mData->semaphore);
|
|
return (res == 0);
|
|
return (res == 0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -86,5 +101,6 @@ bool Semaphore::acquire(bool block, S32 timeoutMS)
|
|
void Semaphore::release()
|
|
void Semaphore::release()
|
|
{
|
|
{
|
|
AssertFatal(mData, "Semaphore::releaseSemaphore - Invalid semaphore.");
|
|
AssertFatal(mData, "Semaphore::releaseSemaphore - Invalid semaphore.");
|
|
- SDL_SemPost(mData->semaphore);
|
|
|
|
|
|
+ sem_post(&mData->semaphore);
|
|
}
|
|
}
|
|
|
|
+//MGT: end
|