+ MutexLock l(mutex);
+ while(1)
+ {
+ TimeStamp stamp;
+ TimeStamp t = now();
+ if(!slots.empty())
+ {
+ next = slots.begin()->slot;
+ stamp = next->get_timeout();
+ if(stamp<=t)
+ break;
+ }
+
+ if(timeout && (!deadline || t<deadline))
+ {
+ SetFlag setf(blocking);
+ mutex.unlock();
+ if(stamp && (!deadline || stamp<deadline))
+ sem.wait(stamp-t);
+ else if(deadline)
+ sem.wait(deadline-t);
+ else
+ sem.wait();
+ mutex.lock();
+ // The slots may have changed while waiting so check again
+ continue;
+ }
+ else
+ return;
+ }
+
+ pop_heap(slots.begin(), slots.end());
+ slots.pop_back();