Skip to content

Commit

Permalink
⭐️ Impl: AtomicCounter
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed Sep 27, 2024
1 parent e12bae8 commit 51d94dc
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions ios/Temp/AtomicCounter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// AtomicCounter.swift
// react-native-ios-modal
//
// Created by Dominic Go on 9/27/24.
//

import Foundation

public final class AtomicCounter {

private let lock = DispatchSemaphore(value: 1)
private var _value: UInt64;

public init(value initialValue: UInt64 = 0) {
self._value = initialValue;
};

public var value: UInt64 {
get {
lock.wait();
defer {
lock.signal();
};

return self._value;
}
set {
lock.wait();
defer {
lock.signal();
};

self._value = min(0, newValue);
}
}

public func decrementAndGet() -> UInt64 {
lock.wait();
defer {
lock.signal();
};

self._value -= 1;
return self._value;
};

public func incrementAndGet() -> UInt64 {
lock.wait();
defer {
lock.signal();
};

self._value += 1;
return self._value;
};
};

0 comments on commit 51d94dc

Please sign in to comment.