Page 1 of 1

malloc, threadsafety and others

Posted: Thu May 24, 2012 12:55 pm
by Prof. Dr. YoMan
Hi,

I am new to ChibiOS and got a few questions.
I got a demo for ARMCM3-STM32F103 (OLIMEX Board) up and running.

Background:
I know that there are several allocators and i know that _sbrk_r() is provided in os/various/syscalls.c and that it uses the ChibiCoreAllocator.

Given:
WINDOWS
GNU Tools for ARM Embedded: https://launchpad.net/gcc-arm-embedded (using redhat libc)
GNU make und GNU fileutils/coreutils (primary for rm -rf): http://gnuwin32.sourceforge.net/

1.
When I use malloc (because i want to use a clib-function which uses malloc, there are a lot of them) is this threadsafe?
malloc uses the given _sbrk_r(), which is threadsafe, but is malloc itself threadsafe?
There are malloc_lock(), malloc_unlock() stubs, which can be filled to e.g. lock the scheduler (chSysLock(), chSysUnlock()), but this doesn't seem right.
How do i overwrite the clib-malloc()-funtions with threadsafe chHeadAlloc() and friends?

2.
Does g++ new() use the same clib-malloc()-functions?
If not, how to overwrite with threadsafe ChibiOS ones?

3.
Many clib- and mlib-functions are not threadsafe (global errno e.g.).
From what I see I need to add an "struct _reent" structure of the clib to the THEAD_EXT_FILEDS, Initialize it in THREAD_EXT_INIT_HOCK with _REENT_INIT_PTR and set the _impure_ptr every task switch in THREAD_CONTEXT_SWITCH_HOOK?

I searched thru close to all documentation and found nothing.

Thanks.

PS: I got some background with FreeRTOS, but wanted more.

Re: malloc, threadsafety and others

Posted: Thu May 24, 2012 1:31 pm
by Giovanni
Hi,

malloc() is not thread safe using the default library stubs. About your questions:

1, 3) Correct, newlib can be compiled to be reentrant but this requires to add a structure _reent to each thread (this can be done using the hook THREAD_EXT_FIELDS that extends the Thread structure) then the global pointer _impure_ptr must be updated on context switch using the hook THREAD_CONTEXT_SWITCH_HOOK. Everything would be transparent at application level.
In my opinion the stubs malloc_lock(), malloc_unlock() should be mapped on chSemWait()/chSemSignal() or alternatively on chMtxLock()/chMtxUnlock(), this should ensure atomicity for allocator functions, chSysLock()/chSysUnlock are not recommended for long operations because interrupts are kept disabled during the lock.

2) As far I know the operator new uses the C library.

Giovanni

Re: malloc, threadsafety and others

Posted: Thu May 24, 2012 1:45 pm
by mabl
Giovanni wrote:
Prof. Dr. YoMan wrote:2.
Does g++ new() use the same clib-malloc()-functions?
If not, how to overwrite with threadsafe ChibiOS ones?

2) As far I know the operator new uses the C library.


True, and since new throws exceptions you get yourself in trouble quite easily. BUT you are free to overload new and delete with your own implementation. e.g.

Code: Select all

#include <stdlib.h>
#include <ch.h>

void *operator new(size_t size) {
    return chHeapAlloc(NULL, size);
}

void *operator new[](size_t size) {
  return chHeapAlloc(NULL, size);
}

void operator delete(void *p) {
    chHeapFree(p);
}

void operator delete[](void *p) {
  chHeapFree(p);
}


namespace __gnu_cxx {
  void __verbose_terminate_handler() {
    chSysHalt();
  }
}

(newest version always at https://github.com/mabl/ChibiOS-Infrast ... erator.cpp)

One might even think about using templates coupled with new and chibios pools :mrgreen: But I haven't tried it yet.

Re: malloc, threadsafety and others

Posted: Thu May 24, 2012 1:56 pm
by Prof. Dr. YoMan
Thanks a lot.

It would be nice to find this here:
http://www.chibios.org/dokuwiki/doku.ph ... :documents

Maybe an "Generic article" or in the "HowTo's" Section.

Summary:
It would be the best to use malloc as spare as possible (only by clib-functions) because i cannot overwrite it and to overload new(), new[](), delete() and delete[]()?
No way to force the clib to use chHeap-functions and to avoid malloc_lock()?

@mabl: Thanks for the github mirror, but it lacks the stable_2.4-branch as far as i see, YFYI. You are from Karlsruhe. also?

Re: malloc, threadsafety and others

Posted: Thu May 24, 2012 2:12 pm
by mabl
Prof. Dr. YoMan wrote:@mabl: Thanks for the github mirror, but it lacks the stable_2.4-branch as far as i see, YFYI. You are from Karlsruhe. also?

Yeah, i have not yet figured out how to mirror all branches... Guess I need to brush up my Git mojo. Yes, I'm a physics student living just next to Durlacher Tor. You're also from Karlsruhe? We might open up a local user group :mrgreen:

Re: malloc, threadsafety and others

Posted: Thu May 24, 2012 2:20 pm
by Prof. Dr. YoMan
git push --mirror?
See git help push

Yes. From Karlsruhe, Working close to the Marktplatz. I guess we should do the rest via PM or email. :)

Re: malloc, threadsafety and others

Posted: Fri May 25, 2012 6:45 am
by mabl
Ok, I've figured it out (turns out it's not only git push --mirror) - but now all branches should be synced to github. :D