Skip to content

Commit

Permalink
thread: set/get priority
Browse files Browse the repository at this point in the history
  • Loading branch information
skypjack committed Oct 3, 2024
1 parent 1678cdb commit 4a1f1c6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/uvw/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ UVW_INLINE bool thread::equal(const thread &tl, const thread &tr) noexcept {
return !(0 == uv_thread_equal(tl.raw(), tr.raw()));
}

UVW_INLINE bool thread::priority(const thread &tl, thread_priority val) noexcept {
return (uv_thread_setpriority(*tl.raw(), static_cast<std::underlying_type_t<thread_priority>>(val)) == 0);
}

UVW_INLINE std::pair<bool, thread::thread_priority> thread::priority(const thread &tl) noexcept {
int prio{};
const bool res = (uv_thread_getpriority(*tl.raw(), &prio) == 0);
return {res, thread_priority{static_cast<std::underlying_type_t<thread_priority>>(prio)}};
}

UVW_INLINE thread::~thread() noexcept {
join();
}
Expand Down
28 changes: 27 additions & 1 deletion src/uvw/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ enum class uvw_thread_create_flags : std::underlying_type_t<uv_thread_create_fla
THREAD_HAS_STACK_SIZE = UV_THREAD_HAS_STACK_SIZE
};

}
enum class uvw_thread_priority : int {
THREAD_PRIO_HIGHEST = UV_THREAD_PRIORITY_HIGHEST,
THREAD_PRIO_ABOVE_NORMAL = UV_THREAD_PRIORITY_ABOVE_NORMAL,
THREAD_PRIO_NORMAL = UV_THREAD_PRIORITY_NORMAL,
THREAD_PRIO_BELOW_NORMAL = UV_THREAD_PRIORITY_BELOW_NORMAL,
THREAD_PRIO_LOWEST = UV_THREAD_PRIORITY_LOWEST
};

} // namespace details

class thread;
class thread_local_storage;
Expand All @@ -49,6 +57,7 @@ class thread final: public uv_type<uv_thread_t> {

public:
using create_flags = details::uvw_thread_create_flags;
using thread_priority = details::uvw_thread_priority;
using task = internal_task;
using type = uv_thread_t;

Expand All @@ -74,6 +83,23 @@ class thread final: public uv_type<uv_thread_t> {
*/
static bool equal(const thread &tl, const thread &tr) noexcept;

/**
* @brief Sets the scheduling priority of a given thread.
* @param tl A valid instance of a thread.
* @param val Thread priority to set.
* @return True in case of success, false otherwise.
*/
static bool priority(const thread &tl, thread_priority val) noexcept;

/**
* @brief Retrieves the scheduling priority of a given thread.
* @param tl A valid instance of a thread.
* @return A `std::pair` composed as it follows:
* * A boolean value that is true in case of success, false otherwise.
* * The scheduled priority for the given thread.
*/
static std::pair<bool, thread_priority> priority(const thread &tl) noexcept;

~thread() noexcept;

/**
Expand Down

0 comments on commit 4a1f1c6

Please sign in to comment.