~osomon/+junk/0ad-snap

« back to all changes in this revision

Viewing changes to snap/patches/use-sdl-semaphores.patch

  • Committer: Olivier Tilloy
  • Date: 2021-08-16 13:43:20 UTC
  • Revision ID: olivier@tilloy.net-20210816134320-e1z4qlhz8ix3wzge
Set the license field.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
Description: Use SDL semaphores instead of named semaphores, that fail under strict confinement
2
 
Forwarded: no
3
 
Author: Olivier Tilloy <olivier@tilloy.net>
4
 
Last-Update: 2017-05-15
5
 
 
6
 
--- a/source/tools/atlas/GameInterface/MessagePasserImpl.h
7
 
+++ b/source/tools/atlas/GameInterface/MessagePasserImpl.h
8
 
@@ -20,6 +20,7 @@
9
 
 
10
 
 #include "MessagePasser.h"
11
 
 
12
 
+#include "lib/external_libraries/libsdl.h"
13
 
 #include "ps/ThreadUtil.h"
14
 
 #include "ps/CStr.h"
15
 
 #include <queue>
16
 
@@ -40,8 +41,7 @@ public:
17
 
 
18
 
 private:
19
 
        CMutex m_Mutex;
20
 
-       CStr m_SemaphoreName;
21
 
-       sem_t* m_Semaphore;
22
 
+       SDL_sem* m_Semaphore;
23
 
        std::queue<AtlasMessage::IMessage*> m_Queue;
24
 
        bool m_Trace;
25
 
 };
26
 
 
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)
32
 
 {
33
 
-       int tries = 0;
34
 
-       while (tries++ < 16) // some arbitrary cut-off point to avoid infinite loops
35
 
-       {
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);
40
 
-
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)
44
 
-               {
45
 
-                       int err = errno;
46
 
-                       if (err == EEXIST)
47
 
-                       {
48
 
-                               // Semaphore already exists - try another one
49
 
-                               continue;
50
 
-                       }
51
 
-                       // Otherwise, it's a probably-fatal error
52
 
-                       debug_warn(L"sem_open failed");
53
 
-                       break;
54
 
-               }
55
 
-               // Succeeded - use this semaphore
56
 
-               m_Semaphore = sem;
57
 
-               m_SemaphoreName = name;
58
 
-               break;
59
 
-       }
60
 
-
61
 
-       if (! m_Semaphore)
62
 
+       m_Semaphore = SDL_CreateSemaphore(0);
63
 
+       if (m_Semaphore == NULL)
64
 
        {
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
68
 
        }
69
 
 }
70
 
 
71
 
@@ -72,9 +43,7 @@ MessagePasserImpl::~MessagePasserImpl()
72
 
 {
73
 
        if (m_Semaphore)
74
 
        {
75
 
-               // Clean up
76
 
-               sem_close(m_Semaphore);
77
 
-               sem_unlink(m_SemaphoreName.c_str());
78
 
+               SDL_DestroySemaphore(m_Semaphore);
79
 
        }
80
 
 }
81
 
 
82
 
@@ -165,19 +134,7 @@ void MessagePasserImpl::Query(QueryMessa
83
 
 //     while (0 != (err = sem_timedwait(psem, &abs_timeout)))
84
 
 // #endif
85
 
 
86
 
-       while (0 != sem_wait(m_Semaphore))
87
 
-       {
88
 
-               // If timed out, call callback and try again
89
 
-//             if (errno == ETIMEDOUT)
90
 
-//                     timeoutCallback();
91
 
-//             else
92
 
-               // Keep retrying while EINTR, but other errors are probably fatal
93
 
-               if (errno != EINTR)
94
 
-               {
95
 
-                       debug_warn(L"Semaphore wait failed");
96
 
-                       return; // (leaks the semaphore)
97
 
-               }
98
 
-       }
99
 
+       SDL_SemWait(m_Semaphore);
100
 
 
101
 
        // Clean up
102
 
        qry->m_Semaphore = NULL;
103
 
 
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)
107
 
                                {
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)
113
 
 
114
 
                                        // It's quite possible that the querier is going to do a tiny