library: refine support for multiple concurrent access

Our new library's now well protected against potential
problems which arise when a multi-threaded application
opens more than one context within the same API at the
same time. However, with a single-threaded application
designed along those same lines, some problems remain.

So, to avoid potential corruption of some data (which
was classified as local 'static __thread') from those
single-threaded designs, we'll move several variables
to the info structure itself and remove the '__thread'
qualifier. Now they're protected against both designs.

[ we'll not be protected against some multi-threaded ]
[ application that shares a single context yet calls ]
[ that interface from separate threads. this is just ]
[ bad application design & no different than sharing ]
[ other modifiable global data between such threads! ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
Jim Warner
2021-11-10 00:00:00 -05:00
committed by Craig Small
parent 126b14470e
commit eafd8e3112
5 changed files with 19 additions and 19 deletions

View File

@@ -236,6 +236,7 @@ struct vmstat_info {
struct stacks_extent *extents;
struct hsearch_data hashtab;
struct vmstat_result get_this;
time_t sav_secs;
};
@@ -1376,7 +1377,6 @@ PROCPS_EXPORT struct vmstat_result *procps_vmstat_get (
struct vmstat_info *info,
enum vmstat_item item)
{
static __thread time_t sav_secs;
time_t cur_secs;
errno = EINVAL;
@@ -1389,10 +1389,10 @@ PROCPS_EXPORT struct vmstat_result *procps_vmstat_get (
/* we will NOT read the vmstat file with every call - rather, we'll offer
a granularity of 1 second between reads ... */
cur_secs = time(NULL);
if (1 <= cur_secs - sav_secs) {
if (1 <= cur_secs - info->sav_secs) {
if (vmstat_read_failed(info))
return NULL;
sav_secs = cur_secs;
info->sav_secs = cur_secs;
}
info->get_this.item = item;