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

Use macros for all the atomic things.

parent b4e6d685
No related branches found
No related tags found
1 merge request!463MRC mods by Codefenix (2024-10-20)
Pipeline #1358 passed
......@@ -57,8 +57,8 @@
lprintf(LOG_ERR, "%04d SSH Error %d destroying Cryptlib Session %d from line %d"
, sock, result, session, line);
else {
int32_t remain = protected_uint32_adjust(&ssh_sessions, -1);
lprintf(LOG_DEBUG, "%04d SSH Cryptlib Session: %d destroyed from line %d (%d remain)"
uint32_t remain = protected_uint32_adjust(&ssh_sessions, -1);
lprintf(LOG_DEBUG, "%04d SSH Cryptlib Session: %d destroyed from line %d (%u remain)"
, sock, session, line, remain);
}
}
......
......@@ -217,44 +217,7 @@ 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
#ifndef __FreeBSD__
int DLLCALL protected_int32_init(protected_int32_t* prot, int32_t value)
{
prot->value = value;
......
......@@ -163,20 +163,41 @@ typedef std::atomic<int32_t> protected_int32_t;
typedef std::atomic<uint32_t> protected_uint32_t;
typedef std::atomic<int64_t> protected_int64_t;
typedef std::atomic<uint64_t> protected_uint64_t;
#define protected_int32_init(pval, val) (std::atomic_store<int32_t>(pval, val), 0)
#define protected_uint32_init(pval, val) (std::atomic_store<uint32_t>(pval, val), 0)
#define protected_int64_init(pval, val) (std::atomic_store<int64_t>(pval, val), 0)
#define protected_uint64_init(pval, val) (std::atomic_store<uint64_t>(pval, val), 0)
#define protected_int32_adjust(pval, adj) (std::atomic_fetch_add<int32_t>(pval, adj) + adj)
#define protected_uint32_adjust(pval, adj) (std::atomic_fetch_add<uint32_t>(pval, adj) + adj)
#define protected_int64_adjust(pval, adj) (std::atomic_fetch_add<int64_t>(pval, adj) + adj)
#define protected_uint64_adjust(pval, adj) (std::atomic_fetch_add<uint64_t>(pval, adj) + adj)
#define protected_int32_value(val) std::atomic_load<int32_t>(&val)
#define protected_uint32_value(val) std::atomic_load<uint32_t>(&val)
#define protected_int64_value(val) std::atomic_load<int64_t>(&val)
#define protected_uint64_value(val) std::atomic_load<uint64_t>(&val)
#else
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;
#endif
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_init(pval, val) (atomic_init(pval, val), 0)
#define protected_uint32_init(pval, val) (atomic_init(pval, val), 0)
#define protected_int64_init(pval, val) (atomic_init(pval, val), 0)
#define protected_uint64_init(pval, val) (atomic_init(pval, val), 0)
#define protected_int32_adjust(pval, adj) (atomic_fetch_add(pval, adj) + adj)
#define protected_uint32_adjust(pval, adj) (atomic_fetch_add(pval, adj) + adj)
#define protected_int64_adjust(pval, adj) (atomic_fetch_add(pval, adj) + adj)
#define protected_uint64_adjust(pval, adj) (atomic_fetch_add(pval, adj) + adj)
#define protected_int32_value(val) atomic_load(&val)
#define protected_uint32_value(val) atomic_load(&val)
#define protected_int64_value(val) atomic_load(&val)
#define protected_uint64_value(val) atomic_load(&val)
#endif
#define protected_int32_destroy(i) 0
#define protected_uint32_destroy(i) 0
......@@ -214,7 +235,6 @@ typedef struct {
#define protected_uint32_value(i) protected_uint32_adjust(&i,0)
#define protected_int64_value(i) protected_int64_adjust(&i,0)
#define protected_uint64_value(i) protected_uint64_adjust(&i,0)
#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);
......@@ -230,6 +250,8 @@ DLLEXPORT int64_t DLLCALL protected_int64_set(protected_int64_t*, int64_t val);
DLLEXPORT uint64_t DLLCALL protected_uint64_adjust(protected_uint64_t*, int64_t adjustment);
DLLEXPORT uint64_t DLLCALL protected_uint64_set(protected_uint64_t*, uint64_t adjustment);
#endif
#if defined(__cplusplus)
}
#endif
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment