int sched_start(ScheduleArray *pScheduleArray, pthread_t *ptid, \ const int stack_size, bool * volatile pcontinue_flag) { int result; pthread_attr_t thread_attr; ScheduleContext *pContext; pContext = (ScheduleContext *)malloc(sizeof(ScheduleContext)); if (pContext == NULL) { result = errno != 0 ? errno : ENOMEM; logError("file: "__FILE__", line: %d, " \ "malloc %d bytes failed, " \ "errno: %d, error info: %s", \ __LINE__, (int)sizeof(ScheduleContext), \ result, STRERROR(result)); return result; } if ((result=init_pthread_attr(&thread_attr, stack_size)) != 0) { free(pContext); return result; } if ((result=sched_dup_array(pScheduleArray, \ &(pContext->scheduleArray))) != 0) { free(pContext); return result; } pContext->pcontinue_flag = pcontinue_flag; if ((result=pthread_create(ptid, &thread_attr, \ sched_thread_entrance, pContext)) != 0) { free(pContext); logError("file: "__FILE__", line: %d, " \ "create thread failed, " \ "errno: %d, error info: %s", \ __LINE__, result, STRERROR(result)); } pthread_attr_destroy(&thread_attr); return result; }
static int print_all_sched_entries(ScheduleArray *pScheduleArray) { ScheduleArray sortedByIdArray; ScheduleEntry *pEntry; ScheduleEntry *pEnd; char timebase[32]; int result; logInfo("schedule entry count: %d", pScheduleArray->count); if (pScheduleArray->count == 0) { return 0; } if ((result=sched_dup_array(pScheduleArray, &sortedByIdArray)) != 0) { return result; } qsort(sortedByIdArray.entries, sortedByIdArray.count, sizeof(ScheduleEntry), sched_cmp_by_id); pEnd = sortedByIdArray.entries + sortedByIdArray.count; for (pEntry=sortedByIdArray.entries; pEntry<pEnd; pEntry++) { if (pEntry->time_base.hour == TIME_NONE) { strcpy(timebase, "<startup>"); } else { sprintf(timebase, "%02d:%02d:%02d", pEntry->time_base.hour, pEntry->time_base.minute, pEntry->time_base.second); } logInfo("id: %u, time_base: %s, interval: %d, " "new_thread: %s, task_func: %p, args: %p", pEntry->id, timebase, pEntry->interval, pEntry->new_thread ? "true" : "false", pEntry->task_func, pEntry->func_args); } free(sortedByIdArray.entries); return 0; }
int sched_start_ex(ScheduleArray *pScheduleArray, pthread_t *ptid, const int stack_size, bool * volatile pcontinue_flag, ScheduleContext **ppContext) { int result; pthread_attr_t thread_attr; ScheduleContext *pContext; pContext = (ScheduleContext *)malloc(sizeof(ScheduleContext)); if (pContext == NULL) { result = errno != 0 ? errno : ENOMEM; logError("file: "__FILE__", line: %d, " \ "malloc %d bytes failed, " \ "errno: %d, error info: %s", \ __LINE__, (int)sizeof(ScheduleContext), \ result, STRERROR(result)); return result; } memset(pContext, 0, sizeof(ScheduleContext)); if ((result=init_pthread_attr(&thread_attr, stack_size)) != 0) { free(pContext); return result; } if ((result=sched_dup_array(pScheduleArray, \ &(pContext->scheduleArray))) != 0) { free(pContext); return result; } if (timer_slot_count > 0) { if ((result=fast_mblock_init(&pContext->mblock, sizeof(FastDelayTask), mblock_alloc_once)) != 0) { free(pContext); return result; } g_current_time = time(NULL); if ((result=fast_timer_init(&pContext->timer, timer_slot_count, g_current_time)) != 0) { free(pContext); return result; } if ((result=init_pthread_lock(&pContext->delay_queue.lock)) != 0) { free(pContext); return result; } pContext->timer_init = true; } pContext->pcontinue_flag = pcontinue_flag; if ((result=pthread_create(ptid, &thread_attr, \ sched_thread_entrance, pContext)) != 0) { free(pContext); logError("file: "__FILE__", line: %d, " \ "create thread failed, " \ "errno: %d, error info: %s", \ __LINE__, result, STRERROR(result)); } *ppContext = pContext; pthread_attr_destroy(&thread_attr); return result; }