0

I am hooked into SpringBoard method, i want to wait a certain event to happen and then continue my code, but whatever i try - i think it pauses the main thread and all threads stop then. My code is:

+(void) startc {
    while([currentNumber isEqual:@""])
    {
        NSLog(@"waiting until currentNumber is not empty %@", currentNumber);
    }
}

id replaced_SBCallAlert_initWithCall_(id self, SEL _cmd, CTCallRef call) {  // Note the 
        NSLog(@"calling replaced");
            [cdBackground startc];
        original_SBCallAlert_initWithCall_(sbc, scc, cls); 
            return NULL;
    }

currentNumber is updated in another thread, but this code blocks it.

Ishu
  • 12,797
  • 5
  • 35
  • 51
Dimitar Marinov
  • 922
  • 6
  • 14

1 Answers1

1

Your while loop in startc is effectively a spin lock, and will prevent any further progress on a thread executing it until currentNumber is not @"". If that same thread is the one responsible for changing currentNumber, then yes - you have a deadlock. It looks like this is the case, as I guess you're expecting the call original_SBCallAlert_initWithCall_ to change this variable but are getting stuck inside the previous line's startc call.

In this case, you'll have to change the way your program is designed. You could put startc in a background thread (look at NSThread) and use either another spin lock, NSCondition or key-value observing to "wake up" when currentNumber has changed. You can then use NSObject performSelectorOnMainThread … (assuming you have a runloop) to kick off your code again in the "main" thread, or carry on in your background thread.

Adam Wright
  • 48,938
  • 12
  • 131
  • 152
  • actually currentNumber is changed with notification from an observer on a background thread. i just want to wait few seconds before calling original_SBCallAlert_initWithCall_(sbc, scc, cls); but this blocks all threads. btw replaced_SBCallAlert_initWithCall is not called by me. i am hooked into SpringBoard and SprnigBoard calls it. Maybe that's the reason that the whole device freezes if I sleep in this function? – Dimitar Marinov Jun 24 '11 at 09:50