Coder Social home page Coder Social logo

safedispatch's Introduction

The SafeDispatch framework is a Cocoa wrapper for Grand Central Dispatch that adds important safety features. Not all of GCD is included โ€“ the main focus is on improving dispatch queues and groups.

This framework is no longer supported, and is hard to use correctly. If you're looking for a better way to do concurrency, check out ReactiveCocoa instead.

Features

Besides an easy-to-use Objective-C API, this framework adds:

  • Recursive synchronous dispatch. dispatch_sync() deadlocks when targeting the current queue โ€“ SDQueue does not.
  • Conditionally asynchronous or synchronous dispatch. Blocks can be run synchronously if targeting the current queue, or asynchronously if targeting another.
  • Exception-safe synchronous dispatch, to support throwing exceptions from synchronous blocks and propagating them to callers.
  • Synchronization across multiple queues, based on a deterministic total ordering of the queues (to prevent deadlocks).
  • Prologue and epilogue blocks, to perform work before or after each block added to a queue.
  • Better detection of the current queue. SafeDispatch can identify whether code is executing on a given queue even if that queue is many layers up the call stack. (This is especially important for recursive and conditional dispatch.)

License

Copyright (c) 2012 Justin Spahr-Summers

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

safedispatch's People

Contributors

jspahrsummers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

safedispatch's Issues

Add an SDQueue key/value stack

As noted in #1, dispatch_get_specific() can't really be used with SDQueue because of the possibility of reentrant dispatch. It'd be nice to have a parallel feature in SafeDispatch that honors the emulated stack (instead of the real one).

"Safe" synchronous operations can deadlock in the face of target queues

Try adding this test to SDQueueTests.m:

- (void)testTargetedQueueDeadlock
{
    dispatch_queue_t firstQueueT = dispatch_queue_create("1", DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t secondQueueT = dispatch_queue_create("2", DISPATCH_QUEUE_SERIAL);

    dispatch_set_target_queue(firstQueueT, secondQueueT);

    SDQueue *firstQueue = [SDQueue queueWithGCDQueue:firstQueueT concurrent:NO private:YES];
    SDQueue *secondQueue = [SDQueue queueWithGCDQueue:secondQueueT concurrent:NO private:YES];

    __block BOOL finished = NO;

    [firstQueue runSynchronously:^{
        [secondQueue runSynchronously:^{
            [firstQueue runSynchronously:^{
                finished = YES;
            }];
        }];
    }];

    STAssertTrue(finished, @"");
}

I'm pretty sure the usage of dispatch_get_current_queue() throughout SDQueue.m isn't safe in the face of target queue usage.

This looks like a pretty contrived example, but it could get much worse much faster. I think it means that the SDQueue constructors which take a dispatch_queue_t argument are only safe if you made the queue yourself, and it never "escapes" your control.

If I provide an API like the following, and the client gives me a dispatch_queue_t which targets another, and then makes two of these, then I can't use any of the safe SDQueue operations:

@interface TestAPI : NSObject
- (instancetype)initWithQueue:(dispatch_queue_t)queue;
- (void)doThingInConcertWithOtherGuy:(TestAPI *)otherGuy;
@end

It might be worth just removing the constructors which take a dispatch_queue_t as argument. But that'd kind of unfortunate, since targeting semantics are actually useful. Maybe you could remove those constructors, then provide targeting semantics yourself on SDQueue which could then attempt to avoid cycles. But I'm not sure you could avoid cycles in a thread-safe way, since the target queues involved could change out from under you on various queues while you're traversing the graph. You'd have to be very careful about the guarantees made about the timing of your setTargetQueue: method: I doubt you could make the same guarantees that GCD makes about when the new target queue's applied.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.