Skip to content

Commit

Permalink
fix(lab3): bump code
Browse files Browse the repository at this point in the history
Signed-off-by: Yiyang Wu <[email protected]>
  • Loading branch information
ToolmanP committed Nov 22, 2024
1 parent 6a02c43 commit 17fb391
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Lab3/kernel/object/cap_group.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ cap_t sys_create_cap_group(unsigned long cap_group_args_p)

/* cap current cap_group */
/* LAB 3 TODO BEGIN */
new_cap_group = obj_alloc(TYPE_CAP_GROUP, sizeof(*new_cap_group));
/* Allocate a new cap_group object */

/* LAB 3 TODO END */
if (!new_cap_group) {
Expand Down
40 changes: 24 additions & 16 deletions Lab3/kernel/object/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ cap_t sys_create_thread(unsigned long thread_args_p)
void sys_thread_exit(void)
{
int cnt;
u32 old_exit_state;

/* As a normal application, the main thread will eventually invoke
* sys_exit_group or trigger unrecoverable fault (e.g., segfault).
Expand All @@ -461,21 +462,28 @@ void sys_thread_exit(void)

kdebug("%s is invoked\n", __func__);

/* Set thread state, which will be recycle afterwards */
current_thread->thread_ctx->thread_exit_state = TE_EXITING;

lock(&(current_cap_group->threads_lock));
cnt = --current_cap_group->thread_cnt;
unlock(&(current_cap_group->threads_lock));

if (cnt == 0) {
/*
* Current thread is the last thread in this cap_group,
* so we invoke sys_exit_group.
*/
kdebug("%s invokes sys_exit_group\n", __func__);
sys_exit_group(0);
/* The control flow will not go through */
/*
* Use cmpxchg here because there are other threads that may modify
* thread_exit_state.
*/
old_exit_state = atomic_cmpxchg_32(
(s32 *)(&current_thread->thread_ctx->thread_exit_state),
TE_RUNNING,
TE_EXITING);
if (old_exit_state == TE_RUNNING) {
lock(&(current_cap_group->threads_lock));
cnt = --current_cap_group->thread_cnt;
unlock(&(current_cap_group->threads_lock));

if (cnt == 0) {
/*
* Current thread is the last thread in this cap_group,
* so we invoke sys_exit_group.
*/
kdebug("%s invokes sys_exit_group\n", __func__);
sys_exit_group(0);
/* The control flow will not go through */
}
}

if (current_thread->clear_child_tid) {
Expand Down Expand Up @@ -563,5 +571,5 @@ int sys_get_prio(cap_t thread_cap)
int sys_set_tid_address(int *tidptr)
{
current_thread->clear_child_tid = tidptr;
return 0;
return current_thread->cap;
}

0 comments on commit 17fb391

Please sign in to comment.