|
| 1 | +/** |
| 2 | + @file BNCThreads.h |
| 3 | + @package Branch-SDK |
| 4 | + @brief Utilities for working with threads, queues, and blocks. |
| 5 | +
|
| 6 | + @author Edward Smith |
| 7 | + @date May 2017 |
| 8 | + @copyright Copyright © 2017 Branch. All rights reserved. |
| 9 | +*/ |
| 10 | + |
| 11 | +#if __has_feature(modules) |
| 12 | +@import Foundation; |
| 13 | +#else |
| 14 | +#import <Foundation/Foundation.h> |
| 15 | +#endif |
| 16 | + |
| 17 | +///@group Blocks and Threads |
| 18 | +#pragma mark - Blocks and Threads |
| 19 | + |
| 20 | +static inline dispatch_time_t BNCDispatchTimeFromSeconds(NSTimeInterval seconds) { |
| 21 | + return dispatch_time(DISPATCH_TIME_NOW, seconds * NSEC_PER_SEC); |
| 22 | +} |
| 23 | + |
| 24 | +static inline void BNCAfterSecondsPerformBlockOnMainThread(NSTimeInterval seconds, dispatch_block_t block) { |
| 25 | + dispatch_after(BNCDispatchTimeFromSeconds(seconds), dispatch_get_main_queue(), block); |
| 26 | +} |
| 27 | + |
| 28 | +static inline void BNCPerformBlockOnMainThreadAsync(dispatch_block_t block) { |
| 29 | + dispatch_async(dispatch_get_main_queue(), block); |
| 30 | +} |
| 31 | + |
| 32 | +static inline uint64_t BNCNanoSecondsFromTimeInterval(NSTimeInterval interval) { |
| 33 | + return interval * ((NSTimeInterval) NSEC_PER_SEC); |
| 34 | +} |
| 35 | + |
| 36 | +static inline void BNCSleepForTimeInterval(NSTimeInterval seconds) { |
| 37 | + double secPart = trunc(seconds); |
| 38 | + double nanoPart = trunc((seconds - secPart) * ((double)NSEC_PER_SEC)); |
| 39 | + struct timespec sleepTime; |
| 40 | + sleepTime.tv_sec = (__typeof(sleepTime.tv_sec)) secPart; |
| 41 | + sleepTime.tv_nsec = (__typeof(sleepTime.tv_nsec)) nanoPart; |
| 42 | + nanosleep(&sleepTime, NULL); |
| 43 | +} |
0 commit comments