diff options
author | Thomas Haller <thaller@redhat.com> | 2017-01-17 17:49:42 (GMT) |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2017-01-17 17:53:54 (GMT) |
commit | 24d669a3a990d1d637fc94698cc4ae2c6e8e178e (patch) | |
tree | fadd57ad1d1521ba1185e9e0c1c01a8d058999cb /lib | |
parent | c33dec5a4a7df01730f90dd41f28a53c188aab46 (diff) | |
download | libnl-24d669a3a990d1d637fc94698cc4ae2c6e8e178e.zip libnl-24d669a3a990d1d637fc94698cc4ae2c6e8e178e.tar.gz libnl-24d669a3a990d1d637fc94698cc4ae2c6e8e178e.tar.bz2 |
lib/utils.c: add mutex to get_psched_settings()
Let's add a mutex to get_psched_settings() hoping to solve worst
case scenarios when calling get_psched_settings() from multiple
threads.
Also, only read the environment variables once, who knows whether
they are modified concurrently.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/utils.c | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/lib/utils.c b/lib/utils.c index 7a791e1..fead196 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -432,13 +432,20 @@ static void get_psched_settings(void) char name[FILENAME_MAX]; FILE *fd; int got_hz = 0; - static int initialized = 0; - if (initialized == 1) { + static volatile int initialized = 0; + const char *ev; + NL_LOCK(mutex); + + if (initialized == 1) return; - } - if (getenv("HZ")) { - long hz = strtol(getenv("HZ"), NULL, 0); + nl_lock(&mutex); + + if (initialized == 1) + return; + + if ((ev = getenv("HZ"))) { + long hz = strtol(ev, NULL, 0); if (LONG_MIN != hz && LONG_MAX != hz) { user_hz = hz; @@ -451,16 +458,15 @@ static void get_psched_settings(void) psched_hz = user_hz; - if (getenv("TICKS_PER_USEC")) { - double t = strtod(getenv("TICKS_PER_USEC"), NULL); + if ((ev = getenv("TICKS_PER_USEC"))) { + double t = strtod(ev, NULL); ticks_per_usec = t; } else { - if (getenv("PROC_NET_PSCHED")) - snprintf(name, sizeof(name), "%s", getenv("PROC_NET_PSCHED")); - else if (getenv("PROC_ROOT")) - snprintf(name, sizeof(name), "%s/net/psched", - getenv("PROC_ROOT")); + if ((ev = getenv("PROC_NET_PSCHED"))) + snprintf(name, sizeof(name), "%s", ev); + else if ((ev = getenv("PROC_ROOT"))) + snprintf(name, sizeof(name), "%s/net/psched", ev); else strncpy(name, "/proc/net/psched", sizeof(name) - 1); @@ -485,6 +491,8 @@ static void get_psched_settings(void) } } initialized = 1; + + nl_unlock(&mutex); } |