-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathcond.c
56 lines (43 loc) · 1.18 KB
/
cond.c
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
#include <sys/types.h>
#include <kernel/mutex.h>
#include <kernel/sched.h>
#include <kernel/thread.h>
#include "linux/list.h"
static LIST_HEAD(cond_head);
static struct thread_info *find_other_thread(pthread_cond_t *cond)
{
struct thread_info *other;
list_for_each_entry(other, &cond_head, ti_q)
{
if (other->ti_private == cond)
return other;
}
return NULL;
}
int sys_pthread_cond_wait(pthread_cond_t *cond, kernel_mutex_t *mutex)
{
CURRENT_THREAD_INFO(curr_thread);
curr_thread->ti_private = cond;
curr_thread->ti_state = THREAD_STATE_BLOCKED;
sched_dequeue(curr_thread);
list_add_tail(&curr_thread->ti_q, &cond_head);
sys_pthread_mutex_unlock(mutex);
/* contend for the lock */
sys_pthread_mutex_lock(mutex);
return 0;
}
int sys_pthread_cond_signal(pthread_cond_t *cond)
{
struct thread_info *other;
other = find_other_thread(cond);
if (!other)
return 0;
list_del(&other->ti_q);
sched_enqueue(other);
CURRENT_THREAD_INFO(curr_thread);
if (other->ti_priority >= curr_thread->ti_priority) {
sched_enqueue(curr_thread);
sched_elect(SCHED_OPT_NONE);
}
return 0;
}