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; }
int list_file(char *path, int lastMaxTime){ char *infile[50]; int newMaxTime = lastMaxTime; int length = 0; struct dirent *ptr; DIR *dir; dir=opendir(path); if(dir == NULL){ printf("[Cook] open dir error"); return lastMaxTime; } while((ptr=readdir(dir))!=NULL) { //跳过'.'和'..'两个目录 if(ptr->d_name[0] == '.' || ptr->d_name[9] == 'l') continue; int cmpRst = file_compare2(path, ptr->d_name, lastMaxTime); if(cmpRst > 0){ infile[length] = (char*)calloc(strlen(ptr->d_name) + 1, sizeof(char)); sprintf(infile[length], "%s", ptr->d_name); printf("[Cook] adding file %s\n",infile[length]); length++; if(newMaxTime < cmpRst) newMaxTime = cmpRst; printf("[Cook] change newMaxTime to %d\n", newMaxTime); } } quicksort(path, infile, length); int j = 0; //TODO add to global queue here printf("[Cook] provides %d new files\n", length); for(; j < length - 1; j++){ Add_Queue_Item(queue, "", infile[j], strlen(infile[j]) + 1); free(infile[j]); } free(infile[length - 1]); closedir(dir); return newMaxTime; }
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; }
void wait_alarms(QALARM *q) { Add_Queue_Item(q->queue, "Wait", NULL, 0); pthread_join(q->tid, NULL); }
void terminate_alarms(QALARM *q) { Add_Queue_Item(q->queue, "Terminate", NULL, 0); pthread_join(q->tid, NULL); }