From d8eadde7d498939c746ea8ddd9dc70a1029b4070 Mon Sep 17 00:00:00 2001 From: Xavi Hernandez Date: Tue, 5 Mar 2019 18:58:20 +0100 Subject: core: avoid dynamic TLS allocation when possible Some interdependencies between logging and memory management functions make it impossible to use the logging framework before initializing memory subsystem because they both depend on Thread Local Storage allocated through pthread_key_create() during initialization. This causes a crash when we try to log something very early in the initialization phase. To prevent this, several dynamically allocated TLS structures have been replaced by static TLS reserved at compile time using '__thread' keyword. This also reduces the number of error sources, making initialization simpler. Updates: bz#1193929 Change-Id: I8ea2e072411e30790d50084b6b7e909c7bb01d50 Signed-off-by: Xavi Hernandez --- .../changelog/lib/src/gf-changelog-helpers.c | 51 ++-------------------- xlators/features/changelog/lib/src/gf-changelog.c | 3 +- 2 files changed, 5 insertions(+), 49 deletions(-) (limited to 'xlators/features/changelog/lib') diff --git a/xlators/features/changelog/lib/src/gf-changelog-helpers.c b/xlators/features/changelog/lib/src/gf-changelog-helpers.c index 7acc5742c25..75f8a6dfc08 100644 --- a/xlators/features/changelog/lib/src/gf-changelog-helpers.c +++ b/xlators/features/changelog/lib/src/gf-changelog-helpers.c @@ -58,20 +58,7 @@ gf_rfc3986_encode_space_newline(unsigned char *s, char *enc, char *estr) * made a part of libglusterfs. */ -static pthread_key_t rl_key; -static pthread_once_t rl_once = PTHREAD_ONCE_INIT; - -static void -readline_destructor(void *ptr) -{ - GF_FREE(ptr); -} - -static void -readline_once(void) -{ - pthread_key_create(&rl_key, readline_destructor); -} +static __thread read_line_t thread_tsd = {}; static ssize_t my_read(read_line_t *tsd, int fd, char *ptr) @@ -91,27 +78,6 @@ my_read(read_line_t *tsd, int fd, char *ptr) return 1; } -static int -gf_readline_init_once(read_line_t **tsd) -{ - if (pthread_once(&rl_once, readline_once) != 0) - return -1; - - *tsd = pthread_getspecific(rl_key); - if (*tsd) - goto out; - - *tsd = GF_CALLOC(1, sizeof(**tsd), gf_changelog_mt_libgfchangelog_rl_t); - if (!*tsd) - return -1; - - if (pthread_setspecific(rl_key, *tsd) != 0) - return -1; - -out: - return 0; -} - ssize_t gf_readline(int fd, void *vptr, size_t maxlen) { @@ -119,10 +85,7 @@ gf_readline(int fd, void *vptr, size_t maxlen) size_t rc = 0; char c = ' '; char *ptr = NULL; - read_line_t *tsd = NULL; - - if (gf_readline_init_once(&tsd)) - return -1; + read_line_t *tsd = &thread_tsd; ptr = vptr; for (n = 1; n < maxlen; n++) { @@ -145,10 +108,7 @@ off_t gf_lseek(int fd, off_t offset, int whence) { off_t off = 0; - read_line_t *tsd = NULL; - - if (gf_readline_init_once(&tsd)) - return -1; + read_line_t *tsd = &thread_tsd; off = sys_lseek(fd, offset, whence); if (off == -1) @@ -163,10 +123,7 @@ gf_lseek(int fd, off_t offset, int whence) int gf_ftruncate(int fd, off_t length) { - read_line_t *tsd = NULL; - - if (gf_readline_init_once(&tsd)) - return -1; + read_line_t *tsd = &thread_tsd; if (sys_ftruncate(fd, 0)) return -1; diff --git a/xlators/features/changelog/lib/src/gf-changelog.c b/xlators/features/changelog/lib/src/gf-changelog.c index 7ed9e553e68..d6acb37f3ef 100644 --- a/xlators/features/changelog/lib/src/gf-changelog.c +++ b/xlators/features/changelog/lib/src/gf-changelog.c @@ -237,9 +237,8 @@ gf_changelog_init_master() { int ret = 0; - mem_pools_init_early(); ret = gf_changelog_init_context(); - mem_pools_init_late(); + mem_pools_init(); return ret; } -- cgit