library: ensure thread safety for all static variables

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 access the same api simultaneously, there is a good
chance some function-local static variables will yield
some of those renowned unpredictable results. So, this
patch protects them with the '__thread' storage class.

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:
Jim Warner
2021-09-28 00:00:00 -05:00
committed by Craig Small
parent 69978e3650
commit 23cfb71366
10 changed files with 28 additions and 28 deletions

View File

@@ -49,14 +49,14 @@ static int loadavg_fd = -1;
// and would need 1258 if the obsolete fields were there.
// As of 3.13 /proc/vmstat needs 2623,
// and /proc/stat needs 3076.
static char buf[8192];
static __thread char buf[8192];
/* This macro opens filename only if necessary and seeks to 0 so
* that successive calls to the functions are more efficient.
* It also reads the current contents of the file into the global buf.
*/
#define FILE_TO_BUF(filename, fd) do{ \
static int local_n; \
static __thread int local_n; \
if (fd == -1 && (fd = open(filename, O_RDONLY)) == -1) { \
fputs(BAD_OPEN_MESSAGE, stderr); \
fflush(NULL); \
@@ -158,7 +158,7 @@ PROCPS_EXPORT unsigned int procps_pid_length(void)
{
FILE *fp;
char pidbuf[24];
static int pid_length=0;
static __thread int pid_length=0;
if (pid_length)
return pid_length;