InterThreadMutex

Offers inter thread mutex which works closely with thread_t

Summary
InterThreadMutexOffers inter thread mutex which works closely with thread_t
CopyrightThis program is free software.
Files
C-kern/api/platform/sync/thrmutex.hHeader file InterThreadMutex.
C-kern/platform/Linux/sync/thrmutex.cImplementation file InterThreadMutex impl.
Types
struct thrmutex_tExport thrmutex_t into global namespace.
Functions
test
unittest_platform_sync_thrmutexTest thrmutex_t functionality.
thrmutex_tMutual exclusion lock.
lastPoints to last entry in list of waiting threads.
lockholderThe threads which acquired the lock and is allowed to run.
lockflagLock flag used to protect access to data members.
lifetime
thrmutex_FREEStatic initializer.
thrmutex_INITStatic initializer.
init_thrmutexSets mutex to thrmutex_INIT.
free_thrmutexChecks that no one is waiting and sets mutex to thrmutex_FREE.
query
isfree_thrmutexReturns true if mutex equals thrmutex_FREE.
islocked_thrmutexReturns true if a thread locked the mutex.
iswaiting_thrmutexReturns true if mutex is locked and wait list is not empty.
lockholder_thrmutexReturns the thread which locked the mutex.
synchronize
lock_thrmutexLocks the mutex.
unlock_thrmutexUnlocks the mutex.
safe-synchronize
slock_thrmutexCalls lock_thrmutex and asserts success.
sunlock_thrmutexCalls sunlock_thrmutex and asserts success.
inline implementation
Macros
init_thrmutexImplements thrmutex_t.init_thrmutex.
slock_thrmutexImplements thrmutex_t.slock_thrmutex.
sunlock_thrmutexImplements thrmutex_t.sunlock_thrmutex.

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

© 2013 Jörg Seebohn

Files

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

Header file InterThreadMutex.

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

Implementation file InterThreadMutex impl.

Types

struct thrmutex_t

typedef struct thrmutex_t thrmutex_t

Export thrmutex_t into global namespace.

Functions

test

unittest_platform_sync_thrmutex

int unittest_platform_sync_thrmutex(void)

Test thrmutex_t functionality.

thrmutex_t

struct thrmutex_t

Mutual exclusion lock.  Used to synchronize access to a data structure shared between multiple threads.  If you want to share data between processes use mutex_t.  Call <lock_threamutex> before you want to use the data structure.  Call <unlock_threadmutex> after you no longer need access to it.

Summary
lastPoints to last entry in list of waiting threads.
lockholderThe threads which acquired the lock and is allowed to run.
lockflagLock flag used to protect access to data members.
lifetime
thrmutex_FREEStatic initializer.
thrmutex_INITStatic initializer.
init_thrmutexSets mutex to thrmutex_INIT.
free_thrmutexChecks that no one is waiting and sets mutex to thrmutex_FREE.
query
isfree_thrmutexReturns true if mutex equals thrmutex_FREE.
islocked_thrmutexReturns true if a thread locked the mutex.
iswaiting_thrmutexReturns true if mutex is locked and wait list is not empty.
lockholder_thrmutexReturns the thread which locked the mutex.
synchronize
lock_thrmutexLocks the mutex.
unlock_thrmutexUnlocks the mutex.
safe-synchronize
slock_thrmutexCalls lock_thrmutex and asserts success.
sunlock_thrmutexCalls sunlock_thrmutex and asserts success.

last

struct slist_node_t * last

Points to last entry in list of waiting threads.  Threads trying to lock the mutex are appended to the end of the list.

lockholder

struct thread_t * lockholder

The threads which acquired the lock and is allowed to run.  If this value is 0 last is also 0 and the no one has locked the mutex.

lockflag

uint8_t lockflag

Lock flag used to protect access to data members.  Set and cleared with atomic operations.

lifetime

thrmutex_FREE

#define thrmutex_FREE { 0, 0, 0 }

Static initializer.

thrmutex_INIT

#define thrmutex_INIT { 0, 0, 0 }

Static initializer.  Used in function init_thrmutex.

init_thrmutex

void init_thrmutex(/*out*/thrmutex_t *mutex)

Sets mutex to thrmutex_INIT.

free_thrmutex

int free_thrmutex(thrmutex_t *mutex)

Checks that no one is waiting and sets mutex to thrmutex_FREE.  Returns EBUSY and does nothing if anyone holds the lock.

