@@ -38,11 +38,8 @@ import com.harrytmthy.safebox.storage.Bytes
38
38
import com.harrytmthy.safebox.storage.SafeBoxBlobStore
39
39
import com.harrytmthy.safebox.strategy.ValueFallbackStrategy
40
40
import com.harrytmthy.safebox.strategy.ValueFallbackStrategy.WARN
41
- import kotlinx.coroutines.CompletableDeferred
42
41
import kotlinx.coroutines.CoroutineDispatcher
43
42
import kotlinx.coroutines.Dispatchers
44
- import kotlinx.coroutines.sync.Mutex
45
- import kotlinx.coroutines.sync.withLock
46
43
import java.util.concurrent.ConcurrentHashMap
47
44
import java.util.concurrent.CopyOnWriteArrayList
48
45
import java.util.concurrent.atomic.AtomicBoolean
@@ -85,10 +82,6 @@ public class SafeBox private constructor(
85
82
86
83
private val listeners = CopyOnWriteArrayList <OnSharedPreferenceChangeListener >()
87
84
88
- private val writeMutex = Mutex ()
89
-
90
- private val writeBarrier = AtomicReference (CompletableDeferred <Unit >().apply { complete(Unit ) })
91
-
92
85
private val scanScheduled = AtomicBoolean (false )
93
86
94
87
private val delegate = object : Delegate {
@@ -101,21 +94,8 @@ public class SafeBox private constructor(
101
94
entries.clear() // Prevents stale mutations on reused editor instance
102
95
updateEntries(entriesToWrite, cleared)
103
96
}
104
- val currentWriteBarrier = CompletableDeferred <Unit >()
105
- val previousWriteBarrier = writeBarrier.getAndSet(currentWriteBarrier)
106
97
return stateManager.launchCommitWithWritingState {
107
- try {
108
- previousWriteBarrier.await()
109
- writeMutex.withLock {
110
- applyChanges(entriesToWrite, cleared)
111
- }
112
- true
113
- } catch (e: Exception ) {
114
- Log .e(" SafeBox" , " Failed to commit changes." , e)
115
- false
116
- } finally {
117
- currentWriteBarrier.complete(Unit )
118
- }
98
+ applyChanges(entriesToWrite, cleared)
119
99
}
120
100
}
121
101
@@ -125,19 +105,8 @@ public class SafeBox private constructor(
125
105
entries.clear() // Prevents stale mutations on reused editor instance
126
106
updateEntries(entriesToWrite, cleared)
127
107
}
128
- val currentWriteBarrier = CompletableDeferred <Unit >()
129
- val previousWriteBarrier = writeBarrier.getAndSet(currentWriteBarrier)
130
108
stateManager.launchApplyWithWritingState {
131
- try {
132
- previousWriteBarrier.await()
133
- writeMutex.withLock {
134
- applyChanges(entriesToWrite, cleared)
135
- }
136
- } catch (e: Exception ) {
137
- Log .e(" SafeBox" , " Failed to commit changes." , e)
138
- } finally {
139
- currentWriteBarrier.complete(Unit )
140
- }
109
+ applyChanges(entriesToWrite, cleared)
141
110
}
142
111
}
143
112
@@ -334,11 +303,8 @@ public class SafeBox private constructor(
334
303
if (! scanScheduled.compareAndSet(false , true )) {
335
304
return
336
305
}
337
- val currentWriteBarrier = CompletableDeferred <Unit >()
338
- val previousWriteBarrier = writeBarrier.getAndSet(currentWriteBarrier)
339
306
stateManager.launchApplyWithWritingState {
340
307
try {
341
- previousWriteBarrier.await()
342
308
val deadKeys = ArrayList <Bytes >()
343
309
for ((encryptedKey, encryptedValue) in entries) {
344
310
if (valueCipherProvider.tryDecrypt(encryptedValue) == null ) {
@@ -347,12 +313,9 @@ public class SafeBox private constructor(
347
313
}
348
314
}
349
315
if (deadKeys.isNotEmpty()) {
350
- writeMutex.withLock {
351
- blobStore.delete(* deadKeys.toTypedArray())
352
- }
316
+ blobStore.delete(* deadKeys.toTypedArray())
353
317
}
354
318
} finally {
355
- currentWriteBarrier.complete(Unit )
356
319
scanScheduled.set(false )
357
320
}
358
321
}
0 commit comments