1 /*
2  * timeout.c
3  *
4  */
5 
6 #include "thread.h"
7 
8 /*
9  * __thread_process_timeouts()
10  *
11  * Look for threads that have timed out.  This should be called
12  * under interrupt lock, before calling __schedule().
13  */
14 void __thread_process_timeouts(void)
15 {
16     struct thread *curr = current();
17     struct thread_list *tp;
18     struct thread *t;
19     mstime_t now = ms_timer();
20     struct thread_block *block;
21     mstime_t timeout;
22 
23     /* The current thread is obviously running, so no need to check... */
24     for (tp = curr->list.next; tp != &curr->list; tp = tp->next) {
25 	t = container_of(tp, struct thread, list);
26 	if ((block = t->blocked) && (timeout = block->timeout)) {
27 	    if ((mstimediff_t)(timeout - now) <= 0) {
28 		struct semaphore *sem = block->semaphore;
29 		/* Remove us from the queue and increase the count */
30 		block->list.next->prev = block->list.prev;
31 		block->list.prev->next = block->list.next;
32 		sem->count++;
33 
34 		t->blocked = NULL;
35 		block->timed_out = true;
36 
37 		__schedule();	/* Normally sets just __need_schedule */
38 	    }
39 	}
40     }
41 }
42