locks in newlib

Asked by Martin Velek

Hello,

by getting deeper and deeper into newlib internals I would like to ask how the locks are managed. Currently for ARM, I think that all __lock_release_* functions are NO-OPS (defines in sys/lock). Malloc and env locks are OK but I am not sure if the __lock_release_* can be redefined.
Such definition would bring race condition in some RTOSes, e.g. FreeRTOS. Or I am completely wrong.

Best
Martin

Question information

Language:
English Edit question
Status:
Solved
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Solved by:
Martin Velek
Solved:
Last query:
Last reply:
Revision history for this message
Joey Ye (jinyun-ye) said :
#1

Please define your own lock functions and link them in.

Revision history for this message
Martin Velek (martin-velek) said :
#2

Hi,

unfortunately this will not lead to success. The __lock_release_* functions are by default macros expanding to (void) 0, the names are not even in the exported symbols of libc.a.
The only way is to put your lock.h file into sys/arm/sys/ directory and recompile the library.

The file below is an example of such my-own-implementation of lock.h. My mutex is the int type. Of course, I had to write md_lock_* implementation in the syscall file.

#ifndef __SYS_LOCK_H__
#define __SYS_LOCK_H__

typedef int _LOCK_T;
typedef int _LOCK_RECURSIVE_T;

#include <_ansi.h>

#define __LOCK_INIT(class,lock) static int lock = 0;
#define __LOCK_INIT_RECURSIVE(class,lock) static int lock = 0;

void md_lock_init(_LOCK_T * lock);
void md_lock_init_recursive(_LOCK_RECURSIVE_T * lock);
void md_lock_close(_LOCK_T * lock);
void md_lock_close_recursive(_LOCK_RECURSIVE_T * lock);
void md_lock_acquire(_LOCK_T * lock);
void md_lock_acquire_recursive(_LOCK_RECURSIVE_T * lock);
int md_lock_try_acquire(_LOCK_T * lock);
int md_lock_try_acquire_recursive(_LOCK_RECURSIVE_T * lock);
void md_lock_release(_LOCK_T * lock);
void md_lock_release_recursive(_LOCK_RECURSIVE_T * lock);

#define __lock_init(lock) md_lock_init(&(lock))
#define __lock_init_recursive(lock) md_lock_init_recursive(&(lock))
#define __lock_close(lock) md_lock_close(&(lock))
#define __lock_close_recursive(lock) md_lock_close_recursive(&(lock))
#define __lock_acquire(lock) md_lock_acquire(&(lock))
#define __lock_acquire_recursive(lock) md_lock_acquire_recursive(&(lock))
#define __lock_try_acquire(lock) md_lock_try_acquire(&(lock))
#define __lock_try_acquire_recursive(lock) md_lock_try_acquire_recursive(&(lock))
#define __lock_release(lock) md_lock_release(&(lock))
#define __lock_release_recursive(lock) md_lock_release_recursive(&(lock))

#endif /* __SYS_LOCK_H__ */

Best
Martin