Skip to content

Added some optional callback functions for the workers#11

Open
john-sharratt wants to merge 1 commit into
robinfriedli:masterfrom
wasmerio:master
Open

Added some optional callback functions for the workers#11
john-sharratt wants to merge 1 commit into
robinfriedli:masterfrom
wasmerio:master

Conversation

@john-sharratt
Copy link
Copy Markdown

Use cases for the callback functions are:

  • Extra logging
  • Thread local initialization
  • Changing the thread priority

Copy link
Copy Markdown
Owner

@robinfriedli robinfriedli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a neat feature, thanks. Just have some minor change requests

Comment thread src/lib.rs
keep_alive: Duration,
channel_data: Arc<ChannelData>,
worker_data: Arc<WorkerData>,
on_worker_start: Option<WorkerCallback>,
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add those fields to the WorkerData struct

Comment thread src/lib.rs
}

/// When a worker thread stops this callback function will be invoked
/// exactly once before
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs seem incomplete

Comment thread src/lib.rs
/// When a worker thread starts this callback function will be invoked
/// exactly once before it starts processing any jobs
pub fn on_worker_start<F>(&mut self, callback: F)
where F: Fn() + Send + Sync + 'static
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure to run cargo fmt, this should look more like

    pub fn on_worker_start<F>(&mut self, callback: F)
    where
        F: Fn() + Send + Sync + 'static,
    {
        self.on_worker_start.replace(Arc::new(callback));
    }

Comment thread src/lib.rs
assert!(val > 0);
});
}
thread::sleep(Duration::from_secs(2));
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use the join() function to reduce the chance of race conditions (if it takes longer than 2 seconds for whatever reason). And perhaps even use the on_worker_stop callback to signal a CondVar when the last worker stops and await it with a timeout, instead of sleeping much longer than necessary in most cases.

@robinfriedli
Copy link
Copy Markdown
Owner

@john-sharratt please let me know if you don't have time to work on this, I'll make the requested changes myself

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants