static void *main_alarm_handler(void *ctx) { QALARM *q = (QALARM *)ctx; QUEUE_ITEM *item; pthread_t tid; QTHREAD *qt; while ((item = Get_Queue_Item(q->queue))) { if (!strcmp(item->action, "Terminate")) { /* Send termination signal */ for (qt = q->threads; qt != NULL; qt = qt->next) Add_Queue_Item(qt->queue, "Terminate", NULL, 0); /* Wait for termination */ for (qt = q->threads; qt != NULL; qt = qt->next) pthread_join(qt->tid, NULL); while (q->threads) delete_alarm(q, q->threads->tid); Free_Queue_Item(item); return NULL; } else if (!strcmp(item->action, "Wait")) { /* Wait for termination */ for (qt = q->threads; qt != NULL; qt = qt->next) pthread_join(qt->tid, NULL); while (q->threads) delete_alarm(q, q->threads->tid); Free_Queue_Item(item); return NULL; } else if (!strcmp(item->action, "Destroy")) { if (item->sz != sizeof(pthread_t)) { Free_Queue_Item(item); continue; } pthread_join(*((pthread_t *)(item->data)), NULL); delete_alarm(q, *((pthread_t *)(item->data))); } else { fprintf(stderr, "Unknown action: %s\n", item->action); } Free_Queue_Item(item); } return NULL; }
void Free_Queue(QUEUE *queue) { QUEUE_ITEM *item; while (queue->head) { item = Pop_Queue_Item(queue); fprintf(stderr, "(filename: %s)(paack_no: %d): (length: %u)\n", item->filename, item->pack_no, item->length); Free_Queue_Item(item); } }
static void *inner_alarm_handler(void *ctx) { QTHREAD *qt = (QTHREAD *)ctx; time_t starttime; time_t newtime; struct timeval t; QUEUE_ITEM *item; starttime = time(NULL); newtime = qt->timeout; while (time(NULL) - starttime < newtime) { t.tv_sec = newtime < POLLSEC ? newtime : POLLSEC; t.tv_usec = 0; select(0, NULL, NULL, NULL, &t); newtime = qt->timeout - (time(NULL) - starttime); if (!Queue_Empty(qt->queue)) { item = Get_Queue_Item(qt->queue); if (!strcmp(item->action, "Terminate")) { Free_Queue_Item(item); return NULL; } Free_Queue_Item(item); } } qt->cb(qt->data); if (qt->flags & QALARM_RECUR) add_alarm(qt->parent, qt->timeout, qt->cb, qt->data, qt->flags); Add_Queue_Item(qt->parent->queue, "Destroy", &(qt->tid), sizeof(pthread_t)); return NULL; }