+static void thraise(struct blocker *bl, int n)
+{
+ int p;
+
+ while(n > 0) {
+ p = (n - 1) >> 1;
+ if(timeheap.b[p]->to <= bl->to)
+ break;
+ timeheap.b[n] = timeheap.b[p];
+ timeheap.b[n]->thpos = n;
+ n = p;
+ }
+ timeheap.b[n] = bl;
+ bl->thpos = n;
+}
+
+static void thlower(struct blocker *bl, int n)
+{
+ int c;
+
+ while(1) {
+ c = (n << 1) + 1;
+ if(c >= timeheap.d)
+ break;
+ if((c + 1 < timeheap.d) && (timeheap.b[c + 1]->to < timeheap.b[c]->to))
+ c = c + 1;
+ if(timeheap.b[c]->to > bl->to)
+ break;
+ timeheap.b[n] = timeheap.b[c];
+ timeheap.b[n]->thpos = n;
+ n = c;
+ }
+ timeheap.b[n] = bl;
+ bl->thpos = n;
+}
+
+static void addtimeout(struct blocker *bl, time_t to)
+{
+ sizebuf(timeheap, ++timeheap.d);
+ thraise(bl, timeheap.d - 1);
+}
+
+static void deltimeout(struct blocker *bl)
+{
+ int n;
+
+ if(bl->thpos == timeheap.d - 1) {
+ timeheap.d--;
+ return;
+ }
+ n = bl->thpos;
+ bl = timeheap.b[--timeheap.d];
+ if((n > 0) && (timeheap.b[(n - 1) >> 1]->to > bl->to))
+ thraise(bl, n);
+ else
+ thlower(bl, n);
+}
+