diff -Nur asterisk-1.6.2.7-rc1-old/include/asterisk/heap.h asterisk-1.6.2.7-rc1-new/include/asterisk/heap.h --- asterisk-1.6.2.7-rc1-old/include/asterisk/heap.h 2010-04-20 00:00:00.000000000 +0800 +++ asterisk-1.6.2.7-rc1-new/include/asterisk/heap.h 2010-04-20 00:00:00.000000000 +0800 @@ -272,6 +272,8 @@ * heap and verifies that every node is properly placed relative to its children. * \since 1.6.1 */ -int ast_heap_verify(struct ast_heap *h); +unsigned int ast_heap_verify(struct ast_heap *h); + +void ast_heap_build(struct ast_heap *h); #endif /* __AST_HEAP_H__ */ diff -Nur asterisk-1.6.2.7-rc1-old/main/heap.c asterisk-1.6.2.7-rc1-new/main/heap.c --- asterisk-1.6.2.7-rc1-old/main/heap.c 2010-04-20 00:00:00.000000000 +0800 +++ asterisk-1.6.2.7-rc1-new/main/heap.c 2010-04-20 00:00:00.000000000 +0800 @@ -83,7 +83,7 @@ } } -int ast_heap_verify(struct ast_heap *h) +unsigned int ast_heap_verify(struct ast_heap *h) { unsigned int i; @@ -93,13 +93,13 @@ if (l <= h->cur_len) { if (h->cmp_fn(heap_get(h, i), heap_get(h, l)) <= 0) { - return -1; + return i; } } if (r <= h->cur_len) { if (h->cmp_fn(heap_get(h, i), heap_get(h, r)) <= 0) { - return -1; + return i; } } } @@ -229,6 +229,14 @@ } } +void ast_heap_build(struct ast_heap *h) +{ + unsigned int i; + + for (i = (h->cur_len / 2); i >= 1; i--) + max_heapify(h, i); +} + #ifdef MALLOC_DEBUG int _ast_heap_push(struct ast_heap *h, void *elm, const char *file, int lineno, const char *func) #else diff -Nur asterisk-1.6.2.7-rc1-old/main/sched.c asterisk-1.6.2.7-rc1-new/main/sched.c --- asterisk-1.6.2.7-rc1-old/main/sched.c 2010-04-20 00:00:00.000000000 +0800 +++ asterisk-1.6.2.7-rc1-new/main/sched.c 2010-04-20 00:00:00.000000000 +0800 @@ -581,7 +581,8 @@ int ast_sched_runq(struct sched_context *con) { struct sched *current; - struct timeval when; + struct timeval when = ast_tvnow(); + unsigned int i; int numevents; int res; @@ -589,6 +590,21 @@ ast_mutex_lock(&con->lock); + if ((i = ast_heap_verify(con->sched_heap)) != 0) { +/*#ifdef DEBUG_SCHEDULER*/ + struct timeval delta; + current = ast_heap_peek(con->sched_heap, i); + delta = ast_tvsub(current->when, when); + ast_debug(1, "ID %d (%p, %p, %ld : %ld) violates heap property!\n", + current->id, + current->callback, + current->data, + (long)delta.tv_sec, + (long int)delta.tv_usec); +/*#endif*/ + ast_heap_build(con->sched_heap); + } + for (numevents = 0; (current = ast_heap_peek(con->sched_heap, 1)); numevents++) { /* schedule all events which are going to expire within 1ms. * We only care about millisecond accuracy anyway, so this will