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
39 changes: 39 additions & 0 deletions include/dmosi.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,33 @@ DMOD_BUILTIN_API( dmosi, 1.0, dmosi_process_t, _process_get_parent, (dmosi_proce
*/
typedef void (*dmosi_thread_entry_t)(void* arg);

/**
* @brief Thread state enumeration
*/
typedef enum {
DMOSI_THREAD_STATE_CREATED, /**< Thread created but not started */
DMOSI_THREAD_STATE_READY, /**< Thread is ready to run */
DMOSI_THREAD_STATE_RUNNING, /**< Thread is currently running */
DMOSI_THREAD_STATE_BLOCKED, /**< Thread is blocked (waiting for resource) */
DMOSI_THREAD_STATE_SUSPENDED, /**< Thread is suspended */
DMOSI_THREAD_STATE_TERMINATED /**< Thread has terminated */
} dmosi_thread_state_t;

/**
* @brief Thread information structure
*
* This structure holds auxiliary information about a thread, including
* stack usage statistics, state, CPU usage, and runtime.
*/
typedef struct {
size_t stack_total; /**< Total stack size in bytes */
size_t stack_current; /**< Current stack usage in bytes */
size_t stack_peak; /**< Peak stack usage in bytes */
dmosi_thread_state_t state; /**< Current thread state */
float cpu_usage; /**< CPU usage as a percentage (0.0 - 100.0) */
uint64_t runtime_ms; /**< Thread runtime in milliseconds */
} dmosi_thread_info_t;

/**
* @brief Create a thread
*
Expand Down Expand Up @@ -467,6 +494,18 @@ DMOD_BUILTIN_API( dmosi, 1.0, size_t, _thread_get_all, (dmosi_thread_t* threads,
*/
DMOD_BUILTIN_API( dmosi, 1.0, size_t, _thread_get_by_process, (dmosi_process_t process, dmosi_thread_t* threads, size_t max_count) );

/**
* @brief Get information about a thread
*
* Fills the provided @p info structure with auxiliary information about
* @p thread, including stack usage statistics, state, CPU usage, and runtime.
*
* @param thread Thread handle (if NULL, returns info for the current thread)
* @param info Pointer to a dmosi_thread_info_t structure to fill
* @return int 0 on success, negative error code on failure
*/
DMOD_BUILTIN_API( dmosi, 1.0, int, _thread_get_info, (dmosi_thread_t thread, dmosi_thread_info_t* info) );

/** @} */ // end of DMOSI_THREAD_API

//==============================================================================
Expand Down
15 changes: 15 additions & 0 deletions src/dmosi.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ DMOD_INPUT_WEAK_API_DECLARATION( dmosi, 1.0, size_t, _thread_get_by_process, (dm
return 0;
}

DMOD_INPUT_WEAK_API_DECLARATION( dmosi, 1.0, int, _thread_get_info, (dmosi_thread_t thread, dmosi_thread_info_t* info) )
{
(void)thread;
if (info == NULL) {
return -EINVAL;
}
info->stack_total = 0;
info->stack_current = 0;
info->stack_peak = 0;
info->state = DMOSI_THREAD_STATE_TERMINATED;
info->cpu_usage = 0.0f;
info->runtime_ms = 0;
return -ENOSYS;
}

//==============================================================================
// Process API
//==============================================================================
Expand Down