25
25
#include "uthash.h"
26
26
#include <dlfcn.h>
27
27
#include <fcntl.h>
28
+ #include <pthread.h>
28
29
#include <stdarg.h>
29
30
#include <stdbool.h>
30
31
#include <stdio.h>
@@ -78,6 +79,7 @@ struct file {
78
79
};
79
80
80
81
static struct file * g_files = NULL ;
82
+ static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER ;
81
83
82
84
static bool real_entry_points_initialized = false;
83
85
@@ -127,7 +129,9 @@ account_opened_fd(int fd)
127
129
new_file -> dirty = 0 ;
128
130
129
131
struct file * old_file = NULL ;
132
+ pthread_mutex_lock (& g_lock );
130
133
HASH_REPLACE_INT (g_files , fd , new_file , old_file );
134
+ pthread_mutex_unlock (& g_lock );
131
135
132
136
if (old_file != NULL ) {
133
137
LOG_ (" unexpected old_file" );
@@ -211,6 +215,7 @@ close(int fd)
211
215
ensure_entry_points_initialized ();
212
216
213
217
struct file * a_file = NULL ;
218
+ pthread_mutex_lock (& g_lock );
214
219
HASH_FIND_INT (g_files , & fd , a_file );
215
220
if (a_file ) {
216
221
HASH_DEL (g_files , a_file );
@@ -219,6 +224,7 @@ close(int fd)
219
224
} else {
220
225
LOG_ (" mismatched close" );
221
226
}
227
+ pthread_mutex_unlock (& g_lock );
222
228
223
229
return real_close (fd );
224
230
}
@@ -227,7 +233,9 @@ static void
227
233
write_throttle (int fd , ssize_t bytes_written )
228
234
{
229
235
struct file * a_file = NULL ;
236
+ pthread_mutex_lock (& g_lock );
230
237
HASH_FIND_INT (g_files , & fd , a_file );
238
+ pthread_mutex_unlock (& g_lock );
231
239
if (a_file == NULL )
232
240
return ;
233
241
0 commit comments