From 05ddf79cfbfc8e5a73cfac658c2955a9b172e3dc Mon Sep 17 00:00:00 2001 From: Thilo Molitor Date: Wed, 27 Nov 2024 04:11:07 +0100 Subject: [PATCH] Use real threads for extra runloops rather than dispatch queues --- Monal/Classes/HelperTools.m | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/Monal/Classes/HelperTools.m b/Monal/Classes/HelperTools.m index 94cc31092..d945de801 100644 --- a/Monal/Classes/HelperTools.m +++ b/Monal/Classes/HelperTools.m @@ -703,13 +703,13 @@ +(NSRunLoop*) getExtraRunloopWithIdentifier:(MLRunLoopIdentifier) identifier runloops = [NSMutableDictionary new]; }); - //every identifier has its own thread priority/qos class - __block dispatch_queue_priority_t priority; + //every identifier has its own thread qos class + __block NSQualityOfService qos; __block char* name; switch(identifier) { - case MLRunLoopIdentifierNetwork: priority = DISPATCH_QUEUE_PRIORITY_BACKGROUND; name = "im.monal.runloop.networking"; break; - case MLRunLoopIdentifierTimer: priority = DISPATCH_QUEUE_PRIORITY_BACKGROUND; name = "im.monal.runloop.timer"; break; + case MLRunLoopIdentifierNetwork: qos = NSQualityOfServiceBackground; name = "im.monal.runloop.networking"; break; + case MLRunLoopIdentifierTimer: qos = NSQualityOfServiceBackground; name = "im.monal.runloop.timer"; break; default: unreachable(@"unknown runloop identifier!"); } @@ -718,9 +718,7 @@ +(NSRunLoop*) getExtraRunloopWithIdentifier:(MLRunLoopIdentifier) identifier { NSCondition* condition = [NSCondition new]; [condition lock]; - dispatch_async(dispatch_queue_create_with_target(name, DISPATCH_QUEUE_SERIAL, dispatch_get_global_queue(priority, 0)), ^{ - //set thread name, too (not only runloop name) - [NSThread.currentThread setName:[NSString stringWithFormat:@"%s", name]]; + NSThread* newRunloopThread = [[NSThread alloc] initWithBlock:^{ //we don't need an @synchronized block around this because the @synchronized block of the outer thread //waits until we signal our condition (e.g. no other thread can race with us) NSRunLoop* localLoop = runloops[@(identifier)] = [NSRunLoop currentRunLoop]; @@ -732,7 +730,13 @@ +(NSRunLoop*) getExtraRunloopWithIdentifier:(MLRunLoopIdentifier) identifier [localLoop run]; usleep(10000); //sleep 10ms if we ever return from our runloop to not consume too much cpu } - }); + }]; + //configure and start thread + [newRunloopThread setName:[NSString stringWithFormat:@"%s", name]]; + //newRunloopThread.threadPriority = 1.0; + newRunloopThread.qualityOfService = qos; + [newRunloopThread start]; + //wait for the new thread to create a new runloop, it will immediately spin the runloop after signalling this condition [condition wait]; [condition unlock]; }