agendafs

A filesystem for your calendar.

git clone git://mccd.space/agendafs
commit 23be0a8ee52c977ab3dc66da42c14ef11b7823a1
parent c1f9df1499a1e706b47cce91d7b642ddb61c1b7c
Author: Marc Coquand <marc@coquand.email>
Date:   Tue, 29 Jul 2025 11:14:34 +0200

Switch to rwlock instead of mutex lock

Diffstat:
Mmain.c | 62+++++++++++++++++++++++++++++++-------------------------------
1 file changed, 31 insertions(+), 31 deletions(-)
diff --git a/main.c b/main.c
@@ -28,14 +28,14 @@
 
 // Listening to file changes of the original content
 #define EVENT_BUF_LEN (1024 * (sizeof(struct inotify_event) + 16))
-static pthread_mutex_t entries_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_rwlock_t entries_lock = PTHREAD_RWLOCK_INITIALIZER;
 
 static int
 fuse_getattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi)
 {
 	LOG("STAT %s", path);
 
-	pthread_mutex_lock(&entries_mutex);
+	pthread_rwlock_rdlock(&entries_lock);
 	memory_region *mreg = create_region();
 	int ret_code = 0;
 
@@ -68,7 +68,7 @@ fuse_getattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi)
 	stbuf->st_atime = time(NULL);
 
 cleanup_return:
-	pthread_mutex_unlock(&entries_mutex);
+	pthread_rwlock_unlock(&entries_lock);
 	rfree_all(mreg);
 	return ret_code;
 }
@@ -87,7 +87,7 @@ fuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
 {
 
 	LOG("READDIR %s", path);
-	pthread_mutex_lock(&entries_mutex);
+	pthread_rwlock_rdlock(&entries_lock);
 	memory_region *mreg = create_region();
 	int status = 0;
 
@@ -114,7 +114,7 @@ fuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
 	}
 
 cleanup_return:
-	pthread_mutex_unlock(&entries_mutex);
+	pthread_rwlock_unlock(&entries_lock);
 	rfree_all(mreg);
 	return status;
 }
@@ -125,7 +125,7 @@ fuse_open(const char *path, struct fuse_file_info *fi)
 	LOG("OPEN %s", path);
 
 	memory_region *mreg = create_region();
-	pthread_mutex_lock(&entries_mutex);
+	pthread_rwlock_rdlock(&entries_lock);
 	int status = 0;
 
 	const struct tree_node *node = get_node_by_path(mreg, path);
@@ -134,7 +134,7 @@ fuse_open(const char *path, struct fuse_file_info *fi)
 		status = -ENOENT;
 	}
 
-	pthread_mutex_unlock(&entries_mutex);
+	pthread_rwlock_unlock(&entries_lock);
 	rfree_all(mreg);
 
 	return status;
@@ -146,12 +146,12 @@ fuse_mkdir(const char *path, mode_t mode)
 	LOG("MKDIR %s", path);
 
 	memory_region *mreg = create_region();
-	pthread_mutex_lock(&entries_mutex);
+	pthread_rwlock_wrlock(&entries_lock);
 	int status = 0;
 
 	status = create_entry_from_fuse(mreg, path, ENTRY_DIRECTORY);
 
-	pthread_mutex_unlock(&entries_mutex);
+	pthread_rwlock_unlock(&entries_lock);
 	rfree_all(mreg);
 	return status;
 }
