Mutex

Encapsulates an os specific exclusive lock.

Summary
MutexEncapsulates an os specific exclusive lock.
CopyrightThis program is free software.
Files
C-kern/api/platform/sync/mutex.hHeader file of Mutex.
C-kern/platform/Linux/sync/mutex.cLinux specific implementation Mutex Linuximpl.
Types
struct mutex_tExports mutex_t into global namespace.
Functions
test
unittest_platform_sync_mutexTests system thread functionality.
mutex_tImplements a mutual exclusion lock.
lifetime
mutex_INIT_DEFAULTStatic initializer for mutex_t without error checking.
init_mutexInitializer for a mutex with error checking.
free_mutexFrees resources of mutex which is not in use.
change
lock_mutexLocks a mutex.
unlock_mutexUnlocks a previously locked mutex.
slock_mutexSame as lock_mutex.
sunlock_mutexSame as unlock_mutex.
inline implementation
slock_mutexImplements mutex_t.slock_mutex.
sunlock_mutexImplements mutex_t.sunlock_mutex.
KONFIG_SUBSYS
init_mutexImplement mutex_t.init_mutex as a no op if !defined(KONFIG_SUBSYS_THREAD)
free_mutexImplement mutex_t.free_mutex as a no op if !defined(KONFIG_SUBSYS_THREAD)
lock_mutexImplement mutex_t.lock_mutex as a no op if !defined(KONFIG_SUBSYS_THREAD)
unlock_mutexImplement mutex_t.unlock_mutex as a no op if !defined(KONFIG_SUBSYS_THREAD)

Copyright

This program is free software.  You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

Author

© 2011 Jörg Seebohn

Files

C-kern/api/platform/sync/mutex.h

Header file of Mutex.

C-kern/platform/Linux/sync/mutex.c

Linux specific implementation Mutex Linuximpl.

Types

struct mutex_t

Exports mutex_t into global namespace.

Functions

Summary
test
unittest_platform_sync_mutexTests system thread functionality.

test

unittest_platform_sync_mutex

int unittest_platform_sync_mutex(void)

Tests system thread functionality.

mutex_t

typedef sys_mutex_t mutex_t

Implements a mutual exclusion lock.  This thread safe object is used to protect a critical section of code against simultaneous execution by several threads.  Use lock_mutex before entering a critical section of code and use unlock_mutex just before you leave it.  This mutex can also be used between different processes.

Summary
lifetime
mutex_INIT_DEFAULTStatic initializer for mutex_t without error checking.
init_mutexInitializer for a mutex with error checking.
free_mutexFrees resources of mutex which is not in use.
change
lock_mutexLocks a mutex.
unlock_mutexUnlocks a previously locked mutex.
slock_mutexSame as lock_mutex.
sunlock_mutexSame as unlock_mutex.

lifetime

mutex_INIT_DEFAULT

#define mutex_INIT_DEFAULT sys_mutex_INIT_DEFAULT

Static initializer for mutex_t without error checking.

Following behaviour is guaranteed

1.  No deadlock detection.  2. Locking it more than once without first unlocking it => DEADLOCK (waits indefinitely).  3. Unlocking a mutex locked by a different thread works.  It is the same as if the thread which holds the lock calls unlock.  4. Unlocking an already unlocked mutex is unspecified.  So never do it.  5. Works only within a single process as inter thread mutex.

init_mutex

int init_mutex(/*out*/mutex_t *mutex)

Initializer for a mutex with error checking.

Following behaviour is guaranteed

1.  No deadlock detection.  2. Locking it more than once without first unlocking it returns error EDEADLK.  3. Unlocking a mutex locked by a different thread is prevented and returns error EPERM.  4. Unlocking an already unlocked mutex is prevented and returns error EPERM.

free_mutex

int free_mutex(mutex_t *mutex)

Frees resources of mutex which is not in use.  Returns EBUSY if a thread holds a lock and nothing is freed.

change

lock_mutex

int lock_mutex(mutex_t *mutex)

Locks a mutex.  If another thread holds the lock the calling thread waits until the lock is released.  If a lock is acquired more than once a DEADLOCK results.  If you try to lock a freed mutex EINVAL is returned.

