This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/TM4C129XNCZAD: TI RTL appears to have nested _lock() calls

Part Number: TM4C129XNCZAD

Tool/software: TI C/C++ Compiler

In a related post, I inquired how to use the TI Run Time Library in a multi-threaded environment. I learned that I could use the _register_lock() and _register_unlock() functions to register custom functions to be used by the RTL for locking and unlocking critical sections. I was able to use those functions to get my custom functions registered. The custom lock function pends on an RTOS mutex, and the custom unlock function posts to the mutex.

However, it appears that the RTL has nested calls to the lock() function, which is causing my system to hang up. The printf() function invokes __TI_file_lock() (which in turn calls my custom lock function), and then invokes fputc(), which also invokes __TI_file_lock(). That second call to __TI_file_lock() can never succeed, since the mutex has already been taken higher up in the call stack.

Any ideas how I can make this work?

Regards,

Dave

  • Have a look at the source code for __TI_file_lock, and you'll see that it expects you to use pthreads to achieve the locking. If you instead choose to use the more primitive system-wide lock (installed by _register_(un)lock), you need to install a lock that can handle recursive locking. In your lock function, if the system-wide lock is already held, first check that it's being held by the current thread, and then increment a counter. If this is unacceptable, you need to look at _register_mutex_api, defined in _pthread.c
  • At the end of the section titled Handling Reentrancy in the ARM compiler manual, it says ...

    The run-time-support nests calls to _lock(), so the primitives must keep track of the nesting level.

    The associated example shows one way to track nested calls.

    Thanks and regards,

    -George

  • Thanks for the quick response, Archaeologist. I did a search in the CCS Help, and could not find any detailed info regarding the use of the register_mutex_api. I also searched the Compiler manual and could not find anything besides the system-wide lock that I tried. Is there any documentation about using it, outside of looking at the source code?

    Also, I noticed in the Help that there is a _mutex.c module that is used for heap allocation. If I use the register_mutex_api approach, will that automatcially get used for heap allocation, or do I need to deal with that separately?

    Regards,

    Dave
  • Thanks, George. I missed that last sentence when I looked at it yesterday.

    Regards,

    Dave
  • OK, I think I figured out how to use the _reg_mutex_api stuff. However, when I build I get a linker error saying that _register_mutex_api is an unresolved symbol. There is a comment in _pThread.c that says:

    /****************************************************************************/
    /* The pthread functions implemented by the TI RTS are intended only for */
    /* use by the implementation of the recursive resource mutexes. To enable */
    /* these, the library builder must define the */
    /* "__TI_RECURSIVE_RESOURCE_MUTEXES" compile-time symbol. */
    /****************************************************************************/

    I searched through the compiler/linker options in CCS, but could not find anyplace to enable that option. I tried adding a definition for __TI_RECURSIVE_RESOURCE_MUTEXES in "CCS Build>ARM Compiler>Predfined Symbols", as well as "CCS Build>ARM Linker>Advanced Options>Command File Preprocessing", but neither helped. The "Runtime support library" option in "CCS General>Project" is set to "automatic". How do I get the project to be able to find the _register_mutex_api symbol?

    Regards,

    Dave
  • See latest post.
  • The fastest thing to do would be to copy every library source file that checks the macro __TI_RECURSIVE_RESOURCE_MUTEXES into your project, and then define __TI_RECURSIVE_RESOURCE_MUTEXES on the command line as a user-defined symbol.
  • Thanks! That did the trick.

    Regards,

    Dave