File system thread safe?

17 Jul 2018

Summary

I'm using the Mbed OS FATFileSystem on an SD card (via sd-driver). Is this thread-safe?

Details

I'm writing to /fs/dir1/file1 from thread 1, and writing to /fs/dir2/file2 from thread 2. The files are in different directories (obviously), but live on the same volume.

I noticed that ChaN's FatFs (which the Mbed OS FATFileSystem wraps) has an FF_FS_REENTRANT option, which Mbed OS doesn't support. Would that be necessary for thread safety?

20 Jul 2018

Hi Brendan,

Yes, the FATFileSystem is thread safe. However, the synchronization is actually implemented in the C++ layer: https://github.com/ARMmbed/mbed-os/blob/master/features/filesystem/fat/FATFileSystem.cpp#L681-L690

ChanN's FF_FS_REENTRANT option would allow us to synchronize the filesystem through the ChaN C API, but synchronizing in the C++ layer allows us to directly use the C++ RTOS APIs and use the same locks for the C++ class's state.

Let us know if that helps clear things up.

20 Jul 2018

Thanks for the info.

I think it's worth a mention in the documentation, or at least in code comments at the function and/or file level. Do you have an opinion on which is more appropriate? I'm wondering whether to report the issue on mbed-os or the handbook.

Also, I'll just add this link for future reference to be clear of what you were pointing at, since master is a moving target. https://github.com/ARMmbed/mbed-os/blob/mbed-os-5.9.3/features/filesystem/fat/FATFileSystem.cpp#L681-L690

23 Jul 2018

Hmm, we could add a note on the functions about thread safety, for example: https://github.com/ARMmbed/mbed-os/blob/master/drivers/Serial.h#L37

More in depth documentation on why we don't turn on REENTRANT could go in a comment in the ffconf.h.

Both of these would go through mbed-os.

But an issue in either place would work.

24 Jul 2018

I've created an issue for this in the documentation repo: https://github.com/ARMmbed/mbed-os-5-docs/issues/638

Please log in to post a reply.