From 3b2e0773ca75e968b2100aba1e0e29bd234ec0e4 Mon Sep 17 00:00:00 2001 From: Our Air Quality Date: Wed, 27 Dec 2017 13:45:11 +1100 Subject: [PATCH] newlib: add an option to combine some locks. The locks are using a bit of the limited ram. It's probably fine to combine some of these to use the same mutex, and this patch does so for the set initialized early. The file locks will still be separate, and dynamically created, so a thread blocking on them will not deadlock all uses of newlib that need a lock. --- core/newlib_syscalls.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/core/newlib_syscalls.c b/core/newlib_syscalls.c index 8fcb49a..e33f923 100644 --- a/core/newlib_syscalls.c +++ b/core/newlib_syscalls.c @@ -239,6 +239,10 @@ extern _lock_t __sinit_recursive_mutex; void init_newlib_locks() { _lock_init(&__arc4random_mutex); + +#if 0 + /* Separate mutex for each lock. + * Each mutex uses about 96 bytes which adds up. */ _lock_init(&__at_quick_exit_mutex); //_lock_init(&__dd_hash_mutex); _lock_init(&__tz_mutex); @@ -248,6 +252,21 @@ void init_newlib_locks() _lock_init_recursive(&__malloc_recursive_mutex); _lock_init_recursive(&__sfp_recursive_mutex); _lock_init_recursive(&__sinit_recursive_mutex); +#else + /* Reuse the same mutex for all these, reducing memory usage. Newlib + * will still allocate other locks dynamically and some of those need + * to be separate such as the file lock where a thread might block with + * them held. */ + __at_quick_exit_mutex = __arc4random_mutex; + //__dd_hash_mutex = __arc4random_mutex; + __tz_mutex = __arc4random_mutex; + + __atexit_recursive_mutex = __arc4random_mutex; + __env_recursive_mutex = __arc4random_mutex; + __malloc_recursive_mutex = __arc4random_mutex; + __sfp_recursive_mutex = __arc4random_mutex; + __sinit_recursive_mutex = __arc4random_mutex; +#endif locks_initialized = 1; }