library: ensure thread safety via function substitutes
Even though we we had to abandon the master branch top multi-thread effort and even though the newlib version of a multi-threaded top provides no real benefit, that whole exercise was not wasted. Rather, it has revealed some deficiencies in our library which this addresses. If two or more threads in the same address space tried to use procps_loadavg or procps_uptime simultaneously, there's a chance they would experience problems due to thread-unsafe functions our library called internally. So, this patch switches them for thread-safe versions. [ along the way we will also make that procps_uptime ] [ initialization of his 'up' & 'idle' variables mean ] [ something by delaying the -ERANGE return a little. ] Reference(s): https://www.freelists.org/post/procps/a-few-more-patches,7 Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
@@ -124,17 +124,17 @@ PROCPS_EXPORT int procps_loadavg(
|
||||
double *restrict av15)
|
||||
{
|
||||
double avg_1=0, avg_5=0, avg_15=0;
|
||||
char savelocale[128];
|
||||
locale_t tmplocale;
|
||||
int retval=0;
|
||||
|
||||
FILE_TO_BUF(LOADAVG_FILE,loadavg_fd);
|
||||
snprintf(savelocale, sizeof(savelocale), "%s", setlocale(LC_NUMERIC, NULL));
|
||||
setlocale(LC_NUMERIC, "C");
|
||||
if (sscanf(buf, "%lf %lf %lf", &avg_1, &avg_5, &avg_15) < 3) {
|
||||
setlocale(LC_NUMERIC, savelocale);
|
||||
tmplocale = newlocale(LC_NUMERIC_MASK, "C", (locale_t)0);
|
||||
uselocale(tmplocale);
|
||||
if (sscanf(buf, "%lf %lf %lf", &avg_1, &avg_5, &avg_15) < 3)
|
||||
retval = -ERANGE;
|
||||
}
|
||||
setlocale(LC_NUMERIC, savelocale);
|
||||
|
||||
uselocale(LC_GLOBAL_LOCALE);
|
||||
freelocale(tmplocale);
|
||||
SET_IF_DESIRED(av1, avg_1);
|
||||
SET_IF_DESIRED(av5, avg_5);
|
||||
SET_IF_DESIRED(av15, avg_15);
|
||||
|
Reference in New Issue
Block a user