I am making use of the C# code located at the following links to implement a Ram-disk project.
As a summary, the code indicated above makes use of a simple tree structure to store the directories, sub-directories and files. At the root is a MemoryFolder
object which stores zero or more 'MemoryFolder' objects and/or MemoryFile
objects. Each MemoryFolder
object in turn stores zero or more MemoryFolder
objects and/or MemoryFile
objects and so forth up to an unlimited depth.
However, the code is not thread safe. What is the most elegant way of implementing thread safety? In addition, how should the following non-exhaustive list of multithreading requirements for a typical file system be enforced by using the appropriate locking strategy?
The creation of two different folder (each by a different thread) simultaneously under the same parent folder can occur concurrently if the thread safe implementation allows it. Otherwise, some locking strategy should be implemented to only allow sequential creation.
None of the direct or indirect parent folders of the folder containing a specific file (that is currently read by another thread) propagating all the way up to the root folder can be moved or deleted by another thread until the
ReadFile
thread completes its execution.With regards to each unique file, allows concurrent access for multiple
ReadFile
threads but restricting access to a singleWriteFile
thread.If two separate
ReadFile
threads (fired almost simultaneously), each from a different application attempts to create a folder with the same name (assuming that the folder does not already exist before both threads are fired), the first thread that enters the Ram-Disk always succeeds while the second one always fails. In other words, the order of thread execution is deterministic.The total disk space calculation method
GetDiskFreeSpace
running under a separate thread should not complete its execution until allWriteFile
threads that are already in progress complete its execution. All subsequentWriteFile
threads that have not begun executing are blocked until theGetDiskFreeSpace
thread completes its execution.