diff options
-rw-r--r-- | ot_mutex.c | 154 |
1 files changed, 57 insertions, 97 deletions
@@ -72,24 +72,9 @@ static pthread_cond_t tasklist_being_filled; | |||
72 | int mutex_workqueue_pushtask( int64 sock, ot_tasktype tasktype ) { | 72 | int mutex_workqueue_pushtask( int64 sock, ot_tasktype tasktype ) { |
73 | struct ot_task ** tmptask, * task; | 73 | struct ot_task ** tmptask, * task; |
74 | 74 | ||
75 | /* Want exclusive access to tasklist */ | ||
76 | MTX_DBG( "pushtask locks.\n" ); | ||
77 | pthread_mutex_lock( &tasklist_mutex ); | ||
78 | MTX_DBG( "pushtask locked.\n" ); | ||
79 | |||
80 | task = malloc(sizeof( struct ot_task)); | 75 | task = malloc(sizeof( struct ot_task)); |
81 | if( !task ) { | 76 | if( !task ) |
82 | MTX_DBG( "pushtask fail unlocks.\n" ); | ||
83 | pthread_mutex_unlock( &tasklist_mutex ); | ||
84 | MTX_DBG( "pushtask fail unlocked.\n" ); | ||
85 | return -1; | 77 | return -1; |
86 | } | ||
87 | |||
88 | /* Skip to end of list */ | ||
89 | tmptask = &tasklist; | ||
90 | while( *tmptask ) | ||
91 | tmptask = &(*tmptask)->next; | ||
92 | *tmptask = task; | ||
93 | 78 | ||
94 | task->taskid = 0; | 79 | task->taskid = 0; |
95 | task->tasktype = tasktype; | 80 | task->tasktype = tasktype; |
@@ -98,12 +83,18 @@ int mutex_workqueue_pushtask( int64 sock, ot_tasktype tasktype ) { | |||
98 | task->iovec = NULL; | 83 | task->iovec = NULL; |
99 | task->next = 0; | 84 | task->next = 0; |
100 | 85 | ||
86 | /* Want exclusive access to tasklist */ | ||
87 | pthread_mutex_lock( &tasklist_mutex ); | ||
88 | |||
89 | /* Skip to end of list */ | ||
90 | tmptask = &tasklist; | ||
91 | while( *tmptask ) | ||
92 | tmptask = &(*tmptask)->next; | ||
93 | *tmptask = task; | ||
94 | |||
101 | /* Inform waiting workers and release lock */ | 95 | /* Inform waiting workers and release lock */ |
102 | MTX_DBG( "pushtask broadcasts.\n" ); | ||
103 | pthread_cond_broadcast( &tasklist_being_filled ); | 96 | pthread_cond_broadcast( &tasklist_being_filled ); |
104 | MTX_DBG( "pushtask broadcasted, mutex unlocks.\n" ); | ||
105 | pthread_mutex_unlock( &tasklist_mutex ); | 97 | pthread_mutex_unlock( &tasklist_mutex ); |
106 | MTX_DBG( "pushtask end mutex unlocked.\n" ); | ||
107 | return 0; | 98 | return 0; |
108 | } | 99 | } |
109 | 100 | ||
@@ -111,31 +102,25 @@ void mutex_workqueue_canceltask( int64 sock ) { | |||
111 | struct ot_task ** task; | 102 | struct ot_task ** task; |
112 | 103 | ||
113 | /* Want exclusive access to tasklist */ | 104 | /* Want exclusive access to tasklist */ |
114 | MTX_DBG( "canceltask locks.\n" ); | ||
115 | pthread_mutex_lock( &tasklist_mutex ); | 105 | pthread_mutex_lock( &tasklist_mutex ); |
116 | MTX_DBG( "canceltask locked.\n" ); | ||
117 | |||
118 | task = &tasklist; | ||
119 | while( *task && ( (*task)->sock != sock ) ) | ||
120 | *task = (*task)->next; | ||
121 | 106 | ||
122 | if( *task && ( (*task)->sock == sock ) ) { | 107 | for (task = &tasklist; *task; task = &((*task)->next)) |
123 | struct iovec *iovec = (*task)->iovec; | 108 | if ((*task)->sock == sock) { |
124 | struct ot_task *ptask = *task; | 109 | struct iovec *iovec = (*task)->iovec; |
125 | int i; | 110 | struct ot_task *ptask = *task; |
111 | int i; | ||
126 | 112 | ||
127 | /* Free task's iovec */ | 113 | /* Free task's iovec */ |
128 | for( i=0; i<(*task)->iovec_entries; ++i ) | 114 | for( i=0; i<(*task)->iovec_entries; ++i ) |
129 | free( iovec[i].iov_base ); | 115 | free( iovec[i].iov_base ); |
130 | 116 | ||
131 | *task = (*task)->next; | 117 | *task = (*task)->next; |
132 | free( ptask ); | 118 | free( ptask ); |
133 | } | 119 | break; |
120 | } | ||
134 | 121 | ||
135 | /* Release lock */ | 122 | /* Release lock */ |
136 | MTX_DBG( "canceltask unlocks.\n" ); | ||
137 | pthread_mutex_unlock( &tasklist_mutex ); | 123 | pthread_mutex_unlock( &tasklist_mutex ); |
138 | MTX_DBG( "canceltask unlocked.\n" ); | ||
139 | } | 124 | } |
140 | 125 | ||
141 | ot_taskid mutex_workqueue_poptask( ot_tasktype *tasktype ) { | 126 | ot_taskid mutex_workqueue_poptask( ot_tasktype *tasktype ) { |
@@ -143,33 +128,26 @@ ot_taskid mutex_workqueue_poptask( ot_tasktype *tasktype ) { | |||
143 | ot_taskid taskid = 0; | 128 | ot_taskid taskid = 0; |
144 | 129 | ||
145 | /* Want exclusive access to tasklist */ | 130 | /* Want exclusive access to tasklist */ |
146 | MTX_DBG( "poptask mutex locks.\n" ); | ||
147 | pthread_mutex_lock( &tasklist_mutex ); | 131 | pthread_mutex_lock( &tasklist_mutex ); |
148 | MTX_DBG( "poptask mutex locked.\n" ); | ||
149 | 132 | ||
150 | while( !taskid ) { | 133 | while( !taskid ) { |
151 | /* Skip to the first unassigned task this worker wants to do */ | 134 | /* Skip to the first unassigned task this worker wants to do */ |
152 | task = tasklist; | 135 | for (task = tasklist; task; task = task->next) |
153 | while( task && ( ( ( TASK_CLASS_MASK & task->tasktype ) != *tasktype ) || task->taskid ) ) | 136 | if (!task->taskid && ( TASK_CLASS_MASK & task->tasktype ) == *tasktype) { |
154 | task = task->next; | 137 | /* If we found an outstanding task, assign a taskid to it |
155 | 138 | and leave the loop */ | |
156 | /* If we found an outstanding task, assign a taskid to it | 139 | task->taskid = taskid = ++next_free_taskid; |
157 | and leave the loop */ | 140 | *tasktype = task->tasktype; |
158 | if( task ) { | 141 | break; |
159 | task->taskid = taskid = ++next_free_taskid; | 142 | } |
160 | *tasktype = task->tasktype; | 143 | |
161 | } else { | 144 | /* Wait until the next task is being fed */ |
162 | /* Wait until the next task is being fed */ | 145 | if (!taskid) |
163 | MTX_DBG( "poptask cond waits.\n" ); | ||
164 | pthread_cond_wait( &tasklist_being_filled, &tasklist_mutex ); | 146 | pthread_cond_wait( &tasklist_being_filled, &tasklist_mutex ); |
165 | MTX_DBG( "poptask cond waited.\n" ); | ||
166 | } | ||
167 | } | 147 | } |
168 | 148 | ||
169 | /* Release lock */ | 149 | /* Release lock */ |
170 | MTX_DBG( "poptask end mutex unlocks.\n" ); | ||
171 | pthread_mutex_unlock( &tasklist_mutex ); | 150 | pthread_mutex_unlock( &tasklist_mutex ); |
172 | MTX_DBG( "poptask end mutex unlocked.\n" ); | ||
173 | 151 | ||
174 | return taskid; | 152 | return taskid; |
175 | } | 153 | } |
@@ -178,24 +156,18 @@ void mutex_workqueue_pushsuccess( ot_taskid taskid ) { | |||
178 | struct ot_task ** task; | 156 | struct ot_task ** task; |
179 | 157 | ||
180 | /* Want exclusive access to tasklist */ | 158 | /* Want exclusive access to tasklist */ |
181 | MTX_DBG( "pushsuccess locks.\n" ); | ||
182 | pthread_mutex_lock( &tasklist_mutex ); | 159 | pthread_mutex_lock( &tasklist_mutex ); |
183 | MTX_DBG( "pushsuccess locked.\n" ); | ||
184 | |||
185 | task = &tasklist; | ||
186 | while( *task && ( (*task)->taskid != taskid ) ) | ||
187 | *task = (*task)->next; | ||
188 | 160 | ||
189 | if( *task && ( (*task)->taskid == taskid ) ) { | 161 | for (task = &tasklist; *task; task = &((*task)->next)) |
190 | struct ot_task *ptask = *task; | 162 | if ((*task)->taskid == taskid) { |
191 | *task = (*task)->next; | 163 | struct ot_task *ptask = *task; |
192 | free( ptask ); | 164 | *task = (*task)->next; |
193 | } | 165 | free( ptask ); |
166 | break; | ||
167 | } | ||
194 | 168 | ||
195 | /* Release lock */ | 169 | /* Release lock */ |
196 | MTX_DBG( "pushsuccess unlocks.\n" ); | ||
197 | pthread_mutex_unlock( &tasklist_mutex ); | 170 | pthread_mutex_unlock( &tasklist_mutex ); |
198 | MTX_DBG( "pushsuccess unlocked.\n" ); | ||
199 | } | 171 | } |
200 | 172 | ||
201 | int mutex_workqueue_pushresult( ot_taskid taskid, int iovec_entries, struct iovec *iovec ) { | 173 | int mutex_workqueue_pushresult( ot_taskid taskid, int iovec_entries, struct iovec *iovec ) { |
@@ -203,24 +175,18 @@ int mutex_workqueue_pushresult( ot_taskid taskid, int iovec_entries, struct iove | |||
203 | const char byte = 'o'; | 175 | const char byte = 'o'; |
204 | 176 | ||
205 | /* Want exclusive access to tasklist */ | 177 | /* Want exclusive access to tasklist */ |
206 | MTX_DBG( "pushresult locks.\n" ); | ||
207 | pthread_mutex_lock( &tasklist_mutex ); | 178 | pthread_mutex_lock( &tasklist_mutex ); |
208 | MTX_DBG( "pushresult locked.\n" ); | ||
209 | 179 | ||
210 | task = tasklist; | 180 | for (task = tasklist; task; task = task->next) |
211 | while( task && ( task->taskid != taskid ) ) | 181 | if (task->taskid == taskid) { |
212 | task = task->next; | 182 | task->iovec_entries = iovec_entries; |
213 | 183 | task->iovec = iovec; | |
214 | if( task ) { | 184 | task->tasktype = TASK_DONE; |
215 | task->iovec_entries = iovec_entries; | 185 | break; |
216 | task->iovec = iovec; | 186 | } |
217 | task->tasktype = TASK_DONE; | ||
218 | } | ||
219 | 187 | ||
220 | /* Release lock */ | 188 | /* Release lock */ |
221 | MTX_DBG( "pushresult unlocks.\n" ); | ||
222 | pthread_mutex_unlock( &tasklist_mutex ); | 189 | pthread_mutex_unlock( &tasklist_mutex ); |
223 | MTX_DBG( "pushresult unlocked.\n" ); | ||
224 | 190 | ||
225 | io_trywrite( g_self_pipe[1], &byte, 1 ); | 191 | io_trywrite( g_self_pipe[1], &byte, 1 ); |
226 | 192 | ||
@@ -233,29 +199,23 @@ int64 mutex_workqueue_popresult( int *iovec_entries, struct iovec ** iovec ) { | |||
233 | int64 sock = -1; | 199 | int64 sock = -1; |
234 | 200 | ||
235 | /* Want exclusive access to tasklist */ | 201 | /* Want exclusive access to tasklist */ |
236 | MTX_DBG( "popresult locks.\n" ); | ||
237 | pthread_mutex_lock( &tasklist_mutex ); | 202 | pthread_mutex_lock( &tasklist_mutex ); |
238 | MTX_DBG( "popresult locked.\n" ); | ||
239 | |||
240 | task = &tasklist; | ||
241 | while( *task && ( (*task)->tasktype != TASK_DONE ) ) | ||
242 | task = &(*task)->next; | ||
243 | 203 | ||
244 | if( *task && ( (*task)->tasktype == TASK_DONE ) ) { | 204 | for (task = &tasklist; *task; task = &((*task)->next)) |
245 | struct ot_task *ptask = *task; | 205 | if ((*task)->tasktype == TASK_DONE) { |
206 | struct ot_task *ptask = *task; | ||
246 | 207 | ||
247 | *iovec_entries = (*task)->iovec_entries; | 208 | *iovec_entries = (*task)->iovec_entries; |
248 | *iovec = (*task)->iovec; | 209 | *iovec = (*task)->iovec; |
249 | sock = (*task)->sock; | 210 | sock = (*task)->sock; |
250 | 211 | ||
251 | *task = (*task)->next; | 212 | *task = (*task)->next; |
252 | free( ptask ); | 213 | free( ptask ); |
253 | } | 214 | break; |
215 | } | ||
254 | 216 | ||
255 | /* Release lock */ | 217 | /* Release lock */ |
256 | MTX_DBG( "popresult unlocks.\n" ); | ||
257 | pthread_mutex_unlock( &tasklist_mutex ); | 218 | pthread_mutex_unlock( &tasklist_mutex ); |
258 | MTX_DBG( "popresult unlocked.\n" ); | ||
259 | return sock; | 219 | return sock; |
260 | } | 220 | } |
261 | 221 | ||