forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASMainSerialQueue.mm
More file actions
76 lines (64 loc) · 1.53 KB
/
Copy pathASMainSerialQueue.mm
File metadata and controls
76 lines (64 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//
// ASMainSerialQueue.mm
// Texture
//
// Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
// Changes after 4/13/2017 are: Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#import <AsyncDisplayKit/ASMainSerialQueue.h>
#import <AsyncDisplayKit/ASThread.h>
#import <AsyncDisplayKit/ASInternalHelpers.h>
#import <queue>
@interface ASMainSerialQueue ()
{
AS::Mutex _serialQueueLock;
std::queue<dispatch_block_t> _blocks;
}
@end
@implementation ASMainSerialQueue
- (NSUInteger)numberOfScheduledBlocks
{
AS::MutexLocker l(_serialQueueLock);
return _blocks.size();
}
- (void)performBlockOnMainThread:(dispatch_block_t)block
{
{
AS::MutexLocker l(_serialQueueLock);
_blocks.push(block);
}
[self runBlocks];
}
- (void)runBlocks
{
dispatch_block_t mainThread = ^{
AS::UniqueLock l(self->_serialQueueLock);
do {
if (self->_blocks.empty()) {
break;
}
dispatch_block_t block = self->_blocks.front();
self->_blocks.pop();
{
l.unlock();
block();
l.lock();
}
} while (true);
};
ASPerformBlockOnMainThread(mainThread);
}
- (NSString *)description
{
NSString *desc = [super description];
std::queue<dispatch_block_t> blocks = _blocks;
[desc stringByAppendingString:@" Blocks: "];
while (!blocks.empty()) {
dispatch_block_t block = blocks.front();
[desc stringByAppendingFormat:@"%@", block];
blocks.pop();
}
return desc;
}
@end