int main() { stpool_t *pool; struct schattr attr[] = { {0, 90, ep_SCHE_TOP}, {0, 40, ep_SCHE_BACK}, {0, 10, ep_SCHE_BACK}, {0, 0, ep_SCHE_BACK}, }; long eCAPs = eCAP_F_THROTTLE|eCAP_F_PRIORITY|eCAP_F_WAIT_ALL|eCAP_F_GROUP; /** * Creat a pool with 1 servering threads */ pool = stpool_create("mypool", eCAPs, 1, 0, 1, 1); /** * Add a few tasks into the pool */ stpool_add_routine(pool, "zero_task", task_run, task_err_handler, NULL, &attr[3]); stpool_add_routine(pool, "zero_task", task_run, task_err_handler, NULL, &attr[3]); stpool_add_routine(pool, "zero_task", task_run, task_err_handler, NULL, &attr[3]); stpool_add_routine(pool, "zero_task", task_run, task_err_handler, NULL, &attr[3]); stpool_add_routine(pool, "low_task", task_run, task_err_handler, NULL, &attr[2]); #if 0 stpool_add_routine(pool, "middle_task", task_run, task_err_handler, NULL, &attr[1]); stpool_add_routine(pool, "hight_task", task_run, task_err_handler, NULL, &attr[0]); #endif getchar(); stpool_remove_all(pool, 1); /** * Wait for all tasks' completions */ stpool_wait_all(pool, -1); puts("All tasks has been removed completely.\n"); getchar(); puts(stpool_stat_print(pool)); /** * Release the pool */ stpool_release(pool); printf("print any key to exit.\n"); getchar(); return 0; }
int main() { long eCAPs; int idx, exe_func_timeout; int gid_slow, gid_fast1, gid_fast2; struct gscheduler_attr attr; stpool_t *pool; /*-----------------------------------------------------------------------*/ /*--------------------Control the ouput of the log messages--------------*/ /*-----------------------------------------------------------------------*/ const char *mentry[] = {"Scheduler", "thread", NULL}; /** * Note: * If our progam is linked with the DEBUG library, the console may always * be filled up with the log messages from modules (Scheduler, thread), so we * filt some messages to make it easier for users to watch the running behaviors * of the libstpool. */ MSG_log_set_level(LOG_DEBUG); MSG_log_mfilter_add_entry(mentry, NULL); MSG_log_mfilter_set_type(eFT_discard); /*----------------------------------------------------------------------*/ eCAPs = eCAP_F_GROUP|eCAP_F_ROUTINE|eCAP_F_GROUP_SUSPEND|eCAP_F_DISABLEQ|eCAP_F_WAIT_ALL; /** * Create a share pool */ pool = stpool_create("share_pool", eCAPs, /** Capabities */ 10, /** max working threads number */ 0, /** min working threads number */ 0, /** Suspended */ 1 /** priority queue number */ ); /** * Create a time-consuming group and set its absolute paralle tasks number to 2 */ attr.limit_paralle_tasks = 2; attr.receive_benifits = 0; gid_slow = stpool_group_create(pool, "g_slow", &attr, 1, 1); /** * Add 100 tasks into the group */ for (idx=0; idx<10; idx++) { exe_func_timeout = 1000 * (idx % 5 + 1); stpool_group_add_routine(pool, gid_slow, "slow_task", time_consuming_task_run, NULL, (void *)exe_func_timeout, NULL); } puts( stpool_scheduler_map_dump(pool) ); puts("\nprint any key to schedule the slow task ...\n"); getchar(); /** * Resume the time-consuming group */ stpool_group_resume(pool, gid_slow); puts("print q to go next test ...\n"); while ('q' != getchar()) puts( stpool_scheduler_map_dump(pool) ); /** * Create two other fast groups */ gid_fast1 = stpool_group_create(pool, "g_fast1", NULL, 1, 1); gid_fast2 = stpool_group_create(pool, "g_fast2", NULL, 1, 1); /** * Add a few tasks into the fast groups */ for (idx=0; idx<10; idx++) { stpool_group_add_routine(pool, gid_fast1, "fast1_task", fast_task_run1, NULL, NULL, NULL); stpool_group_add_routine(pool, gid_fast2, "fast2_task", fast_task_run2, NULL, NULL, NULL); } /** * print the scheduler map */ puts( stpool_scheduler_map_dump(pool) ); /** * resume the fast groups */ getchar(); puts("print any key to resume the fast groups ...\n"); stpool_group_resume_all(pool); puts("print q to go next test ...\n"); while ('q' != getchar()) puts( stpool_scheduler_map_dump(pool) ); /** * remove all tasks */ getchar(); puts("print any key to remove all tasks ...\n"); stpool_mark_all(pool, TASK_VMARK_REMOVE|TASK_VMARK_DISABLE_QUEUE); stpool_wait_all(pool, -1); /** * Show the realtime scheduler map */ puts( stpool_scheduler_map_dump(pool) ); /** * release the pool * * NOTE: * If the pool's reference is changed to zero, all groups will be * deleted by the pool automatically and the pool itself will be destroyed * later in the background after doing all of the active tasks existing * in the pool */ getchar(); puts("print any key to release the pool ...\n"); stpool_release(pool); puts("print any key to exit ...\n"); getchar(); return 0; }
int main() { stpool_t *pool; int i, error, c = 0; struct schattr attr = {0, 1, ep_SCHE_TOP}; struct sttask *ptsk; long eCAPs = eCAP_F_DYNAMIC|eCAP_F_SUSPEND|eCAP_F_THROTTLE|eCAP_F_ROUTINE| eCAP_F_DISABLEQ|eCAP_F_PRIORITY|eCAP_F_WAIT_ALL; /** NO buffer */ setbuf(stdout, 0); /** Create a pool */ pool = stpool_create("mypool", /** pool name */ eCAPs, /** neccessary capabilites */ 20, /** limited threads number*/ 0, /** number of threads reserved to waiting for tasks*/ 0, /** do not suspend the pool */ 1 /** priority queue num */ ); if (!pool) abort(); /** Print the status of the pool */ printf("@stpool_create(20, 0, 0, 10)\n%s\n", stpool_stat_print(pool)); /*------------------------------------------------------------/ /--------------------Test @stpool_adjust(_abs)----------------/ /------------------------------------------------------------*/ printf("\nPress any key to test the @stpool_adjust(300, 4) ....\n"); getchar(); stpool_adjust_abs(pool, 300, 4); printf("@stpool_adjust_abs(pool, 300, 4)\n%s\n", stpool_stat_print(pool)); /** We call @stpool_adjust to recover the pool env */ printf("\nPress any key to test the @stpool_adjust(-280, -4) ....\n"); getchar(); stpool_adjust(pool, -280, -4); printf("@stpool_adjust(pool, -280, -4)\n%s\n", stpool_stat_print(pool)); /*------------------------------------------------------------------/ /----------------Test rescheduling task----------------------------/ /------------------------------------------------------------------*/ printf("\nPress any key to test rescheduling task. <then press key to stop testing.>\n"); getchar(); /** We creat a customed task to do the test */ ptsk = stpool_task_new(NULL, "test-reschedule", task_reschedule, NULL, &c); /** Attach the destinational pool */ error = stpool_task_set_p(ptsk, pool); if (error) printf("***Err: %d(%s). (try eCAP_F_CUSTOM_TASK)\n", error, stpool_strerror(error)); else { stpool_task_set_userflags(ptsk, 0x1); stpool_task_queue(ptsk); getchar(); /** Set the flag to notify the task to exit the rescheduling test */ stpool_task_set_userflags(ptsk, 0); stpool_task_wait(ptsk, -1); } stpool_task_delete(ptsk); /*-------------------------------------------------------------------/ /--------------------Test the throttle------------------------------/ /-------------------------------------------------------------------*/ printf("\nPress any key to test the throttle ....\n"); getchar(); /** Turn the throttle on */ stpool_throttle_enable(pool, 1); error = stpool_add_routine(pool, "test-throttle", task_run, task_err_handler, &c, NULL); if (error) printf("***Err: @stpool_add_task: %d\n", error); /** Turn the throttle off */ stpool_throttle_enable(pool, 0); /*-------------------------------------------------------------------/ /------------------Test the priority--------------------------------/ /-------------------------------------------------------------------*/ printf("\nPress any key to test the priority ....\n"); getchar(); stpool_suspend(pool, 0); /** * Add a task with zero priority, and the task will be pushed into the * lowest priority queue. */ stpool_add_routine(pool, "zero-priority", task_run, task_err_handler, &c, NULL); /** * task("non-zero-priority") will be scheduled prior to the task("zero-priority") * since it has a higher priority. */ stpool_add_routine(pool, "non-zero-priority", task_run, task_err_handler, &c, &attr); /** Wake up the pool to schedule the tasks */ stpool_resume(pool); /** Wait for all tasks' being done completely */ stpool_wait_all(pool, -1); /*------------------------------------------------------------------/ /---------------Test running amount of tasks-----------------------/ /------------------------------------------------------------------*/ printf("\nPress any key to add tasks ... <then can press any key to remove them.>\n"); getchar(); /** * We can suspend the pool firstly, and then resume the pool after delivering our * tasks into the pool, It'll be more effecient to do it like that if there are * a large amount of tasks that will be added into the pool. */ for (i=0; i<8000; i++) stpool_add_routine(pool, "task_run", task_run, task_err_handler, &c, NULL); /*----------------------------------------------------------------/ /-------------Test stoping all tasks fastly----------------------/ /----------------------------------------------------------------*/ printf("\nPress any key to test stoping all tasks fastly.\n"); getchar(); stpool_throttle_enable(pool, 1); stpool_remove_all(pool, 1); /** Wait for all tasks' being done */ stpool_wait_all(pool, -1); printf("---------------------------tasks have been finished.\n"); /*---------------------------------------------------------------/ /-------------Release the pool-----------------------------------/ /---------------------------------------------------------------*/ printf("Press any key to release the pool...\n"); getchar(); /** Release the pool */ printf("%s\n", stpool_stat_print(pool)); stpool_release(pool); printf("Press any key to exit ...\n"); getchar(); return 0; }
int main() { time_t now; int i, times; int sum, *arg; stpool_t *pool; /** Creat a task pool */ pool = stpool_create("mypool", /** pool name */ eCAP_F_DYNAMIC|eCAP_F_SUSPEND|eCAP_F_ROUTINE|eCAP_F_WAIT_ALL, 50, /** max servering threads */ 0, /** 0 servering threads that reserved for waiting for tasks */ 1, /** suspend the pool */ 0); /** default number of priority queue */ /** * Add tasks */ times = 90000; arg = (int *)malloc(times * sizeof(int)); for (i=0; i<times; i++) { /** * It may take a long time to load a large amount of tasks * if the program is linked with the debug library */ if (i % 4000 == 0 || (i + 1) ==times) { printf("\rLoading ... %.2f%% ", (float)i * 100/ times); fflush(stdout); } arg[i] = i; stpool_add_routine(pool, "sche", task_run, NULL, (void *)&arg[i], NULL); } printf("\nAfter having executed @stpool_add_routine for %d times:\n" "--------------------------------------------------------\n%s\n", times, stpool_stat_print(pool)); /** * Wait for all tasks' being done. */ { unsigned us; /** * Wake up the pool to schedule tasks */ stpool_resume(pool); us_start(); stpool_wait_all(pool, -1); us = us_end(); printf("Test costs %.2f ms \n", (double)us / 1000); } /** * Get the sum */ for (i=0, sum=0; i<times; i++) sum += arg[i]; free(arg); now = time(NULL); printf("--OK. finished. <arg: %d> %s\n%s\n", sum, ctime(&now), stpool_stat_print(pool)); #if 0 /** * You can use debug library to watch the status of the pool */ { int c; while ('q' != getchar()) { for (i=0; i<40; i++) stpool_add_routine(pool, "debug", task_run, NULL, &sum, NULL); } /** * Clear the stdio cache */ while ((c=getchar()) && c != '\n' && c != EOF) ; } #endif getchar(); /** * Release the pool */ printf("Shut down the pool now.\n"); stpool_release(pool); getchar(); return 0; }
int main() { stpool_t *pool; struct schattr attr[] = { {0, 0, ep_SCHE_BACK}, {0, 10, ep_SCHE_BACK}, {0, 40, ep_SCHE_BACK}, {0, 90, ep_SCHE_TOP}, }; long eCAPs = eCAP_F_THROTTLE|eCAP_F_PRIORITY|eCAP_F_WAIT_ALL; /** * Creat a pool with 1 servering threads */ pool = stpool_create("mypool", eCAPs, 1, 0, 1, 1); /** * Add a few tasks into the pool */ stpool_add_routine(pool, "zero_task", task_run, task_err_handler, NULL, &attr[0]); stpool_add_routine(pool, "low_task", task_run, task_err_handler, NULL, &attr[1]); stpool_add_routine(pool, "middle_task", task_run, task_err_handler, NULL, &attr[2]); stpool_add_routine(pool, "hight_task", task_run, task_err_handler, NULL, &attr[3]); puts( stpool_scheduler_map_dump(pool) ); puts("Print any key to resume the pool ...\n"); getchar(); /** * Wake up the pool to run the tasks */ stpool_resume(pool); puts("Print any key to exit the test ...\n"); getchar(); /** * Turn the throttle on */ stpool_throttle_enable(pool, 1); /** * Remove all pendings task */ stpool_remove_all(pool, 1); /** * Wait for all tasks' completions */ stpool_wait_all(pool, -1); puts("All tasks have been removed completely.\n"); getchar(); puts(stpool_stat_print(pool)); /** * Release the pool */ stpool_release(pool); printf("print any key to exit.\n"); getchar(); return 0; }