Skip to content
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
19 changes: 15 additions & 4 deletions include/atframe/modules/worker_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,22 @@

#include <cstdint>
#include <functional>
#include <list>
#include <memory>

LIBATAPP_MACRO_NAMESPACE_BEGIN

struct UTIL_SYMBOL_VISIBLE worker_context {
// worker id 指示当前是第几个worker,0表示主线程,1表示第一个工作线程,依次类推。
// worker id 可能被复用或转移工作线程,但同时每个 worker id 指向唯一一个线程
uint32_t worker_id = 0;

inline worker_context() noexcept : worker_id(0) {}
explicit inline worker_context(uint32_t id) noexcept : worker_id(id) {}
// worker_unique_id 指示当前worker的唯一标识,不会随着线程转移而变化
// 注意: 在foreach接口中,如果对于stable的worker尚未分配完成,这个值可能传0
uint64_t worker_unique_id = 0;

inline worker_context() noexcept : worker_id(0), worker_unique_id(0) {}
explicit inline worker_context(uint32_t id, uint64_t unique_id) noexcept
: worker_id(id), worker_unique_id(unique_id) {}
Comment on lines 19 to +30
Comment on lines +28 to +30
};

enum class worker_job_event_type : uint32_t {
Expand All @@ -44,6 +51,11 @@ struct UTIL_SYMBOL_VISIBLE worker_meta {

using worker_job_action_type = std::function<void(const worker_context&)>;

using worker_event_callback_type = std::function<void(const worker_context&)>;

struct UTIL_SYMBOL_VISIBLE worker_event_callback_handle_data;
using worker_event_callback_handle_type = std::shared_ptr<worker_event_callback_handle_data>;

using worker_job_action_pointer = ::atfw::util::memory::strong_rc_ptr<worker_job_action_type>;

struct UTIL_SYMBOL_VISIBLE worker_job_data {
Expand Down Expand Up @@ -74,4 +86,3 @@ enum class worker_type : int32_t {
};

LIBATAPP_MACRO_NAMESPACE_END

30 changes: 29 additions & 1 deletion include/atframe/modules/worker_pool_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class worker_pool_module : public ::atframework::atapp::module_impl {

public:
LIBATAPP_MACRO_API worker_pool_module();
LIBATAPP_MACRO_API virtual ~worker_pool_module();
LIBATAPP_MACRO_API ~worker_pool_module() override;

private:
class worker;
Expand Down Expand Up @@ -78,6 +78,34 @@ class worker_pool_module : public ::atframework::atapp::module_impl {
// thread-safe
LIBATAPP_MACRO_API std::chrono::microseconds get_tick_interval(const worker_context& context) const;

// thread-safe
LIBATAPP_MACRO_API worker_event_callback_handle_type
add_event_callback_on_worker_created(worker_event_callback_type action);

// thread-safe
LIBATAPP_MACRO_API void remove_event_callback_on_worker_created(const worker_event_callback_handle_type& handle);

// thread-safe
LIBATAPP_MACRO_API worker_event_callback_handle_type
add_event_callback_on_worker_removed(worker_event_callback_type action);

// thread-safe
LIBATAPP_MACRO_API void remove_event_callback_on_worker_removed(const worker_event_callback_handle_type& handle);

// thread-safe
LIBATAPP_MACRO_API worker_event_callback_handle_type
add_event_callback_on_worker_started(worker_event_callback_type action);

// thread-safe
LIBATAPP_MACRO_API void remove_event_callback_on_worker_started(const worker_event_callback_handle_type& handle);

// thread-safe
LIBATAPP_MACRO_API worker_event_callback_handle_type
add_event_callback_on_worker_exiting(worker_event_callback_type action);

// thread-safe
LIBATAPP_MACRO_API void remove_event_callback_on_worker_exiting(const worker_event_callback_handle_type& handle);

// thread-safe
LIBATAPP_MACRO_API size_t get_current_worker_count() const noexcept;

Expand Down
Loading
Loading