Skip to content

Commit

Permalink
fix(Coroutine):
Browse files Browse the repository at this point in the history
fix finishedBlock may be cover.

fix alibaba#44
  • Loading branch information
NianJi committed Mar 11, 2019
1 parent ec5ae3d commit 302c442
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
11 changes: 8 additions & 3 deletions coobjc/co/COCoroutine.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ @interface COCoroutine ()
@property(nonatomic, assign) BOOL isCancelled;
@property(nonatomic, assign) BOOL isResume;
@property(nonatomic, strong) NSMutableDictionary *parameters;
@property(nonatomic, copy, nullable) dispatch_block_t joinBlock;


- (void)execute;
Expand Down Expand Up @@ -80,8 +81,12 @@ static void co_exec(coroutine_t *co) {
coObj.isFinished = YES;
if (coObj.finishedBlock) {
coObj.finishedBlock();
coObj.finishedBlock = nil;
}
if (coObj.joinBlock) {
coObj.joinBlock();
coObj.joinBlock = nil;
}
coObj.finishedBlock = nil;
}
}

Expand Down Expand Up @@ -234,7 +239,7 @@ - (void)join {
[chan send_nonblock:@(1)];
}
else{
[self setFinishedBlock:^{
[self setJoinBlock:^{
[chan send_nonblock:@(1)];
}];
}
Expand All @@ -249,7 +254,7 @@ - (void)cancelAndJoin {
[chan send_nonblock:@(1)];
}
else{
[self setFinishedBlock:^{
[self setJoinBlock:^{
[chan send_nonblock:@(1)];
}];
[self _internalCancel];
Expand Down
11 changes: 8 additions & 3 deletions coswift/Coroutine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ open class Coroutine {
/// Callback when coroutine is finished.
public var finishedBlock: (() -> Void)?

/// Callback when coroutine is finished.
private var joinBlock: (() -> Void)?

/// The code body of the coroutine.
public var execBlock: () throws -> Void

Expand Down Expand Up @@ -90,13 +93,15 @@ open class Coroutine {
if let finishBlock = finishedBlock {
finishBlock()
}
if let join = joinBlock {
join()
}
}

do {
try self.execBlock()
} catch {
self.lastError = error

}
}

Expand Down Expand Up @@ -194,7 +199,7 @@ open class Coroutine {
if self.isFinished {
chan.send_nonblock(val: true)
} else {
self.finishedBlock = {
self.joinBlock = {
chan.send_nonblock(val: true)
}
}
Expand All @@ -218,7 +223,7 @@ open class Coroutine {
if self.isFinished {
chan.send_nonblock(val: true)
} else {
self.finishedBlock = {
self.joinBlock = {
chan.send_nonblock(val: true)
}
self.internalCancel()
Expand Down

0 comments on commit 302c442

Please sign in to comment.