SyncMutexEvent(), SyncMutexEvent_r()

Attach an event to a mutex

Synopsis:

#include <sys/neutrino.h>

int SyncMutexEvent( sync_t * sync,
                    struct sigevent * event );

int SyncMutexEvent_r( sync_t * sync,
                      struct sigevent * event );

Arguments:

sync
A pointer to the synchronization object for the mutex that you want to attach an event to.
event
A pointer to the sigevent structure that describes the event that you want to attach, or NULL if you want to detach the currently registered event.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The SyncMutexEvent() and SyncMutexEvent_r() kernel calls attach the specified event to a mutex pointed to by sync. This event is delivered when the mutex enters the DEAD state, which happens when a process holding the mutex removes its last mapping of the mutex without unlocking it (including when the process dies while holding the mutex in shared memory).

Note: This situation is likely caused by a bug in the process. Although the OS provides a way to detect it and revive the mutex, the data that the mutex is protecting might be in an inconsistent state.

SyncMutexEvent() and SyncMutexEvent_r() are similar, except for the way they indicate errors. See the Returns section for details.

When you're notified that a mutex has been put into the DEAD state, you can revive it with SyncMutexRevive().

If you call SyncMutexEvent() with a NULL event, the function deletes any existing event registration.

Note:
  • In order to use this function with a SIGEV_PULSE sigevent that's going to a channel that was created by a different process with a different user ID, your process must have the PROCMGR_AID_CONNECTION ability enabled. For more information, see procmgr_ability().
  • You can't use SyncMutexEvent() with a robust mutex (see pthread_mutexattr_setrobust()). The two mechanisms achieve the same goal in different ways.
  • Robust mutexes, mutexes with priority ceilings, and those using SyncMutexEvent() use more system resources than other mutexes.

Managing the death of a mutex

Here's how you might use a mutex in shared memory between threads in two or more processes and attempt to deal with the death of one of the processes while it's holding the mutex.

  1. Designate one process as the shared memory's owner/manager.
  2. This process creates and initializes the mutex, and attaches an event using SyncMutexEvent().
  3. This process marks the shared memory area and the mutex as usable.
  4. Everyone goes ahead and uses the mutex and shared memory area; everything goes well until a process dies.
  5. If a process dies, and one of its threads holds the mutex (i.e., has locked it), then the kernel marks the mutex as DEAD and sends the event registered with SyncMutexEvent() to the shared memory's owner/manager process.

    If it's the shared memory's owner/manager process that died, then there's no event delivered, and the mutex is marked as DESTROYED; you can use procmgr_guardian() to try to handle this case. If there's no event registered, the mutex is marked as DESTROYED. In both these cases, the kernel sends a SIGDEADLK signal to all threads that are waiting, without a timeout, on the mutex. If these threads have blocked SIGDEADLK, they're made READY, as are threads that are waiting on the mutex, but with a timeout.

  6. It's up to the shared memory's owner/manager process to handle the event. It should have some way of telling other threads that the shared memory area controlled by the mutex, and the mutex itself, are currently unusable, and should also have a mechanism to indicate when they again become usable.
  7. Other threads already waiting on the dead mutex continue to wait on it.
  8. The process acting on the event calls SyncMutexRevive() on the dead mutex and revives and acquires the mutex in one atomic operation.

If you call pthread_mutex_lock():

Returns:

The only difference between these functions is the way they indicate errors:

SyncMutexEvent()
If an error occurs, the function returns -1 and sets errno. Any other value returned indicates success.
SyncMutexEvent_r()
Returns EOK on success. This function does NOT set errno. If an error occurs, the function returns any value listed in the Errors section.

Errors:

EAGAIN
All kernel synchronization event objects are in use.
EFAULT
A fault occurred when the kernel tried to access sync.
EINVAL
The synchronization object pointed to by sync doesn't exist, or it's a robust mutex.
EPERM
The calling process doesn't have the required permission; see procmgr_ability().

Classification:

QNX Neutrino

Safety:  
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes