summaryrefslogtreecommitdiff
path: root/kern/thread.h
blob: 8e1f961ffaea2505c1002b504f2824a14d15dabc (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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * Copyright (c) 2012 Richard Braun.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _KERN_THREAD_H
#define _KERN_THREAD_H

#include <kern/assert.h>
#include <kern/list.h>
#include <kern/macros.h>
#include <kern/param.h>
#include <machine/cpu.h>
#include <machine/tcb.h>

/*
 * Forward declaration.
 */
struct task;

/*
 * Thread name buffer size.
 */
#define THREAD_NAME_SIZE 32

/*
 * Thread flags.
 */
#define THREAD_RESCHEDULE   0x1 /* Thread marked for reschedule */

/*
 * Thread structure.
 */
struct thread {
    struct tcb tcb;
    short flags;
    unsigned short pinned;
    unsigned short preempt;
    struct list runq_node;
    struct list task_node;
    struct task *task;
    void *stack;
    char name[THREAD_NAME_SIZE];
    void (*fn)(void *);
    void *arg;
} __aligned(CPU_L1_SIZE);

/*
 * Early initialization of the thread module.
 *
 * This function makes it possible to use migration and preemption control
 * operations while the system is initializing itself.
 */
void thread_bootstrap(void);

/*
 * Early initialization of the TCB on APs.
 */
void thread_ap_bootstrap(void);

/*
 * Initialize the thread module.
 */
void thread_setup(void);

/*
 * Create a thread.
 *
 * If the given name is null, the task name is used instead.
 */
int thread_create(struct thread **threadp, struct task *task, const char *name,
                  void (*fn)(void *), void *arg);

/*
 * Start running threads on the local processor.
 *
 * Interrupts must be enabled when calling this function.
 */
void __noreturn thread_run(void);

/*
 * Invoke the scheduler.
 */
void thread_schedule(void);

/*
 * Invoke the scheduler from interrupt context.
 */
void thread_intr_schedule(void);

/*
 * Invoke the scheduler from preemption control functions.
 */
void thread_preempt_schedule(void);

/*
 * Report a periodic timer interrupt on the thread currently running on
 * the local processor.
 *
 * Called from interrupt context.
 */
void thread_tick(void);

static inline struct thread *
thread_current(void)
{
    return structof(tcb_current(), struct thread, tcb);
}

/*
 * Migration control functions.
 *
 * Functions that change the migration state are implicit compiler barriers.
 *
 * TODO Make thread_current migration-safe.
 */

static inline int
thread_pinned(void)
{
    return (thread_current()->pinned != 0);
}

static inline void
thread_pin(void)
{
    struct thread *thread;

    thread = thread_current();
    thread->pinned++;
    assert(thread->pinned != 0);
    barrier();
}

static inline void
thread_unpin(void)
{
    struct thread *thread;

    barrier();
    thread = thread_current();
    assert(thread->pinned != 0);
    thread->pinned--;
}

/*
 * Preemption control functions.
 *
 * Functions that change the preemption state are implicit compiler barriers.
 */

static inline int
thread_preempt_enabled(void)
{
    return (thread_current()->preempt == 0);
}

static inline void
thread_preempt_enable_no_resched(void)
{
    struct thread *thread;

    barrier();
    thread = thread_current();
    assert(thread->preempt != 0);
    thread->preempt--;
}

static inline void
thread_preempt_enable(void)
{
    struct thread *thread;

    barrier();
    thread = thread_current();
    assert(thread->preempt != 0);
    thread->preempt--;

    if (thread->flags & THREAD_RESCHEDULE)
        thread_preempt_schedule();
}

static inline void
thread_preempt_disable(void)
{
    struct thread *thread;

    thread = thread_current();
    thread->preempt++;
    assert(thread->preempt != 0);
    barrier();
}

#endif /* _KERN_THREAD_H */