Shizam
Shizam

Reputation: 9672

Getting OSSpinLockLock when using Grand Central Dispatch (GCD)

We're using GCD to perform some image processing operations 'in the background' in our image editor view which works great. The problem is if we open the editor view, do some processing and then just sit in the editor view for 10-20 minutes we get these OSSpinLockLock freezes, but we're not using SpinLocks or locks of any kind, we have these properties:

@property (nonatomic, readonly) dispatch_semaphore_t processingSemaphore;
@property (nonatomic, readonly) dispatch_queue_t serialQueue;

and setup the queues like so:

processingSemaphore = dispatch_semaphore_create(1);
serialQueue = dispatch_queue_create("com.myapp.imageProcessingQueue", NULL);
dispatch_set_target_queue(serialQueue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, NULL));

and process thusly:

dispatch_async(self.serialQueue, ^{
    dispatch_semaphore_wait(self.processingSemaphore, DISPATCH_TIME_FOREVER);

    ....<do stuff>....

    dispatch_semaphore_signal(self.processingSemaphore);

    dispatch_sync(dispatch_get_main_queue(), ^{
        ....<notify that we're done>....
    }];
});

I'm wondering if its the semaphore somehow.

Upvotes: 1

Views: 1177

Answers (2)

das
das

Reputation: 3681

libdispatch does not use OSSpinLockLock either in the queue or the semaphore implementation, but malloc does (and thus Block_copy, which libdispatch calls as part of dispatch_async).

Can you show the backtraces of all threads when you are blocked in OSSpinLockLock ?

Upvotes: 2

Ants
Ants

Reputation: 1338

Perhaps instead of using the semaphore you could create a serial queue. Create your queue like this:

serialQueue = dispatch_queue_create("com.myapp.imageProcessingQueue", DISPATCH_QUEUE_SERIAL);

This will ensure only one block at a time is executed.

You cannot cancel operations on this queue though. To do that you will need to use NSOperationQueue.

Upvotes: 0

Related Questions