When I was recompiling my FUSE filesystem on new workstation I noticed deprecation warnings regarding readdir_r
. Since readdir
is not MT Safe and requires own synchronization I'm a bit puzzled.
I'm not sure what exactly is not MT Safe - is it readdir
alone or is it entire process of reading certain directory until the end? It seems that standard tries to justify this decision by pointing out that readdir
is MT Safe if it's called on different directories but that's obviously not safe assumption if your "program" is multi-user overlay FUSE filesystem. So should I put entire directory reading procedure in critical section or just individual readdir
calls?
Both cases to be honest sound to me pretty bad from performance standpoint - are my concerns valid or are there some other bottlenecks in Linux kernel that make it impossible anyways to perform parallel read on single directory by more than one process/thread?
In the current POSIX.1 specification (POSIX.1-2008), readdir(3) is not required to be thread-safe. However, in modern implementations (including the glibc implementation), concurrent calls to readdir(3) that specify different directory streams are thread-safe.
The man page says it's thread safe (assuming you are using glibc, which I think is a fair assumption on Linux) -- provided you are using different directory streams - not different directory.
A directory stream is the DIR * parameter to readdir.