AtomicIntSeq.kt 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 2019-2022 Mamoe Technologies and contributors.
  3. *
  4. * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  5. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
  6. *
  7. * https://github.com/mamoe/mirai/blob/dev/LICENSE
  8. */
  9. // IDE doesn't show warnings but compiler do.
  10. @file:Suppress("NOTHING_TO_INLINE", "KotlinRedundantDiagnosticSuppress")
  11. package net.mamoe.mirai.internal.utils
  12. import kotlinx.atomicfu.AtomicInt
  13. import kotlinx.atomicfu.atomic
  14. import kotlinx.atomicfu.update
  15. import net.mamoe.mirai.utils.getRandomUnsignedInt
  16. import kotlin.jvm.JvmStatic
  17. internal object AtomicIntSeq {
  18. @JvmStatic
  19. fun forMessageSeq(): AtomicIntMaxSeq = AtomicIntMaxSeq(0)
  20. @JvmStatic
  21. fun forPrivateSync(): AtomicInt65535Seq = AtomicInt65535Seq(getRandomUnsignedInt())
  22. }
  23. // value classes to optimize space
  24. internal class AtomicIntMaxSeq(
  25. value: Int
  26. ) {
  27. private val value: AtomicInt = atomic(value)
  28. /**
  29. * Increment [value] within the range from `0` (inclusive) to [Int.MAX_VALUE] (exclusive).
  30. */
  31. inline fun next(): Int = value.incrementAndGet().mod(Int.MAX_VALUE)
  32. /**
  33. * Atomically update [value] if it is smaller than [new].
  34. *
  35. * @param new should be positive
  36. */
  37. inline fun updateIfSmallerThan(new: Int): Boolean {
  38. value.update { instant ->
  39. if (instant < new) new else return false
  40. }
  41. return true
  42. }
  43. /**
  44. * Atomically update [value] if it different with [new].
  45. *
  46. * @param new should be positive
  47. */
  48. inline fun updateIfDifferentWith(new: Int): Boolean {
  49. value.update { instant ->
  50. if (instant == new) return false
  51. new
  52. }
  53. return true
  54. }
  55. }
  56. internal class AtomicInt65535Seq(
  57. value: Int
  58. ) {
  59. private val value: AtomicInt = atomic(value)
  60. /**
  61. * Increment [value] within the range from `0` (inclusive) to `65535` (exclusive).
  62. */
  63. inline fun next(): Int = value.incrementAndGet().mod(65535)
  64. /**
  65. * Atomically update [value] if it is smaller than [new].
  66. *
  67. * @param new should be positive
  68. */
  69. inline fun updateIfSmallerThan(new: Int): Boolean {
  70. value.update { instant ->
  71. if (instant < new) new else return false
  72. }
  73. return true
  74. }
  75. /**
  76. * Atomically update [value] if it different with [new].
  77. *
  78. * @param new should be positive
  79. */
  80. inline fun updateIfDifferentWith(new: Int): Boolean {
  81. value.update { instant ->
  82. if (instant == new) return false
  83. new
  84. }
  85. return true
  86. }
  87. }