unlock_mutex

int unlock_mutex(mutex_t *mutex)

Unlocks a previously locked mutex.  Unlocking a mutex more than once is unspecified and may return success but corrupts internal counters.  Unlocking a mutex which another thread has locked works as if the lock holder thread would call unlock.  If you try to lock a freed mutex EINVAL is returned.

slock_mutex

void slock_mutex(mutex_t *mutex)

Same as lock_mutex.  Except that an error leads to a system abort.

sunlock_mutex

void sunlock_mutex(mutex_t *mutex)

Same as unlock_mutex.  Except that an error leads to a system abort.

inline implementation

Summary
slock_mutexImplements mutex_t.slock_mutex.
sunlock_mutexImplements mutex_t.sunlock_mutex.
KONFIG_SUBSYS
init_mutexImplement mutex_t.init_mutex as a no op if !defined(KONFIG_SUBSYS_THREAD)
free_mutexImplement mutex_t.free_mutex as a no op if !defined(KONFIG_SUBSYS_THREAD)
lock_mutexImplement mutex_t.lock_mutex as a no op if !defined(KONFIG_SUBSYS_THREAD)
unlock_mutexImplement mutex_t.unlock_mutex as a no op if !defined(KONFIG_SUBSYS_THREAD)

slock_mutex

#define slock_mutex(/*mutex_t **/mutex) (assert(!lock_mutex(mutex))) ;

Implements mutex_t.slock_mutex.

sunlock_mutex

#define sunlock_mutex(/*mutex_t **/mutex) (assert(!unlock_mutex(mutex))) ;

Implements mutex_t.sunlock_mutex.

KONFIG_SUBSYS

init_mutex

#define init_mutex(mutex) (0)

Implement mutex_t.init_mutex as a no op if !defined(KONFIG_SUBSYS_THREAD)

free_mutex

#define free_mutex(mutex) (0)

Implement mutex_t.free_mutex as a no op if !defined(KONFIG_SUBSYS_THREAD)

lock_mutex

#define lock_mutex(mutex) (0)

Implement mutex_t.lock_mutex as a no op if !defined(KONFIG_SUBSYS_THREAD)

unlock_mutex

#define unlock_mutex(mutex) (0)

Implement mutex_t.unlock_mutex as a no op if !defined(KONFIG_SUBSYS_THREAD)

Encapsulates an os specific exclusive lock.
Implements Mutex.
typedef sys_mutex_t mutex_t
Implements a mutual exclusion lock.
int unittest_platform_sync_mutex(void)
Tests system thread functionality.
#define mutex_INIT_DEFAULT sys_mutex_INIT_DEFAULT
Static initializer for mutex_t without error checking.
int init_mutex(/*out*/mutex_t *mutex)
Initializer for a mutex with error checking.
int free_mutex(mutex_t *mutex)
Frees resources of mutex which is not in use.
int lock_mutex(mutex_t *mutex)
Locks a mutex.
int unlock_mutex(mutex_t *mutex)
Unlocks a previously locked mutex.
void slock_mutex(mutex_t *mutex)
Same as lock_mutex.
void sunlock_mutex(mutex_t *mutex)
Same as unlock_mutex.
#define slock_mutex(/*mutex_t **/mutex) (assert(!lock_mutex(mutex))) ;
Implements mutex_t.slock_mutex.
#define sunlock_mutex(/*mutex_t **/mutex) (assert(!unlock_mutex(mutex))) ;
Implements mutex_t.sunlock_mutex.
#define init_mutex(mutex) (0)
Implement mutex_t.init_mutex as a no op if !defined(KONFIG_SUBSYS_THREAD)
#define free_mutex(mutex) (0)
Implement mutex_t.free_mutex as a no op if !defined(KONFIG_SUBSYS_THREAD)
#define lock_mutex(mutex) (0)
Implement mutex_t.lock_mutex as a no op if !defined(KONFIG_SUBSYS_THREAD)
#define unlock_mutex(mutex) (0)
Implement mutex_t.unlock_mutex as a no op if !defined(KONFIG_SUBSYS_THREAD)
Close