sghiassy / thread-safe-block-queue

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

thread-safe-block-queue

CI Status Version License Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

thread-safe-block-queue is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "thread-safe-block-queue"

Install the Pod

pod install

Then add to your code

#import <thread-safe-block-queue/ThreadSafeBlockQueue.h>
ThreadSafeBlockQueue *queue = [[ThreadSafeBlockQueue alloc] init];

Description

Thread Safe Block Queue (TSBQ) is a special data structure I needed once. Much like NSOperation, it provides a FIFO queue for running blocks. The queue is initially suspended and later is resumed by the developer.

What TSBQ offers beyond an NSOperationQueue is ability to replay the blocks given to TSBQ. This unique feature was necessary in a project I was working on and I'm making it available publicly since its highly tested and thread-safe.

//   ┌────────────────────────────────┐
//   │  Queue starts at HEAD with 0   │
//   └────────────────────────────────┘
//       │
//       │
//       │
//       │
//       │
//       │
//       ▼
//
//      null
//
//
//
//
//
//
//   ┌─────────────────────────────────────────────┐
//   │     Blocks come in and are queued FIFO      │
//   └─────────────────────────────────────────────┘
//       │
//       │
//       │
//       │
//       │
//       │
//       ▼
//  ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐
//  │         │ │         │ │         │ │         │ │         │
//  │  Block  │ │  Block  │ │  Block  │ │  Block  │ │  Block  │
//  │         │ │         │ │         │ │         │ │         │
//  └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘
//
//
//
//
//
//
//
//
//   ┌───────────────────────────────────────────────────────────────────────────────┐
//   │ Dequeuing the data structure causes the blocks to be run in sequential order  │
//   └───────────────────────────────────────────────────────────────────────────────┘
//                                                       │
//                                                       │
//                                                       │
//          ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ▶  │
//                                                       │
//                                                       │
//       ▼                                               ▼
//  ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐
//  │         │ │         │ │         │ │         │ │         │
//  │  Block  │ │  Block  │ │  Block  │ │  Block  │ │  Block  │
//  │         │ │         │ │         │ │         │ │         │
//  └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘
//
//
//
//
//
//
//
//
//
//
//                                        ┌──────────────────────────────────────────┐
//                                        │Subsequent blocks will be run immediately │
//                                        └──────────────────────────────────────────┘
//                                                                   │
//                                                                   │
//                                                        ─ ─ ─ ─ ─▶ │
//                                                                   │
//                                                                   │
//                                                                   │
//                                                       ▼           ▼
//  ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐
//  │         │ │         │ │         │ │         │ │         │ │         │
//  │  Block  │ │  Block  │ │  Block  │ │  Block  │ │  Block  │ │  Block  │
//  │         │ │         │ │         │ │         │ │         │ │         │
//  └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘
//
//
//
//
//
//
//
//
//
//
//
//
//    ┌────────────────────────────────────────────────────────────┐
//    │  Hitting replay will cause the queue to be rerun in FIFO   │
//    └────────────────────────────────────────────────────────────┘
//
//       ╳                                                           │
//       ╳                                                           │
//       ╳  ─ ─ ─ ─ ─ ─ ─ ─ ─ ─replay ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─▶ │
//       ╳                                                           │
//       ╳                                                           │
//       ╳                                                           │
//       ▼                                                           ▼
//  ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐
//  │         │ │         │ │         │ │         │ │         │ │         │
//  │  Block  │ │  Block  │ │  Block  │ │  Block  │ │  Block  │ │  Block  │
//  │         │ │         │ │         │ │         │ │         │ │         │
//  └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘
//
//
//
//
//
//
//
//
//
//
//
//
//
//   ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────┐
//   │    Should new blocks be added during replay mode they will simply be appended and run in FIFO order     │
//   └─────────────────────────────────────────────────────────────────────────────────────────────────────────┘
//
//                               ╳                                             │
//                               ╳                                             │
//                               ╳  ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─▶ │
//                               ╳                     replay                  │
//                               ╳                                             │
//                               ╳                                             │
//                               ▼                                             ▼
// ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐
// │         │ │         │ │         │ │         │ │         │ │         │ │         │
// │  Block  │ │  Block  │ │  Block  │ │  Block  │ │  Block  │ │  Block  │ │  Block  │
// │         │ │         │ │         │ │         │ │         │ │         │ │         │
// └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘

Author

Shaheen Ghiassy, shaheen.ghiassy@gmail.com

License

thread-safe-block-queue is available under the MIT license. See the LICENSE file for more info.

About

License:MIT License


Languages

Language:Objective-C 87.6%Language:Shell 11.1%Language:Ruby 1.4%