2.2.4. Managing locks in multithreaded applications
A thread‑safe function protects shared resources from concurrent access using locks. In RVCT v2.2 and above, there are functions that you can re-implement to enable you to manage the locking mechanisms and so prevent the corruption of shared data such as the heap. These functions have the prototypes:
_mutex_initialize()Accepts a pointer to a 32‑bit word and initializes it as a valid mutex.
int _mutex_initialize(mutex *m);
By default, _mutex_initialize() returns zero for a non threaded application. Therefore, in a multithreaded application, _mutex_initialize() must return a nonzero value on success so that, at runtime, the library knows that it is being used in a multithreaded environment.
_mutex_acquire()Causes the calling thread to obtain a lock on the supplied mutex.
void _mutex_acquire(mutex *m);
_mutex_acquire() returns immediately if the mutex has no owner. If the mutex is currently owned by another thread, _mutex_acquire() must block it until it becomes available.
_mutex_release()This causes the calling thread to release the supplied mutex.
void _mutex_release(mutex *m);
_mutex_release() assumes that the mutex is owned by the calling thread.
For the C library, a mutex is specified as a single 32‑bit word of memory that can be placed anywhere. However, if your mutex implementation requires more space than this, or demands that the mutex be in special memory area, then you must treat the default mutex as a pointer to a real mutex.