ソースを参照

lpac-jni: Convert all @Synchronized usage to a ReentrantLock

Peter Cai 4 ヶ月 前
コミット
2cda633fd0

+ 26 - 26
libs/lpac-jni/src/main/java/net/typeblog/lpac_jni/impl/LocalProfileAssistantImpl.kt

@@ -11,6 +11,8 @@ import net.typeblog.lpac_jni.LocalProfileInfo
 import net.typeblog.lpac_jni.LocalProfileNotification
 import net.typeblog.lpac_jni.ProfileDownloadCallback
 import net.typeblog.lpac_jni.Version
+import java.util.concurrent.locks.ReentrantLock
+import kotlin.concurrent.withLock
 
 class LocalProfileAssistantImpl(
     isdrAid: ByteArray,
@@ -74,6 +76,10 @@ class LocalProfileAssistantImpl(
             }
     }
 
+    // Controls concurrency of every single method in this class, since
+    // the C-side is explicitly NOT thread-safe
+    private val lock = ReentrantLock()
+
     private val apduInterface = ApduInterfaceWrapper(rawApduInterface)
     private val httpInterface = HttpInterfaceWrapper(rawHttpInterface)
 
@@ -105,8 +111,7 @@ class LocalProfileAssistantImpl(
         }
 
     override val profiles: List<LocalProfileInfo>
-        @Synchronized
-        get() {
+        get() = lock.withLock {
             val head = LpacJni.es10cGetProfilesInfo(contextHandle)
             var curr = head
             val ret = mutableListOf<LocalProfileInfo>()
@@ -132,8 +137,7 @@ class LocalProfileAssistantImpl(
         }
 
     override val notifications: List<LocalProfileNotification>
-        @Synchronized
-        get() {
+        get() = lock.withLock {
             val head = LpacJni.es10bListNotification(contextHandle)
             var curr = head
 
@@ -161,12 +165,10 @@ class LocalProfileAssistantImpl(
         }
 
     override val eID: String
-        @Synchronized
-        get() = LpacJni.es10cGetEid(contextHandle)!!
+        get() = lock.withLock { LpacJni.es10cGetEid(contextHandle)!! }
 
     override val euiccInfo2: EuiccInfo2?
-        @Synchronized
-        get() {
+        get() = lock.withLock {
             val cInfo = LpacJni.es10cexGetEuiccInfo2(contextHandle)
             if (cInfo == 0L) return null
 
@@ -200,23 +202,22 @@ class LocalProfileAssistantImpl(
             }
         }
 
-    @Synchronized
-    override fun enableProfile(iccid: String, refresh: Boolean): Boolean =
+    override fun enableProfile(iccid: String, refresh: Boolean): Boolean = lock.withLock {
         LpacJni.es10cEnableProfile(contextHandle, iccid, refresh) == 0
+    }
 
-    @Synchronized
-    override fun disableProfile(iccid: String, refresh: Boolean): Boolean =
+    override fun disableProfile(iccid: String, refresh: Boolean): Boolean = lock.withLock {
         LpacJni.es10cDisableProfile(contextHandle, iccid, refresh) == 0
+    }
 
-    @Synchronized
-    override fun deleteProfile(iccid: String): Boolean =
+    override fun deleteProfile(iccid: String): Boolean = lock.withLock {
         LpacJni.es10cDeleteProfile(contextHandle, iccid) == 0
+    }
 
-    @Synchronized
     override fun downloadProfile(
         smdp: String, matchingId: String?, imei: String?,
         confirmationCode: String?, callback: ProfileDownloadCallback
-    ) {
+    ) = lock.withLock {
         val res = LpacJni.downloadProfile(
             contextHandle,
             smdp,
@@ -243,18 +244,17 @@ class LocalProfileAssistantImpl(
         }
     }
 
-    @Synchronized
-    override fun deleteNotification(seqNumber: Long): Boolean =
+    override fun deleteNotification(seqNumber: Long): Boolean = lock.withLock {
         LpacJni.es10bDeleteNotification(contextHandle, seqNumber) == 0
+    }
 
-    @Synchronized
-    override fun handleNotification(seqNumber: Long): Boolean =
+    override fun handleNotification(seqNumber: Long): Boolean = lock.withLock {
         LpacJni.handleNotification(contextHandle, seqNumber).also {
             Log.d(TAG, "handleNotification $seqNumber = $it")
         } == 0
+    }
 
-    @Synchronized
-    override fun setNickname(iccid: String, nickname: String) {
+    override fun setNickname(iccid: String, nickname: String) = lock.withLock {
         val encoded = try {
             Charsets.UTF_8.encode(nickname).array()
         } catch (e: CharacterCodingException) {
@@ -272,13 +272,13 @@ class LocalProfileAssistantImpl(
         }
     }
 
-    @Synchronized
     override fun euiccMemoryReset() {
-        LpacJni.es10cEuiccMemoryReset(contextHandle)
+        lock.withLock {
+            LpacJni.es10cEuiccMemoryReset(contextHandle)
+        }
     }
 
-    @Synchronized
-    override fun close() {
+    override fun close() = lock.withLock {
         if (!finalized) {
             LpacJni.euiccFini(contextHandle)
             LpacJni.destroyContext(contextHandle)