From fde79836e6ebb339f67f99768b5b75dde619779e Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Mon, 19 Apr 2021 22:12:50 +0200 Subject: Try accessing the access lists without locks by making the replacement process as atomic as possible. --- ot_accesslist.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ot_accesslist.c b/ot_accesslist.c index cdb964d..12bd29e 100644 --- a/ot_accesslist.c +++ b/ot_accesslist.c @@ -35,7 +35,7 @@ static int vector_compare_hash(const void *hash1, const void *hash2 ) { /* Read initial access list */ static void accesslist_readfile( void ) { - ot_hash *info_hash, *accesslist_new = NULL; + ot_hash *info_hash, *accesslist_new = NULL, *accesslist_old; char *map, *map_end, *read_offs; size_t maplen; @@ -90,19 +90,19 @@ static void accesslist_readfile( void ) { /* Now exchange the accesslist vector in the least race condition prone way */ pthread_mutex_lock(&g_accesslist_mutex); - free( g_accesslist ); - g_accesslist = accesslist_new; - g_accesslist_size = info_hash - accesslist_new; + + accesslist_old = g_accesslist; /* Keep a copy for later free */ + g_accesslist_size = 0; /* Set size to 0 to prevent clients from searching through uninitialised memory */ + g_accesslist = accesslist_new; /* Only now set a new list */ + g_accesslist_size = info_hash - accesslist_new; /* And finally store it's size */ + free(g_accesslist_old); /* If new list is active, the old one can be destroyed */ pthread_mutex_unlock(&g_accesslist_mutex); } int accesslist_hashisvalid( ot_hash hash ) { void *exactmatch; - /* Lock should hardly ever be contended */ - pthread_mutex_lock(&g_accesslist_mutex); exactmatch = bsearch( hash, g_accesslist, g_accesslist_size, OT_HASH_COMPARE_SIZE, vector_compare_hash ); - pthread_mutex_unlock(&g_accesslist_mutex); #ifdef WANT_ACCESSLIST_BLACK return exactmatch == NULL; -- cgit v1.2.3