Skip to content

Mutex

Description

Mutex struct. Layout is platform-conditional but the API is uniform. Stack-declare with MutexInit(); do not poke fields.

Usage example (Cross-references)

Usage examples (Cross-references)
        _Atomic int _state;
    #else
    #    error "Mutex: unsupported platform/architecture (no direct-syscall path)"
    #endif
        } Mutex;
    #    error "Mutex: unsupported platform/architecture (no direct-syscall path)"
    #endif
        } Mutex;
    
    ///
    #if PLATFORM_WINDOWS
    // _lock is a void* layout-compatible with SRWLOCK. NULL = SRWLOCK_INIT.
    #    define MutexInit() ((Mutex) {._lock = NULL})
    #elif FEATURE_DIRECT_SYSCALL
    // _Atomic int = 0 is "unlocked" in the futex / __ulock state machine.
    #elif FEATURE_DIRECT_SYSCALL
    // _Atomic int = 0 is "unlocked" in the futex / __ulock state machine.
    #    define MutexInit() ((Mutex) {._state = 0})
    #endif
        /// TAGS: Sys, Mutex, Deinit
        ///
        void MutexDeinit(Mutex *m);
    
        ///
        /// TAGS: Sys, Mutex, Lock
        ///
        Mutex *MutexLock(Mutex *m);
    
        ///
        /// TAGS: Sys, Mutex, Unlock
        ///
        Mutex *MutexUnlock(Mutex *m);
    
    #ifdef __cplusplus
    ///     lock, kernel32, no init call needed (just zero-init).
    
    #include <Misra/Sys/Mutex.h>
    
    #include <Misra/Std.h>
    #    endif
    #else
    #    error "Mutex: unsupported platform/architecture (no direct-syscall path)"
    #endif
    #endif
    
    void MutexDeinit(Mutex *m) {
        if (!m) {
            return;
    #    error "MutexDeinit: unsupported platform/architecture (no direct-syscall path)"
    #endif
        MemSet(m, 0, sizeof(Mutex));
    }
    }
    
    Mutex *MutexLock(Mutex *m) {
        if (!m) {
            LOG_FATAL("MutexLock: NULL mutex");
    }
    
    Mutex *MutexUnlock(Mutex *m) {
        if (!m) {
            LOG_FATAL("MutexUnlock: NULL mutex");
Last updated on