commaai / msgq

MSGQ: A lock free single producer multi consumer message queue
184 stars 251 forks source link

msgq: use shared memory mutex and condition variables for synchronization #617

Closed deanlee closed 4 months ago

deanlee commented 5 months ago

This PR resolve issue #208, enhances the msgq with shared memory mutexes and condition variables for improved IPC synchronization. While handling signals interrupts all processes, shared memory mechanisms provide a more robust and efficient solution, reducing overhead and complexity in high-frequency event scenarios.

key changes:

Each block of shared memory used for messages now includes a header:


struct SharedMemoryHeader {
  pthread_mutex_t mutex;
  pthread_cond_t cond;
  bool initialized;
};

This header contains the IPC mutex, condition variable, and an initialization flag.

When sending a message, the corresponding condition variable is used to notify the receiver to retrieve the message. The Poller uses a global shared memory context, PollerContext, to handle message notifications and reception efficiently.

@pd0wm If you have a moment, your review of this PR would be greatly appreciated to ensure I haven't missed anything or introduced new issues.

deanlee commented 4 months ago

Closed. linux futex https://github.com/commaai/msgq/pull/625 appears to be a more efficient solution compared to using mutex and condition variables.