1
Description: Use SDL semaphores instead of named semaphores, that fail under strict confinement
3
Author: Olivier Tilloy <olivier@tilloy.net>
4
Last-Update: 2017-05-15
6
--- a/source/tools/atlas/GameInterface/MessagePasserImpl.h
7
+++ b/source/tools/atlas/GameInterface/MessagePasserImpl.h
10
#include "MessagePasser.h"
12
+#include "lib/external_libraries/libsdl.h"
13
#include "ps/ThreadUtil.h"
16
@@ -40,8 +41,7 @@ public:
20
- CStr m_SemaphoreName;
22
+ SDL_sem* m_Semaphore;
23
std::queue<AtlasMessage::IMessage*> m_Queue;
27
--- a/source/tools/atlas/GameInterface/MessagePasserImpl.cpp
28
+++ b/source/tools/atlas/GameInterface/MessagePasserImpl.cpp
29
@@ -32,39 +32,10 @@ using namespace AtlasMessage;
30
MessagePasserImpl::MessagePasserImpl()
31
: m_Trace(false), m_Semaphore(NULL)
34
- while (tries++ < 16) // some arbitrary cut-off point to avoid infinite loops
36
- static char name[64];
37
- sprintf_s(name, ARRAY_SIZE(name), "/wfg-atlas-msgpass-%d-%d",
38
- (int)rand(1, 1000), (int)(time(0)%1000));
39
- sem_t* sem = sem_open(name, O_CREAT | O_EXCL, 0700, 0);
41
- // This cast should not be necessary, but apparently SEM_FAILED is not
42
- // a value of a pointer type
43
- if (sem == (sem_t*)SEM_FAILED || !sem)
48
- // Semaphore already exists - try another one
51
- // Otherwise, it's a probably-fatal error
52
- debug_warn(L"sem_open failed");
55
- // Succeeded - use this semaphore
57
- m_SemaphoreName = name;
62
+ m_Semaphore = SDL_CreateSemaphore(0);
63
+ if (m_Semaphore == NULL)
65
debug_warn(L"Failed to create semaphore for Atlas - giving up");
66
- // We will probably crash later - maybe we could fall back on sem_init, if this
67
- // ever fails in practice
71
@@ -72,9 +43,7 @@ MessagePasserImpl::~MessagePasserImpl()
76
- sem_close(m_Semaphore);
77
- sem_unlink(m_SemaphoreName.c_str());
78
+ SDL_DestroySemaphore(m_Semaphore);
82
@@ -165,19 +134,7 @@ void MessagePasserImpl::Query(QueryMessa
83
// while (0 != (err = sem_timedwait(psem, &abs_timeout)))
86
- while (0 != sem_wait(m_Semaphore))
88
- // If timed out, call callback and try again
89
-// if (errno == ETIMEDOUT)
90
-// timeoutCallback();
92
- // Keep retrying while EINTR, but other errors are probably fatal
95
- debug_warn(L"Semaphore wait failed");
96
- return; // (leaks the semaphore)
99
+ SDL_SemWait(m_Semaphore);
102
qry->m_Semaphore = NULL;
104
--- a/source/tools/atlas/GameInterface/GameLoop.cpp
105
+++ b/source/tools/atlas/GameInterface/GameLoop.cpp
106
@@ -184,7 +184,7 @@ static void* RunEngine(void* data)
108
// For queries, we need to notify MessagePasserImpl::Query
109
// that the query has now been processed.
110
- sem_post((sem_t*) static_cast<QueryMessage*>(msg)->m_Semaphore);
111
+ SDL_SemPost((SDL_sem*) static_cast<QueryMessage*>(msg)->m_Semaphore);
112
// (msg may have been destructed at this point, so don't use it again)
114
// It's quite possible that the querier is going to do a tiny