Skip to content

Commit 87a6c65

Browse files
authored
Refactoring: thread_await() (#87)
Refactor ` thread_await()` into a separate function.
1 parent ab15a68 commit 87a6c65

File tree

3 files changed

+51
-37
lines changed

3 files changed

+51
-37
lines changed

include/kernel/domain/entities/thread.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ void thread_run_first(thread_t *thread);
5050

5151
void thread_run(thread_t *thread);
5252

53+
void thread_yield_current(void);
54+
5355
void thread_terminate_current(void);
5456

5557
void thread_switch_to(thread_t *to);
@@ -58,7 +60,7 @@ void thread_switch_to_and_block(thread_t *to);
5860

5961
void thread_block_current_and_unlock(spinlock_t *lock);
6062

61-
void thread_yield_current(void);
63+
int thread_await(thread_t *thread);
6264

6365
void thread_set_local_storage(thread_t *thread, addr_t addr, size_t size);
6466

kernel/application/syscalls/await_thread.c

+1-22
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,7 @@ static int with_thread(descriptor_t *thread_desc) {
4949
return -JINUE_EPERM;
5050
}
5151

52-
thread_t *current = get_current_thread();
53-
54-
if(thread == current) {
55-
return -JINUE_EDEADLK;
56-
}
57-
58-
spin_lock(&thread->await_lock);
59-
60-
if(thread->state == THREAD_STATE_CREATED || thread->awaiter != NULL) {
61-
spin_unlock(&thread->await_lock);
62-
return -JINUE_ESRCH;
63-
}
64-
65-
thread->awaiter = current;
66-
67-
if(thread->state == THREAD_STATE_ZOMBIE) {
68-
spin_unlock(&thread->await_lock);
69-
} else {
70-
thread_block_current_and_unlock(&thread->await_lock);
71-
}
72-
73-
return 0;
52+
return thread_await(thread);
7453
}
7554

7655
int await_thread(int fd) {

kernel/domain/entities/thread.c

+47-14
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030
*/
3131

32+
#include <jinue/shared/asm/errno.h>
3233
#include <kernel/domain/alloc/page_alloc.h>
3334
#include <kernel/domain/entities/descriptor.h>
3435
#include <kernel/domain/entities/object.h>
@@ -270,6 +271,33 @@ static thread_t *reschedule(bool current_can_run) {
270271
return to;
271272
}
272273

274+
/**
275+
* Yield the current thread
276+
*
277+
* The current thread is added at the tail of the ready queue. It continues
278+
* running if no other thread is ready to run.
279+
*/
280+
void thread_yield_current(void) {
281+
thread_t *current = get_current_thread();
282+
thread_t *to = reschedule(true);
283+
284+
if(to == current) {
285+
return;
286+
}
287+
288+
to->state = THREAD_STATE_RUNNING;
289+
290+
if(current->process != to->process) {
291+
process_switch_to(to->process);
292+
}
293+
294+
spin_lock(&ready_queue.lock);
295+
296+
thread_ready_locked(current);
297+
298+
machine_switch_thread_and_unlock(current, to, &ready_queue.lock);
299+
}
300+
273301
/**
274302
* Terminate the current thread
275303
*
@@ -388,30 +416,35 @@ void thread_block_current_and_unlock(spinlock_t *lock) {
388416
}
389417

390418
/**
391-
* Yield the current thread
419+
* Block until thread terminates
392420
*
393-
* The current thread is added at the tail of the ready queue. It continues
394-
* running if no other thread is ready to run.
421+
* @param thread awaited thread
422+
* @return zero on success, negated error code on failure
423+
*
395424
*/
396-
void thread_yield_current(void) {
397-
thread_t *current = get_current_thread();
398-
thread_t *to = reschedule(true);
425+
int thread_await(thread_t *thread) {
426+
thread_t *current = get_current_thread();
399427

400-
if(to == current) {
401-
return;
428+
if(thread == current) {
429+
return -JINUE_EDEADLK;
402430
}
403431

404-
to->state = THREAD_STATE_RUNNING;
432+
spin_lock(&thread->await_lock);
405433

406-
if(current->process != to->process) {
407-
process_switch_to(to->process);
434+
if(thread->state == THREAD_STATE_CREATED || thread->awaiter != NULL) {
435+
spin_unlock(&thread->await_lock);
436+
return -JINUE_ESRCH;
408437
}
409438

410-
spin_lock(&ready_queue.lock);
439+
thread->awaiter = current;
411440

412-
thread_ready_locked(current);
441+
if(thread->state == THREAD_STATE_ZOMBIE) {
442+
spin_unlock(&thread->await_lock);
443+
} else {
444+
thread_block_current_and_unlock(&thread->await_lock);
445+
}
413446

414-
machine_switch_thread_and_unlock(current, to, &ready_queue.lock);
447+
return 0;
415448
}
416449

417450
/**

0 commit comments

Comments
 (0)