-
Notifications
You must be signed in to change notification settings - Fork 0
/
k-threadgroup.cc
231 lines (203 loc) · 6.27 KB
/
k-threadgroup.cc
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "kernel.hh"
#include "k-wait.hh"
#include "k-vmiter.hh"
threadgroup* tgtable[NTHREADGROUP]; // array of process descriptor pointers
spinlock tgtable_lock;
threadgroup::threadgroup() {
}
pid_t threadgroup::assign_to_empty_tgid(spinlock_guard &guard, threadgroup* tg) {
for (pid_t i = 1; i < NTHREADGROUP; i++) {
if (tgtable[i] == nullptr) {
tgtable[i] = tg;
tg->tgid_ = i;
return i;
}
}
return -1;
}
void threadgroup::init(pid_t tgid, pid_t ppid, x86_64_pagetable* pt) {
tgid_ = tgid;
pagetable_ = pt;
ppid_ = ppid;
init_fd_table();
init_shm_table();
}
void threadgroup::init_fd_table() {
for (int i = 0; i < N_FILE_DESCRIPTORS; i++) {
fd_table_[i] = nullptr;
}
}
void threadgroup::init_shm_table() {
for (int i = 0; i < N_PER_PROC_SHMS; i++) {
shm_mapping_table_[i].shm_ = nullptr;
shm_mapping_table_[i].va_ = 0;
}
}
void threadgroup::add_proc_to_thread_list(proc* p) {
spinlock_guard guard(thread_list_lock_);
thread_list_.push_back(p);
}
void threadgroup::copy_fd_table_from_threadgroup(threadgroup* tg) {
spinlock_guard fd_guard(tg->fd_table_lock_);
for (int i = 0; i < N_FILE_DESCRIPTORS; i++) {
file* file = tg->fd_table_[i];
if (file) {
spinlock_guard guard(file->ref_count_lock_);
file->ref_count_++;
fd_table_[i] = file;
}
}
}
void threadgroup::copy_shm_mapping_table_from_threadgroup(threadgroup* tg) {
spinlock_guard fd_guard(tg->shm_mapping_table_lock_);
for (int i = 0; i < N_PER_PROC_SHMS; i++) {
shm_mapping* shm_mapping = &tg->shm_mapping_table_[i];
if (shm_mapping->shm_ != nullptr && shm_mapping->va_ != 0) {
spinlock_guard guard(shm_mapping->shm_->ref_count_lock_);
shm_mapping->shm_->ref_count_++;
shm_mapping_table_[i].shm_ = shm_mapping->shm_;
shm_mapping_table_[i].va_ = shm_mapping->va_;
}
}
}
bool threadgroup::is_exited(spinlock_guard &guard) {
return thread_list_.front() == nullptr;
}
void threadgroup::exit(int status) {
}
void threadgroup::put_shm(int shmid, spinlock_guard& guard) {
shm_mapping* sm = &shm_mapping_table_[shmid];
{
spinlock_guard global_shm_store_guard(global_shm_store.list_lock_);
spinlock_guard ref_count_guard(sm->shm_->ref_count_lock_);
sm->shm_->ref_count_--;
log_printf("decrementing for %p refcount[%d] %d\n", sm->shm_->kptr_, sm->shm_->ref_count_, sm->shm_->id_);
if (sm->shm_->ref_count_ == 0) {
global_shm_store.list_[sm->shm_->id_] = nullptr;
ref_count_guard.unlock();
kfree(sm->shm_->kptr_);
kfree(sm->shm_);
}
}
}
void threadgroup::exit_cleanup(int status) {
// First assert that there are no running threads
{
spinlock_guard guard(thread_list_lock_);
assert(thread_list_.front() == nullptr);
}
{
spinlock_guard guard(process_hierarchy_lock);
spinlock_guard tgtable_guard(tgtable_lock);
threadgroup* parent = tgtable[ppid_];
tgtable_guard.unlock();
threadgroup* child = children_list_.pop_front();
while (child) {
child->ppid_ = 1;
tgtable[1]->children_list_.push_back(child);
child = children_list_.pop_front();
}
{
spinlock_guard fd_table_guard(fd_table_lock_);
for (int i = 0; i < N_FILE_DESCRIPTORS; i++) {
if (fd_table_[i]) {
// TODO, replace current() with more reasonable
current()->close_fd(i, fd_table_guard);
}
}
}
free_shm_table();
x86_64_pagetable* original_pagetable = pagetable_;
kfree_all_user_mappings(original_pagetable);
set_pagetable(early_pagetable);
kfree_pagetable(original_pagetable);
pagetable_ = early_pagetable;
process_exit_status_ = status;
is_exited_ = true;
parent->process_wq_.wake_all();
{
spinlock_guard timer_guard(timer_lock);
parent->interrupt_sleep_ = true;
timer_queue.wake_all();
}
}
}
void threadgroup::free_shm_table() {
spinlock_guard shm_mapping_table_guard(shm_mapping_table_lock_);
for (int i = 0; i < N_PER_PROC_SHMS; i++) {
if (shm_mapping_table_[i].shm_ != nullptr) {
if (shm_mapping_table_[i].va_ != 0) {
vmiter it(pagetable_, shm_mapping_table_[i].va_);
it.unmap();
}
put_shm(i, shm_mapping_table_guard);
}
}
}
int threadgroup::waitpid(pid_t tgid, int* stat, int options) {
threadgroup* wait_child = nullptr;
if (tgid != 0) {
spinlock_guard guard(process_hierarchy_lock);
wait_child = get_child(tgid, guard);
if (!wait_child) {
return E_CHILD;
}
if (!wait_child->is_exited_) {
if (options == W_NOHANG) {
return E_AGAIN;
} else {
waiter().block_until(process_wq_, [&] () {
return wait_child->is_exited_.load() == true;
}, guard);
}
}
wait_child->sibling_links_.erase();
} else {
spinlock_guard guard(process_hierarchy_lock);
if (children_list_.empty()) {
return E_CHILD;
}
wait_child = get_any_exited_child(guard);
if (!wait_child) {
if (options == W_NOHANG) {
return E_AGAIN;
} else {
waiter().block_until(process_wq_, [&] () {
wait_child = get_any_exited_child(guard);
return !!wait_child;
}, guard);
}
}
wait_child->sibling_links_.erase();
}
pid_t freed_tgid = wait_child->tgid_;
if (stat != nullptr) {
*stat = wait_child->process_exit_status_;
}
{
spinlock_guard guard(tgtable_lock);
tgtable[freed_tgid] = nullptr;
}
kfree(wait_child);
return freed_tgid;
}
threadgroup* threadgroup::get_child(pid_t tgid, spinlock_guard &guard) {
threadgroup* child = nullptr;
for (threadgroup* tg = children_list_.front(); tg; tg = children_list_.next(tg)) {
if (tg->tgid_ == tgid) {
child = tg;
break;
}
}
return child;
}
threadgroup* threadgroup::get_any_exited_child(spinlock_guard &guard) {
threadgroup* wait_child = nullptr;
for (threadgroup* tg = children_list_.front(); tg; tg = children_list_.next(tg)) {
if (tg->is_exited_.load()) {
wait_child = tg;
break;
}
}
return wait_child;
}