Skip to content

Commit 349bf44

Browse files
msgq: add (janky) macOS support (#667)
* start mac MSGQ support * lil more
1 parent f3f4401 commit 349bf44

4 files changed

Lines changed: 33 additions & 30 deletions

File tree

msgq/ipc.cc

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,8 @@
77
#include "msgq/impl_msgq.h"
88
#include "msgq/impl_fake.h"
99

10-
#ifdef __APPLE__
11-
const bool MUST_USE_ZMQ = true;
12-
#else
13-
const bool MUST_USE_ZMQ = false;
14-
#endif
15-
1610
bool messaging_use_zmq(){
17-
if (std::getenv("ZMQ") || MUST_USE_ZMQ) {
18-
if (std::getenv("OPENPILOT_PREFIX")) {
19-
std::cerr << "OPENPILOT_PREFIX not supported with ZMQ backend\n";
20-
assert(false);
21-
}
22-
return true;
23-
}
24-
return false;
11+
return std::getenv("ZMQ") != nullptr;
2512
}
2613

2714
bool messaging_use_fake(){

msgq/msgq.cc

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,16 @@ int msgq_new_queue(msgq_queue_t * q, const char * path, size_t size){
8787
assert(size < 0xFFFFFFFF); // Buffer must be smaller than 2^32 bytes
8888
std::signal(SIGUSR2, sigusr2_handler);
8989

90-
std::string full_path = "/dev/shm/msgq_";
90+
#ifdef __APPLE__
91+
std::string base_path = "/tmp/msgq_";
92+
#else
93+
std::string base_path = "/dev/shm/msgq_";
94+
#endif
9195
const char* prefix = std::getenv("OPENPILOT_PREFIX");
9296
if (prefix) {
93-
full_path += std::string(prefix) + "/";
97+
base_path += std::string(prefix) + "/";
9498
}
95-
full_path += path;
99+
std::string full_path = base_path + path;
96100

97101
auto fd = open(full_path.c_str(), O_RDWR | O_CREAT, 0664);
98102
if (fd < 0) {
@@ -168,8 +172,11 @@ void msgq_init_publisher(msgq_queue_t * q) {
168172
}
169173

170174
static void thread_signal(uint32_t tid) {
171-
#ifndef SYS_tkill
172-
// TODO: this won't work for multithreaded programs
175+
#ifdef __APPLE__
176+
// macOS doesn't have tkill, rely on polling instead
177+
(void)tid;
178+
#elif !defined(SYS_tkill)
179+
// fallback for systems without tkill
173180
kill(tid, SIGUSR2);
174181
#else
175182
syscall(SYS_tkill, tid, SIGUSR2);

msgq/msgq_tests.cc

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
#include "catch2/catch.hpp"
22
#include "msgq/msgq.h"
33

4+
static void cleanup_test_queue() {
5+
#ifdef __APPLE__
6+
remove("/tmp/msgq_test_queue");
7+
#else
8+
remove("/dev/shm/msgq_test_queue");
9+
#endif
10+
}
11+
412
TEST_CASE("ALIGN")
513
{
614
REQUIRE(ALIGN(0) == 0);
@@ -43,7 +51,7 @@ TEST_CASE("msgq_msg_init_data")
4351

4452
TEST_CASE("msgq_init_subscriber")
4553
{
46-
remove("/dev/shm/msgq_test_queue");
54+
cleanup_test_queue();
4755
msgq_queue_t q;
4856
msgq_new_queue(&q, "test_queue", 1024);
4957
REQUIRE(*q.num_readers == 0);
@@ -63,7 +71,7 @@ TEST_CASE("msgq_init_subscriber")
6371

6472
TEST_CASE("msgq_msg_send first message")
6573
{
66-
remove("/dev/shm/msgq_test_queue");
74+
cleanup_test_queue();
6775
msgq_queue_t q;
6876
msgq_new_queue(&q, "test_queue", 1024);
6977
msgq_init_publisher(&q);
@@ -100,7 +108,7 @@ TEST_CASE("msgq_msg_send first message")
100108

101109
TEST_CASE("msgq_msg_send test wraparound")
102110
{
103-
remove("/dev/shm/msgq_test_queue");
111+
cleanup_test_queue();
104112
msgq_queue_t q;
105113
msgq_new_queue(&q, "test_queue", 1024);
106114
msgq_init_publisher(&q);
@@ -132,7 +140,7 @@ TEST_CASE("msgq_msg_send test wraparound")
132140

133141
TEST_CASE("msgq_msg_recv test wraparound")
134142
{
135-
remove("/dev/shm/msgq_test_queue");
143+
cleanup_test_queue();
136144
msgq_queue_t q_pub, q_sub;
137145
msgq_new_queue(&q_pub, "test_queue", 1024);
138146
msgq_new_queue(&q_sub, "test_queue", 1024);
@@ -178,7 +186,7 @@ TEST_CASE("msgq_msg_recv test wraparound")
178186

179187
TEST_CASE("msgq_msg_send test invalidation")
180188
{
181-
remove("/dev/shm/msgq_test_queue");
189+
cleanup_test_queue();
182190
msgq_queue_t q_pub, q_sub;
183191
msgq_new_queue(&q_pub, "test_queue", 1024);
184192
msgq_new_queue(&q_sub, "test_queue", 1024);
@@ -214,7 +222,7 @@ TEST_CASE("msgq_msg_send test invalidation")
214222

215223
TEST_CASE("msgq_init_subscriber init 2 subscribers")
216224
{
217-
remove("/dev/shm/msgq_test_queue");
225+
cleanup_test_queue();
218226
msgq_queue_t q1, q2;
219227
msgq_new_queue(&q1, "test_queue", 1024);
220228
msgq_new_queue(&q2, "test_queue", 1024);
@@ -237,7 +245,7 @@ TEST_CASE("msgq_init_subscriber init 2 subscribers")
237245

238246
TEST_CASE("Write 1 msg, read 1 msg", "[integration]")
239247
{
240-
remove("/dev/shm/msgq_test_queue");
248+
cleanup_test_queue();
241249
const size_t msg_size = 128;
242250
msgq_queue_t writer, reader;
243251

@@ -273,7 +281,7 @@ TEST_CASE("Write 1 msg, read 1 msg", "[integration]")
273281

274282
TEST_CASE("Write 2 msg, read 2 msg - conflate = false", "[integration]")
275283
{
276-
remove("/dev/shm/msgq_test_queue");
284+
cleanup_test_queue();
277285
const size_t msg_size = 128;
278286
msgq_queue_t writer, reader;
279287

@@ -310,7 +318,7 @@ TEST_CASE("Write 2 msg, read 2 msg - conflate = false", "[integration]")
310318

311319
TEST_CASE("Write 2 msg, read 2 msg - conflate = true", "[integration]")
312320
{
313-
remove("/dev/shm/msgq_test_queue");
321+
cleanup_test_queue();
314322
const size_t msg_size = 128;
315323
msgq_queue_t writer, reader;
316324

@@ -348,7 +356,7 @@ TEST_CASE("Write 2 msg, read 2 msg - conflate = true", "[integration]")
348356

349357
TEST_CASE("1 publisher, 1 slow subscriber", "[integration]")
350358
{
351-
remove("/dev/shm/msgq_test_queue");
359+
cleanup_test_queue();
352360
msgq_queue_t writer, reader;
353361

354362
msgq_new_queue(&writer, "test_queue", 1024);
@@ -391,7 +399,7 @@ TEST_CASE("1 publisher, 1 slow subscriber", "[integration]")
391399

392400
TEST_CASE("1 publisher, 2 subscribers", "[integration]")
393401
{
394-
remove("/dev/shm/msgq_test_queue");
402+
cleanup_test_queue();
395403
msgq_queue_t writer, reader1, reader2;
396404

397405
msgq_new_queue(&writer, "test_queue", 1024);

test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ scons -j8
1616
pre-commit run --all-files
1717

1818
# *** test ***
19+
msgq/test_runner
1920
pytest

0 commit comments

Comments
 (0)