@@ -331,9 +331,11 @@ kevent_copyin(struct kqueue *kq, const struct kevent changelist[], int nchanges,
331331
332332#ifndef _WIN32
333333static void
334- kevent_release_kq_mutex (void * kq )
334+ kevent_release_kq_mutex (void * arg )
335335{
336- kqueue_unlock ((struct kqueue * )kq );
336+ struct kqueue * kq = arg ;
337+ dbg_printf ("Unlocking kq=%p due to cancellation" , kq );
338+ kqueue_unlock (kq );
337339}
338340#endif
339341
@@ -367,7 +369,7 @@ kevent(int kqfd,
367369 if (!changelist ) changelist = null_kev ;
368370
369371#ifndef _WIN32
370- prev_cancel_state = pthread_setcancelstate (PTHREAD_CANCEL_DISABLE , NULL );
372+ pthread_setcancelstate (PTHREAD_CANCEL_DISABLE , & prev_cancel_state );
371373#endif
372374 /*
373375 * Grab the global mutex. This prevents
@@ -439,8 +441,10 @@ kevent(int kqfd,
439441 */
440442#ifndef _WIN32
441443 (void )pthread_setcancelstate (prev_cancel_state , NULL );
442- if (prev_cancel_state == PTHREAD_CANCEL_ENABLE )
444+ if (prev_cancel_state == PTHREAD_CANCEL_ENABLE ) {
445+ dbg_printf ("Checking for deferred cancellations" );
443446 pthread_testcancel ();
447+ }
444448#endif
445449 rv = kqops .kevent_wait (kq , nevents , timeout );
446450#ifndef _WIN32
@@ -482,16 +486,23 @@ kevent(int kqfd,
482486
483487out :
484488#ifndef _WIN32
485- pthread_cleanup_pop (0 );
489+ /*
490+ * Test for cancellations first, so we don't
491+ * double unlock the kqueue.
492+ */
493+ pthread_setcancelstate (prev_cancel_state , NULL );
494+ if (prev_cancel_state == PTHREAD_CANCEL_ENABLE ) {
495+ dbg_printf ("Checking for deferred cancellations" );
496+ pthread_testcancel ();
497+ }
486498#endif
487- kqueue_unlock (kq );
488- dbg_printf ("--- END kevent %u ret %d ---" , myid , rv );
489499
490500#ifndef _WIN32
491- pthread_setcancelstate (prev_cancel_state , NULL );
492- if (prev_cancel_state == PTHREAD_CANCEL_ENABLE )
493- pthread_testcancel ();
501+ pthread_cleanup_pop (0 );
494502#endif
495503
504+ kqueue_unlock (kq );
505+ dbg_printf ("--- END kevent %u ret %d ---" , myid , rv );
506+
496507 return (rv );
497508}
0 commit comments