Skip to content

Commit

Permalink
docs: missing comment blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Dec 14, 2024
1 parent cbdc1f9 commit 786b1dd
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
6 changes: 6 additions & 0 deletions include/dpp/cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@

namespace dpp {

/**
* @brief Pass this value into the constructor of dpp::cluster for the shard count to create a cluster with no shards.
* A cluster with no shards does not connect to a websocket, but can still use the event loop to dispatch HTTPS API
* requests to Discord. This is useful for bots that do not need to receive websocket events as it will save a lot of
* resources.
*/
constexpr uint32_t NO_SHARDS = ~0U;

/**
Expand Down
1 change: 1 addition & 0 deletions include/dpp/sslclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ class DPP_EXPORT ssl_client

/**
* @brief Connect to a specified host and port. Throws std::runtime_error on fatal error.
* @param creator Creating cluster
* @param _hostname The hostname to connect to
* @param _port the Port number to connect to
* @param plaintext_downgrade Set to true to connect using plaintext only, without initialising SSL.
Expand Down
49 changes: 48 additions & 1 deletion include/dpp/thread_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,35 @@

namespace dpp {

/**
* @brief A work unit is a lambda executed in the thread pool
*/
using work_unit = std::function<void()>;

/**
* A task within a thread pool. A simple lambda that accepts no parameters and returns void.
* @brief A task within a thread pool. A simple lambda that accepts no parameters and returns void.
*/
struct DPP_EXPORT thread_pool_task {
/**
* @brief Task priority, lower value is higher priority
*/
int priority;
/**
* @brief Work unit to execute as the task
*/
work_unit function;
};

/**
* @brief Compares two thread pool tasks by priority
*/
struct DPP_EXPORT thread_pool_task_comparator {
/**
* @brief Compare two tasks
* @param a first task
* @param b second task
* @return true if a > b
*/
bool operator()(const thread_pool_task &a, const thread_pool_task &b) const {
return a.priority > b.priority;
};
Expand All @@ -51,10 +69,30 @@ struct DPP_EXPORT thread_pool_task_comparator {
* into a queue, which is processed in-order by whichever thread is free.
*/
struct DPP_EXPORT thread_pool {

/**
* @brief Threads that comprise the thread pool
*/
std::vector<std::thread> threads;

/**
* @brief Priority queue of tasks to be executed
*/
std::priority_queue<thread_pool_task, std::vector<thread_pool_task>, thread_pool_task_comparator> tasks;

/**
* @brief Mutex for accessing the priority queue
*/
std::mutex queue_mutex;

/**
* @brief Condition variable to notify for new tasks to run
*/
std::condition_variable cv;

/**
* @brief True if the thread pool is due to stop
*/
bool stop{false};

/**
Expand All @@ -63,7 +101,16 @@ struct DPP_EXPORT thread_pool {
* @param num_threads number of threads in the pool
*/
explicit thread_pool(class cluster* creator, size_t num_threads = std::thread::hardware_concurrency());

/**
* @brief Destroy the thread pool
*/
~thread_pool();

/**
* @brief Enqueue a new task to the thread pool
* @param task task to enqueue
*/
void enqueue(thread_pool_task task);
};

Expand Down
9 changes: 9 additions & 0 deletions include/dpp/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,16 @@ struct DPP_EXPORT timer_t {
timer_callback_t on_stop{};
};

/**
* @brief Used to compare two timers next tick times in a priority queue
*/
struct DPP_EXPORT timer_comparator {
/**
* @brief Compare two timers
* @param a first timer
* @param b second timer
* @return returns true if a > b
*/
bool operator()(const timer_t &a, const timer_t &b) const {
return a.next_tick > b.next_tick;
};
Expand Down
1 change: 1 addition & 0 deletions include/dpp/wsclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ class DPP_EXPORT websocket_client : public ssl_client {

/**
* @brief Connect to a specific websocket server.
* @param creator Creating cluster
* @param hostname Hostname to connect to
* @param port Port to connect to
* @param urlpath The URL path components of the HTTP request to send
Expand Down

0 comments on commit 786b1dd

Please sign in to comment.