- int tmp;
- pthread_testcancel();
- while ((tmp = t->tid)) __timedwait_cp(&t->tid, tmp, 0, 0, 0);
+ int state, cs, r = 0;
+ __pthread_testcancel();
+ __pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
+ if (cs == PTHREAD_CANCEL_ENABLE) __pthread_setcancelstate(cs, 0);
+ while ((state = t->detach_state) && r != ETIMEDOUT && r != EINVAL) {
+ if (state >= DT_DETACHED) a_crash();
+ r = __timedwait_cp(&t->detach_state, state, CLOCK_REALTIME, at, 1);
+ }
+ __pthread_setcancelstate(cs, 0);
+ if (r == ETIMEDOUT || r == EINVAL) return r;
+ __tl_sync(t);