1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- #pragma once
- #include <util/system/condvar.h>
- #include <util/system/mutex.h>
- #include <util/system/platform.h>
- class TFutexLike {
- private:
- #ifdef _linux_
- int Value;
- #else
- TAtomic Value;
- TMutex Mutex;
- TCondVar CondVar;
- #endif
- public:
- TFutexLike()
- : Value(0)
- {
- }
- int AddAndGet(int add) {
- #ifdef _linux_
-
- return __sync_add_and_fetch(&Value, add);
- #else
- return AtomicAdd(Value, add);
- #endif
- }
- int GetAndAdd(int add) {
- return AddAndGet(add) - add;
- }
- #if 0
- int GetAndSet(int newValue) {
- #ifdef _linux_
- return __atomic_exchange_n(&Value, newValue, __ATOMIC_SEQ_CST);
- #else
- return AtomicSwap(&Value, newValue);
- #endif
- }
- #endif
- int Get() {
- #ifdef _linux_
-
- __sync_synchronize();
- return Value;
- #else
- return AtomicGet(Value);
- #endif
- }
- void Set(int newValue) {
- #ifdef _linux_
-
- Value = newValue;
- __sync_synchronize();
- #else
- AtomicSet(Value, newValue);
- #endif
- }
- int GetAndIncrement() {
- return AddAndGet(1) - 1;
- }
- int IncrementAndGet() {
- return AddAndGet(1);
- }
- int GetAndDecrement() {
- return AddAndGet(-1) + 1;
- }
- int DecrementAndGet() {
- return AddAndGet(-1);
- }
- void Wake(size_t count = Max<size_t>());
- void Wait(int expected);
- };
|