tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: fd code multithreaded race?

On Wed, 4 Aug 2010 17:06:59 +0300
Antti Kantee <> wrote:
> Never realized file descriptors and threads were so tricky ;)

I'm just finishing writing multi-threaded logging subsystem for one of
my projects. I think I faced a similar issue with threads and shared
file descriptors. Threads can log data to the same file, or different
files on the filesystem. They can log data to one file, or many
different files simultaneously.

I implemented a new type, which is basically a structure that contains
mutex, file descriptor, file path string and reference counter. Any
thread doing I/O on a particular descriptor has to lock its mutex.
Since, logging data can remain in buffers, waiting to be flushed,
threads increment descriptors' reference counters, when they flush
buffers they decrement those counters. This ensures that descriptors
are not freed from memory, until reference counter drops to 0. If the
value of the descriptor == -1, thread holding the lock calls open()
with the file path as the first argument.

My code uses a hash table to lookup descriptors, however I don't use
the value of an open file descriptor (which can change between
multiple open()/close() calls), but the address of the structure that
describes a particular file descriptor. 

Home | Main Index | Thread Index | Old Index