Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Synchronet
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
128
Issues
128
List
Boards
Labels
Service Desk
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Environments
Packages & Registries
Packages & Registries
Package Registry
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Main
Synchronet
Compare Revisions
40953725f612005809cbe7ff2404ed7275252c9d...e06cc807457b2cdc79fa04d4cf8001521742adcb
Source
e06cc807457b2cdc79fa04d4cf8001521742adcb
Select Git revision
...
Target
40953725f612005809cbe7ff2404ed7275252c9d
Select Git revision
Compare
Commits (1)
Use atomics for protected types.
· e06cc807
Deucе
authored
Feb 18, 2021
e06cc807
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
8 deletions
+66
-8
src/xpdev/threadwrap.c
src/xpdev/threadwrap.c
+39
-0
src/xpdev/threadwrap.h
src/xpdev/threadwrap.h
+27
-8
No files found.
src/xpdev/threadwrap.c
View file @
e06cc807
...
...
@@ -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
src/xpdev/threadwrap.h
View file @
e06cc807
...
...
@@ -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
...
...