// get current label
_queueLabel = [[NSString alloc] initWithFormat:@"%s", dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL)];
// specific check is need queue
void *key = (__bridge void *)self;
void *nonNullValue = (__bridge void *)self;
dispatch_queue_set_specific(_loggerQueue, key, nonNullValue, NULL);
// create group
_loggingGroup = dispatch_group_create();
// add to group
for (DDLoggerNode *loggerNode in self._loggers) {
...
dispatch_group_async(_loggingGroup, loggerNode->_loggerQueue, ^{ @autoreleasepool {
...
} });
}
// wait for excutor 等待执行
dispatch_group_wait(_loggingGroup, DISPATCH_TIME_FOREVER);
// create semaphore 1000
+ (void)initialize {
static dispatch_once_t DDLogOnceToken;
dispatch_once(&DDLogOnceToken, ^{
...
_queueSemaphore = dispatch_semaphore_create(DDLOG_MAX_QUEUE_SIZE);
...
});
}
// enqueue -1
dispatch_block_t logBlock = ^{
dispatch_semaphore_wait(_queueSemaphore, DISPATCH_TIME_FOREVER);
// We're now sure we won't overflow the queue.
// It is time to queue our log message.
@autoreleasepool {
[self lt_log:logMessage];
}
};
// dequeue +1
- (void)lt_log:(DDLogMessage *)logMessage {
...
dispatch_semaphore_signal(_queueSemaphore);
...
}
group 保证多任务,完成通知
semaphore 保证限制处理的资源数量,进行的数量控制
Dispatch_get_queue_label 获取当前队列的名称
dispatch_queue_set_specific 设置 特定值
网友评论