static void *worker(void *_dummy) { worker_cleanup_t wc; pth_msgport_t mp; pth_event_t ev; struct query *q; int i; fprintf(stderr, "worker: start\n"); wc.mp = mp = pth_msgport_create("worker"); wc.ev = ev = pth_event(PTH_EVENT_MSG, mp); pth_cleanup_push(worker_cleanup, &wc); for (;;) { if ((i = pth_wait(ev)) != 1) continue; while ((q = (struct query *)pth_msgport_get(mp)) != NULL) { fprintf(stderr, "worker: recv query <%s>\n", q->string); for (i = 0; q->string[i] != NUL; i++) q->string[i] = toupper(q->string[i]); fprintf(stderr, "worker: send reply <%s>\n", q->string); pth_msgport_reply((pth_message_t *)q); } } return NULL; }
void * task_pth_run(void *data) { Task *task; task = (Task *) data; char msgPortName[10]; pth_msgport_t mp; pth_event_t ev; #ifdef _LDEBUG printf("F=%s,L=%d,data_pth_run=%p,%p,name=%s,%p\n", __FILE__, __LINE__, data, task, task->name, task->name); #endif if (task == NULL) return NULL; append_List(&Tasks, (void *) task); local_print_List(&Tasks); snprintf(msgPortName, 10, "%ld", task->id); #ifdef _MDEBUG printf("F=%s,L=%d,task->id=%ld,msgPortName=%s\n", __FILE__, __LINE__, task->id, msgPortName); #endif mp = pth_msgport_create(msgPortName); ev = pth_event(PTH_EVENT_MSG, mp); if (task->run) task->run(task->data); pth_event_free(ev, PTH_FREE_THIS); pth_msgport_destroy(mp); if (task->destroy) task->destroy(task->data); stopcount = 0; Task_START(); removeIt_List(&Tasks, task); // local_print_List(&Tasks); free(task->name); free(task); #ifdef _LDEBUG printf(" task end run=%p\n", task); printf("F=%s,L=%d\n", __FILE__, __LINE__); #endif return NULL; }
FrSimThreadEvent::FrSimThreadEvent() { msgPrt = pth_msgport_create(NULL); ev = pth_event(PTH_EVENT_MSG, msgPrt); }
int main(int argc, char *argv[]) { char caLine[MAXLINELEN]; pth_event_t ev = NULL; pth_event_t evt = NULL; pth_t t_worker = NULL; pth_t t_ticker = NULL; pth_attr_t t_attr; pth_msgport_t mp = NULL; pth_msgport_t mp_worker = NULL; struct query *q = NULL; int n; if (!pth_init()) { perror("pth_init"); exit(1); } /* murray added for tmp debug */ /* pth_time_t intval, former; printf("-------------------------\n"); pth_time_set(&former, PTH_TIME_NOW); pth_usleep(300); pth_time_set(&intval, PTH_TIME_NOW); pth_time_sub(&intval, &former); double val = pth_time_t2d(&intval); printf("the intval is [%f]\n", val); pth_debug2("the intval is [%f]\n", val); return 0; */ fprintf(stderr, "This is TEST_MP, a Pth test using message ports.\n"); fprintf(stderr, "\n"); fprintf(stderr, "Lines on stdin are send to a worker thread via message\n"); fprintf(stderr, "ports, translated to upper case by the worker thread and\n"); fprintf(stderr, "send back to the main thread via message ports.\n"); fprintf(stderr, "Additionally a useless ticker thread awakens every 5s.\n"); fprintf(stderr, "Enter \"quit\" on stdin for stopping this test.\n"); fprintf(stderr, "\n"); t_attr = pth_attr_new(); pth_attr_set(t_attr, PTH_ATTR_NAME, "worker"); pth_attr_set(t_attr, PTH_ATTR_JOINABLE, TRUE); pth_attr_set(t_attr, PTH_ATTR_STACK_SIZE, 16*1024); t_worker = pth_spawn(t_attr, worker, NULL); pth_attr_set(t_attr, PTH_ATTR_NAME, "ticker"); t_ticker = pth_spawn(t_attr, ticker, NULL); pth_attr_destroy(t_attr); pth_yield(NULL); mp_worker = pth_msgport_find("worker"); mp = pth_msgport_create("main"); q = (struct query *)malloc(sizeof(struct query)); ev = pth_event(PTH_EVENT_MSG, mp); evt = NULL; for (;;) { if (evt == NULL) evt = pth_event(PTH_EVENT_TIME, pth_timeout(20,0)); else evt = pth_event(PTH_EVENT_TIME|PTH_MODE_REUSE, evt, pth_timeout(20,0)); n = pth_readline_ev(STDIN_FILENO, caLine, MAXLINELEN, evt); if (n == -1 && pth_event_status(evt) == PTH_STATUS_OCCURRED) { fprintf(stderr, "main: Hey, what are you waiting for? Type in something!\n"); continue; } if (n < 0) { fprintf(stderr, "main: I/O read error on stdin\n"); break; } if (n == 0) { fprintf(stderr, "main: EOF on stdin\n"); break; } caLine[n-1] = NUL; if (strcmp(caLine, "quit") == 0) { fprintf(stderr, "main: quit\n"); break; } fprintf(stderr, "main: out --> <%s>\n", caLine); q->string = caLine; q->head.m_replyport = mp; pth_msgport_put(mp_worker, (pth_message_t *)q); pth_wait(ev); q = (struct query *)pth_msgport_get(mp); fprintf(stderr, "main: in <-- <%s>\n", q->string); } free(q); pth_event_free(ev, PTH_FREE_THIS); pth_event_free(evt, PTH_FREE_THIS); pth_msgport_destroy(mp); pth_cancel(t_worker); pth_join(t_worker, NULL); pth_cancel(t_ticker); pth_join(t_ticker, NULL); pth_kill(); return 0; }