+ if ((m->_m_type&15) && (m->_m_lock&INT_MAX) != __pthread_self()->tid)
+ return EPERM;
+
+ if (ts && ts->tv_nsec >= 1000000000UL)
+ return EINVAL;
+
+ __pthread_testcancel();
+
+ if (c->_c_shared) {
+ shared = 1;
+ fut = &c->_c_seq;
+ seq = c->_c_seq;
+ a_inc(&c->_c_waiters);
+ } else {
+ lock(&c->_c_lock);
+
+ seq = node.barrier = 2;
+ fut = &node.barrier;
+ node.state = WAITING;
+ node.next = c->_c_head;
+ c->_c_head = &node;
+ if (!c->_c_tail) c->_c_tail = &node;
+ else node.next->prev = &node;
+
+ unlock(&c->_c_lock);
+ }
+
+ __pthread_mutex_unlock(m);
+
+ __pthread_setcancelstate(PTHREAD_CANCEL_MASKED, &cs);
+ if (cs == PTHREAD_CANCEL_DISABLE) __pthread_setcancelstate(cs, 0);
+
+ do e = __timedwait_cp(fut, seq, clock, ts, !shared);
+ while (*fut==seq && (!e || e==EINTR));
+ if (e == EINTR) e = 0;
+
+ if (shared) {
+ /* Suppress cancellation if a signal was potentially
+ * consumed; this is a legitimate form of spurious
+ * wake even if not. */
+ if (e == ECANCELED && c->_c_seq != seq) e = 0;