Find the appropriate NSThread for NSRunLoop (needed to fix Socket Rocket)

I am working on fixing a race condition in a Socket Rocket . This error has been reported for a long time and has not yet been fixed.

More than a year ago, I wrote a patch that violates the API (only a common thread can be used), and this code works successfully in production (no crashes at all when there are a large number of users).

Now I want to configure my fix so that it does not violate the API SRWebSocket. For this I need to find a match NSThreadforgiven NSRunLoop. This is one to one relationship, but I have a problem finding an API that could help me.


PS. The fix is ​​pretty simple. Each operation performed on NSRunLoopmust be performed from the corresponding thread. No NSRunLoopor CFRunLoop API that can be safely used from another thread. So I've added such API toSRRunLoopThread`:
- (void)scheduleBlock: (void(^)())block
{
    [self performSelector: @selector(_runBlock:)
                 onThread: self
               withObject: [block copy]
            waitUntilDone: NO];
}

- (void)_runBlock: (void(^)())block
{
    block();
}

and use it in all places where something is done on it NSRunLoop.

This fix shows why I need to find a match NSThread.

Note documentation claims to performSelector:onThread:withObject:waitUntilDone:be thread safe

This method can be used to deliver messages to other threads in your application.

I must emphasize once again that the documentation clearly warns that the NSRunLoopAPI is NOT thread safe:

Attention

NSRunLoop . NSRunLoop , .

CFRunLoop - , / NSRunLoop, . , , API , , ( @DisableR ).

+4
1

, runloop . , .

CFRunLoopPerformBlock([myNSRunLoop getCFRunLoop], kCFRunLoopCommonModes, block);
CFRunLoopWakeUp([myNSRunLoop getCFRunLoop]);

-performSelectorOnMainThread: NSThread macOS Tiger, , , : http://www.cocoabuilder.com/archive/cocoa/112261-cfrunlooptimer-firing-delay.html

+1

Source: https://habr.com/ru/post/1690547/


All Articles