aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar gingerBill 2016-05-16 19:04:30 +0100
committerGravatar gingerBill 2016-05-16 19:04:30 +0100
commitb50dec34de42c8549bec2eebbf4c6fb8accd494f (patch)
tree07e35f25f1888b280020d3f749440fb8cd1a1326
parentRecursive "Mutex"; Key States; gbRandom (diff)
gb_atomic(32|64)_spin_(lock|unlock)
-rw-r--r--README.md2
-rw-r--r--gb.h36
2 files changed, 35 insertions, 3 deletions
diff --git a/README.md b/README.md
index efbd4f2..5ef526c 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,7 @@ gb single-file public domain libraries for C & C++
library | latest version | category | description
----------------|----------------|----------|-------------
-**gb.h** | 0.15 | misc | Helper library (Standard library _improvement_)
+**gb.h** | 0.15a | misc | Helper library (Standard library _improvement_)
**gb_math.h** | 0.06c | math | Vector math library geared towards game development
**gb_gl.h** | 0.04c | graphics | OpenGL Helper Library
**gb_string.h** | 0.95 | strings | A better string library (this is built into gb.h too with custom allocator support!)
diff --git a/gb.h b/gb.h
index cf60a4a..b378698 100644
--- a/gb.h
+++ b/gb.h
@@ -1,4 +1,4 @@
-/* gb.h - v0.15 - Ginger Bill's C Helper Library - public domain
+/* gb.h - v0.15a - Ginger Bill's C Helper Library - public domain
- no warranty implied; use at your own risk
This is a single header file with a bunch of useful stuff
@@ -37,6 +37,7 @@ Conventions used:
Version History:
+ 0.15a - gb_atomic(32|64)_spin_(lock|unlock)
0.15 - Recursive "Mutex"; Key States; gbRandom
0.14 - Better File Handling and better printf (WIN32 Only)
0.13 - Highly experimental platform layer (WIN32 Only)
@@ -82,10 +83,10 @@ CREDITS
TODOS
- Remove CRT dependency for people who want that
+ - But do I really?
- Older compiler support?
- How old do you wanna go?
- File handling
- - But do I really?
- All files to be UTF-8 (even on windows)
- Better Virtual Memory handling
- Generic Heap Allocator (tcmalloc/dlmalloc/?)
@@ -737,6 +738,9 @@ GB_DEF i32 gb_atomic32_exchanged (gbAtomic32 volatile *a, i32 desired);
GB_DEF i32 gb_atomic32_fetch_add (gbAtomic32 volatile *a, i32 operand);
GB_DEF i32 gb_atomic32_fetch_and (gbAtomic32 volatile *a, i32 operand);
GB_DEF i32 gb_atomic32_fetch_or (gbAtomic32 volatile *a, i32 operand);
+GB_DEF void gb_atomic32_spin_lock (gbAtomic32 volatile *a);
+GB_DEF void gb_atomic32_spin_unlock (gbAtomic32 volatile *a);
+
GB_DEF i64 gb_atomic64_load (gbAtomic64 const volatile *a);
GB_DEF void gb_atomic64_store (gbAtomic64 volatile *a, i64 value);
@@ -745,6 +749,9 @@ GB_DEF i64 gb_atomic64_exchanged (gbAtomic64 volatile *a, i64 desired);
GB_DEF i64 gb_atomic64_fetch_add (gbAtomic64 volatile *a, i64 operand);
GB_DEF i64 gb_atomic64_fetch_and (gbAtomic64 volatile *a, i64 operand);
GB_DEF i64 gb_atomic64_fetch_or (gbAtomic64 volatile *a, i64 operand);
+GB_DEF void gb_atomic64_spin_lock (gbAtomic64 volatile *a);
+GB_DEF void gb_atomic64_spin_unlock (gbAtomic64 volatile *a);
+
@@ -3448,6 +3455,31 @@ gb_atomic64_fetch_or(gbAtomic64 volatile *a, i64 operand)
}
#endif
+gb_inline void
+gb_atomic32_spin_lock(gbAtomic32 volatile *a)
+{
+ a->value = 0;
+ for (;;) {
+ i32 expected = 0;
+ if (gb_atomic32_compare_exchange(a, expected, 1))
+ break;
+ }
+}
+gb_inline void gb_atomic32_spin_unlock(gbAtomic32 volatile *a) { gb_atomic32_store(a, 0); }
+
+gb_inline void
+gb_atomic64_spin_lock(gbAtomic64 volatile *a)
+{
+ a->value = 0;
+ for (;;) {
+ i64 expected = 0;
+ if (gb_atomic64_compare_exchange(a, expected, 1))
+ break;
+ }
+}
+gb_inline void gb_atomic64_spin_unlock(gbAtomic64 volatile *a) { gb_atomic64_store(a, 0); }
+
+
#if defined(GB_SYSTEM_WINDOWS)