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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
#pragma once
#include <proc/context.h>
#include <proc/sched.h>
#include <proc/spinlock.h>
#include <util/list.h>
/*=====================
* Types and structures
*====================*/
/*
* Alias for an entry point function of a new thread.
*/
typedef context_func_t kthread_func_t;
/*
* Thread states.
*/
typedef enum
{
KT_NO_STATE, /* Illegal state */
KT_ON_CPU, /* Currently running */
KT_RUNNABLE, /* On the run queue */
KT_SLEEP, /* Blocked indefinitely */
KT_SLEEP_CANCELLABLE, /* Blocked, but can be interrupted */
KT_EXITED /* Exited, waiting to be joined */
} kthread_state_t;
/*
* Thread descriptor.
*/
typedef struct kthread
{
context_t kt_ctx; /* Thread context */
char *kt_kstack; /* Kernel stack */
void *kt_retval; /* Return value */
long kt_errno; /* Errno of most recent syscall */
struct proc *kt_proc; /* Corresponding process */
long kt_cancelled; /* Set if the thread has been cancelled */
ktqueue_t *kt_wchan; /* If blocking, the queue this thread is blocked on */
kthread_state_t kt_state;
list_link_t kt_plink; /* Link on the process's thread list, p_threads */
list_link_t
kt_qlink; /* Link on some ktqueue if the thread is not running */
list_t kt_mutexes; /* List of owned mutexes, for use in debugging */
long kt_recent_core; /* For SMP */
uint64_t kt_preemption_count;
} kthread_t;
/*==========
* Functions
*=========*/
/**
* Initializes the kthread subsystem at system startup.
*/
void kthread_init(void);
/**
* Allocates and initializes a kernel thread.
*
* @param proc the process in which the thread will run
* @param func the function that will be called when the newly created
* thread starts executing
* @param arg1 the first argument to func
* @param arg2 the second argument to func
* @return the newly created thread
*
*/
kthread_t *kthread_create(struct proc *proc, kthread_func_t func, long arg1,
void *arg2);
/**
* Creates a clone of the specified thread
*
* @param thr the thread to clone
* @return a clone of thr
*/
kthread_t *kthread_clone(kthread_t *thr);
/**
* Frees resources associated with a thread.
*
* @param thr the thread to free
*/
void kthread_destroy(kthread_t *thr);
/**
* Cancels a thread.
*
* @param kthr the thread to be cancelled
* @param retval the return value for the thread
*/
void kthread_cancel(kthread_t *kthr, void *retval);
/**
* Exits the current thread.
*
* @param retval the return value for the thread
*/
void kthread_exit(void *retval);
|