Mercurial > foo_out_sdl
comparison foosdk/sdk/pfc/lockless.h @ 1:20d02a178406 default tip
*: check in everything else
yay
| author | Paper <paper@tflc.us> |
|---|---|
| date | Mon, 05 Jan 2026 02:15:46 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 0:e9bb126753e7 | 1:20d02a178406 |
|---|---|
| 1 #pragma once | |
| 2 | |
| 3 #ifdef _MSC_VER | |
| 4 #include <intrin.h> | |
| 5 #endif | |
| 6 namespace pfc { | |
| 7 | |
| 8 class threadSafeInt { | |
| 9 public: | |
| 10 typedef long val_t; | |
| 11 typedef val_t t_val; | |
| 12 | |
| 13 threadSafeInt(t_val p_val = 0) : m_val(p_val) {} | |
| 14 long operator++() throw() { return inc(); } | |
| 15 long operator--() throw() { return dec(); } | |
| 16 long operator++(int) throw() { return inc() - 1; } | |
| 17 long operator--(int) throw() { return dec() + 1; } | |
| 18 operator t_val() const throw() { return m_val; } | |
| 19 | |
| 20 static val_t exchangeHere( volatile val_t & here, val_t newVal ) { | |
| 21 #ifdef _MSC_VER | |
| 22 return InterlockedExchange(&here, newVal); | |
| 23 #else | |
| 24 return __sync_lock_test_and_set(&here, newVal); | |
| 25 #endif | |
| 26 } | |
| 27 | |
| 28 t_val exchange(t_val newVal) { | |
| 29 return exchangeHere( m_val, newVal ); | |
| 30 } | |
| 31 private: | |
| 32 t_val inc() { | |
| 33 #ifdef _MSC_VER | |
| 34 return _InterlockedIncrement(&m_val); | |
| 35 #else | |
| 36 return __sync_add_and_fetch(&m_val, 1); | |
| 37 #endif | |
| 38 } | |
| 39 t_val dec() { | |
| 40 #ifdef _MSC_VER | |
| 41 return _InterlockedDecrement(&m_val); | |
| 42 #else | |
| 43 return __sync_sub_and_fetch(&m_val, 1); | |
| 44 #endif | |
| 45 } | |
| 46 | |
| 47 volatile t_val m_val; | |
| 48 }; | |
| 49 | |
| 50 typedef threadSafeInt counter; | |
| 51 typedef threadSafeInt refcounter; | |
| 52 | |
| 53 void yield(); // forward declaration | |
| 54 | |
| 55 } |
