Skip to content

Commit b787b0d

Browse files
h1v9Repiteo
authored andcommitted
Thread Syncronization for FileAccessHandler and DirectoryAccessHandler
- Switching to ReentrantLock - Thread locking for DirectoryAccessHandler (dependencies) Newline @ EOF Forgot import Revert Better locking Forgot return Restore last empty line
1 parent b1792e5 commit b787b0d

File tree

2 files changed

+36
-29
lines changed

2 files changed

+36
-29
lines changed

platform/android/java/lib/src/org/godotengine/godot/io/directory/DirectoryAccessHandler.kt

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import android.util.Log
3535
import org.godotengine.godot.Godot
3636
import org.godotengine.godot.io.StorageScope
3737
import org.godotengine.godot.io.directory.DirectoryAccessHandler.AccessType.ACCESS_RESOURCES
38+
import java.util.concurrent.locks.ReentrantLock
39+
import kotlin.concurrent.withLock
3840

3941
/**
4042
* Handles files and directories access and manipulation for the Android platform
@@ -145,9 +147,10 @@ class DirectoryAccessHandler(context: Context) {
145147

146148
private val assetsDirAccess = AssetsDirectoryAccess(context)
147149
private val fileSystemDirAccess = FilesystemDirectoryAccess(context, storageScopeIdentifier)
150+
private val lock = ReentrantLock()
148151

149-
fun assetsFileExists(assetsPath: String) = assetsDirAccess.fileExists(assetsPath)
150-
fun filesystemFileExists(path: String) = fileSystemDirAccess.fileExists(path)
152+
fun assetsFileExists(assetsPath: String) = lock.withLock { assetsDirAccess.fileExists(assetsPath) }
153+
fun filesystemFileExists(path: String) = lock.withLock { fileSystemDirAccess.fileExists(path) }
151154

152155
private fun hasDirId(accessType: AccessType, dirId: Int): Boolean {
153156
return when (accessType) {
@@ -156,7 +159,7 @@ class DirectoryAccessHandler(context: Context) {
156159
}
157160
}
158161

159-
fun dirOpen(nativeAccessType: Int, path: String?): Int {
162+
fun dirOpen(nativeAccessType: Int, path: String?): Int = lock.withLock {
160163
if (path == null) {
161164
return INVALID_DIR_ID
162165
}
@@ -176,7 +179,7 @@ class DirectoryAccessHandler(context: Context) {
176179
return dirAccessId
177180
}
178181

179-
fun dirNext(dirAccessId: Int): String {
182+
fun dirNext(dirAccessId: Int): String = lock.withLock {
180183
val (accessType, dirId) = AccessType.fromDirAccessId(dirAccessId)
181184
if (accessType == null || !hasDirId(accessType, dirId)) {
182185
Log.w(TAG, "dirNext: Invalid dir id: $dirId")
@@ -189,7 +192,7 @@ class DirectoryAccessHandler(context: Context) {
189192
}
190193
}
191194

192-
fun dirClose(dirAccessId: Int) {
195+
fun dirClose(dirAccessId: Int) = lock.withLock {
193196
val (accessType, dirId) = AccessType.fromDirAccessId(dirAccessId)
194197
if (accessType == null || !hasDirId(accessType, dirId)) {
195198
Log.w(TAG, "dirClose: Invalid dir id: $dirId")
@@ -202,7 +205,7 @@ class DirectoryAccessHandler(context: Context) {
202205
}
203206
}
204207

205-
fun dirIsDir(dirAccessId: Int): Boolean {
208+
fun dirIsDir(dirAccessId: Int): Boolean = lock.withLock {
206209
val (accessType, dirId) = AccessType.fromDirAccessId(dirAccessId)
207210
if (accessType == null || !hasDirId(accessType, dirId)) {
208211
Log.w(TAG, "dirIsDir: Invalid dir id: $dirId")
@@ -215,7 +218,7 @@ class DirectoryAccessHandler(context: Context) {
215218
}
216219
}
217220

218-
fun isCurrentHidden(dirAccessId: Int): Boolean {
221+
fun isCurrentHidden(dirAccessId: Int): Boolean = lock.withLock {
219222
val (accessType, dirId) = AccessType.fromDirAccessId(dirAccessId)
220223
if (accessType == null || !hasDirId(accessType, dirId)) {
221224
return false
@@ -227,7 +230,7 @@ class DirectoryAccessHandler(context: Context) {
227230
}
228231
}
229232

230-
fun dirExists(nativeAccessType: Int, path: String?): Boolean {
233+
fun dirExists(nativeAccessType: Int, path: String?): Boolean = lock.withLock {
231234
if (path == null) {
232235
return false
233236
}
@@ -241,7 +244,7 @@ class DirectoryAccessHandler(context: Context) {
241244
}
242245
}
243246

244-
fun fileExists(nativeAccessType: Int, path: String?): Boolean {
247+
fun fileExists(nativeAccessType: Int, path: String?): Boolean = lock.withLock {
245248
if (path == null) {
246249
return false
247250
}
@@ -255,23 +258,23 @@ class DirectoryAccessHandler(context: Context) {
255258
}
256259
}
257260

258-
fun getDriveCount(nativeAccessType: Int): Int {
261+
fun getDriveCount(nativeAccessType: Int): Int = lock.withLock {
259262
val accessType = AccessType.fromNative(nativeAccessType) ?: return 0
260263
return when(accessType) {
261264
ACCESS_RESOURCES -> assetsDirAccess.getDriveCount()
262265
else -> fileSystemDirAccess.getDriveCount()
263266
}
264267
}
265268

266-
fun getDrive(nativeAccessType: Int, drive: Int): String {
269+
fun getDrive(nativeAccessType: Int, drive: Int): String = lock.withLock {
267270
val accessType = AccessType.fromNative(nativeAccessType) ?: return ""
268271
return when (accessType) {
269272
ACCESS_RESOURCES -> assetsDirAccess.getDrive(drive)
270273
else -> fileSystemDirAccess.getDrive(drive)
271274
}
272275
}
273276

274-
fun makeDir(nativeAccessType: Int, dir: String?): Boolean {
277+
fun makeDir(nativeAccessType: Int, dir: String?): Boolean = lock.withLock {
275278
if (dir == null) {
276279
return false
277280
}
@@ -285,23 +288,23 @@ class DirectoryAccessHandler(context: Context) {
285288
}
286289
}
287290

288-
fun getSpaceLeft(nativeAccessType: Int): Long {
291+
fun getSpaceLeft(nativeAccessType: Int): Long = lock.withLock {
289292
val accessType = AccessType.fromNative(nativeAccessType) ?: return 0L
290293
return when (accessType) {
291294
ACCESS_RESOURCES -> assetsDirAccess.getSpaceLeft()
292295
else -> fileSystemDirAccess.getSpaceLeft()
293296
}
294297
}
295298

296-
fun rename(nativeAccessType: Int, from: String, to: String): Boolean {
299+
fun rename(nativeAccessType: Int, from: String, to: String): Boolean = lock.withLock {
297300
val accessType = AccessType.fromNative(nativeAccessType) ?: return false
298301
return when (accessType) {
299302
ACCESS_RESOURCES -> assetsDirAccess.rename(from, to)
300303
else -> fileSystemDirAccess.rename(from, to)
301304
}
302305
}
303306

304-
fun remove(nativeAccessType: Int, filename: String?): Boolean {
307+
fun remove(nativeAccessType: Int, filename: String?): Boolean = lock.withLock {
305308
if (filename == null) {
306309
return false
307310
}

platform/android/java/lib/src/org/godotengine/godot/io/file/FileAccessHandler.kt

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import java.io.FileNotFoundException
3939
import java.io.InputStream
4040
import java.lang.UnsupportedOperationException
4141
import java.nio.ByteBuffer
42+
import java.util.concurrent.locks.ReentrantLock
43+
import kotlin.concurrent.withLock
4244

4345
/**
4446
* Handles regular and media store file access and interactions.
@@ -110,8 +112,11 @@ class FileAccessHandler(val context: Context) {
110112
internal val storageScopeIdentifier = StorageScope.Identifier(context)
111113
private val files = SparseArray<DataAccess>()
112114
private var lastFileId = STARTING_FILE_ID
115+
private val lock = ReentrantLock()
113116

114-
private fun hasFileId(fileId: Int) = files.indexOfKey(fileId) >= 0
117+
private fun hasFileId(fileId: Int): Boolean {
118+
return files.indexOfKey(fileId) >= 0
119+
}
115120

116121
fun canAccess(filePath: String?): Boolean {
117122
return storageScopeIdentifier.canAccess(filePath)
@@ -121,7 +126,7 @@ class FileAccessHandler(val context: Context) {
121126
* Returns a positive (> 0) file id when the operation succeeds.
122127
* Otherwise, returns a negative value of [Error].
123128
*/
124-
fun fileOpen(path: String?, modeFlags: Int): Int {
129+
fun fileOpen(path: String?, modeFlags: Int): Int = lock.withLock {
125130
val (fileError, fileId) = fileOpen(path, FileAccessFlags.fromNativeModeFlags(modeFlags))
126131
return if (fileError == Error.OK) {
127132
fileId
@@ -145,7 +150,6 @@ class FileAccessHandler(val context: Context) {
145150
return try {
146151
path?.let {
147152
val dataAccess = DataAccess.generateDataAccess(storageScope, context, it, accessFlag) ?: return FILE_OPEN_FAILED
148-
149153
files.put(++lastFileId, dataAccess)
150154
Pair(Error.OK, lastFileId)
151155
} ?: FILE_OPEN_FAILED
@@ -159,47 +163,47 @@ class FileAccessHandler(val context: Context) {
159163
}
160164
}
161165

162-
fun fileGetSize(fileId: Int): Long {
166+
fun fileGetSize(fileId: Int): Long = lock.withLock {
163167
if (!hasFileId(fileId)) {
164168
return 0L
165169
}
166170

167171
return files[fileId].size()
168172
}
169173

170-
fun fileSeek(fileId: Int, position: Long) {
174+
fun fileSeek(fileId: Int, position: Long) = lock.withLock {
171175
if (!hasFileId(fileId)) {
172176
return
173177
}
174178

175179
files[fileId].seek(position)
176180
}
177181

178-
fun fileSeekFromEnd(fileId: Int, position: Long) {
182+
fun fileSeekFromEnd(fileId: Int, position: Long) = lock.withLock {
179183
if (!hasFileId(fileId)) {
180184
return
181185
}
182186

183187
files[fileId].seekFromEnd(position)
184188
}
185189

186-
fun fileRead(fileId: Int, byteBuffer: ByteBuffer?): Int {
190+
fun fileRead(fileId: Int, byteBuffer: ByteBuffer?): Int = lock.withLock {
187191
if (!hasFileId(fileId) || byteBuffer == null) {
188192
return 0
189193
}
190194

191195
return files[fileId].read(byteBuffer)
192196
}
193197

194-
fun fileWrite(fileId: Int, byteBuffer: ByteBuffer?): Boolean {
198+
fun fileWrite(fileId: Int, byteBuffer: ByteBuffer?): Boolean = lock.withLock {
195199
if (!hasFileId(fileId) || byteBuffer == null) {
196200
return false
197201
}
198202

199203
return files[fileId].write(byteBuffer)
200204
}
201205

202-
fun fileFlush(fileId: Int) {
206+
fun fileFlush(fileId: Int) = lock.withLock {
203207
if (!hasFileId(fileId)) {
204208
return
205209
}
@@ -243,7 +247,7 @@ class FileAccessHandler(val context: Context) {
243247
}
244248
}
245249

246-
fun fileResize(fileId: Int, length: Long): Int {
250+
fun fileResize(fileId: Int, length: Long): Int = lock.withLock {
247251
if (!hasFileId(fileId)) {
248252
return Error.FAILED.toNativeValue()
249253
}
@@ -266,28 +270,28 @@ class FileAccessHandler(val context: Context) {
266270
}
267271
}
268272

269-
fun fileGetPosition(fileId: Int): Long {
273+
fun fileGetPosition(fileId: Int): Long = lock.withLock {
270274
if (!hasFileId(fileId)) {
271275
return 0L
272276
}
273277

274278
return files[fileId].position()
275279
}
276280

277-
fun isFileEof(fileId: Int): Boolean {
281+
fun isFileEof(fileId: Int): Boolean = lock.withLock {
278282
if (!hasFileId(fileId)) {
279283
return false
280284
}
281285

282286
return files[fileId].endOfFile
283287
}
284288

285-
fun setFileEof(fileId: Int, eof: Boolean) {
289+
fun setFileEof(fileId: Int, eof: Boolean) = lock.withLock {
286290
val file = files[fileId] ?: return
287291
file.endOfFile = eof
288292
}
289293

290-
fun fileClose(fileId: Int) {
294+
fun fileClose(fileId: Int) = lock.withLock {
291295
if (hasFileId(fileId)) {
292296
files[fileId].close()
293297
files.remove(fileId)

0 commit comments

Comments
 (0)