[vlc-commits] linux: use pthread_once() in config_GetLibDir()

Rémi Denis-Courmont git at videolan.org
Sun Jan 14 19:07:01 CET 2018


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sun Jan 14 20:03:00 2018 +0200| [d36863eac3095ece17f1c5743d0794276e254c8b] | committer: Rémi Denis-Courmont

linux: use pthread_once() in config_GetLibDir()

The mutex was overkill there.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=d36863eac3095ece17f1c5743d0794276e254c8b
---

 src/linux/dirs.c | 43 ++++++++++++++++++++++---------------------
 1 file changed, 22 insertions(+), 21 deletions(-)

diff --git a/src/linux/dirs.c b/src/linux/dirs.c
index 701f1027fc..48ecb2c326 100644
--- a/src/linux/dirs.c
+++ b/src/linux/dirs.c
@@ -24,31 +24,15 @@
 
 #include <stdio.h>
 #include <string.h>
+#include <pthread.h>
 #include <linux/limits.h>
 
 #include <vlc_common.h>
 #include "libvlc.h"
 #include "config/configuration.h"
 
-char *config_GetLibDir (void)
+static char *config_GetLibDirRaw(void)
 {
-    static struct
-    {
-        vlc_mutex_t lock;
-        char path[PATH_MAX];
-    } cache = {
-        VLC_STATIC_MUTEX, "",
-    };
-
-    /* Reading and parsing /proc/self/maps is slow, so cache the value since
-     * it's guaranteed not to change during the life-time of the process. */
-    vlc_mutex_lock(&cache.lock);
-    if (cache.path[0] != '\0')
-    {
-        char *ret = strdup(cache.path);
-        vlc_mutex_unlock(&cache.lock);
-        return ret;
-    }
     char *path = NULL;
 
     /* Find the path to libvlc (i.e. ourselves) */
@@ -92,12 +76,29 @@ char *config_GetLibDir (void)
 error:
     if (path == NULL)
         path = strdup(PKGLIBDIR);
-    if (likely(path != NULL && sizeof(cache.path) > strlen(path)))
-        strcpy(cache.path, path);
-    vlc_mutex_unlock(&cache.lock);
     return path;
 }
 
+static char cached_path[PATH_MAX] = PKGLIBDIR;
+
+static void config_GetLibDirOnce(void)
+{
+    char *path = config_GetLibDirRaw();
+    if (likely(path != NULL && sizeof (cached_path) > strlen(path)))
+        strcpy(cached_path, path);
+    free(path);
+}
+
+char *config_GetLibDir(void)
+{
+    static pthread_once_t once = PTHREAD_ONCE_INIT;
+
+    /* Reading and parsing /proc/self/maps is slow, so cache the value since
+     * it's guaranteed not to change during the life-time of the process. */
+    pthread_once(&once, config_GetLibDirOnce);
+    return strdup(cached_path);
+}
+
 char *config_GetDataDir (void)
 {
     const char *path = getenv ("VLC_DATA_PATH");



More information about the vlc-commits mailing list