Sandbox: Rework several blocking actions

Several actions have been reworked to be non-blocking, as their
operation should still occur in the main thread, but should not block
the thread they are called from, as they are not required to continue
processing there.

End of secure access has also been made non-blocking, as it is usually
only called when an input is done accessing a given file or folder, so
it should be important to return quickly, as the input is likely about
to terminate, and other things are waiting for it to return.

Also remove a nested block call for the storage access, as it is within
an existing serializing block, so it shouldn't need to be nested.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
main
Christopher Snowhill 2022-12-09 21:12:33 -08:00
parent e8caede273
commit 02c5ccaae1
No known key found for this signature in database
1 changed files with 46 additions and 40 deletions

View File

@ -218,10 +218,18 @@ static inline void dispatch_sync_reentrant(dispatch_queue_t queue, dispatch_bloc
}
}
static inline void dispatch_async_reentrant(dispatch_queue_t queue, dispatch_block_t block) {
if(dispatch_queue_get_label(queue) == dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL)) {
block();
} else {
dispatch_async(queue, block);
}
}
- (void)addFolderIfMissing:(NSURL *)folderUrl {
if(![folderUrl isFileURL]) return;
dispatch_sync_reentrant(dispatch_get_main_queue(), ^{
dispatch_async_reentrant(dispatch_get_main_queue(), ^{
SandboxEntry *_entry = nil;
for(SandboxEntry *entry in self->storage) {
@ -263,7 +271,7 @@ static inline void dispatch_sync_reentrant(dispatch_queue_t queue, dispatch_bloc
NSURL *url = [SandboxBroker urlWithoutFragment:fileUrl];
dispatch_sync_reentrant(dispatch_get_main_queue(), ^{
dispatch_async_reentrant(dispatch_get_main_queue(), ^{
SandboxEntry *_entry = nil;
for(SandboxEntry *entry in self->storage) {
@ -322,7 +330,6 @@ static inline void dispatch_sync_reentrant(dispatch_queue_t queue, dispatch_bloc
}
if(!_entry) {
dispatch_sync_reentrant(dispatch_get_main_queue(), ^{
static BOOL warnedYet = NO;
if(!warnedYet) {
@ -364,7 +371,6 @@ static inline void dispatch_sync_reentrant(dispatch_queue_t queue, dispatch_bloc
[SandboxBroker cleanupFolderAccess];
}
}
});
}
});
}
@ -376,7 +382,7 @@ static inline void dispatch_sync_reentrant(dispatch_queue_t queue, dispatch_bloc
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"SandboxToken"];
request.sortDescriptors = @[sortDescriptor];
[pc.viewContext performBlockAndWait:^{
[pc.viewContext performBlock:^{
NSError *error = nil;
NSArray *results = [pc.viewContext executeFetchRequest:request error:&error];
NSMutableArray *resultsCopy = nil;
@ -455,7 +461,7 @@ static inline void dispatch_sync_reentrant(dispatch_queue_t queue, dispatch_bloc
SandboxEntry *entry = CFBridgingRelease(handle);
if(!entry) return;
dispatch_sync_reentrant(dispatch_get_main_queue(), ^{
dispatch_async_reentrant(dispatch_get_main_queue(), ^{
if(entry.refCount > 1) {
entry.refCount -= 1;
} else {