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
29d56cc3
Commit
29d56cc3
authored
13 years ago
by
rswindell
Browse files
Options
Downloads
Patches
Plain Diff
Added types, functions, macros for creating and using thread-safe protected
(interlocked/atomic) integer variables.
parent
e47fbcf6
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/xpdev/threadwrap.c
+52
-0
52 additions, 0 deletions
src/xpdev/threadwrap.c
src/xpdev/threadwrap.h
+47
-0
47 additions, 0 deletions
src/xpdev/threadwrap.h
with
99 additions
and
0 deletions
src/xpdev/threadwrap.c
+
52
−
0
View file @
29d56cc3
...
...
@@ -188,3 +188,55 @@ int pthread_mutex_destroy(pthread_mutex_t* mutex)
}
#endif
/* POSIX thread mutexes */
/************************************************************************/
/* Protected (thread-safe) Integers (e.g. atomic/interlocked variables) */
/************************************************************************/
int
protected_int32_init
(
protected_int32_t
*
prot
,
int32_t
value
)
{
prot
->
value
=
value
;
return
pthread_mutex_init
(
&
prot
->
mutex
,
NULL
);
}
int
protected_int64_init
(
protected_int64_t
*
prot
,
int64_t
value
)
{
prot
->
value
=
value
;
return
pthread_mutex_init
(
&
prot
->
mutex
,
NULL
);
}
int32_t
protected_int32_adjust
(
protected_int32_t
*
i
,
int32_t
adjustment
)
{
int32_t
newval
;
pthread_mutex_lock
(
&
i
->
mutex
);
newval
=
i
->
value
+=
adjustment
;
pthread_mutex_unlock
(
&
i
->
mutex
);
return
newval
;
}
uint32_t
protected_uint32_adjust
(
protected_uint32_t
*
i
,
int32_t
adjustment
)
{
uint32_t
newval
;
pthread_mutex_lock
(
&
i
->
mutex
);
newval
=
i
->
value
+=
adjustment
;
pthread_mutex_unlock
(
&
i
->
mutex
);
return
newval
;
}
int64_t
protected_int64_adjust
(
protected_int64_t
*
i
,
int64_t
adjustment
)
{
int64_t
newval
;
pthread_mutex_lock
(
&
i
->
mutex
);
newval
=
i
->
value
+=
adjustment
;
pthread_mutex_unlock
(
&
i
->
mutex
);
return
newval
;
}
uint64_t
protected_uint64_adjust
(
protected_uint64_t
*
i
,
int64_t
adjustment
)
{
uint64_t
newval
;
pthread_mutex_lock
(
&
i
->
mutex
);
newval
=
i
->
value
+=
adjustment
;
pthread_mutex_unlock
(
&
i
->
mutex
);
return
newval
;
}
This diff is collapsed.
Click to expand it.
src/xpdev/threadwrap.h
+
47
−
0
View file @
29d56cc3
...
...
@@ -129,6 +129,53 @@ int pthread_mutex_destroy(pthread_mutex_t*);
#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP pthread_mutex_initializer_np(
/* recursive: */
TRUE)
#endif
/************************************************************************/
/* Protected (thread-safe) Integers (e.g. atomic/interlocked variables) */
/************************************************************************/
/* Use of these types and functions is not as fast as your compiler or */
/* platform-specific functions (e.g. InterlockedIncrement on Windows or */
/* atomic_add_int on FreeBSD) but they have the advantage of always */
/* working and being thread-safe on all platforms that support pthread */
/* mutexes. */
/************************************************************************/
typedef
struct
{
int32_t
value
;
pthread_mutex_t
mutex
;
}
protected_int32_t
;
typedef
struct
{
uint32_t
value
;
pthread_mutex_t
mutex
;
}
protected_uint32_t
;
typedef
struct
{
int64_t
value
;
pthread_mutex_t
mutex
;
}
protected_int64_t
;
typedef
struct
{
uint64_t
value
;
pthread_mutex_t
mutex
;
}
protected_uint64_t
;
/* Return 0 on success, non-zero on failure (see pthread_mutex_init): */
int
protected_int32_init
(
protected_int32_t
*
,
int32_t
value
);
#define protected_uint32_init(i, val) protected_int32_init((protected_int32_t*)i, val)
int
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: */
int32_t
protected_int32_adjust
(
protected_int32_t
*
,
int32_t
adjustment
);
uint32_t
protected_uint32_adjust
(
protected_uint32_t
*
,
int32_t
adjustment
);
int64_t
protected_int64_adjust
(
protected_int64_t
*
,
int64_t
adjustment
);
uint64_t
protected_uint64_adjust
(
protected_uint64_t
*
,
int64_t
adjustment
);
/* 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