summaryrefslogtreecommitdiff
path: root/user/inc/thr_internals.h
blob: e0ad52e8e705195b271bb35045ed535d57057d47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
 * @file thr_internals.h
 * @author Hanjie Wu (hanjiew)
 * @author Devang Acharya (devanga)
 * @brief tcb structure and management functions and excpetion stack
 * @date 2023-02-22
 *
 */

#ifndef THR_INTERNALS_H
#define THR_INTERNALS_H

#include <stdint.h>

#include <cond.h>
#include <mutex.h>
#include <syscall.h>

/**
 * @brief stack for excpetion handler
 */
extern char ex_stack[];

/**
 * @brief pointer to the highest address of exception stack
 */
extern void* ex_stack_end;

/**
 * @brief thread control block structure
 */
typedef struct tcb_s {
    int tid;            /* thread id */
    uintptr_t stack_lo; /* low limit of stack */
    uintptr_t stack_hi; /* high limit of stack */
    int is_main;        /* is main thread */

    int color;            /* rbtree color */
    struct tcb_s* parent; /* rbtree parent */
    struct tcb_s* left;   /* rbtree left node */
    struct tcb_s* right;  /* rbtree right node */

    struct tcb_s* mutex_next; /* queue for block on mutex */
    struct tcb_s* mutex_prev; /* queue for block on mutex */
    int mutex_resume; /* flag to show is other thread is going to resume this
                         thread */

    int waiter;       /* tid of other thread that called thr_join on it */
    int exited;       /* is exited */
    void* exit_value; /* return value */
    cond_t wait_cv;   /* cv to wait for thread exit */
} tcb_t;

/**
 * @brief tcb for main thread
 */
extern tcb_t main_tcb;

/**
 * @brief find tcb by thread id
 */
tcb_t* rb_find_tcb(int tid);
/**
 * @brief insert tcb into rbtree
 */
void rb_insert_tcb(tcb_t* tcb);
/**
 * @brief delete tcb from rbtree
 */
void rb_delete_tcb(tcb_t* tcb);

#define PAGE_ALIGN_MASK ((uintptr_t) ~((uintptr_t)(PAGE_SIZE - 1)))

#endif