-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update lock-free queue for multi-threading, add remove task process in the wait method of Task. #436
Merged
Merged
Update lock-free queue for multi-threading, add remove task process in the wait method of Task. #436
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d9be4c5
Update lock-free queue for multi-threading, add remove task process i…
kevingpqi123 f042c88
Merge branch 'main' into feature/kevingpqi_threads
kevingpqi123 f00d2dd
update
kevingpqi123 27ae210
Modify the Task interface and improve the implementation.
kevingpqi123 3d39078
update
kevingpqi123 41c5136
Add taskStatus method to the Task class.
kevingpqi123 bf1ea98
Renmae some methods.
domchen 9fd14e3
Add some comments.
domchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,42 +34,54 @@ std::shared_ptr<Task> Task::Run(std::function<void()> block) { | |
Task::Task(std::function<void()> block) : block(std::move(block)) { | ||
} | ||
|
||
bool Task::executing() { | ||
std::lock_guard<std::mutex> autoLock(locker); | ||
return _executing; | ||
} | ||
|
||
bool Task::cancelled() { | ||
std::lock_guard<std::mutex> autoLock(locker); | ||
return _cancelled; | ||
} | ||
|
||
bool Task::finished() { | ||
std::lock_guard<std::mutex> autoLock(locker); | ||
return !_executing && !_cancelled; | ||
} | ||
|
||
void Task::wait() { | ||
std::unique_lock<std::mutex> autoLock(locker); | ||
if (!_executing) { | ||
auto oldStatus = _status.load(std::memory_order_relaxed); | ||
if (oldStatus == TaskStatus::Canceled || oldStatus == TaskStatus::Finished) { | ||
return; | ||
} | ||
condition.wait(autoLock); | ||
// If wait() is called from the thread pool, all threads might block, leaving no thread to execute | ||
// this task. To avoid deadlock, execute the task directly on the current thread if it's queued. | ||
if (oldStatus == TaskStatus::Queueing) { | ||
if (_status.compare_exchange_weak(oldStatus, TaskStatus::Executing, std::memory_order_acq_rel, | ||
std::memory_order_relaxed)) { | ||
block(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这句之前加一行注释:If wait() is called from the thread pool, all threads might block, leaving no thread to execute this task. To avoid deadlock, execute the task directly on the current thread if it's queued. |
||
oldStatus = TaskStatus::Executing; | ||
while (!_status.compare_exchange_weak(oldStatus, TaskStatus::Finished, | ||
std::memory_order_acq_rel, std::memory_order_relaxed)) | ||
; | ||
return; | ||
} | ||
} | ||
std::unique_lock<std::mutex> autoLock(locker); | ||
if (_status.load(std::memory_order_acquire) == TaskStatus::Executing) { | ||
condition.wait(autoLock); | ||
} | ||
} | ||
|
||
void Task::cancel() { | ||
std::unique_lock<std::mutex> autoLock(locker); | ||
if (!_executing) { | ||
return; | ||
auto currentStatus = _status.load(std::memory_order_relaxed); | ||
if (currentStatus == TaskStatus::Queueing) { | ||
_status.compare_exchange_weak(currentStatus, TaskStatus::Canceled, std::memory_order_acq_rel, | ||
std::memory_order_relaxed); | ||
} | ||
_executing = false; | ||
_cancelled = true; | ||
} | ||
|
||
void Task::execute() { | ||
block(); | ||
std::lock_guard<std::mutex> auoLock(locker); | ||
_executing = false; | ||
condition.notify_all(); | ||
auto oldStatus = _status.load(std::memory_order_relaxed); | ||
if (oldStatus == TaskStatus::Queueing && | ||
_status.compare_exchange_weak(oldStatus, TaskStatus::Executing, std::memory_order_acq_rel, | ||
std::memory_order_relaxed)) { | ||
block(); | ||
oldStatus = TaskStatus::Executing; | ||
while (!_status.compare_exchange_weak(oldStatus, TaskStatus::Finished, | ||
std::memory_order_acq_rel, std::memory_order_relaxed)) | ||
; | ||
std::unique_lock<std::mutex> autoLock(locker); | ||
condition.notify_all(); | ||
} | ||
} | ||
|
||
TaskStatus Task::status() const { | ||
return _status.load(std::memory_order_relaxed); | ||
} | ||
} // namespace tgfx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个Capacity还有一种写法,可以声明到template的参数里, template <typename T, size_t N>, 这样就可以直接声明了。