Skip to content

Commit 4226c6d

Browse files
Ramakrishna PallalaAnas Nashif
authored andcommitted
lib: posix: Fix mutex locking in pthread_cancel
Fix mutex locking sequence in pthread_cancel() Coverity-CID: 183055 Signed-off-by: Ramakrishna Pallala <[email protected]>
1 parent 46931c9 commit 4226c6d

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

lib/posix/pthread.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,30 +213,29 @@ int pthread_setcancelstate(int state, int *oldstate)
213213
int pthread_cancel(pthread_t pthread)
214214
{
215215
struct posix_thread *thread = (struct posix_thread *) pthread;
216+
int cancel_state;
216217

217218
if (thread == NULL || thread->state == PTHREAD_TERMINATED) {
218219
return ESRCH;
219220
}
220221

221222
pthread_mutex_lock(&thread->cancel_lock);
222223
thread->cancel_pending = 1;
224+
cancel_state = thread->cancel_state;
225+
pthread_mutex_unlock(&thread->cancel_lock);
223226

224-
if (thread->cancel_state == PTHREAD_CANCEL_ENABLE) {
227+
if (cancel_state == PTHREAD_CANCEL_ENABLE) {
228+
pthread_mutex_lock(&thread->state_lock);
225229
if (thread->state == PTHREAD_DETACHED) {
226230
thread->state = PTHREAD_TERMINATED;
227-
pthread_mutex_unlock(&thread->cancel_lock);
228231
} else {
229232
thread->retval = PTHREAD_CANCELED;
230-
pthread_mutex_unlock(&thread->cancel_lock);
231-
pthread_mutex_lock(&thread->state_lock);
232233
thread->state = PTHREAD_EXITED;
233234
pthread_cond_broadcast(&thread->state_cond);
234-
pthread_mutex_unlock(&thread->state_lock);
235235
}
236+
pthread_mutex_unlock(&thread->state_lock);
236237

237238
k_thread_abort((k_tid_t) thread);
238-
} else {
239-
pthread_mutex_unlock(&thread->cancel_lock);
240239
}
241240

242241
return 0;

0 commit comments

Comments
 (0)