Skip to content
Snippets Groups Projects
Commit e06cc807 authored by Deucе's avatar Deucе :ok_hand_tone4:
Browse files

Use atomics for protected types.

parent 40953725
No related branches found
No related tags found
1 merge request!463MRC mods by Codefenix (2024-10-20)
Pipeline #1352 failed
......@@ -217,6 +217,44 @@ int DLLCALL pthread_mutex_destroy(pthread_mutex_t* mutex)
/* Protected (thread-safe) Integers (e.g. atomic/interlocked variables) */
/************************************************************************/
#ifdef __FreeBSD__
#define PROTECTED_TYPE_INIT(type) \
int DLLCALL protected_##type##_init(protected_##type##_t* prot, type##_t value) \
{ \
atomic_init(prot, value); \
return 0; \
}
PROTECTED_TYPE_INIT(int32)
PROTECTED_TYPE_INIT(int64)
PROTECTED_TYPE_INIT(uint32)
PROTECTED_TYPE_INIT(uint64)
#define PROTECTED_TYPE_SET(type) \
type##_t DLLCALL protected_##type##_set(protected_##type##_t* prot, type##_t value) \
{ \
atomic_store(prot, value); \
return value; \
}
PROTECTED_TYPE_SET(int32)
PROTECTED_TYPE_SET(int64)
PROTECTED_TYPE_SET(uint32)
PROTECTED_TYPE_SET(uint64)
#define PROTECTED_TYPE_ADJUST(type, adjtype) \
type##_t DLLCALL protected_##type##_adjust(protected_##type##_t* prot, adjtype##_t value) \
{ \
type##_t old = atomic_fetch_add(prot, value); \
return old + value; \
}
PROTECTED_TYPE_ADJUST(int32, int32)
PROTECTED_TYPE_ADJUST(int64, int64)
PROTECTED_TYPE_ADJUST(uint32, int32)
PROTECTED_TYPE_ADJUST(uint64, int64)
#else
int DLLCALL protected_int32_init(protected_int32_t* prot, int32_t value)
{
prot->value = value;
......@@ -300,3 +338,4 @@ uint64_t DLLCALL protected_uint64_set(protected_uint64_t* i, uint64_t val)
pthread_mutex_unlock(&i->mutex);
return newval;
}
#endif
......@@ -40,6 +40,10 @@
#include "gen_defs.h" /* HANDLE */
#include "wrapdll.h" /* DLLEXPORT and DLLCALL */
#ifdef __FreeBSD__
#include <stdatomic.h>
#endif
#if defined(__cplusplus)
extern "C" {
#endif
......@@ -148,6 +152,20 @@ DLLEXPORT int DLLCALL pthread_once(pthread_once_t *oc, void (*init)(void));
/* working and being thread-safe on all platforms that support pthread */
/* mutexes. */
/************************************************************************/
#ifdef __FreeBSD__
typedef _Atomic(int32_t) protected_int32_t;
typedef _Atomic(uint32_t) protected_uint32_t;
typedef _Atomic(int64_t) protected_int64_t;
typedef _Atomic(uint64_t) protected_uint64_t;
DLLEXPORT int DLLCALL protected_uint32_init(protected_uint32_t*, uint32_t value);
DLLEXPORT int DLLCALL protected_uint64_init(protected_uint64_t*, uint64_t value);
#define protected_int32_destroy(i) 0
#define protected_uint32_destroy(i) 0
#define protected_int64_destroy(i) 0
#define protected_uint64_destroy(i) 0
#else
typedef struct {
int32_t value;
pthread_mutex_t mutex;
......@@ -168,11 +186,18 @@ typedef struct {
pthread_mutex_t mutex;
} protected_uint64_t;
#define protected_uint32_init(i, val) protected_int32_init((protected_int32_t*)i, val)
#define protected_uint64_init(i, val) protected_int64_init((protected_int64_t*)i, val)
/* Return 0 on success, non-zero on failure (see pthread_mutex_destroy): */
#define protected_int32_destroy(i) pthread_mutex_destroy(&i.mutex)
#define protected_uint32_destroy protected_int32_destroy
#define protected_int64_destroy protected_int32_destroy
#define protected_uint64_destroy protected_int32_destroy
#endif
/* Return 0 on success, non-zero on failure (see pthread_mutex_init): */
DLLEXPORT int DLLCALL protected_int32_init(protected_int32_t*, int32_t value);
#define protected_uint32_init(i, val) protected_int32_init((protected_int32_t*)i, val)
DLLEXPORT int DLLCALL protected_int64_init(protected_int64_t*, int64_t value);
#define protected_uint64_init(i, val) protected_int64_init((protected_int64_t*)i, val)
/* Return new value: */
DLLEXPORT int32_t DLLCALL protected_int32_adjust(protected_int32_t*, int32_t adjustment);
......@@ -188,12 +213,6 @@ DLLEXPORT uint64_t DLLCALL protected_uint64_adjust(protected_uint64_t*, int64_t
DLLEXPORT uint64_t DLLCALL protected_uint64_set(protected_uint64_t*, uint64_t adjustment);
#define protected_uint64_value(i) protected_uint64_adjust(&i,0)
/* Return 0 on success, non-zero on failure (see pthread_mutex_destroy): */
#define protected_int32_destroy(i) pthread_mutex_destroy(&i.mutex)
#define protected_uint32_destroy protected_int32_destroy
#define protected_int64_destroy protected_int32_destroy
#define protected_uint64_destroy protected_int32_destroy
#if defined(__cplusplus)
}
#endif
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment