Skip to content

Commit 12c0fd1

Browse files
committed
guard hash access with locks
1 parent 1e8ad32 commit 12c0fd1

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.6)
33

44
set(CMAKE_C_STANDARD 99)
55

6+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
67
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-unused-parameter")
78
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=format")
89
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,--unresolved-symbols=report-all")

autofsync.c

+8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "uthash.h"
2626
#include <dlfcn.h>
2727
#include <fcntl.h>
28+
#include <pthread.h>
2829
#include <stdarg.h>
2930
#include <stdbool.h>
3031
#include <stdio.h>
@@ -78,6 +79,7 @@ struct file {
7879
};
7980

8081
static struct file *g_files = NULL;
82+
static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
8183

8284
static bool real_entry_points_initialized = false;
8385

@@ -127,7 +129,9 @@ account_opened_fd(int fd)
127129
new_file->dirty = 0;
128130

129131
struct file *old_file = NULL;
132+
pthread_mutex_lock(&g_lock);
130133
HASH_REPLACE_INT(g_files, fd, new_file, old_file);
134+
pthread_mutex_unlock(&g_lock);
131135

132136
if (old_file != NULL) {
133137
LOG_(" unexpected old_file");
@@ -211,6 +215,7 @@ close(int fd)
211215
ensure_entry_points_initialized();
212216

213217
struct file *a_file = NULL;
218+
pthread_mutex_lock(&g_lock);
214219
HASH_FIND_INT(g_files, &fd, a_file);
215220
if (a_file) {
216221
HASH_DEL(g_files, a_file);
@@ -219,6 +224,7 @@ close(int fd)
219224
} else {
220225
LOG_(" mismatched close");
221226
}
227+
pthread_mutex_unlock(&g_lock);
222228

223229
return real_close(fd);
224230
}
@@ -227,7 +233,9 @@ static void
227233
write_throttle(int fd, ssize_t bytes_written)
228234
{
229235
struct file *a_file = NULL;
236+
pthread_mutex_lock(&g_lock);
230237
HASH_FIND_INT(g_files, &fd, a_file);
238+
pthread_mutex_unlock(&g_lock);
231239
if (a_file == NULL)
232240
return;
233241

0 commit comments

Comments
 (0)