diff --git a/PlayTools/PlayLoader.m b/PlayTools/PlayLoader.m index 42881cef..ca928d69 100644 --- a/PlayTools/PlayLoader.m +++ b/PlayTools/PlayLoader.m @@ -281,25 +281,29 @@ static int pt_usleep(useconds_t time) { if ([[PlaySettings shared] blockSleepSpamming]) { int thread_id = pthread_mach_thread_np(pthread_self()); NSNumber *threadKey = @(thread_id); - - int thread_sleep_counter = [thread_sleep_counters[threadKey] intValue]; - int last_sleep_attempt = [last_sleep_attempts[threadKey] intValue]; - - if (time == 100000) { - int timestamp = (int)[[NSDate date] timeIntervalSince1970]; - // If it sleeps too fast, increase counter - if (timestamp - last_sleep_attempt < 2) { - thread_sleep_counter++; - } else { - thread_sleep_counter = 1; + + BOOL exceeded_sleep_limit = NO; + @synchronized (thread_sleep_counters) { + int thread_sleep_counter = [thread_sleep_counters[threadKey] intValue]; + int last_sleep_attempt = [last_sleep_attempts[threadKey] intValue]; + + if (time == 100000) { + int timestamp = (int)[[NSDate date] timeIntervalSince1970]; + // If it sleeps too fast, increase counter + if (timestamp - last_sleep_attempt < 2) { + thread_sleep_counter++; + } else { + thread_sleep_counter = 1; + } + last_sleep_attempt = timestamp; + thread_sleep_counters[threadKey] = @(thread_sleep_counter); + last_sleep_attempts[threadKey] = @(last_sleep_attempt); } - last_sleep_attempt = timestamp; - thread_sleep_counters[threadKey] = @(thread_sleep_counter); - last_sleep_attempts[threadKey] = @(last_sleep_attempt); - + + exceeded_sleep_limit = thread_sleep_counter > 100; } - - if (thread_sleep_counter > 100) { + + if (exceeded_sleep_limit) { // Stop this thread from spamming usleep calls NSLog(@"[PC] Thread %i exceeded usleep limit. Seem sus, stopping this " @"thread FOREVER",