General Info

Interface: Mutex
Files: lib/mutex.h
Last change: 22/12/2001
Author: Luiz Henrique Shigunov
Description
Structures
Functions
  • 0x00 - Destroy - Destroy a mutex
  • 0x01 - Init - Initialize a mutex
  • 0x02 - Lock - Lock a mutex
  • 0x03 - TryLock - Try to lock a mutex
  • 0x04 - Unlock - Unlock a mutex
  • Description

    This page describes Mutex user interface.

    The ideia behind a Mutex interface is to make possible different implementations of this interface. One implementation can have a very fast mutex, but with no error checking; another can implement a recursive mutex, etc.

    If we had a Sync interface with all types of synchronizers, each implementation would have to implement all synchronizers of the interface.

    A mutex is a MUTual EXclusion synchronizer and is useful for protecting shared data structures from concurrent modifications, implementing critical sections and monitors.

    A mutex has two possible states: unlocked (not owned by any thread), and locked (owned by one thread). A mutex can never be owned by two different threads simultaneously. A thread attempting to lock a mutex that is already locked by another thread is suspended until the owning thread unlocks the mutex first.

    Structures

    typedef struct {
        unsigned int data[4];
    } Mutex;
    

    data are specific data and must be initialized with Init.

    0x00 - Destroy

    Syntax

    int Destroy(Mutex *m);

    Description

    This function destroys mutex m, freeing its resources.

    The mutex can't be in use.

    Return value

    0x01 - Init

    Syntax

    int Init(Mutex *m, int prop);

    Description

    This function initializes mutex m with properties prop.

    prop isn't used yet.

    Return value

    0x02 - Lock

    Syntax

    int Lock(Mutex *m);

    Description

    This function locks mutex m.

    If the mutex is currently unlocked, it becomes locked and owned by the calling thread. If the mutex is already locked by another thread, this function suspends the calling thread until the mutex is unlocked.

    If the mutex is already locked by the calling thread, the behavior depends on the implementation.

    Return value

    0x03 - TryLock

    Syntax

    int TryLock(Mutex *m);

    Description

    This functions tries to lock mutex m.

    It behaves identically to Lock, but returns an error if the thread would block.

    Return value

    0x04 - Unlock

    Syntax

    int Unlock(Mutex *m);

    Description

    This function unlocks mutex m.

    Return value