int ldap_int_thread_destroy( void ) { pth_attr_destroy(detach_attr); pth_kill(); return 0; }
int main(int argc, char **argv) { long num_threads; if (argc != 2) { fprintf(stderr, "Usage: %s num_threads\n", argv[0]); exit(1); } num_threads = strtol(argv[1], NULL, 10); printf("Using %ld threads\n", num_threads); pth_init(); for (long i = 1; i <= num_threads; i++) { char *name = malloc(1024); sprintf(name, "busy%ld", i); pth_t t = spawn_with_attr(name, 1, 5, busy_thread, NULL); printf("Spawned thread #%ld with id %p\n", i, t); free(name); } printf("main thread now sleeping for %d seconds...\n", TEST_LENGTH); pth_sleep(TEST_LENGTH); pth_kill(); return 0; }
void SDL_Quit(void) { /* Quit all subsystems */ #ifdef DEBUG_BUILD printf("[SDL_Quit] : Enter! Calling QuitSubSystem()\n"); fflush(stdout); #endif SDL_QuitSubSystem(SDL_INIT_EVERYTHING); #ifdef CHECK_LEAKS #ifdef DEBUG_BUILD printf("[SDL_Quit] : CHECK_LEAKS\n"); fflush(stdout); #endif /* Print the number of surfaces not freed */ if ( surfaces_allocated != 0 ) { fprintf(stderr, "SDL Warning: %d SDL surfaces extant\n", surfaces_allocated); } #endif #ifdef DEBUG_BUILD printf("[SDL_Quit] : SDL_UninstallParachute()\n"); fflush(stdout); #endif /* Uninstall any parachute signal handlers */ SDL_UninstallParachute(); #if !SDL_THREADS_DISABLED && SDL_THREAD_PTH pth_kill(); #endif #ifdef DEBUG_BUILD printf("[SDL_Quit] : Returning!\n"); fflush(stdout); #endif }
/* Core functionality */ int dnsrv_fork_and_capture(RESOLVEFUNC f, dns_io di) { int left_fds[2], right_fds[2]; int pid; /* Create left and right pipes */ if (pipe(left_fds) < 0 || pipe(right_fds) < 0) return -1; pid = pth_fork(); if (pid < 0) return -1; else if (pid > 0) /* Parent */ { /* Close unneeded file handles */ close(left_fds[STDIN_FILENO]); close(right_fds[STDOUT_FILENO]); /* Return the in and out file descriptors */ di->in = right_fds[STDIN_FILENO]; di->out = left_fds[STDOUT_FILENO]; return pid; } else /* Child */ { /* Close unneeded file handles */ pth_kill(); close(left_fds[STDOUT_FILENO]); close(right_fds[STDIN_FILENO]); /* Start the specified function, passing the in/out descriptors */ di->in = left_fds[STDIN_FILENO]; di->out = right_fds[STDOUT_FILENO]; return (*f)(di); } }
static void myexit(int sig) { close(s); pth_attr_destroy(attr); pth_kill(); fprintf(stderr, "**Break\n"); exit(0); }
/* Shutdown TSRM (call once for the entire process) */ TSRM_API void tsrm_shutdown(void) {/*{{{*/ int i; if (!in_main_thread) { /* ensure singleton */ return; } if (tsrm_tls_table) { for (i=0; i<tsrm_tls_table_size; i++) { tsrm_tls_entry *p = tsrm_tls_table[i], *next_p; while (p) { int j; next_p = p->next; for (j=0; j<p->count; j++) { if (p->storage[j]) { if (resource_types_table && !resource_types_table[j].done && resource_types_table[j].dtor) { resource_types_table[j].dtor(p->storage[j]); } free(p->storage[j]); } } free(p->storage); free(p); p = next_p; } } free(tsrm_tls_table); tsrm_tls_table = NULL; } if (resource_types_table) { free(resource_types_table); resource_types_table=NULL; } tsrm_mutex_free(tsmm_mutex); tsmm_mutex = NULL; TSRM_ERROR((TSRM_ERROR_LEVEL_CORE, "Shutdown TSRM")); if (tsrm_error_file!=stderr) { fclose(tsrm_error_file); } #if defined(GNUPTH) pth_kill(); #elif defined(PTHREADS) pthread_setspecific(tls_key, 0); pthread_key_delete(tls_key); #elif defined(TSRM_WIN32) TlsFree(tls_key); #endif if (tsrm_shutdown_handler) { tsrm_shutdown_handler(); } tsrm_new_thread_begin_handler = NULL; tsrm_new_thread_end_handler = NULL; tsrm_shutdown_handler = NULL; }/*}}}*/
int main(void) { pth_init(); pth_t t1 = spawn_with_attr("busy", 1, 2, busy_thread, NULL); printf("Spawned busy thread with id %p\n", t1); pth_t t2 = spawn_with_attr("nice", 1, 2, nice_thread, NULL); printf("Spawned nice thread with id %p\n", t2); printf("main thread now sleeping for %d seconds...\n", TEST_LENGTH); pth_sleep(TEST_LENGTH); pth_kill(); return 0; }
void SDL_Quit(void) { /* Quit all subsystems */ SDL_QuitSubSystem(SDL_INIT_EVERYTHING); #ifdef CHECK_LEAKS /* Print the number of surfaces not freed */ if ( surfaces_allocated != 0 ) { fprintf(stderr, "SDL Warning: %d SDL surfaces extant\n", surfaces_allocated); } #endif /* Uninstall any parachute signal handlers */ SDL_UninstallParachute(); #if !defined(DISABLE_THREADS) && defined(ENABLE_PTH) pth_kill(); #endif }
int main(int argc, char *argv[]) { pth_attr_t attr; sigset_t sigs; pth_init(); fprintf(stderr, "This is TEST_SIG, a Pth test using signals.\n"); fprintf(stderr, "\n"); fprintf(stderr, "Hit CTRL-C three times to stop this test.\n"); fprintf(stderr, "But only after all threads were terminated.\n"); fprintf(stderr, "\n"); fprintf(stderr, "main: init\n"); /* block signals */ pth_sigmask(SIG_SETMASK, NULL, &sigs); sigaddset(&sigs, SIGUSR1); sigaddset(&sigs, SIGUSR2); sigaddset(&sigs, SIGINT); pth_sigmask(SIG_SETMASK, &sigs, NULL); /* spawn childs */ attr = pth_attr_new(); pth_attr_set(attr, PTH_ATTR_NAME, "child1"); child1 = pth_spawn(attr, child, (void *)"child1"); pth_attr_set(attr, PTH_ATTR_NAME, "child2"); child2 = pth_spawn(attr, child, (void *)"child2"); pth_attr_set(attr, PTH_ATTR_NAME, "inthandler"); pth_spawn(attr, inthandler, (void *)"inthandler"); pth_attr_destroy(attr); /* wait until childs are finished */ while (pth_join(NULL, NULL)); fprintf(stderr, "main: exit\n"); pth_kill(); return 0; }
int main(int argc, char *argv[]) { int i; sigset_t ss; int sig; pth_event_t ev; /* initialize Pth library */ pth_init(); /* display test program header */ printf("This is TEST_PHILO, a Pth test showing the Five Dining Philosophers\n"); printf("\n"); printf("This is a demonstration showing the famous concurrency problem of the\n"); printf("Five Dining Philosophers as analysed 1965 by E.W.Dijkstra:\n"); printf("\n"); printf("Five philosophers are sitting around a round table, each with a bowl of\n"); printf("Chinese food in front of him. Between periods of talking they may start\n"); printf("eating whenever they want to, with their bowls being filled frequently.\n"); printf("But there are only five chopsticks available, one each to the left of\n"); printf("each bowl - and for eating Chinese food one needs two chopsticks. When\n"); printf("a philosopher wants to start eating, he must pick up the chopstick to\n"); printf("the left of his bowl and the chopstick to the right of his bowl. He\n"); printf("may find, however, that either one (or even both) of the chopsticks is\n"); printf("unavailable as it is being used by another philosopher sitting on his\n"); printf("right or left, so he has to wait.\n"); printf("\n"); printf("This situation shows classical contention under concurrency (the\n"); printf("philosophers want to grab the chopsticks) and the possibility of a\n"); printf("deadlock (all philosophers wait that the chopstick to their left becomes\n"); printf("available).\n"); printf("\n"); printf("The demonstration runs max. 60 seconds. To stop before, press CTRL-C.\n"); printf("\n"); printf("+----P1----+----P2----+----P3----+----P4----+----P5----+\n"); /* initialize the control table */ tab = (table *)malloc(sizeof(table)); if (!pth_mutex_init(&(tab->mutex))) { perror("pth_mutex_init"); exit(1); } for (i = 0; i < PHILNUM; i++) { (tab->self)[i] = i; (tab->status)[i] = thinking; if (!pth_cond_init(&((tab->condition)[i]))) { perror("pth_cond_init"); exit(1); } } /* spawn the philosopher threads */ for (i = 0; i < PHILNUM; i++) { if (((tab->tid)[i] = pth_spawn(PTH_ATTR_DEFAULT, philosopher, &((tab->self)[i]))) == NULL) { perror("pth_spawn"); exit(1); } } /* wait until 60 seconds have elapsed or CTRL-C was pressed */ sigemptyset(&ss); sigaddset(&ss, SIGINT); ev = pth_event(PTH_EVENT_TIME, pth_timeout(60,0)); pth_sigwait_ev(&ss, &sig, ev); pth_event_free(ev, PTH_FREE_ALL); /* cancel and join the philosopher threads */ for (i = 0; i < PHILNUM; i++) pth_cancel((tab->tid)[i]); while (pth_join(NULL, NULL)); /* finish display */ printf("+----------+----------+----------+----------+----------+\n"); /* free the control table */ free(tab); /* shutdown Pth library */ pth_kill(); return 0; }
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; }