void __phsa_builtin_waitfbar (uint32_t addr, PHSAWorkItem *wi) { fbarrier *fbar = (fbarrier *) (wi->wg->group_base_ptr + addr); ++fbar->arrive_count; ++fbar->wait_count; while (fbar->arrive_count < fbar->member_count) pth_yield (NULL); --fbar->wait_count; while (fbar->wait_count > 0) pth_yield (NULL); fbar->arrive_count = 0; }
void FrSimThreadEvent::trigger() { // std::cout << "trigger before " << pth_msgport_pending(msgPrt) << std::endl; if (!pth_msgport_put(msgPrt, &msg)) { // need to put proper failure method here. exit(1); } // std::cout << "trigger after " << pth_msgport_pending(msgPrt) << std::endl; pth_yield(NULL); }
static void* task2(void* arg) { static volatile long res = 1; for (long i = 0; i < 1000000; ++i) { res ^= (i + 1) * 3 >> 2; if (i % 100000 != 0) { continue; } if (*static_cast<bool*>(arg)) { pth_yield(NULL); } else { sched_yield(); } } return doneTask(arg); }
static void *handler(void *_arg) { int fd = (int)((long)_arg); char caLine[MAXREQLINE]; char str[1024]; int n; /* read request */ for (;;) { n = pth_readline(fd, caLine, MAXREQLINE); if (n < 0) { fprintf(stderr, "read error: errno=%d\n", errno); close(fd); return NULL; } if (n == 0) break; if (n == 1 && caLine[0] == '\n') break; caLine[n-1] = NUL; } /* simulate a little bit of processing ;) */ pth_yield(NULL); /* generate response */ sprintf(str, "HTTP/1.0 200 Ok\r\n" "Server: test_httpd/%x\r\n" "Connection: close\r\n" "Content-type: text/plain\r\n" "\r\n" "Just a trivial test for GNU Pth\n" "to show that it's serving data.\r\n", PTH_VERSION); pth_write(fd, str, strlen(str)); /* close connection and let thread die */ fprintf(stderr, "connection shutdown (fd: %d)\n", fd); close(fd); return NULL; }
int ldap_pvt_thread_yield( void ) { return pth_yield(NULL) ? 0 : errno; }
int main (int ac, char *ag[]) { int index; pth_init (); argp_parse (&argp, ac, ag, ARGP_IN_ORDER, &index, &arg); // if you ever want this to be fatal, doing it here would be too late if (getuid () == 0) ERRORPRINTF (arg.tracer(), E_WARNING | 20, 0, "EIBD should not run as root"); signal (SIGPIPE, SIG_IGN); if (arg.daemon) { int fd = open (arg.daemon, O_WRONLY | O_APPEND | O_CREAT, FILE_MODE); if (fd == -1) die ("Can not open file %s", arg.daemon); int i = fork (); if (i < 0) die ("fork failed"); if (i > 0) exit (0); close (1); close (2); close (0); dup2 (fd, 1); dup2 (fd, 2); close (fd); setsid (); } FILE *pidf; if (arg.pidfile) if ((pidf = fopen (arg.pidfile, "w")) != NULL) { fprintf (pidf, "%d", getpid ()); fclose (pidf); } signal (SIGINT, SIG_IGN); signal (SIGTERM, SIG_IGN); // main loop #ifdef HAVE_SYSTEMD sd_notify(0,"READY=1"); #endif int sig; if (! arg.stop_now) do { sigset_t t1; sigemptyset (&t1); sigaddset (&t1, SIGINT); sigaddset (&t1, SIGHUP); sigaddset (&t1, SIGTERM); pth_sigwait (&t1, &sig); if (sig == SIGHUP && arg.daemon) { int fd = open (arg.daemon, O_WRONLY | O_APPEND | O_CREAT, FILE_MODE); if (fd == -1) { ERRORPRINTF (arg.tracer(), E_ERROR | 21, 0, "can't open log file %s", arg.daemon); continue; } close (1); close (2); dup2 (fd, 1); dup2 (fd, 2); close (fd); } } while (sig == SIGHUP); #ifdef HAVE_SYSTEMD sd_notify(0,"STOPPING=1"); #endif signal (SIGINT, SIG_DFL); signal (SIGTERM, SIG_DFL); #ifdef HAVE_GROUPCACHE DeleteGroupCache (); #endif arg.free_l3(); if (arg.pidfile) unlink (arg.pidfile); pth_yield (0); pth_yield (0); pth_yield (0); pth_yield (0); pth_exit (0); return 0; }
/* The thread started by start_feede3. */ static void * feeder_thread (void *arg) { struct feeder_thread_parms *parm = arg; char buffer[4096]; int rc; if (parm->direction) { size_t nread = 0; DWORD nwritten; log_debug ("feeder_thread estream->pipe: stream=%p pipe=%p\n", parm->stream, parm->hd); while (parm->stream_valid && !es_read (parm->stream, buffer, sizeof buffer, &nread)) { do { pth_enter (); rc = WriteFile (parm->hd, buffer, nread, &nwritten, NULL); pth_leave (); if (!rc) { log_debug ("feeder(%p): WriteFile error: rc=%d\n", parm->hd, (int)GetLastError ()); goto leave; } nread -= nwritten; } while (nread); } if (!parm->stream_valid) log_debug ("feeder(%p): closed by other thread\n", parm->hd); else if (nread) log_debug ("feeder(%p): es_read error: %s\n", parm->hd, strerror (errno)); } else { DWORD nread = 0; size_t nwritten; log_debug ("feeder_thread pipe->estream: stream=%p pipe=%p\n", parm->stream, parm->hd); while ( (pth_enter (), (rc = ReadFile (parm->hd, buffer, sizeof buffer, &nread, NULL)), pth_leave (), rc) && nread) { log_debug ("feeder_thread pipe->estream: read %d bytes\n", (int)nread); do { if (parm->stream_valid && es_write (parm->stream, buffer, nread, &nwritten)) { log_debug ("feeder(%p): es_write error: %s\n", parm->hd, strerror (errno)); goto leave; } log_debug ("feeder_thread pipe->estream: es_wrote %d bytes\n", (int)nwritten); nread -= nwritten; } while (nread && parm->stream_valid); } if (!parm->stream_valid) log_debug ("feeder(%p): closed by other thread\n", parm->hd); else if (nread) log_debug ("feeder(%p): ReadFile error: rc=%d\n", parm->hd, (int)GetLastError ()); else log_debug ("feeder(%p): eof\n", parm->hd); } leave: log_debug ("feeder(%p): waiting for es_fclose\n", parm->hd); while (parm->stream_valid) pth_yield (NULL); log_debug ("feeder(%p): about to close the pipe handle\n", parm->hd); CloseHandle (parm->hd); log_debug ("feeder(%p): pipe handle closed\n", parm->hd); xfree (parm); return NULL; }
void FrSimThread::yield() { pth_yield(NULL); }
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; }