Thread-secure dregister(3) and dunregister(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2014-01-09 00:57:41 +01:00
parent a1ccba00f7
commit ff443c9f5e
3 changed files with 10 additions and 0 deletions

View File

@ -31,6 +31,8 @@ extern "C" { DIR* __firstdir = NULL; }
extern "C" int dcloseall(void)
{
int result = 0;
// We do not lock __firstdir_lock here because this function is called on
// process termination and only one thread can call exit(3).
while ( __firstdir )
result |= closedir(__firstdir);
return result ? EOF : 0;

View File

@ -24,6 +24,9 @@
#include <dirent.h>
#include <DIR.h>
#include <pthread.h>
extern "C" pthread_mutex_t __dirname_lock;
extern "C" void dregister(DIR* dir)
{

View File

@ -24,11 +24,15 @@
#include <dirent.h>
#include <DIR.h>
#include <pthread.h>
extern "C" { pthread_mutex_t __dirname_lock = PTHREAD_MUTEX_INITIALIZER; }
extern "C" void dunregister(DIR* dir)
{
if ( !(dir->flags & _DIR_REGISTERED) )
return;
pthread_mutex_lock(&__dirname_lock);
if ( !dir->prev )
__firstdir = dir->next;
if ( dir->prev )
@ -36,4 +40,5 @@ extern "C" void dunregister(DIR* dir)
if ( dir->next )
dir->next->prev = dir->prev;
dir->flags &= ~_DIR_REGISTERED;
pthread_mutex_unlock(&__dirname_lock);
}