Recursive mutex?
Moderators: RoccoMarco, lbednarz, tfAteba
-
Freddie Chopin
- Posts: 36
- Joined: Wed Oct 17, 2012 8:57 am
Re: Recursive mutex?
Sure you can do that, but all library functions from newlib don't call "malloc", but "_malloc_r", so if you really want to reimplement I'd suggest reimplementing that instead... Same goes for free()/_free_r(). Moreover - you don't reimplement calloc(), realloc() etc. this way, and these functions may be used somewhere (now, in the future) in newlib. The most generic solution is to NOT override malloc() but lock it with mechanism provided.
4\/3!!
4\/3!!
- Prof. Dr. YoMan
- Posts: 57
- Joined: Thu May 24, 2012 11:00 am
- Been thanked: 1 time
- Prof. Dr. YoMan
- Posts: 57
- Joined: Thu May 24, 2012 11:00 am
- Been thanked: 1 time
Re: Recursive mutex?
Hi,
I guess this is the right way:
Correct?
These are all locks which are named in the newlibc documentation.
If I look into the libc.a in GNU-ARM-Embedded\arm-none-eabi\lib\armv7-m (this is the right one for cortex-m3 stm32f?) there are other _lock_... used.
_spf_lock_acquire/release, _sinit_lock_acquire/release, _fp_lock_all, ...
I want to get libc fully integrated. See also here: viewtopic.php?f=2&t=428 about _impure_ptr handling and don't want to miss a thing. What to do?
This newlib stuff looks weird for me.
I guess this is the right way:
Code: Select all
/**
* @file newlib_locking.c
* @brief Recursive mutexes macros and structures. Newlib locking functions.
*/
#include <reent.h>
#include "ch.h"
/**
* @brief Recursive mutex structure.
*/
typedef struct recursive_mutex_s {
Mutex mutex;
uint32_t count;
} recursive_mutex_t;
/**
* @brief Static recursive mutex initializer.
* @details Statically initialized recursive mutexes require no explicit initialization.
*
* @param[in] name the name of the recursive mutex variable
*/
#define EXT_MUTEX_DECL(name) recursive_mutex_t name = { _MUTEX_DATA(name.mutex), 0 }
/**
* @brief Locks the specified recursive mutex.
*
* @param[in] rmp pointer to the @p recursive_mutex_t structure
*/
static void lock_recursive(recursive_mutex_t *rmp) {
chSysLock();
if (chThdSelf() == rmp->mutex.m_owner) {
// Already owned. Recursion.
rmp->count++;
}
else {
// Not owned. Lock.
chMtxLockS(&rmp->mutex);
}
chSysUnlock();
}
/**
* @brief Unlocks the specified recursive mutex.
*
* @param[in] rmp pointer to the @p recursive_mutex_t structure
*/
static void unlock_recursive(recursive_mutex_t *rmp) {
chSysLock();
if (rmp->count == 0) {
// Last owned. Unlock.
chMtxUnlockS();
}
else {
// Still in recursion.
rmp->count--;
}
chSysUnlock();
}
/**
* @brief Recursive mutex for __malloc_lock.
*/
static EXT_MUTEX_DECL(malloc_recursive_mutex);
/**
* @brief Implements __malloc_lock from newlib.
*
* @param[in] reent pointer to the @p _reent structure
*/
void __malloc_lock (struct _reent *reent) {
(void)reent;
lock_recursive(&malloc_recursive_mutex);
}
/**
* @brief Implements __malloc_unlock from newlib.
*
* @param[in] reent pointer to the @p _reent structure
*/
void __malloc_unlock (struct _reent *reent) {
(void)reent;
unlock_recursive(&malloc_recursive_mutex);
}
/**
* @brief Recursive mutex for __env_lock.
*/
static EXT_MUTEX_DECL(env_recursive_mutex);
/**
* @brief Implements __env_lock from newlib.
*
* @param[in] reent pointer to the @p _reent structure
*/
void __env_lock(struct _reent *reent) {
(void)reent;
lock_recursive(&env_recursive_mutex);
}
/**
* @brief Implements __env_unlock from newlib.
*
* @param[in] reent pointer to the @p _reent structure
*/
void __env_unlock (struct _reent *reent) {
(void)reent;
unlock_recursive(&env_recursive_mutex);
}
/**
* @brief Mutex for __tz_lock.
*/
static MUTEX_DECL(tz_mutex);
void __tz_lock (void) {
chMtxLock(&tz_mutex);
}
void __tz_unlock (void) {
chMtxUnlock();
}
Correct?
These are all locks which are named in the newlibc documentation.
If I look into the libc.a in GNU-ARM-Embedded\arm-none-eabi\lib\armv7-m (this is the right one for cortex-m3 stm32f?) there are other _lock_... used.
_spf_lock_acquire/release, _sinit_lock_acquire/release, _fp_lock_all, ...
I want to get libc fully integrated. See also here: viewtopic.php?f=2&t=428 about _impure_ptr handling and don't want to miss a thing. What to do?
This newlib stuff looks weird for me.
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: Recursive mutex?
Looks OK, but I would would modify the counter to be ==1 when the mutex is taken the first time and released in the transition to zero. This way count would mean exactly "count" times.
An implementation like this could be included, the problem is with condition variables, waiting on one of those would release the mutex only once even if it is taken N times, probably an assertion should explicitly prevent this scenario.
Giovanni
An implementation like this could be included, the problem is with condition variables, waiting on one of those would release the mutex only once even if it is taken N times, probably an assertion should explicitly prevent this scenario.
Giovanni
- Prof. Dr. YoMan
- Posts: 57
- Joined: Thu May 24, 2012 11:00 am
- Been thanked: 1 time
Re: Recursive mutex?
Code: Select all
static void lock_recursive(recursive_mutex_t *rmp) {
chSysLock();
if (chThdSelf() != rmp->mutex.m_owner) {
// Not owned. Lock.
chMtxLockS(&rmp->mutex);
}
rmp->count++;
chSysUnlock();
}
static void unlock_recursive(recursive_mutex_t *rmp) {
chSysLock();
if (--rmp->count == 0) {
// Last owned. Unlock.
chMtxUnlockS();
}
chSysUnlock();
}Like this, I guess. Feel free to include it.
I dont understand the problem with the condition variables. Just need to check what they are/do/dont do.
What about the other locks in the newlibc. Can you help out with informations how this works?
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: Recursive mutex?
Yes, like that, there is also one less branch in the code. About condvars, those are a Posix mechanism, you can find a lot of documentation about those.
Giovanni
Giovanni
-
Freddie Chopin
- Posts: 36
- Joined: Wed Oct 17, 2012 8:57 am
Re: Recursive mutex?
Prof. Dr. YoMan wrote:What about the other locks in the newlibc. Can you help out with informations how this works?
You need to recompile the whole library with proper sys/lock.h header in place - these cannot be easily overriden as malloc() locks, because in stub header all these locks are replaced (via #define) with "(void)0".
4\/3!!
- Prof. Dr. YoMan
- Posts: 57
- Joined: Thu May 24, 2012 11:00 am
- Been thanked: 1 time
Re: Recursive mutex?
Ok, but what about the _spf_lock_acquire/release, _sinit_lock_acquire/release, _fp_lock_all, ... which I find in the libc.a and which seem to get used by sprinft and other stuff?
-
Freddie Chopin
- Posts: 36
- Joined: Wed Oct 17, 2012 8:57 am
Re: Recursive mutex?
You can try overriding that, which will probably work, but you cannot override things like initializers - this will have to be done manually.
But... These are just some function-wrappers for a really small subset of newlib's libc locks - check newlib's source and search for strings like "__lock_acquire" or other things from lock.h - these are used too, but the stub replaces them with "(void)0", so these are not even called. That's why to have a proper and complete mechanism, you need to recompile the lib...
4\/3!!
But... These are just some function-wrappers for a really small subset of newlib's libc locks - check newlib's source and search for strings like "__lock_acquire" or other things from lock.h - these are used too, but the stub replaces them with "(void)0", so these are not even called. That's why to have a proper and complete mechanism, you need to recompile the lib...
4\/3!!
- Prof. Dr. YoMan
- Posts: 57
- Joined: Thu May 24, 2012 11:00 am
- Been thanked: 1 time
Re: Recursive mutex?
And even if I do I get the problem with condvars as Giovanni describes above if I extend the mutexes to be recursive. ...
Not nice. For me a fully working libc is needed. Same for libstdc++ even if it is a memory hog. Attaching memory is cheap and if it brings down development time and code readability up I want to use working blocks.
Not nice. For me a fully working libc is needed. Same for libstdc++ even if it is a memory hog. Attaching memory is cheap and if it brings down development time and code readability up I want to use working blocks.