This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Warning: FlashFS is not thread safe when used with ARM libraries

This is just a warning that the latest Keil FlashFS can cause a file system deadlock when used with multiple tasks and the ARM libraries.

The release notes for RL-ARM 3.46 state

[Flash File System]
Added RTX multithreading protection to the File System. This allows you to read and write files or use standard input output from different tasks simultaneously in an RTX environment.

However this is not the case when using RTX and the ARM C libraries. According to Keil this is due to an interaction of their C libraries with FlashFS and they are current unable to resolve this problem.

The following very simple code will cause a mutex lock of the file system.

#include <RTL.h>
#include <91x_lib.H>
#include <stdio.h>

__task void task2 (void) {
    FILE *fp;
    while (1)
        {
        // comment out the following three lines no crash will happen.
        fp=fopen("TEST2.TXT", "r");
        if (fp)
            {
            fclose(fp);
            }
        // to here
        }
}

__task void task1 (void) {

    FILE *fp;
    finit ();
    os_tsk_create(task2, 8);
    while (1)
        {
        fp=fopen("TEST1.TXT", "r");
        if (fp)
            {
            fclose(fp);
            }
        }


}


/*----------------------------------------------------------------------------
 *        Main: Initialize and start RTX Kernel
 *---------------------------------------------------------------------------*/
int main (void) {

    os_sys_init_prio (task1, 8);                    /* Initialize RTX and start init    */

}

I have found that if you put your own mutexes around all file system code then it will work correctly, no longer causing a deadlock. See code below.

#include <RTL.h>
#include <91x_lib.H>
#include <stdio.h>

OS_MUT FS_MUTEX;
#define INIT_FS_LOCK os_mut_init(FS_MUTEX)
#define LOCK_FS os_mut_wait(FS_MUTEX,0xFFFF)
#define RELEASE_FS os_mut_release(FS_MUTEX)

__task void task2 (void) {
    FILE *fp;
    while (1)
        {
        LOCK_FS;
        fp=fopen("TEST2.TXT", "r");
        RELEASE_FS;
        if (fp)
            {
            LOCK_FS;
            fclose(fp);
            RELEASE_FS;
            }
        }
}

__task void task1 (void) {

    FILE *fp;
    finit ();
    INIT_FS_LOCK;
    os_tsk_create(task2, 8);
    while (1)
        {
        LOCK_FS;
        fp=fopen("TEST1.TXT", "r");
        RELEASE_FS;
        if (fp)
            {
            LOCK_FS;
            fclose(fp);
            RELEASE_FS;
            }
        }


}


/*----------------------------------------------------------------------------
 *        Main: Initialize and start RTX Kernel
 *---------------------------------------------------------------------------*/
int main (void) {

    os_sys_init_prio (task1, 8);                    /* Initialize RTX and start init    */

}

This is clearly not ideal but it works.

0