Skip to content
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

Signal the worker progress event from the worker progress thread #28

Merged
merged 2 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cpp/include/ucxx/worker_progress_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace ucxx {

typedef std::function<void(void)> SignalWorkerFunction;
typedef std::function<void(void*)> ProgressThreadStartCallback;
typedef void* ProgressThreadStartCallbackArg;

Expand All @@ -21,6 +22,9 @@ class WorkerProgressThread {
std::thread _thread{}; ///< Thread object
bool _stop{false}; ///< Signal to stop on next iteration
bool _pollingMode{false}; ///< Whether thread will use polling mode to progress
SignalWorkerFunction _signalWorkerFunction{
nullptr}; ///< Function signaling worker to wake the progress event (when _pollingMode is
///< `false`)
ProgressThreadStartCallback _startCallback{
nullptr}; ///< Callback to execute at start of the progress thread
ProgressThreadStartCallbackArg _startCallbackArg{
Expand Down Expand Up @@ -71,6 +75,8 @@ class WorkerProgressThread {
* @param[in] pollingMode whether the thread should use polling mode to
* progress.
* @param[in] progressFunction user-defined progress function implementation.
* @param[in] signalWorkerFunction user-defined function to wake the worker
* progress event (when `pollingMode` is `false`).
* @param[in] startCallback user-defined callback function to be executed
* at the start of the progress thread.
* @param[in] startCallbackArg an argument to be passed to the start callback.
Expand All @@ -79,6 +85,7 @@ class WorkerProgressThread {
*/
WorkerProgressThread(const bool pollingMode,
std::function<bool(void)> progressFunction,
std::function<void(void)> signalWorkerFunction,
ProgressThreadStartCallback startCallback,
ProgressThreadStartCallbackArg startCallbackArg,
std::shared_ptr<DelayedSubmissionCollection> delayedSubmissionCollection);
Expand Down
9 changes: 4 additions & 5 deletions cpp/src/worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,19 +254,18 @@ void Worker::startProgressThread(const bool pollingMode)
if (!pollingMode) initBlockingProgressMode();
auto progressFunction = pollingMode ? std::bind(&Worker::progress, this)
: std::bind(&Worker::progressWorkerEvent, this);
auto signalWorkerFunction =
pollingMode ? std::function<void()>{[]() {}} : std::bind(&Worker::signal, this);

_progressThread = std::make_shared<WorkerProgressThread>(pollingMode,
progressFunction,
signalWorkerFunction,
_progressThreadStartCallback,
_progressThreadStartCallbackArg,
_delayedSubmissionCollection);
}

void Worker::stopProgressThreadNoWarn()
{
if (_progressThread && !_progressThread->pollingMode()) signal();
_progressThread = nullptr;
}
void Worker::stopProgressThreadNoWarn() { _progressThread = nullptr; }
wence- marked this conversation as resolved.
Show resolved Hide resolved

void Worker::stopProgressThread()
{
Expand Down
7 changes: 6 additions & 1 deletion cpp/src/worker_progress_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ void WorkerProgressThread::progressUntilSync(
WorkerProgressThread::WorkerProgressThread(
const bool pollingMode,
std::function<bool(void)> progressFunction,
std::function<void(void)> signalWorkerFunction,
ProgressThreadStartCallback startCallback,
ProgressThreadStartCallbackArg startCallbackArg,
std::shared_ptr<DelayedSubmissionCollection> delayedSubmissionCollection)
: _pollingMode(pollingMode), _startCallback(startCallback), _startCallbackArg(startCallbackArg)
: _pollingMode(pollingMode),
_signalWorkerFunction(signalWorkerFunction),
_startCallback(startCallback),
_startCallbackArg(startCallbackArg)
{
_thread = std::thread(WorkerProgressThread::progressUntilSync,
progressFunction,
Expand All @@ -49,6 +53,7 @@ WorkerProgressThread::~WorkerProgressThread()
}

_stop = true;
_signalWorkerFunction();
_thread.join();
}
wence- marked this conversation as resolved.
Show resolved Hide resolved

Expand Down