Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Synchronet
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Main
Synchronet
Commits
e06cc807
Commit
e06cc807
authored
4 years ago
by
Deucе
Browse files
Options
Downloads
Patches
Plain Diff
Use atomics for protected types.
parent
40953725
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!463
MRC mods by Codefenix (2024-10-20)
Pipeline
#1352
failed
4 years ago
Stage: build
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/xpdev/threadwrap.c
+39
-0
39 additions, 0 deletions
src/xpdev/threadwrap.c
src/xpdev/threadwrap.h
+27
-8
27 additions, 8 deletions
src/xpdev/threadwrap.h
with
66 additions
and
8 deletions
src/xpdev/threadwrap.c
+
39
−
0
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
This diff is collapsed.
Click to expand it.
src/xpdev/threadwrap.h
+
27
−
8
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
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment