Skip to content

Commit 97b11c9

Browse files
authored
use old osSendMesg and osRecvMesg implementations (Kenix3#569)
1 parent d7ff859 commit 97b11c9

File tree

1 file changed

+8
-72
lines changed

1 file changed

+8
-72
lines changed

src/public/libultra/os_mesg.cpp

+8-72
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,51 @@
11
#include "libultraship/libultraship.h"
22

3-
std::map<OSMesgQueue*, std::pair<std::shared_ptr<std::mutex>, std::shared_ptr<std::condition_variable>>>
4-
__lusMesgLockMap;
5-
63
extern "C" {
74

85
__OSEventState __osEventStateTab[OS_NUM_EVENTS] = { 0 };
96

10-
#define MQ_GET_COUNT(mq) ((mq)->validCount)
11-
#define MQ_IS_EMPTY(mq) (MQ_GET_COUNT(mq) == 0)
12-
#define MQ_IS_FULL(mq) (MQ_GET_COUNT(mq) >= (mq)->msgCount)
13-
14-
#define MQ_MUTEX(mq) (*__lusMesgLockMap[mq].first)
15-
#define MQ_CVAR(mq) (*__lusMesgLockMap[mq].second)
16-
177
void osCreateMesgQueue(OSMesgQueue* mq, OSMesg* msgBuf, int32_t count) {
18-
198
mq->validCount = 0;
209
mq->first = 0;
2110
mq->msgCount = count;
2211
mq->msg = msgBuf;
23-
24-
mq->mtqueue = nullptr;
25-
mq->fullqueue = nullptr;
26-
27-
__lusMesgLockMap[mq] = std::make_pair(std::make_shared<std::mutex>(), std::make_shared<std::condition_variable>());
12+
return;
2813
}
2914

3015
int32_t osSendMesg(OSMesgQueue* mq, OSMesg msg, int32_t flag) {
31-
32-
// mq is not initialised by osCreateMesgQueue
33-
if (!__lusMesgLockMap.contains(mq))
34-
return -1;
35-
36-
std::unique_lock<std::mutex> ul(MQ_MUTEX(mq));
37-
38-
while (MQ_IS_FULL(mq)) {
39-
if (flag == OS_MESG_NOBLOCK)
40-
return -1;
41-
42-
// Wait for space in the queue.
43-
MQ_CVAR(mq).wait(ul);
44-
}
45-
16+
int32_t index;
4617
if (mq->validCount >= mq->msgCount) {
4718
return -1;
4819
}
49-
50-
int32_t last = (mq->first + mq->validCount) % mq->msgCount;
51-
mq->msg[last] = msg;
20+
index = (mq->first + mq->validCount) % mq->msgCount;
21+
mq->msg[index] = msg;
5222
mq->validCount++;
5323

54-
// Wake threads waiting on this queue.
55-
MQ_CVAR(mq).notify_all();
56-
5724
return 0;
5825
}
5926

6027
int32_t osJamMesg(OSMesgQueue* mq, OSMesg msg, int32_t flag) {
61-
62-
// mq is not initialised by osCreateMesgQueue
63-
if (!__lusMesgLockMap.contains(mq))
28+
if (mq->validCount == 0) {
6429
return -1;
65-
66-
std::unique_lock ul(MQ_MUTEX(mq));
67-
68-
while (MQ_IS_FULL(mq)) {
69-
if (flag == OS_MESG_NOBLOCK) {
70-
return -1;
71-
}
72-
73-
// Wait for space in the queue.
74-
MQ_CVAR(mq).wait(ul);
7530
}
7631

7732
mq->first = (mq->first + mq->msgCount - 1) % mq->msgCount;
7833
mq->msg[mq->first] = msg;
7934
mq->validCount++;
8035

81-
// Wake threads waiting on this queue.
82-
MQ_CVAR(mq).notify_all();
83-
8436
return 0;
8537
}
8638

8739
int32_t osRecvMesg(OSMesgQueue* mq, OSMesg* msg, int32_t flag) {
88-
89-
// mq is not initialised by osCreateMesgQueue
90-
if (!__lusMesgLockMap.contains(mq))
40+
if (mq->validCount == 0) {
9141
return -1;
92-
93-
std::unique_lock ul(MQ_MUTEX(mq));
94-
95-
while (MQ_IS_EMPTY(mq)) {
96-
if (flag == OS_MESG_NOBLOCK)
97-
return -1;
98-
99-
// Wait for mesg
100-
MQ_CVAR(mq).wait(ul);
10142
}
102-
103-
if (msg != nullptr) {
104-
*msg = mq->msg[mq->first];
43+
if (msg != NULL) {
44+
*msg = *(mq->first + mq->msg);
10545
}
106-
10746
mq->first = (mq->first + 1) % mq->msgCount;
10847
mq->validCount--;
10948

110-
// Wake threads waiting on this queue.
111-
MQ_CVAR(mq).notify_all();
112-
11349
return 0;
11450
}
11551

0 commit comments

Comments
 (0)