2013-11-27 16:28:31 +01:00
|
|
|
/*
|
2015-12-25 20:59:33 +01:00
|
|
|
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
* 2013, 2014 Freie Universität Berlin
|
2013-11-27 16:28:31 +01:00
|
|
|
*
|
2014-08-23 15:43:13 +02:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
2010-09-22 15:10:42 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2019-01-07 15:34:15 +01:00
|
|
|
* @defgroup core_sync_mutex Mutex
|
|
|
|
* @ingroup core_sync
|
2013-11-27 16:28:31 +01:00
|
|
|
* @brief Mutex for thread synchronization
|
|
|
|
* @{
|
|
|
|
*
|
2015-05-22 07:34:41 +02:00
|
|
|
* @file
|
2019-01-07 15:34:15 +01:00
|
|
|
* @brief Mutex for thread synchronization
|
2013-11-27 16:28:31 +01:00
|
|
|
*
|
2010-09-22 15:10:42 +02:00
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
*/
|
|
|
|
|
2017-01-18 13:00:05 +01:00
|
|
|
#ifndef MUTEX_H
|
|
|
|
#define MUTEX_H
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2015-12-25 20:59:33 +01:00
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
#include "list.h"
|
2014-10-13 14:44:28 +02:00
|
|
|
|
2014-10-09 01:18:16 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
/**
|
2014-04-01 16:31:48 +02:00
|
|
|
* @brief Mutex structure. Must never be modified by the user.
|
2010-09-22 15:10:42 +02:00
|
|
|
*/
|
2015-12-25 20:59:33 +01:00
|
|
|
typedef struct {
|
2014-04-01 16:31:48 +02:00
|
|
|
/**
|
|
|
|
* @brief The process waiting queue of the mutex. **Must never be changed
|
|
|
|
* by the user.**
|
2014-07-29 14:36:17 +02:00
|
|
|
* @internal
|
2014-04-01 16:31:48 +02:00
|
|
|
*/
|
2015-12-25 20:59:33 +01:00
|
|
|
list_node_t queue;
|
2010-09-22 15:10:42 +02:00
|
|
|
} mutex_t;
|
|
|
|
|
2014-07-18 13:44:29 +02:00
|
|
|
/**
|
|
|
|
* @brief Static initializer for mutex_t.
|
2014-08-05 17:08:53 +02:00
|
|
|
* @details This initializer is preferable to mutex_init().
|
2014-07-18 13:44:29 +02:00
|
|
|
*/
|
2015-12-25 20:59:33 +01:00
|
|
|
#define MUTEX_INIT { { NULL } }
|
2014-07-18 13:44:29 +02:00
|
|
|
|
2016-12-14 10:10:41 +01:00
|
|
|
/**
|
|
|
|
* @brief Static initializer for mutex_t with a locked mutex
|
|
|
|
*/
|
|
|
|
#define MUTEX_INIT_LOCKED { { MUTEX_LOCKED } }
|
|
|
|
|
|
|
|
/**
|
2017-01-15 12:03:48 +01:00
|
|
|
* @cond INTERNAL
|
2016-12-14 10:10:41 +01:00
|
|
|
* @brief This is the value of the mutex when locked and no threads are waiting
|
|
|
|
* for it
|
|
|
|
*/
|
2018-03-25 09:05:58 +02:00
|
|
|
#define MUTEX_LOCKED ((list_node_t *)-1)
|
2017-01-15 12:03:48 +01:00
|
|
|
/**
|
|
|
|
* @endcond
|
|
|
|
*/
|
2016-12-14 10:10:41 +01:00
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
/**
|
2014-04-01 16:31:48 +02:00
|
|
|
* @brief Initializes a mutex object.
|
2014-08-05 17:08:53 +02:00
|
|
|
* @details For initialization of variables use MUTEX_INIT instead.
|
2014-07-18 13:44:29 +02:00
|
|
|
* Only use the function call for dynamically allocated mutexes.
|
2014-04-01 16:31:48 +02:00
|
|
|
* @param[out] mutex pre-allocated mutex structure, must not be NULL.
|
2010-09-22 15:10:42 +02:00
|
|
|
*/
|
2014-07-18 13:44:29 +02:00
|
|
|
static inline void mutex_init(mutex_t *mutex)
|
|
|
|
{
|
2015-12-25 20:59:33 +01:00
|
|
|
mutex->queue.next = NULL;
|
2014-07-18 13:44:29 +02:00
|
|
|
}
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2015-12-25 20:59:33 +01:00
|
|
|
/**
|
|
|
|
* @brief Lock a mutex, blocking or non-blocking.
|
|
|
|
*
|
|
|
|
* @details For commit purposes you should probably use mutex_trylock() and
|
|
|
|
* mutex_lock() instead.
|
|
|
|
*
|
|
|
|
* @param[in] mutex Mutex object to lock. Has to be initialized first.
|
|
|
|
* Must not be NULL.
|
|
|
|
* @param[in] blocking if true, block until mutex is available.
|
|
|
|
*
|
|
|
|
* @return 1 if mutex was unlocked, now it is locked.
|
|
|
|
* @return 0 if the mutex was locked.
|
|
|
|
*/
|
|
|
|
int _mutex_lock(mutex_t *mutex, int blocking);
|
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
/**
|
|
|
|
* @brief Tries to get a mutex, non-blocking.
|
|
|
|
*
|
2014-04-01 16:31:48 +02:00
|
|
|
* @param[in] mutex Mutex object to lock. Has to be initialized first. Must not
|
|
|
|
* be NULL.
|
2010-09-22 15:10:42 +02:00
|
|
|
*
|
|
|
|
* @return 1 if mutex was unlocked, now it is locked.
|
|
|
|
* @return 0 if the mutex was locked.
|
|
|
|
*/
|
2015-12-25 20:59:33 +01:00
|
|
|
static inline int mutex_trylock(mutex_t *mutex)
|
|
|
|
{
|
|
|
|
return _mutex_lock(mutex, 0);
|
|
|
|
}
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
/**
|
2014-07-18 13:44:11 +02:00
|
|
|
* @brief Locks a mutex, blocking.
|
2010-09-22 15:10:42 +02:00
|
|
|
*
|
2014-07-18 13:44:11 +02:00
|
|
|
* @param[in] mutex Mutex object to lock. Has to be initialized first. Must not be NULL.
|
2010-09-22 15:10:42 +02:00
|
|
|
*/
|
2015-12-25 20:59:33 +01:00
|
|
|
static inline void mutex_lock(mutex_t *mutex)
|
|
|
|
{
|
|
|
|
_mutex_lock(mutex, 1);
|
|
|
|
}
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Unlocks the mutex.
|
|
|
|
*
|
2014-04-01 16:31:48 +02:00
|
|
|
* @param[in] mutex Mutex object to unlock, must not be NULL.
|
2010-09-22 15:10:42 +02:00
|
|
|
*/
|
2014-07-18 13:44:11 +02:00
|
|
|
void mutex_unlock(mutex_t *mutex);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-02-18 08:25:16 +01:00
|
|
|
/**
|
|
|
|
* @brief Unlocks the mutex and sends the current thread to sleep
|
|
|
|
*
|
2014-04-01 16:31:48 +02:00
|
|
|
* @param[in] mutex Mutex object to unlock, must not be NULL.
|
2014-02-18 08:25:16 +01:00
|
|
|
*/
|
2014-07-18 13:44:11 +02:00
|
|
|
void mutex_unlock_and_sleep(mutex_t *mutex);
|
2014-02-18 08:25:16 +01:00
|
|
|
|
2014-10-09 01:18:16 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-01-18 13:00:05 +01:00
|
|
|
#endif /* MUTEX_H */
|
2010-09-22 15:10:42 +02:00
|
|
|
/** @} */
|