Skip to content

Commit

Permalink
Use real threads for extra runloops rather than dispatch queues
Browse files Browse the repository at this point in the history
  • Loading branch information
tmolitor-stud-tu committed Nov 27, 2024
1 parent cd99f47 commit 05ddf79
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions Monal/Classes/HelperTools.m
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}

Expand All @@ -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];
Expand All @@ -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];
}
Expand Down

0 comments on commit 05ddf79

Please sign in to comment.