pthread_mutex.c revision 1.1.2.2 1 1.1.2.1 nathanw /* Copyright */
2 1.1.2.1 nathanw
3 1.1.2.1 nathanw #include <assert.h>
4 1.1.2.1 nathanw #include <errno.h>
5 1.1.2.1 nathanw #include <signal.h>
6 1.1.2.1 nathanw #include <stdlib.h>
7 1.1.2.1 nathanw #include <ucontext.h>
8 1.1.2.1 nathanw #include <sys/queue.h>
9 1.1.2.1 nathanw
10 1.1.2.1 nathanw #include "pthread.h"
11 1.1.2.1 nathanw #include "pthread_int.h"
12 1.1.2.1 nathanw
13 1.1.2.1 nathanw
14 1.1.2.1 nathanw int
15 1.1.2.1 nathanw pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
16 1.1.2.1 nathanw {
17 1.1.2.1 nathanw
18 1.1.2.1 nathanw assert(mutex != NULL);
19 1.1.2.1 nathanw
20 1.1.2.1 nathanw /* XXX No mutex attr support yet. */
21 1.1.2.1 nathanw if (attr != NULL)
22 1.1.2.1 nathanw return EINVAL;
23 1.1.2.1 nathanw
24 1.1.2.1 nathanw /* Allocate. */
25 1.1.2.1 nathanw
26 1.1.2.2 nathanw mutex->ptm_magic = PT_MUTEX_MAGIC;
27 1.1.2.2 nathanw mutex->ptm_owner = NULL;
28 1.1.2.2 nathanw pthread_lockinit(&mutex->ptm_lock);
29 1.1.2.2 nathanw pthread_lockinit(&mutex->ptm_interlock);
30 1.1.2.2 nathanw PTQ_INIT(&mutex->ptm_blocked);
31 1.1.2.1 nathanw
32 1.1.2.1 nathanw return 0;
33 1.1.2.1 nathanw }
34 1.1.2.1 nathanw
35 1.1.2.1 nathanw
36 1.1.2.1 nathanw int
37 1.1.2.1 nathanw pthread_mutex_destroy(pthread_mutex_t *mutex)
38 1.1.2.1 nathanw {
39 1.1.2.1 nathanw
40 1.1.2.2 nathanw assert(mutex != NULL);
41 1.1.2.2 nathanw assert(mutex->ptm_lock == __SIMPLELOCK_UNLOCKED);
42 1.1.2.1 nathanw
43 1.1.2.2 nathanw mutex->ptm_magic = PT_MUTEX_DEAD;
44 1.1.2.1 nathanw
45 1.1.2.1 nathanw return 0;
46 1.1.2.1 nathanw }
47 1.1.2.1 nathanw
48 1.1.2.1 nathanw
49 1.1.2.2 nathanw /*
50 1.1.2.2 nathanw * Note regarding memory visibility: Pthreads has rules about memory
51 1.1.2.2 nathanw * visibility and mutexes. Very roughly: Memory a thread can see when
52 1.1.2.2 nathanw * it unlocks a mutex can be seen by another thread that locks the
53 1.1.2.2 nathanw * same mutex.
54 1.1.2.2 nathanw *
55 1.1.2.2 nathanw * A memory barrier after a lock and before an unlock will provide
56 1.1.2.2 nathanw * this behavior. This code relies on __cpu_simple_lock_try() to issue
57 1.1.2.2 nathanw * a barrier after obtaining a lock, and on __cpu_simple_unlock() to
58 1.1.2.2 nathanw * issue a barrier before releasing a lock.
59 1.1.2.2 nathanw */
60 1.1.2.2 nathanw
61 1.1.2.1 nathanw int
62 1.1.2.1 nathanw pthread_mutex_lock(pthread_mutex_t *mutex)
63 1.1.2.1 nathanw {
64 1.1.2.1 nathanw pthread_t self;
65 1.1.2.2 nathanw #ifdef ERRORCHECK
66 1.1.2.2 nathanw if ((mutex == NULL) || (mutex->ptm_magic != PT_MUTEX_MAGIC))
67 1.1.2.1 nathanw return EINVAL;
68 1.1.2.2 nathanw #endif
69 1.1.2.1 nathanw
70 1.1.2.2 nathanw while (/*CONSTCOND*/1) {
71 1.1.2.2 nathanw if (__cpu_simple_lock_try(&mutex->ptm_lock))
72 1.1.2.2 nathanw break; /* got it! */
73 1.1.2.1 nathanw
74 1.1.2.2 nathanw self = pthread__self();
75 1.1.2.2 nathanw /* Okay, didn't look free. Get the interlock... */
76 1.1.2.2 nathanw pthread_spinlock(self, &mutex->ptm_interlock);
77 1.1.2.2 nathanw /* The mutex_unlock routine will get the interlock
78 1.1.2.2 nathanw * before looking at the list of sleepers, so if the
79 1.1.2.2 nathanw * lock is held we can safely put ourselves on the
80 1.1.2.2 nathanw * sleep queue. If it's not held, we can try taking it
81 1.1.2.2 nathanw * again.
82 1.1.2.2 nathanw */
83 1.1.2.2 nathanw if (mutex->ptm_lock == __SIMPLELOCK_LOCKED) {
84 1.1.2.2 nathanw PTQ_INSERT_TAIL(&mutex->ptm_blocked, self, pt_sleep);
85 1.1.2.2 nathanw self->pt_state = PT_STATE_BLOCKED;
86 1.1.2.2 nathanw pthread__block(self, &mutex->ptm_interlock);
87 1.1.2.2 nathanw /* interlock is not held when we return */
88 1.1.2.2 nathanw } else {
89 1.1.2.2 nathanw pthread_spinunlock(self, &mutex->ptm_interlock);
90 1.1.2.2 nathanw }
91 1.1.2.2 nathanw /* Go around for another try. */
92 1.1.2.1 nathanw }
93 1.1.2.1 nathanw
94 1.1.2.2 nathanw /* We have the lock! */
95 1.1.2.2 nathanw #ifdef ERRORCHECK
96 1.1.2.2 nathanw mutex->ptm_owner = self;
97 1.1.2.2 nathanw #endif
98 1.1.2.1 nathanw return 0;
99 1.1.2.1 nathanw }
100 1.1.2.1 nathanw
101 1.1.2.1 nathanw
102 1.1.2.1 nathanw int
103 1.1.2.1 nathanw pthread_mutex_trylock(pthread_mutex_t *mutex)
104 1.1.2.1 nathanw {
105 1.1.2.1 nathanw
106 1.1.2.2 nathanw #ifdef ERRORCHECK
107 1.1.2.2 nathanw if ((mutex == NULL) || (mutex->ptm_magic != PT_MUTEX_MAGIC))
108 1.1.2.1 nathanw return EINVAL;
109 1.1.2.2 nathanw #endif
110 1.1.2.1 nathanw
111 1.1.2.2 nathanw if (__cpu_simple_lock_try(&mutex->ptm_lock) == 0)
112 1.1.2.1 nathanw return EBUSY;
113 1.1.2.1 nathanw
114 1.1.2.2 nathanw #ifdef ERRORCHECK
115 1.1.2.2 nathanw mutex->ptm_owner = pthread__self();
116 1.1.2.2 nathanw #endif
117 1.1.2.1 nathanw return 0;
118 1.1.2.1 nathanw }
119 1.1.2.1 nathanw
120 1.1.2.1 nathanw
121 1.1.2.1 nathanw int
122 1.1.2.1 nathanw pthread_mutex_unlock(pthread_mutex_t *mutex)
123 1.1.2.1 nathanw {
124 1.1.2.1 nathanw pthread_t self, blocked;
125 1.1.2.2 nathanw struct pt_queue_t blockedq, nullq = PTQ_HEAD_INITIALIZER;
126 1.1.2.1 nathanw
127 1.1.2.2 nathanw #ifdef ERRORCHECK
128 1.1.2.2 nathanw if ((mutex == NULL) || (mutex->ptm_magic != PT_MUTEX_MAGIC))
129 1.1.2.1 nathanw return EINVAL;
130 1.1.2.1 nathanw
131 1.1.2.2 nathanw if (mutex->ptm_lock != __SIMPLELOCK_LOCKED)
132 1.1.2.1 nathanw return EPERM; /* Not exactly the right error. */
133 1.1.2.1 nathanw
134 1.1.2.1 nathanw /* One is only permitted to unlock one's own mutexes. */
135 1.1.2.2 nathanw if (mutex->ptm_owner != self)
136 1.1.2.1 nathanw return EPERM;
137 1.1.2.2 nathanw #endif
138 1.1.2.1 nathanw
139 1.1.2.2 nathanw self = pthread__self();
140 1.1.2.2 nathanw pthread_spinlock(self, &mutex->ptm_interlock);
141 1.1.2.2 nathanw blockedq = mutex->ptm_blocked;
142 1.1.2.2 nathanw mutex->ptm_blocked = nullq;
143 1.1.2.2 nathanw #ifdef ERRORCHECK
144 1.1.2.2 nathanw mutex->ptm_owner = NULL;
145 1.1.2.2 nathanw #endif
146 1.1.2.2 nathanw __cpu_simple_unlock(&mutex->ptm_lock);
147 1.1.2.2 nathanw pthread_spinunlock(self, &mutex->ptm_interlock);
148 1.1.2.1 nathanw
149 1.1.2.2 nathanw /* Give everyone on the sleep queue another chance at the lock. */
150 1.1.2.2 nathanw PTQ_FOREACH(blocked, &blockedq, pt_sleep)
151 1.1.2.1 nathanw pthread__sched(self, blocked);
152 1.1.2.1 nathanw
153 1.1.2.1 nathanw return 0;
154 1.1.2.1 nathanw }
155