query

isfree_thrmutex

bool isfree_thrmutex(thrmutex_t *mutex)

Returns true if mutex equals thrmutex_FREE.

islocked_thrmutex

bool islocked_thrmutex(thrmutex_t *mutex)

Returns true if a thread locked the mutex.  If this functions returns true lockholder_thrmutex returns a value != 0.

iswaiting_thrmutex

bool iswaiting_thrmutex(thrmutex_t *mutex)

Returns true if mutex is locked and wait list is not empty.  If a thread calls lock_thrmutex but the mutex is already locked it is stored in an internal wait list.

lockholder_thrmutex

struct thread_t * lockholder_thrmutex(thrmutex_t *mutex)

Returns the thread which locked the mutex.  Returns 0 if the mutex is unlocked.

synchronize

lock_thrmutex

int lock_thrmutex(thrmutex_t *mutex)

Locks the mutex.  If the mutex is already locked the caller is stored into an internal wait list as last entry and then it is suspended.  If the lockholder calls unlock_thrmutex the first thread on the wait list is resumed and returns from this function.  The function returns EDEADLK if the caller already locked the mutex.  Recursive calls to lock and unlock are not supported.

unlock_thrmutex

int unlock_thrmutex(thrmutex_t *mutex)

Unlocks the mutex.  The functions returns EPERM if the lock is already unlocked or locked by another thread.

safe-synchronize

slock_thrmutex

void slock_thrmutex(thrmutex_t *mutex)

Calls lock_thrmutex and asserts success.

sunlock_thrmutex

void sunlock_thrmutex(thrmutex_t *mutex)

Calls sunlock_thrmutex and asserts success.

Macros

init_thrmutex

#define init_thrmutex(mutex) ((void)(*(mutex) = (thrmutex_t) thrmutex_INIT))

Implements thrmutex_t.init_thrmutex.

slock_thrmutex

#define slock_thrmutex(mutex) (assert(!lock_thrmutex(mutex)))

Implements thrmutex_t.slock_thrmutex.

sunlock_thrmutex

#define sunlock_thrmutex(mutex) (assert(!unlock_thrmutex(mutex)))

Implements thrmutex_t.sunlock_thrmutex.

Offers inter thread mutex which works closely with thread_t
Implements InterThreadMutex.
typedef struct thrmutex_t thrmutex_t
Export thrmutex_t into global namespace.
struct thrmutex_t
Mutual exclusion lock.
int unittest_platform_sync_thrmutex(void)
Test thrmutex_t functionality.
struct slist_node_t * last
Points to last entry in list of waiting threads.
struct thread_t * lockholder
The threads which acquired the lock and is allowed to run.
uint8_t lockflag
Lock flag used to protect access to data members.
#define thrmutex_FREE { 0, 0, 0 }
Static initializer.
#define thrmutex_INIT { 0, 0, 0 }
Static initializer.
void init_thrmutex(/*out*/thrmutex_t *mutex)
Sets mutex to thrmutex_INIT.
int free_thrmutex(thrmutex_t *mutex)
Checks that no one is waiting and sets mutex to thrmutex_FREE.
bool isfree_thrmutex(thrmutex_t *mutex)
Returns true if mutex equals thrmutex_FREE.
bool islocked_thrmutex(thrmutex_t *mutex)
Returns true if a thread locked the mutex.
bool iswaiting_thrmutex(thrmutex_t *mutex)
Returns true if mutex is locked and wait list is not empty.
struct thread_t * lockholder_thrmutex(thrmutex_t *mutex)
Returns the thread which locked the mutex.
int lock_thrmutex(thrmutex_t *mutex)
Locks the mutex.
int unlock_thrmutex(thrmutex_t *mutex)
Unlocks the mutex.
void slock_thrmutex(thrmutex_t *mutex)
Calls lock_thrmutex and asserts success.
void sunlock_thrmutex(thrmutex_t *mutex)
Calls sunlock_thrmutex and asserts success.
#define init_thrmutex(mutex) ((void)(*(mutex) = (thrmutex_t) thrmutex_INIT))
Implements thrmutex_t.init_thrmutex.
#define slock_thrmutex(mutex) (assert(!lock_thrmutex(mutex)))
Implements thrmutex_t.slock_thrmutex.
#define sunlock_thrmutex(mutex) (assert(!unlock_thrmutex(mutex)))
Implements thrmutex_t.sunlock_thrmutex.
Close