Discussion Forum

Ensure thread safety in ARM C/C++ libraries

Next Thread | Thread List | Previous Thread Start a Thread | Settings

DetailsMessage
Read-Only
Author
Jan Raddatz
Posted
29-Jan-2010 16:42 GMT
Toolset
ARM
New! Ensure thread safety in ARM C/C++ libraries

Dear forum,
right now I am struggling with enabling thread safety for my ARM C/C++ libraries.

Here's the problem I am facing:
Actually I am running a multithreaded application on my Cortex-M3 device.
I am facing spontaneous crashes and suspect the source of these crashes rooted inside the C or C++ library.

As a solution I am trying to make the libraries thread safe as described here:
[1]
http://www.keil.com/support/man/docs/armlib/armlib_Chdcgdbh.htm
and here:
[2]
http://www.keil.com/support/man/docs/armlib/armlib_Chdfjddj.htm

Actually I tried to implement [2] by writing
extern "C" int _mutex_initialize(int* p_pMutex)
{ return 1;
}

inside my main.cpp file.
The problem is that _mutex_initialize is never called. Is there a special switch or anything that I have turn to force the C library calling my _mutex_initialize function?

Many thanks in advance and best regards
Jan

Read-Only
Author
David Dominguez
Posted
9-Feb-2010 09:42 GMT
Toolset
ARM
New! RE: Ensure thread safety in ARM C/C++ libraries

Hello

The ARM library references _mutex_initialize with a weak reference.

Therefore, the linker will remove this function unless your code references it. I don't remember how I solved it in my code, but maybe it is as simple as calling it in your main with null arguments.

Remember to return 1 from the function, if you don't the library won't use the mutex.

void main(void)
{ _mutex_initialize(NULL);

....

}

Read-Only
Author
Jaroslav Fojtík
Posted
19-May-2011 10:59 GMT
Toolset
ARM
New! RE: Ensure thread safety in ARM C/C++ libraries

I have obtained solution for this issue from Keil support:

*** 2011-05-19 10:09 BST *** Barth, Andreas/ARM:1187425 andreas.barth@arm.com ***

Dear Jaroslav,
here my explanation of the problem.
No, I think this does not depend on the amount of compiled code.

You can activate this effect also in my example by using the C/C++ option "One ELF section per
function" (which you have also enabled in your project).

Then the following manual entry is also relevant:

http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0206j/CACCEEIF.html

The functions _mutex_acquire and _mutex_release are only weakly referenced by the library. As
there is no other object in the the ELF sections with this function, they will not pulled in by
the linker. This explains the manual entry.

So to solve the problem, don't use "One ELF section per function" or better, use the
__attribute__((used)) on that 2 functions.

__attribute__((used)) void _mutex_release (void* mutex)
{ }

__attribute__((used)) void _mutex_acquire(void* mutex)
{ }

Opposite to this, function _mutex_initialize is strongly referenced by the library and will not
have this problem.

I hope this information explains the situation.

Best regards,
Andreas Barth

Read-Only
Author
Jan Raddatz
Posted
19-May-2011 11:53 GMT
Toolset
ARM
New! RE: Ensure thread safety in ARM C/C++ libraries

Dear Jaroslav,
thanks for the detailed response to my question!

Best regards!
Jan

Next Thread | Thread List | Previous Thread Start a Thread | Settings