@@ -162,7 +162,7 @@ fuse_read(const char *path, char *buf, size_t size, off_t offset,
 {
 	LOG("READ called: offset=%ld, size=%zu", offset, size);
 	memory_region *mreg = create_region();
-	pthread_mutex_lock(&entries_mutex);
+	pthread_rwlock_rdlock(&entries_lock);
 	int ret_i = 0;
 
 	const struct tree_node *n = get_node_by_path(mreg, path);
@@ -198,7 +198,7 @@ fuse_read(const char *path, char *buf, size_t size, off_t offset,
 	ret_i = bytes_to_read;
 
 cleanup_return:
-	pthread_mutex_unlock(&entries_mutex);
+	pthread_rwlock_unlock(&entries_lock);
 	rfree_all(mreg);
 	return ret_i;
 }
@@ -211,7 +211,7 @@ fuse_write(const char *path, const char *buf, size_t size, off_t offset,
 	LOG("WRITE %s, %zu, %zu", buf, size, offset);
 
 	memory_region *mreg = create_region();
-	pthread_mutex_lock(&entries_mutex);
+	pthread_rwlock_wrlock(&entries_lock);
 	int ret_i = 0;
 
 	if (pathIsHidden(path)) {
@@ -237,7 +237,7 @@ fuse_write(const char *path, const char *buf, size_t size, off_t offset,
 	ret_i = ret_i >= 0 ? size : ret_i;
 
 cleanup_return:
-	pthread_mutex_unlock(&entries_mutex);
+	pthread_rwlock_unlock(&entries_lock);
 	rfree_all(mreg);
 	return ret_i;
 }
@@ -249,7 +249,7 @@ fuse_unlink(const char *file)
 	LOG("DELETE %s", file);
 
 	memory_region *mreg = create_region();
-	pthread_mutex_lock(&entries_mutex);
+	pthread_rwlock_wrlock(&entries_lock);
 	int res = 0;
 
 	struct tree_node *node = get_node_by_path(mreg, file);
@@ -267,7 +267,7 @@ fuse_unlink(const char *file)
 	res = delete_vdir_entry(mreg, node);
 
 cleanup_return:
-	pthread_mutex_unlock(&entries_mutex);
+	pthread_rwlock_unlock(&entries_lock);
 	rfree_all(mreg);
 	return res;
 }
@@ -278,7 +278,7 @@ fuse_rmdir(const char *filepath)
 	LOG("DELETE %s", filepath);
 
 	memory_region *mreg = create_region();
-	pthread_mutex_lock(&entries_mutex);
+	pthread_rwlock_wrlock(&entries_lock);
 	int res = 0;
 
 	struct tree_node *node = get_node_by_path(mreg, filepath);
@@ -301,7 +301,7 @@ fuse_rmdir(const char *filepath)
 	res = delete_vdir_entry(mreg, node);
 
 cleanup_return:
-	pthread_mutex_unlock(&entries_mutex);
+	pthread_rwlock_unlock(&entries_lock);
 	rfree_all(mreg);
 	return res;
 }
@@ -311,7 +311,7 @@ fuse_rename(const char *old, const char *new, unsigned int flags)
 {
 
 	memory_region *mreg = create_region();
-	pthread_mutex_lock(&entries_mutex);
+	pthread_rwlock_wrlock(&entries_lock);
 	LOG("RENAME %s -> %s", old, new);
 	int res = 0;
 
@@ -330,7 +330,7 @@ fuse_rename(const char *old, const char *new, unsigned int flags)
 	res = do_agenda_rename(mreg, old, new);
 
 cleanup_return:
-	pthread_mutex_unlock(&entries_mutex);
+	pthread_rwlock_unlock(&entries_lock);
 	rfree_all(mreg);
 	return res;
 }
@@ -340,7 +340,7 @@ fuse_create(const char *filepath, mode_t mode, struct fuse_file_info *info)
 {
 
 	memory_region *mreg = create_region();
-	pthread_mutex_lock(&entries_mutex);
+	pthread_rwlock_wrlock(&entries_lock);
 	LOG("CREATE %s", filepath);
 
 	int status = 0;
@@ -361,7 +361,7 @@ fuse_create(const char *filepath, mode_t mode, struct fuse_file_info *info)
 	create_entry_from_fuse(mreg, filepath, ENTRY_FILE);
 
 cleanup_return:
-	pthread_mutex_unlock(&entries_mutex);
+	pthread_rwlock_unlock(&entries_lock);
 	rfree_all(mreg);
 	return status;
 }
@@ -372,7 +372,7 @@ fuse_removexattr(const char *path, const char *attribute)
 	LOG("REMOVEXATTR '%s' '%s'", path, attribute);
 	memory_region *mreg = create_region();
 	int status = -EINVAL;
-	pthread_mutex_lock(&entries_mutex);
+	pthread_rwlock_wrlock(&entries_lock);
 	struct tree_node *node = get_node_by_path(mreg, path);
 	if (!node) {
 		status = -ENONET;
@@ -397,7 +397,7 @@ fuse_removexattr(const char *path, const char *attribute)
 	}
 
 cleanup_return:
-	pthread_mutex_unlock(&entries_mutex);
+	pthread_rwlock_unlock(&entries_lock);
 	rfree_all(mreg);
 	return status;
 }
@@ -409,7 +409,7 @@ fuse_setxattr(const char *path, const char *attribute, const char *value,
 	LOG("SETXATTR '%s' '%s' '%zu' '%s' '%d'", path, attribute, s, attribute,
 	    flags);
 	memory_region *mreg = create_region();
-	pthread_mutex_lock(&entries_mutex);
+	pthread_rwlock_wrlock(&entries_lock);
 	int status = 0;
 
 	// Limit imposed in xattr(7)
@@ -454,7 +454,7 @@ fuse_setxattr(const char *path, const char *attribute, const char *value,
 	}
 
 cleanup_return:
-	pthread_mutex_unlock(&entries_mutex);
+	pthread_rwlock_unlock(&entries_lock);
 	rfree_all(mreg);
 	return status;
 }
@@ -464,7 +464,7 @@ fuse_listxattr(const char *path, char *list, size_t size)
 {
 	LOG("LISTXATTR %s %zu", list, size);
 	memory_region *mreg = create_region();
-	pthread_mutex_lock(&entries_mutex);
+	pthread_rwlock_rdlock(&entries_lock);
 	int return_i = 0;
 
 	FILE *stream;
@@ -525,7 +525,7 @@ cleanup_return:
 	free(membuffer);
 cleanup_return_2:
 	rfree_all(mreg);
-	pthread_mutex_unlock(&entries_mutex);
+	pthread_rwlock_unlock(&entries_lock);
 	return return_i;
 }
 
@@ -533,7 +533,7 @@ static int
 fuse_getxattr(const char *path, const char *attribute, char *buf, size_t s)
 {
 	LOG("GETXATTR '%s' '%s' '%zu'\n", path, attribute, s);
-	pthread_mutex_lock(&entries_mutex);
+	pthread_rwlock_rdlock(&entries_lock);
 	memory_region *mreg = create_region();
 	int status = 0;
 
@@ -600,7 +600,7 @@ fuse_getxattr(const char *path, const char *attribute, char *buf, size_t s)
 	}
 
 cleanup_return:
-	pthread_mutex_unlock(&entries_mutex);
+	pthread_rwlock_unlock(&entries_lock);
 	rfree_all(mreg);
 	return status;
 }
@@ -612,7 +612,7 @@ handle_vdir_event(struct inotify_event *event)
 		return;
 
 	memory_region *mreg = create_region();
-	pthread_mutex_lock(&entries_mutex);
+	pthread_rwlock_wrlock(&entries_lock);
 
 	LOG("Detected change in ICS file: %s\n", event->name);
 
@@ -632,7 +632,7 @@ handle_vdir_event(struct inotify_event *event)
 		update_or_create_fuse_entry_from_vdir(mreg, full_path);
 	}
 
-	pthread_mutex_unlock(&entries_mutex);
+	pthread_rwlock_unlock(&entries_lock);
 	rfree_all(mreg);
 }