Skip to content

Commit

Permalink
fix(co_queue.m): fix crash cause by co_get_current_queue
Browse files Browse the repository at this point in the history
fix crash cause by co_get_current_queue, add co_is_current_queue_eqaul todo memory equal, fix
progress tests crash on ios11

fix alibaba#52
  • Loading branch information
pengyutang125 committed Mar 20, 2019
1 parent 7fe2e25 commit a083644
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 27 deletions.
20 changes: 8 additions & 12 deletions Examples/coSwiftDemo/coSwiftDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -265,20 +265,18 @@
files = (
);
inputPaths = (
"${SRCROOT}/Pods/Target Support Files/Pods-coSwiftDemo/Pods-coSwiftDemo-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/cocore-iOS11.0/cocore.framework",
"${BUILT_PRODUCTS_DIR}/coswift-iOS11.0/coswift.framework",
"${BUILT_PRODUCTS_DIR}/fishhook-iOS11.0/fishhook.framework",
"${PODS_ROOT}/Target Support Files/Pods-coSwiftDemo/Pods-coSwiftDemo-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/cocore-iOS10.0/cocore.framework",
"${BUILT_PRODUCTS_DIR}/coswift-iOS10.0/coswift.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/cocore.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/coswift.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/fishhook.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-coSwiftDemo/Pods-coSwiftDemo-frameworks.sh\"\n";
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-coSwiftDemo/Pods-coSwiftDemo-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
3CB0D2E8875A209F43DBBCA4 /* [CP] Check Pods Manifest.lock */ = {
Expand All @@ -305,24 +303,22 @@
files = (
);
inputPaths = (
"${SRCROOT}/Pods/Target Support Files/Pods-coSwiftDemoTests/Pods-coSwiftDemoTests-frameworks.sh",
"${PODS_ROOT}/Target Support Files/Pods-coSwiftDemoTests/Pods-coSwiftDemoTests-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/cocore-iOS12.1/cocore.framework",
"${BUILT_PRODUCTS_DIR}/coswift-iOS12.1/coswift.framework",
"${BUILT_PRODUCTS_DIR}/fishhook-iOS12.1/fishhook.framework",
"${BUILT_PRODUCTS_DIR}/Nimble/Nimble.framework",
"${BUILT_PRODUCTS_DIR}/Quick/Quick.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/cocore.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/coswift.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/fishhook.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Nimble.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Quick.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-coSwiftDemoTests/Pods-coSwiftDemoTests-frameworks.sh\"\n";
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-coSwiftDemoTests/Pods-coSwiftDemoTests-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
/* End PBXShellScriptBuildPhase section */
Expand Down Expand Up @@ -501,7 +497,7 @@
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = 8JWVBB5S2P;
INFOPLIST_FILE = coSwiftDemo/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.nj.coSwiftDemo;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -518,7 +514,7 @@
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = 8JWVBB5S2P;
INFOPLIST_FILE = coSwiftDemo/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.nj.coSwiftDemo;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1010"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "A3D67CF921F086F5009328CC"
BuildableName = "coSwiftDemoTests.xctest"
BlueprintName = "coSwiftDemoTests"
ReferencedContainer = "container:coSwiftDemo.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,33 @@ static id testPromise3() {
}
}];
[task resume];
[promise setupWithProgress:task.progress];
if (@available(iOS 11.0, *)) {
[promise setupWithProgress:task.progress];
} else {
// Fallback on earlier versions
NSProgress *progress = [NSProgress progressWithTotalUnitCount:10];

[promise setupWithProgress:progress];


dispatch_source_t timer = nil;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), 0.1 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
dispatch_source_set_event_handler(timer, ^{
if (progress.completedUnitCount < progress.totalUnitCount) {
progress.completedUnitCount += 1;
}
else{
dispatch_source_cancel(timer);
}
});
dispatch_resume(timer);

[promise onCancel:^(COPromise * _Nonnull promise) {
dispatch_source_cancel(timer);
}];
}
return promise;
}

Expand Down
6 changes: 6 additions & 0 deletions cocore/co_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,11 @@
*/
dispatch_queue_t co_get_current_queue(void);

/**
check the current queue is equal to q.
@return YES if the current queue is equal to q, NO ifthe current queue is not equal to q
*/
BOOL co_is_current_queue_equal(dispatch_queue_t q);

#endif /* co_queue_h */
38 changes: 32 additions & 6 deletions cocore/co_queue.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#import <pthread/pthread.h>
#import <mach/mach.h>


dispatch_queue_t co_get_current_queue() {
if ([NSThread isMainThread]) {
return dispatch_get_main_queue();
Expand All @@ -30,14 +31,39 @@ dispatch_queue_t co_get_current_queue() {
kern_return_t kr = thread_info(thread,
THREAD_IDENTIFIER_INFO, (thread_info_t)&tiid, &cnt);
if (kr == KERN_SUCCESS) {
if (tiid.dispatch_qaddr == thread) {
return NULL;
ptrdiff_t thread_queue_offset = (void*)(uintptr_t)tiid.dispatch_qaddr - (void*)thread;

if (thread_queue_offset == 0) {
return nil;
}

if (tiid.dispatch_qaddr != 0) {
return *(__unsafe_unretained dispatch_queue_t*)(void*)tiid.dispatch_qaddr;
else{
__unsafe_unretained dispatch_queue_t *qptr = (__unsafe_unretained dispatch_queue_t *)((void*)thread + thread_queue_offset);
return *qptr;
}
return NULL;
}
return NULL;
}

BOOL co_is_current_queue_equal(dispatch_queue_t q){
if ([NSThread isMainThread]) {
return dispatch_get_main_queue() == q;
}
thread_identifier_info_data_t tiid;
thread_t thread = mach_thread_self();
mach_msg_type_number_t cnt = THREAD_IDENTIFIER_INFO_COUNT;
kern_return_t kr = thread_info(thread,
THREAD_IDENTIFIER_INFO, (thread_info_t)&tiid, &cnt);
if (kr == KERN_SUCCESS) {
ptrdiff_t thread_queue_offset = (void*)(uintptr_t)tiid.dispatch_qaddr - (void*)thread;

if (thread_queue_offset == 0) {
return NO;
}
else{
void *qptr = *((void**)((void*)thread + thread_queue_offset));
void *originptr = (__bridge void*)q;
return originptr == qptr;
}
}
return NO;
}
10 changes: 4 additions & 6 deletions coobjc/co/COCoroutine.m
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,7 @@ - (id)paramForKey:(NSString *)key {
}

- (BOOL)isCurrentQueue {
if (co_get_current_queue() == self.queue) {
return YES;
}
return NO;
return co_is_current_queue_equal(self.queue);
}

+ (COCoroutine *)currentCoroutine {
Expand Down Expand Up @@ -178,9 +175,10 @@ + (instancetype)coroutineWithBlock:(void(^)(void))block onQueue:(dispatch_queue_

- (void)performBlockOnQueue:(dispatch_block_t)block {
dispatch_queue_t queue = self.queue;
if (queue == co_get_current_queue()) {
if (co_is_current_queue_equal(queue)) {
block();
} else {
}
else{
dispatch_async(queue, block);
}
}
Expand Down
5 changes: 3 additions & 2 deletions coswift/Coroutine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,10 @@ open class Coroutine {

/// Execute code on the coroutine's queue
private func performBlockOnQueue(block: @escaping ()->Void ) {
if co_get_current_queue() == self.queue {
if co_is_current_queue_equal(self.queue){
block()
} else {
}
else{
self.queue.async {
block()
}
Expand Down

0 comments on commit a083644

Please sign in to comment.