int SDL_Init(Uint32 flags) { #if !SDL_THREADS_DISABLED && SDL_THREAD_PTH if (!pth_init()) { return -1; } #endif /* Clear the error message */ SDL_ClearError(); #if defined(__WIN32__) if (SDL_HelperWindowCreate() < 0) { return -1; } #endif /* Initialize the desired subsystems */ if (SDL_InitSubSystem(flags) < 0) { return (-1); } /* Everything is initialized */ if (!(flags & SDL_INIT_NOPARACHUTE)) { SDL_InstallParachute(); } return (0); }
int SDL_Init(Uint32 flags) { #if defined (__XENON__) /* !!!! NOTE: THE CALLING CODE in main() needs to call these first !!!!*/ /* if SDL_Init() is called too late in the code with these functions it seems to crash libxenon **/ //xenon_make_it_faster(XENON_SPEED_FULL); //xenos_init(VIDEO_MODE_AUTO); //xenon_sound_init(); //console_init(); //usb_init(); //usb_do_poll(); #endif #if !SDL_THREADS_DISABLED && SDL_THREAD_PTH if (!pth_init()) { return -1; } #endif /* Clear the error message */ SDL_ClearError(); /* Initialize the desired subsystems */ if ( SDL_InitSubSystem(flags) < 0 ) { return(-1); } /* Everything is initialized */ if ( !(flags & SDL_INIT_NOPARACHUTE) ) { SDL_InstallParachute(); } 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; }
int main() { pth_t thread; int rc; int i, j, s; arg = -1; res = 0; pth_init(); thread = pth_spawn(PTH_ATTR_DEFAULT, thread_routine, NULL); for(i = 0; i < 100; i++) { s = 0; for(j = 0; j < 10000; j++) { pth_mutex_acquire(&mutex, FALSE, NULL); res = -1; arg = j; pth_mutex_release(&mutex); pth_cond_notify(&c1, FALSE); pth_mutex_acquire(&mutex, FALSE, NULL); if(res < 0) pth_cond_await(&c2, &mutex, NULL); s += res; arg = -1; pth_mutex_release(&mutex); } } printf("%d\n", s); return 0; }
int main(int argc, char* argv[]) { float result[ORDER]; signal(SIGINT, Exit); signal(SIGTERM, Exit); #ifdef SW #ifdef USE_GNUPTH pth_init(); #endif init_pipe_handler(); PTHREAD_DECL(conversionTest); PTHREAD_CREATE(conversionTest); #endif PTHREAD_DECL(Sender); PTHREAD_CREATE(Sender); uint8_t idx; read_uint64_n("out_data",((uint64_t*) result),ORDER/2); for(idx = 0; idx < ORDER; idx++) { fprintf(stdout,"Result = %f, expected = %f.\n", result[idx],expected_result[idx]); } PTHREAD_CANCEL(Sender); #ifdef SW close_pipe_handler(); PTHREAD_CANCEL(conversionTest); #endif return(0); }
TASK_DLLEXPORT void Task_INIT(void) { if (pth_inited != 0) return; pth_init(); pth_inited = 1; }
static int mutex_entry( pth_mutex_t *mutex ) { if ( !pth_init_called ) { pth_init(); pth_init_called = 1; } return pth_mutex_acquire( mutex, 0, NULL ); }
static int mutex_entry( void ) { if ( !pth_init_called ) { pth_init(); pth_init_called = 1; } return pth_mutex_acquire( &mutex_log, 0, NULL ); }
void task_init (void) { memset (task_data_table, 0, sizeof (task_data_table)); pth_init (); task_data_table[0].pid = task_getpid (); task_data_table[0].gid = GID_FIRST_TASK; task_data_table[0].duration = TASK_DURATION_INF; }
void mgt_dispatch_t::init () { if (pth_init () != TRUE) { if (errno == EPERM) warn << "Pth library has already been initialized.\n"; else fatal << "Cannot initialize GNU Pth library\n"; } mtdispatch_t::init (); }
int main (int ac, char *ag[]) { int addr; int len; int index; CArray result; LowLevelDriverInterface *iface = 0; memset (&arg, 0, sizeof (arg)); argp_parse (&argp, ac, ag, 0, &index, &arg); if (index > ac - 3) die ("more parameter expected"); if (index < ac - 3) die ("unexpected parameter"); signal (SIGPIPE, SIG_IGN); pth_init (); Logs t; t.setTraceLevel (arg.tracelevel); iface = Create (ag[index], &t); if (!iface) die ("initialisation failed"); if (!iface->init ()) die ("initialisation failed"); addr = readHex (ag[index + 1]); len = atoi (ag[index + 2]); int res = readEMIMem (iface, addr, len, result); if (!res) { printf ("Read failed"); } else { for (int i = 0; i < result (); i++) printf ("%02x ", result[i]); printf ("\n"); } delete iface; if (Cleanup) Cleanup (); pth_exit (0); return 0; }
int ldap_int_thread_initialize( void ) { if( !pth_init() ) { return -1; } detach_attr = pth_attr_new(); joined_attr = pth_attr_new(); #ifdef LDAP_PVT_THREAD_SET_STACK_SIZE pth_attr_set( joined_attr, PTH_ATTR_STACK_SIZE, LDAP_PVT_THREAD_STACK_SIZE ); pth_attr_set( detach_attr, PTH_ATTR_STACK_SIZE, LDAP_PVT_THREAD_STACK_SIZE ); #endif return pth_attr_set( detach_attr, PTH_ATTR_JOINABLE, FALSE ); }
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; }
TASK_DLLEXPORT Task * Task_new(const char *name, long stacksize, void *data, void *(*run) (void *data), void (*destroy) (void *data)) { Task *task = NEW(Task); pth_t ptask; pth_attr_t attr; #ifdef _LDEBUG printf("F=%s,L=%d\n", __FILE__, __LINE__); #endif if (pth_inited == 0) { if (pth_init() == 0) return NULL; pth_inited = 1; } if (stacksize < TASK_STACK_MIN) stacksize = TASK_STACK_MIN; ++seqNo; task->id = seqNo; task->stacklen = stacksize; task->name = strdup(name); task->data = data; task->run = run; task->destroy = destroy; attr = pth_attr_new(); pth_attr_set(attr, PTH_ATTR_NAME, name); pth_attr_set(attr, PTH_ATTR_STACK_SIZE, (unsigned int) stacksize); ptask = pth_spawn(attr, task_pth_run, task); pth_attr_destroy(attr); if (ptask == NULL) { free(task); return NULL; } task->ref = (void *) ptask; #ifdef _LDEBUG printf(" open ptask=%p,task=%p,name=%s,%p\n", (void *) ptask, (void *) task, task->name, task->name); printf("F=%s,L=%d\n", __FILE__, __LINE__); #endif return task; }
/* Startup TSRM (call once for the entire process) */ TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debug_level, char *debug_filename) { #if defined(GNUPTH) pth_init(); #elif defined(PTHREADS) pthread_key_create( &tls_key, 0 ); #elif defined(TSRM_ST) st_init(); st_key_create(&tls_key, 0); #elif defined(TSRM_WIN32) tls_key = TlsAlloc(); #elif defined(BETHREADS) tls_key = tls_allocate(); #endif tsrm_error_file = stderr; tsrm_error_set(debug_level, debug_filename); tsrm_tls_table_size = expected_threads; tsrm_tls_table = (tsrm_tls_entry **) calloc(tsrm_tls_table_size, sizeof(tsrm_tls_entry *)); if (!tsrm_tls_table) { TSRM_ERROR((TSRM_ERROR_LEVEL_ERROR, "Unable to allocate TLS table")); return 0; } id_count=0; resource_types_table_size = expected_resources; resource_types_table = (tsrm_resource_type *) calloc(resource_types_table_size, sizeof(tsrm_resource_type)); if (!resource_types_table) { TSRM_ERROR((TSRM_ERROR_LEVEL_ERROR, "Unable to allocate resource types table")); free(tsrm_tls_table); tsrm_tls_table = NULL; return 0; } tsmm_mutex = tsrm_mutex_alloc(); tsrm_new_thread_begin_handler = tsrm_new_thread_end_handler = NULL; TSRM_ERROR((TSRM_ERROR_LEVEL_CORE, "Started up TSRM, %d expected threads, %d expected resources", expected_threads, expected_resources)); return 1; }
int main () { #if TEST_PTH_THREADS if (!pth_init ()) abort (); #endif #if DO_TEST_COND printf ("Starting test_cond ..."); fflush (stdout); test_cond (); printf (" OK\n"); fflush (stdout); #endif #if DO_TEST_TIMEDCOND printf ("Starting test_timedcond ..."); fflush (stdout); test_timedcond (); printf (" OK\n"); fflush (stdout); #endif return 0; }
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 SDL_Init(Uint32 flags) { #if !defined(DISABLE_THREADS) && defined(ENABLE_PTH) if (!pth_init()) { return -1; } #endif /* Clear the error message */ SDL_ClearError(); /* Initialize the desired subsystems */ if ( SDL_InitSubSystem(flags) < 0 ) { return(-1); } /* Everything is initialized */ if ( !(flags & SDL_INIT_NOPARACHUTE) ) { SDL_InstallParachute(); } return(0); }
// shows the use of the pipeHandler. int main(int argc, char* argv[]) { pth_init(); // must be initialized before doing anything init_pipe_handler(); // register a FIFO pipe test_pipe with // depth 32, word-width 32. register_pipe("test_pipe",32,32,0); register_pipe("test_pipe_64",1,64,0); // write a integer to test_pipe. write_uint32("test_pipe",1); uint64_t tmp = 1; tmp = tmp | (tmp << 32); write_uint64("test_pipe_64",tmp); // read back and print integer. uint32_t v = read_uint32("test_pipe"); fprintf(stderr,"TEST: received uint32_t %d\n", v); uint64_t v64 = read_uint64("test_pipe_64"); fprintf(stderr,"TEST: received uint64_t %llu\n", v64); // write another integer write_uint32("test_pipe",1); // read back and print integer. v = read_uint32("test_pipe"); fprintf(stderr,"TEST: received %d\n", v); // close the handler. close_pipe_handler(); 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) { struct sigaction action; struct sigaction o_action; #ifdef MPISRC int myid; MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD,&myid); std::cout << "Running processes " << getpid() << std::endl; // system("sleep 20"); #endif #ifdef PTH // For debugging put interrupt here // Then on interrupt in console window type: // for gdb console: handle SIGUSR1 nostop print pass // for lldb: pro hand -p true -s false SIGUSR1 // Then hit continue int rc = pth_init(); if (!rc) { std::cerr << "couldn't start pth environment" << std::endl; } #endif #ifdef petsc PetscErrorCode err = PetscInitialize(&argc,&argv,(char *)0,help); CHKERRABORT(MPI_COMM_WORLD,err); #endif /* INTERRUPT HANDLER FOR GRACEFUL EXIT ON CTRL-C */ action.sa_handler = ctrlc; sigemptyset(&action.sa_mask); action.sa_flags = 0; if (sigaction(SIGTERM,&action,&o_action)) std::cerr << "interrupt handler failed" << std::endl; /* NORMAL SIMULATION */ if (argc < 2) { std::cerr << "# Need to specify input file" << std::endl; sim::abort(__LINE__,__FILE__,&std::cerr); } sim::blks.go(argv[1]); #ifdef petsc PetscFinalize(); #endif #ifdef PTH pth_exit(NULL); #endif #ifdef MPISRC MPI_Finalize(); #endif // //#ifdef DEBUG // extern FLT f1(int n, FLT x, FLT y); // // myblock.init(argv[1]); // myblock.minvrt_test(10,f1); // myblock.output(-2,text); // myblock.go(); //#endif // // //#ifdef FINDMAX // tri_basis b; // hp_mgrid hp; // b.initialize(4,5); // hp.in_mesh(argv[1],grid,1.0); // hp.tri_hp::allocate(&b); // hp.input(argv[1],tecplot); // hp.findintercept(CURV_MASK,&ydist); // //hp.findmaxy(CURV_MASK); // // hp.findmaxx(CURV_MASK); // //FLT avg[5]; // //hp.integrated_averages(avg); //#endif return 0; }
static int fixed_gcry_pth_init (void) { return pth_self ()? 0 : (pth_init () == FALSE) ? errno : 0; }
int main(int argc, char *argv[]) { struct sockaddr_in sar; struct protoent *pe; struct sockaddr_in peer_addr; socklen_t peer_len; int sr; int port; /* initialize scheduler */ pth_init(); signal(SIGPIPE, SIG_IGN); signal(SIGINT, myexit); signal(SIGTERM, myexit); /* argument line parsing */ if (argc != 2) { fprintf(stderr, "Usage: %s <port>\n", argv[0]); exit(1); } port = atoi(argv[1]); if (port <= 0 || port >= 65535) { fprintf(stderr, "Illegal port: %d\n", port); exit(1); } fprintf(stderr, "This is TEST_HTTPD, a Pth test using socket I/O.\n"); fprintf(stderr, "\n"); fprintf(stderr, "Multiple connections are accepted on the specified port.\n"); fprintf(stderr, "For each connection a separate thread is spawned which\n"); fprintf(stderr, "reads a HTTP request the socket and writes back a constant\n"); fprintf(stderr, "(and useless) HTTP response to the socket.\n"); fprintf(stderr, "Additionally a useless ticker thread awakens every 5s.\n"); fprintf(stderr, "Watch the average scheduler load the ticker displays.\n"); fprintf(stderr, "Hit CTRL-C for stopping this test.\n"); fprintf(stderr, "\n"); /* run a just for fun ticker thread */ attr = pth_attr_new(); pth_attr_set(attr, PTH_ATTR_NAME, "ticker"); pth_attr_set(attr, PTH_ATTR_JOINABLE, FALSE); pth_attr_set(attr, PTH_ATTR_STACK_SIZE, 64*1024); pth_spawn(attr, ticker, NULL); /* create TCP socket */ if ((pe = getprotobyname("tcp")) == NULL) { perror("getprotobyname"); exit(1); } if ((s = socket(AF_INET, SOCK_STREAM, pe->p_proto)) == -1) { perror("socket"); exit(1); } /* bind socket to port */ sar.sin_family = AF_INET; sar.sin_addr.s_addr = INADDR_ANY; sar.sin_port = htons(port); if (bind(s, (struct sockaddr *)&sar, sizeof(struct sockaddr_in)) == -1) { perror("socket"); exit(1); } /* start listening on the socket with a queue of 10 */ if (listen(s, REQ_MAX) == -1) { perror("listen"); exit(1); } /* finally loop for requests */ pth_attr_set(attr, PTH_ATTR_NAME, "handler"); fprintf(stderr, "listening on port %d (max %d simultaneous connections)\n", port, REQ_MAX); for (;;) { /* accept next connection */ peer_len = sizeof(peer_addr); if ((sr = pth_accept(s, (struct sockaddr *)&peer_addr, &peer_len)) == -1) { perror("accept"); pth_sleep(1); continue; } if (pth_ctrl(PTH_CTRL_GETTHREADS) >= REQ_MAX) { fprintf(stderr, "currently no more connections acceptable\n"); continue; } fprintf(stderr, "connection established (fd: %d, ip: %s, port: %d)\n", sr, inet_ntoa(peer_addr.sin_addr), ntohs(peer_addr.sin_port)); /* spawn new handling thread for connection */ pth_spawn(attr, handler, (void *)((long)sr)); } }
int main(int argc, char **argv) { struct sigaction action; struct sigaction o_action; #ifdef MPISRC int myid; MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD,&myid); #endif #ifdef PTH // For debugging put interrupt here // On interrupt type this into gdb console: handle SIGUSR1 nostop print pass // Then continue int rc = pth_init(); if (!rc) { std::cerr << "couldn't start pth environment\n"; } #endif /* INTERRUPT HANDLER FOR GRACEFUL EXIT ON CTRL-C */ action.sa_handler = ctrlc; sigemptyset(&action.sa_mask); action.sa_flags = 0; if (sigaction(SIGTERM,&action,&o_action)) printf("interrupt handler failed\n"); /* NORMAL SIMULATION */ if (argc < 2) { std::cerr << "# Need to specify input file" << std::endl; exit(1); } // sleep(20); sim::blks.go(argv[1]); #ifdef PTH pth_exit(NULL); #endif #ifdef MPISRC MPI_Finalize(); #endif // //#ifdef DEBUG // extern FLT f1(int n, FLT x, FLT y); // // myblock.init(argv[1]); // myblock.minvrt_test(10,f1); // myblock.output(-2,text); // myblock.go(); //#endif // // //#ifdef FINDMAX // tri_basis b; // hp_mgrid hp; // b.initialize(4,5); // hp.in_mesh(argv[1],grid,1.0); // hp.tri_hp::allocate(&b); // hp.input(argv[1],tecplot); // hp.findintercept(CURV_MASK,&ydist); // //hp.findmaxy(CURV_MASK); // // hp.findmaxx(CURV_MASK); // //FLT avg[5]; // //hp.integrated_averages(avg); // //printf("%e %e %e %e %e\n",avg[0],avg[1],avg[2],avg[3],avg[4]); //#endif }
int main(int argc, char *argv[]) { if (argc < 5) { return 1; } auto processNum = 4; auto threadNum = atoi(argv[4]); auto dtime = atol(argv[3]) * 1000; auto isPth = std::string(argv[2]) == "pth"; auto taskNum = atoi(argv[1]); long cycles = 0; switch(taskNum) { case 1: task = task1; break; case 2: task = task2; break; case 3: task = task3; break; } time_start(); for (auto i = 0; i < processNum; ++i) { if (fork() != 0) { continue; } if (isPth) { pth_init(); pth_attr_t attr = pth_attr_new(); pth_attr_set(attr, PTH_ATTR_NAME, "task"); pth_attr_set(attr, PTH_ATTR_STACK_SIZE, 64*1024); pth_attr_set(attr, PTH_ATTR_JOINABLE, true); pth_mutex_t mutex; pth_mutex_init(&mutex); pth_cond_init(&pthCond); while (time_stop() < dtime) { for (auto i = workingNum; i < threadNum; ++i) { ++workingNum; pth_spawn(attr, task, &isPth); } int rc; if ((rc = pth_mutex_acquire(&mutex, FALSE, NULL)) != 0) { std::cout << "pthread_mutex_lock " << rc << " " << strerror(rc) << std::endl; return 3; } if (workingNum == threadNum) { if ((rc = pth_cond_await(&pthCond, &mutex, NULL)) != 0) { std::cout << "pthread_cond_wait " << rc << " " << strerror(rc) << std::endl; return 3; } } if ((rc = pth_mutex_release(&mutex)) != 0) { std::cout << "pthread_mutex_unlock " << rc << " " << strerror(rc) << std::endl; return 3; } cycles += threadNum - workingNum; } } else { pthread_attr_t attr; pthread_attr_setstacksize(&attr, 64*1024); pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutexattr_t mutexattr; pthread_mutex_init(&mutex, &mutexattr); pthread_condattr_t condattr; pthread_cond_init(&cond, &condattr); pthread_t pid; while (time_stop() < dtime) { for (auto i = workingNum; i < threadNum; ++i) { ++workingNum; if (pthread_create(&pid, NULL, task, &isPth) == -1) { return 2; } } int rc; if ((rc = pthread_mutex_lock(&mutex)) != 0) { std::cout << "pthread_mutex_lock " << rc << " " << strerror(rc) << std::endl; return 3; } if (workingNum == threadNum) { if ((rc = pthread_cond_wait(&cond, &mutex)) != 0) { std::cout << "pthread_cond_wait " << rc << " " << strerror(rc) << std::endl; return 3; } } if ((rc = pthread_mutex_unlock(&mutex)) != 0) { std::cout << "pthread_mutex_unlock " << rc << " " << strerror(rc) << std::endl; return 3; } //pthread_join(pids.front(), NULL); //pids.pop_front(); cycles += threadNum - workingNum; } } std::cout << cycles << std::endl; return 0; } for (auto i = 0; i < processNum; ++i) { wait(NULL); } return 0; }
int main (int ac, char *ag[]) { int index; Queue < Server * >server; Server *s; Layer2Interface *l2; Layer3 *l3; #ifdef HAVE_EIBNETIPSERVER EIBnetServer *serv = 0; #endif memset (&arg, 0, sizeof (arg)); arg.addr = 0x0001; arg.errorlevel = LEVEL_WARNING; argp_parse (&argp, ac, ag, 0, &index, &arg); if (index > ac - 1) die ("url expected"); if (index < ac - 1) die ("unexpected parameter"); if (arg.port == 0 && arg.name == 0 && arg.serverip == 0) die ("No listen-address given"); signal (SIGPIPE, SIG_IGN); pth_init (); Trace t; t.SetTraceLevel (arg.tracelevel); t.SetErrorLevel (arg.errorlevel); /* if (getuid () == 0) ERRORPRINTF (&t, 0x37000001, 0, "EIBD should not run as root"); */ if(arg.eibnetname) { if(arg.eibnetname[0] == '=') arg.eibnetname++; if(strlen(arg.eibnetname) >= 30) die("EIBnetServer/IP name can't be longer then 30 char"); } 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); } l2 = Create (ag[index], arg.backendflags, &t); if (!l2 || !l2->init ()) die ("initialisation of the backend failed"); l3 = new Layer3 (l2, &t); if (arg.port) { s = new InetServer (l3, &t, arg.port); if (!s->init ()) die ("initialisation of the knxd inet protocol failed"); server.put (s); } if (arg.name) { s = new LocalServer (l3, &t, arg.name); if (!s->init ()) die ("initialisation of the knxd unix protocol failed"); server.put (s); } #ifdef HAVE_EIBNETIPSERVER serv = startServer (l3, &t, arg.eibnetname); #endif #ifdef HAVE_GROUPCACHE if (!CreateGroupCache (l3, &t, arg.groupcache)) die ("initialisation of the group cache failed"); #endif signal (SIGINT, SIG_IGN); signal (SIGTERM, SIG_IGN); int sig; 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 (&t, 0x27000002, 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); signal (SIGINT, SIG_DFL); signal (SIGTERM, SIG_DFL); while (!server.isempty ()) delete server.get (); #ifdef HAVE_EIBNETIPSERVER if (serv) delete serv; #endif #ifdef HAVE_GROUPCACHE DeleteGroupCache (); #endif delete l3; if (Cleanup) Cleanup (); if (arg.pidfile) unlink (arg.pidfile); pth_exit (0); return 0; }
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; }
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; }