@@ -91,6 +91,22 @@ final case class AtomicInt private (unsafe: AtomicInt.Unsafe):
91
91
*/
92
92
inline def addAndGet (v : Int )(using inline frame : Frame ): Int < IO = IO .Unsafe (unsafe.addAndGet(v))
93
93
94
+ /** Atomically updates the current value using the given function and returns the old value.
95
+ * @param f
96
+ * The function to apply to the current value
97
+ * @return
98
+ * The previous value
99
+ */
100
+ inline def getAndUpdate (inline f : Int => Int )(using inline frame : Frame ): Int < IO = IO .Unsafe (unsafe.getAndUpdate(f))
101
+
102
+ /** Atomically updates the current value using the given function and returns the updated value.
103
+ * @param f
104
+ * The function to apply to the current value
105
+ * @return
106
+ * The updated value
107
+ */
108
+ inline def updateAndGet (inline f : Int => Int )(using inline frame : Frame ): Int < IO = IO .Unsafe (unsafe.updateAndGet(f))
109
+
94
110
/** Returns a string representation of the current value.
95
111
* @return
96
112
* A string representation of the atomic integer
@@ -158,6 +174,8 @@ object AtomicInt:
158
174
inline def getAndDecrement ()(using inline allow : AllowUnsafe ): Int = self.getAndDecrement()
159
175
inline def getAndAdd (v : Int )(using inline allow : AllowUnsafe ): Int = self.getAndAdd(v)
160
176
inline def addAndGet (v : Int )(using inline allow : AllowUnsafe ): Int = self.addAndGet(v)
177
+ inline def getAndUpdate (inline f : Int => Int )(using inline allow : AllowUnsafe ): Int = self.getAndUpdate(f(_))
178
+ inline def updateAndGet (inline f : Int => Int )(using inline allow : AllowUnsafe ): Int = self.updateAndGet(f(_))
161
179
inline def safe : AtomicInt = AtomicInt (self)
162
180
end extension
163
181
end Unsafe
@@ -252,6 +270,22 @@ final case class AtomicLong private (unsafe: AtomicLong.Unsafe):
252
270
*/
253
271
inline def addAndGet (v : Long )(using inline frame : Frame ): Long < IO = IO .Unsafe (unsafe.addAndGet(v))
254
272
273
+ /** Atomically updates the current value using the given function and returns the old value.
274
+ * @param f
275
+ * The function to apply to the current value
276
+ * @return
277
+ * The previous value
278
+ */
279
+ inline def getAndUpdate (inline f : Long => Long )(using inline frame : Frame ): Long < IO = IO .Unsafe (unsafe.getAndUpdate(f(_)))
280
+
281
+ /** Atomically updates the current value using the given function and returns the updated value.
282
+ * @param f
283
+ * The function to apply to the current value
284
+ * @return
285
+ * The updated value
286
+ */
287
+ inline def updateAndGet (inline f : Long => Long )(using inline frame : Frame ): Long < IO = IO .Unsafe (unsafe.updateAndGet(f(_)))
288
+
255
289
/** Returns a string representation of the current value.
256
290
* @return
257
291
* A string representation of the atomic long
@@ -320,6 +354,8 @@ object AtomicLong:
320
354
inline def getAndDecrement ()(using inline allow : AllowUnsafe ): Long = self.getAndDecrement()
321
355
inline def getAndAdd (v : Long )(using inline allow : AllowUnsafe ): Long = self.getAndAdd(v)
322
356
inline def addAndGet (v : Long )(using inline allow : AllowUnsafe ): Long = self.addAndGet(v)
357
+ inline def getAndUpdate (inline f : Long => Long )(using inline allow : AllowUnsafe ): Long = self.getAndUpdate(f(_))
358
+ inline def updateAndGet (inline f : Long => Long )(using inline allow : AllowUnsafe ): Long = self.updateAndGet(f(_))
323
359
inline def safe : AtomicLong = AtomicLong (self)
324
360
end extension
325
361
end Unsafe
@@ -494,19 +530,21 @@ final case class AtomicRef[A] private (unsafe: AtomicRef.Unsafe[A]):
494
530
*/
495
531
inline def compareAndSet (curr : A , next : A )(using inline frame : Frame ): Boolean < IO = IO .Unsafe (unsafe.compareAndSet(curr, next))
496
532
497
- /** Atomically updates the current value using the given function.
533
+ /** Atomically updates the current value using the given function and returns the old value .
498
534
* @param f
499
535
* The function to apply to the current value
536
+ * @return
537
+ * The previous value
500
538
*/
501
- inline def update [ S ]( f : A => A )(using inline frame : Frame ): Unit < IO = updateAndGet(f).unit
539
+ inline def getAndUpdate ( inline f : A => A )(using inline frame : Frame ): A < IO = IO . Unsafe (unsafe.getAndUpdate(f(_)))
502
540
503
541
/** Atomically updates the current value using the given function and returns the updated value.
504
542
* @param f
505
543
* The function to apply to the current value
506
544
* @return
507
545
* The updated value
508
546
*/
509
- inline def updateAndGet [ S ]( f : A => A )(using inline frame : Frame ): A < IO = IO .Unsafe (unsafe.updateAndGet(f(_)))
547
+ inline def updateAndGet ( inline f : A => A )(using inline frame : Frame ): A < IO = IO .Unsafe (unsafe.updateAndGet(f(_)))
510
548
511
549
/** Returns a string representation of the current value.
512
550
* @return
@@ -556,8 +594,8 @@ object AtomicRef:
556
594
inline def lazySet (v : A )(using inline allow : AllowUnsafe ): Unit = self.lazySet(v)
557
595
inline def getAndSet (v : A )(using inline allow : AllowUnsafe ): A = self.getAndSet(v)
558
596
inline def compareAndSet (curr : A , next : A )(using inline allow : AllowUnsafe ): Boolean = self.compareAndSet(curr, next)
559
- def update [ S ]( f : A => A )(using AllowUnsafe ): Unit = discard( self.updateAndGet (f(_) ))
560
- def updateAndGet [ S ]( f : A => A )(using AllowUnsafe ): A = self.updateAndGet(f(_))
597
+ inline def getAndUpdate ( inline f : A => A )(using inline allow : AllowUnsafe ): A = self.getAndUpdate (f(_))
598
+ inline def updateAndGet ( inline f : A => A )(using inline allow : AllowUnsafe ): A = self.updateAndGet(f(_))
561
599
inline def safe : AtomicRef [A ] = AtomicRef (self)
562
600
end extension
563
601
end Unsafe
0 commit comments