Skip to content

Commit

Permalink
tls: thread-local, revert 1a9b0b6 as double-layer locking is redundant
Browse files Browse the repository at this point in the history
- the 2nd lock was put in place as defensive programming for shm contention
- GH #3695: the underlying issue is early init of thread-locals
  • Loading branch information
space88man committed Jan 5, 2024
1 parent 798cc26 commit 1c70775
Showing 1 changed file with 0 additions and 39 deletions.
39 changes: 0 additions & 39 deletions src/modules/tls/tls_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,6 @@ static void *ser_malloc(size_t size, const char *file, int line)
static ticks_t st = 0;
#endif

if(ksr_tls_init_mode & TLS_MODE_PTHREAD_LOCK_SHM)
pthread_mutex_lock(ksr_tls_lock_shm);

#ifdef RAND_NULL_MALLOC
/* start random null returns only after
* NULL_GRACE_PERIOD from first call */
Expand All @@ -288,8 +285,6 @@ static void *ser_malloc(size_t size, const char *file, int line)
size, file, line, bt_buf);
}
#endif
if(ksr_tls_init_mode & TLS_MODE_PTHREAD_LOCK_SHM)
pthread_mutex_unlock(ksr_tls_lock_shm);
return p;
}

Expand All @@ -303,9 +298,6 @@ static void *ser_realloc(void *ptr, size_t size, const char *file, int line)
static ticks_t st = 0;
#endif

if(ksr_tls_init_mode & TLS_MODE_PTHREAD_LOCK_SHM)
pthread_mutex_lock(ksr_tls_lock_shm);

#ifdef RAND_NULL_MALLOC
/* start random null returns only after
* NULL_GRACE_PERIOD from first call */
Expand Down Expand Up @@ -333,21 +325,14 @@ static void *ser_realloc(void *ptr, size_t size, const char *file, int line)
}
#endif

if(ksr_tls_init_mode & TLS_MODE_PTHREAD_LOCK_SHM)
pthread_mutex_unlock(ksr_tls_lock_shm);

return p;
}

static void ser_free(void *ptr, const char *fname, int fline)
{
if(ksr_tls_init_mode & TLS_MODE_PTHREAD_LOCK_SHM)
pthread_mutex_lock(ksr_tls_lock_shm);
if(ptr) {
shm_free(ptr);
}
if(ksr_tls_init_mode & TLS_MODE_PTHREAD_LOCK_SHM)
pthread_mutex_unlock(ksr_tls_lock_shm);
}

#endif /* LIBRESSL_VERSION_NUMBER */
Expand All @@ -361,46 +346,30 @@ static void *ser_malloc(size_t size)
{
void *p;

if(ksr_tls_init_mode & TLS_MODE_PTHREAD_LOCK_SHM)
pthread_mutex_lock(ksr_tls_lock_shm);
p = shm_malloc(size);
if(ksr_tls_init_mode & TLS_MODE_PTHREAD_LOCK_SHM)
pthread_mutex_unlock(ksr_tls_lock_shm);
return p;
}


static void *ser_realloc(void *ptr, size_t size)
{
void *p;
if(ksr_tls_init_mode & TLS_MODE_PTHREAD_LOCK_SHM)
pthread_mutex_lock(ksr_tls_lock_shm);
p = shm_realloc(ptr, size);
if(ksr_tls_init_mode & TLS_MODE_PTHREAD_LOCK_SHM)
pthread_mutex_unlock(ksr_tls_lock_shm);
return p;
}
#else
static void *ser_malloc(size_t size, const char *fname, int fline)
{
void *p;
if(ksr_tls_init_mode & TLS_MODE_PTHREAD_LOCK_SHM)
pthread_mutex_lock(ksr_tls_lock_shm);
p = shm_malloc(size);
if(ksr_tls_init_mode & TLS_MODE_PTHREAD_LOCK_SHM)
pthread_mutex_unlock(ksr_tls_lock_shm);
return p;
}


static void *ser_realloc(void *ptr, size_t size, const char *fname, int fline)
{
void *p;
if(ksr_tls_init_mode & TLS_MODE_PTHREAD_LOCK_SHM)
pthread_mutex_lock(ksr_tls_lock_shm);
p = shm_realloc(ptr, size);
if(ksr_tls_init_mode & TLS_MODE_PTHREAD_LOCK_SHM)
pthread_mutex_unlock(ksr_tls_lock_shm);
return p;
}
#endif
Expand All @@ -417,24 +386,16 @@ static void ser_free(void *ptr)
* As shm_free() aborts on null pointers, we have to check for null pointer
* here in the wrapper function.
*/
if(ksr_tls_init_mode & TLS_MODE_PTHREAD_LOCK_SHM)
pthread_mutex_lock(ksr_tls_lock_shm);
if(ptr) {
shm_free(ptr);
}
if(ksr_tls_init_mode & TLS_MODE_PTHREAD_LOCK_SHM)
pthread_mutex_unlock(ksr_tls_lock_shm);
}
#else
static void ser_free(void *ptr, const char *fname, int fline)
{
if(ksr_tls_init_mode & TLS_MODE_PTHREAD_LOCK_SHM)
pthread_mutex_lock(ksr_tls_lock_shm);
if(ptr) {
shm_free(ptr);
}
if(ksr_tls_init_mode & TLS_MODE_PTHREAD_LOCK_SHM)
pthread_mutex_unlock(ksr_tls_lock_shm);
}
#endif

Expand Down

0 comments on commit 1c70775

Please sign in to comment.