/*
 * Copyright (c) 2026, Chloe M.
 * Provided under the BSD-3 clause.
 *
 * Description: Thread management
 * Author:      Chloe M.
 */

#ifndef _PS_THREAD_H_
#define _PS_THREAD_H_ 1

#include <ptapi/status.h>
#include <hal/tcb.h>
#include <ptdef.h>
#include <queue.h>

#define THREAD_POOL_TAG 'TD'

struct _PROCESS;

/*
 * Represents a runnable thread that exists within
 * processes.
 *
 * @Tid:        Thread ID
 * @Tcb:        Thread control block
 * @StackBase:  Execution stack base
 * @Parent:     Parent process
 * @ThreadLink: Thread queue link
 */
typedef struct _THREAD {
    UQUAD Tid;
    TCB Tcb;
    UPTR StackBase;
    struct _PROCESS *Parent;
    TAILQ_ENTRY(_THREAD) ThreadLink;
} THREAD;

/*
 * Initialize the threading layer
 */
VOID PsThreadInit(VOID);

/*
 * Exit the current thread
 *
 * @ExitCode:  Exit code to terminate with
 */
NO_RETURN VOID PsExitThread(ULONG ExitCode);

/*
 * Create a new thread
 *
 * @Parent:     Parent of thread to create
 * @Flags:      Optional init flags
 * @Ip:         Instruction pointer value to start off with
 * @Result:     Result is written here
 */
PT_STATUS PsCreateThread(
    struct _PROCESS *Parent, ULONG Flags,
    UPTR Ip, THREAD **Result
);

#endif  /* !_PS_THREAD_H_ */
