Skip to content
Snippets Groups Projects
Commit 4879ec1b authored by Rob Swindell's avatar Rob Swindell :speech_balloon:
Browse files

Resolve discrepency between std-atomic and mutex-based protected_int*_adjust()

Since commit 692c8128 (4 years ago), the std-atomic and mutex-based
based implementations of protected_int*_adjust() have different in a critical
detail:
The std-atomic verisons return the old/original integer value while the
mutex versions (used in most or all .c files) return the new/adjusted integer
value.

Standardize on the std-atomic pattern: if you want the new/adjusted integer
value, use the protected_int*_adjust_fetch() version of these functions.
parent 2d4af7be
No related branches found
No related tags found
No related merge requests found
...@@ -244,7 +244,49 @@ void protected_int64_init(protected_int64_t* prot, int64_t value) ...@@ -244,7 +244,49 @@ void protected_int64_init(protected_int64_t* prot, int64_t value)
pthread_mutex_init(&prot->mutex, NULL); pthread_mutex_init(&prot->mutex, NULL);
} }
// protected_int*_adjust() return the *old* (original) integer value
int32_t protected_int32_adjust(protected_int32_t* i, int32_t adjustment) int32_t protected_int32_adjust(protected_int32_t* i, int32_t adjustment)
{
int32_t oldval;
pthread_mutex_lock(&i->mutex);
oldval = i->value;
i->value += adjustment;
pthread_mutex_unlock(&i->mutex);
return oldval;
}
uint32_t protected_uint32_adjust(protected_uint32_t* i, int32_t adjustment)
{
uint32_t oldval;
pthread_mutex_lock(&i->mutex);
oldval = i->value;
i->value += adjustment;
pthread_mutex_unlock(&i->mutex);
return oldval;
}
int64_t protected_int64_adjust(protected_int64_t* i, int64_t adjustment)
{
int64_t oldval;
pthread_mutex_lock(&i->mutex);
oldval = i->value;
i->value += adjustment;
pthread_mutex_unlock(&i->mutex);
return oldval;
}
uint64_t protected_uint64_adjust(protected_uint64_t* i, int64_t adjustment)
{
uint64_t oldval;
pthread_mutex_lock(&i->mutex);
oldval = i->value;
i->value += adjustment;
pthread_mutex_unlock(&i->mutex);
return newval;
}
// protected_int*_adjust_fetch() return the *new* (adjusted) integer value
int32_t protected_int32_adjust_fetch(protected_int32_t* i, int32_t adjustment)
{ {
int32_t newval; int32_t newval;
pthread_mutex_lock(&i->mutex); pthread_mutex_lock(&i->mutex);
......
...@@ -152,6 +152,9 @@ DLLEXPORT int pthread_once(pthread_once_t *oc, void (*init)(void)); ...@@ -152,6 +152,9 @@ DLLEXPORT int pthread_once(pthread_once_t *oc, void (*init)(void));
/* atomic_add_int on FreeBSD) but they have the advantage of always */ /* atomic_add_int on FreeBSD) but they have the advantage of always */
/* working and being thread-safe on all platforms that support pthread */ /* working and being thread-safe on all platforms that support pthread */
/* mutexes. */ /* mutexes. */
/* */
/* Note: protected_int*_adjust() return the *old* integer value, while */
/* protected_int*_adjust_fetch() return the *new* integer value. */
/************************************************************************/ /************************************************************************/
#if !__STDC_NO_ATOMICS__ #if !__STDC_NO_ATOMICS__
#ifdef __cplusplus #ifdef __cplusplus
...@@ -252,11 +255,6 @@ typedef struct { ...@@ -252,11 +255,6 @@ typedef struct {
#define protected_int64_value(i) protected_int64_adjust(&(i),0) #define protected_int64_value(i) protected_int64_adjust(&(i),0)
#define protected_uint64_value(i) protected_uint64_adjust(&(i),0) #define protected_uint64_value(i) protected_uint64_adjust(&(i),0)
#define protected_int32_adjust_fetch(a, b) protected_int32_adjust(a, b)
#define protected_uint32_adjust_fetch(a, b) protected_uint32_adjust(a, b)
#define protected_int64_adjust_fetch(a, b) protected_int64_adjust(a, b)
#define protected_uint64_adjust_fetch(a, b) protected_uint64_adjust(a, b)
/* Return 0 on success, non-zero on failure (see pthread_mutex_init): */ /* Return 0 on success, non-zero on failure (see pthread_mutex_init): */
DLLEXPORT void protected_int32_init(protected_int32_t*, int32_t value); DLLEXPORT void protected_int32_init(protected_int32_t*, int32_t value);
DLLEXPORT void protected_int64_init(protected_int64_t*, int64_t value); DLLEXPORT void protected_int64_init(protected_int64_t*, int64_t value);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment