static void on_cancelling(struct ythreadex *threadex, bool started) { struct targ *ta = ythreadex_get_arg(threadex); ta->st[ta->sti++] = YTHREADEX_CANCELLING; ylogv("cancelling\n"); ta->cancel = TRUE; }
static int task_runnable(struct ytask *tsk, void **result) { int i; struct targ *ta = ytask_get_arg(tsk); ylogv("%s(%ld)[%d] run\n", ytask_get_name(tsk), ytask_get_id(tsk), ytask_get_priority(tsk)); for (i = 0; i < ta->sleep_cnt; i++) { usleep(ta->sleep_interval * 1000); } *result = NULL; return 0; }
static int thread_run(struct ythreadex *threadex, void **result) { int i; struct tres *tr; struct targ *ta = ythreadex_get_arg(threadex); ylogv("thread run\n"); *result = ymalloc(sizeof(*tr)); tr = (struct tres *)*result; memset(tr, 0, sizeof(*tr)); ythreadex_publish_progress_init(threadex, 100); for (i = 0; i < ta->sleep_cnt; i++) usleep(ta->sleep_interval * 1000); if (ta->retval) return ta->retval; memset(tr, 0, sizeof(*tr)); tr->s = ymalloc(32); sprintf(tr->s, "res:%d", tr->r); *result = tr; return 0; }
static void tc2(struct ymsghandler *mh) { struct ythreadex *yt; struct targ *ta; struct tres *tr; void *retval; /* Normal successful thread */ ta = ymalloc(sizeof(*ta)); memset(ta, 0, sizeof(*ta)); ta->s = ymalloc(16); strcpy(ta->s, "ok:arg"); yt = ythreadex_create("ok", mh, YTHREADEX_NORMAL, &_lis, ta, &free_arg, &free_result, &thread_run); yassert(ythreadex_destroy(yt)); yassert(!ythreadex_start(yt)); ythreadex_join(yt, &retval); tr = ythreadex_get_result(yt); /* STARTED, DONE. And PROGRESS_INIT */ ylogv("%d, %d, %d, %d, %d, %c\n", ta->sti, ta->st[0], ta->st[1], ta->progcnt, tr->r, tr->s[0]); yassert(2 == ta->sti && YTHREADEX_STARTED == ta->st[0] && YTHREADEX_DONE == ta->st[1] && 1000 == ta->progcnt && 0 == tr->r && 'r' == tr->s[0]); yassert(!ythreadex_destroy(yt)); }
static void on_started(struct ythreadex *threadex) { struct targ *ta = ythreadex_get_arg(threadex); ta->st[ta->sti++] = YTHREADEX_STARTED; ylogv("started\n"); }
static void tc4(struct ymsghandler *mh) { #define NR_THREADS 10 int i; struct ythreadex *yt[NR_THREADS]; struct targ *ta; struct tres *tr; void *retval[NR_THREADS]; for (i = 0; i < NR_THREADS; i++) { /* Normal successful thread */ ta = ymalloc(sizeof(*ta)); memset(ta, 0, sizeof(*ta)); /* sleep enough to get 'cancel' message after start */ ta->sleep_cnt = 10 + rand() % 20; ta->sleep_interval = 10; ta->s = ymalloc(16); strcpy(ta->s, "ok:arg"); yt[i] = ythreadex_create("ok", mh, YTHREADEX_NORMAL, &_lis, ta, &free_arg, &free_result, &thread_run); } for (i = 0; i < NR_THREADS; i++) yassert(!ythreadex_start(yt[i])); for (i = 0; i < NR_THREADS; i++) { if (i % 3) ythreadex_cancel(yt[i], i % 2); } for (i = 0; i < NR_THREADS; i++) ythreadex_join(yt[i], &retval[i]); for (i = 0; i < NR_THREADS; i++) { if (i % 3) { /* This is cancelled thread */ ylogv("state: %d\n", ythreadex_get_state(yt[i])); yassert(YTHREADEX_TERMINATED_CANCELLED == ythreadex_get_state(yt[i])); } else { yassert(YTHREADEX_TERMINATED == ythreadex_get_state(yt[i])); tr = ythreadex_get_result(yt[i]); ta = ythreadex_get_arg(yt[i]); /* STARTED, DONE. And PROGRESS_INIT */ ylogv("%d, %d, %d, %d, %d, %c\n", ta->sti, ta->st[0], ta->st[1], ta->progcnt, tr->r, tr->s[0]); yassert(2 == ta->sti && YTHREADEX_STARTED == ta->st[0] && YTHREADEX_DONE == ta->st[1] && 1000 == ta->progcnt && 0 == tr->r && 'r' == tr->s[0]); } yassert(!ythreadex_destroy(yt[i])); } #undef NR_THREADS }
static void on_progress(struct ythreadex *threadex, long prog) { struct targ *ta = ythreadex_get_arg(threadex); ta->progcnt++; ylogv("prog\n"); }
static void on_progress_init(struct ythreadex *threadex, long max_prog) { struct targ *ta = ythreadex_get_arg(threadex); ta->progcnt += 1000; ylogv("prog init\n"); }
static void on_cancelled(struct ythreadex *threadex, int errcode) { struct targ *ta = ythreadex_get_arg(threadex); ta->st[ta->sti++] = YTHREADEX_CANCELLED; ylogv("cancelled\n"); }
static void on_done(struct ythreadex *threadex, void *result, int errcode) { struct targ *ta = ythreadex_get_arg(threadex); ta->st[ta->sti++] = YTHREADEX_DONE; ylogv("done\n"); }