How to implement thread safety for the tree structure used for the following scenario?
I am making use of the C# code located at the following links to implement a Ram-disk project.
- Link to description of source code
- Link to source code
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 simultaneousl开发者_JAVA技巧y), 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.
The easiest way to do this would be to protect the entire tree with a ReaderWriterLockSlim. That allows concurrent access by multiple readers or exclusive access by a single writer. Any method that will modify the structure in any way will have to acquire the write lock, and no other threads will be allowed to read or write to the structure until that thread releases the write lock.
Any thread that wants to read the structure has to acquire the read lock. Multiple readers can acquire the read lock concurrently, but if a thread wants to acquire the write lock--which means waiting until all existing read locks are released.
There might be a way to make that data structure lock-free. Doing so, however, could be quite difficult. The reader/writer lock will give you the functionality you want, and I suspect it would be fast enough.
If you want to share this across processes, that's another story. The ReaderWriterLockSlim
doesn't work across processes. You could, however, implement something similar using a combination of the synchronization primitives, or create a device driver (or service) that serves the requests, thereby keeping it all in the same process.
精彩评论