GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Added `SDCallbackPolicyMainAsyncSafe` which has better compatibility … · SDWebImage/SDWebImage@9db39a4

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • SDWebImage/Core
    • SDCallbackQueue.h
    • SDCallbackQueue.m
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎SDWebImage/Core/SDCallbackQueue.h‎

Copy file name to clipboardExpand all lines: SDWebImage/Core/SDCallbackQueue.h
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ typedef NS_ENUM(NSUInteger, SDCallbackPolicy) {
1616
/// Follow async/sync using the correspond `dispatch_async`/`dispatch_sync` to dispatch block on queue
1717
SDCallbackPolicyDispatch = 1,
1818
/// Ignore any async/sync and just directly invoke `block` in current queue (without `dispatch_async`/`dispatch_sync`)
19-
SDCallbackPolicyInvoke = 2
19+
SDCallbackPolicyInvoke = 2,
20+
/// Ensure callback in main queue and main thread, will do `dispatch_async` if the current queue is not main queue; else do invoke `block`. Never use `dispatch_sync`, suitable for UI-related work
21+
SDCallbackPolicyMainAsyncSafe = 3
2022
};
2123

2224
/// SDCallbackQueue is a wrapper used to control how the completionBlock should perform on queues, used by our `Cache`/`Manager`/`Loader`.
@@ -32,7 +34,7 @@ typedef NS_ENUM(NSUInteger, SDCallbackPolicy) {
3234
/// The global concurrent queue (user-initiated QoS). Using `dispatch_get_global_queue`.
3335
@property (nonnull, class, readonly) SDCallbackQueue *globalQueue;
3436

35-
/// The current queue's callback policy, defaults to `SDCallbackPolicySafeExecute`, which behaves like the old macro `dispatch_main_async_safe`
37+
/// The current queue's callback policy, defaults to `SDCallbackPolicyMainAsyncSafe`, which behaves like the old macro `dispatch_main_async_safe`
3638
@property (assign, readwrite) SDCallbackPolicy policy;
3739

3840
- (nonnull instancetype)init NS_UNAVAILABLE;
Collapse file

‎SDWebImage/Core/SDCallbackQueue.m‎

Copy file name to clipboardExpand all lines: SDWebImage/Core/SDCallbackQueue.m
+31-1Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
@interface SDCallbackQueue ()
1313

1414
@property (nonatomic, strong, nonnull) dispatch_queue_t queue;
15+
@property (nonatomic, strong, nonnull) NSThread *thread;
1516

1617
@end
1718

@@ -20,8 +21,28 @@ static void SDReleaseBlock(void *context) {
2021
CFRelease(context);
2122
}
2223

24+
static void SDSafeMainAsync(SDCallbackQueue *callbackQueue, dispatch_block_t _Nonnull block) {
25+
// Extend gcd queue's life cycle
26+
dispatch_queue_t queue = callbackQueue.queue;
27+
if (NSThread.isMainThread) {
28+
// Check 1, for cases when global queue call `dispatch_sync` on main thread.
29+
const char *currentLabel = dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL);
30+
const char *mainLabel = dispatch_queue_get_label(dispatch_get_main_queue());
31+
if (mainLabel == currentLabel) {
32+
block();
33+
return;
34+
}
35+
// Check 2, since main queue can never execute on non-main thread.
36+
if (queue == dispatch_get_main_queue()) {
37+
block();
38+
return;
39+
}
40+
}
41+
dispatch_async(dispatch_get_main_queue(), block);
42+
}
43+
2344
static void SDSafeExecute(SDCallbackQueue *callbackQueue, dispatch_block_t _Nonnull block, BOOL async) {
24-
// Extendc gcd queue's life cycle
45+
// Extend gcd queue's life cycle
2546
dispatch_queue_t queue = callbackQueue.queue;
2647
// Special handle for main queue label only (custom queue can have the same label)
2748
const char *label = dispatch_queue_get_label(queue);
@@ -57,6 +78,9 @@ - (instancetype)initWithDispatchQueue:(dispatch_queue_t)queue {
5778
CFUUIDRef UUID = CFUUIDCreate(kCFAllocatorDefault);
5879
dispatch_queue_set_specific(queue, SDCallbackQueueKey, (void *)UUID, SDReleaseBlock);
5980
_queue = queue;
81+
_policy = SDCallbackPolicyMainAsyncSafe;
82+
// global queue can execute on main thread, like call `dispatch_sync(globalQueue)` in main thread
83+
_thread = NSThread.currentThread;
6084
}
6185
return self;
6286
}
@@ -94,6 +118,9 @@ - (void)sync:(nonnull dispatch_block_t)block {
94118
case SDCallbackPolicyInvoke:
95119
block();
96120
break;
121+
case SDCallbackPolicyMainAsyncSafe:
122+
SDSafeMainAsync(self, block);
123+
break;
97124
default:
98125
SDSafeExecute(self, block, NO);
99126
break;
@@ -111,6 +138,9 @@ - (void)async:(nonnull dispatch_block_t)block {
111138
case SDCallbackPolicyInvoke:
112139
block();
113140
break;
141+
case SDCallbackPolicyMainAsyncSafe:
142+
SDSafeMainAsync(self, block);
143+
break;
114144
default:
115145
SDSafeExecute(self, block, YES);
116146
break;

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.