int main(int argc, char * argv[]) { int ret, i; pthread_mutexattr_t ma; pthread_condattr_t ca; testdata_t * td; testdata_t alternativ; pid_t child_pr=0, chkpid; int status; pthread_t child_th; output_init(); /********** * Allocate space for the testdata structure */ /* Cannot mmap a file, we use an alternative method */ td = &alternativ; #if VERBOSE > 0 output("Testdata allocated in the process memory.\n"); #endif /********** * For each test scenario, initialize the attributes and other variables. */ for ( i=0; i< (sizeof(scenarii) / sizeof(scenarii[0])); i++) { #if VERBOSE > 1 output("[parent] Preparing attributes for: %s\n", scenarii[i].descr); #endif /* set / reset everything */ ret = pthread_mutexattr_init(&ma); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to initialize the mutex attribute object"); } ret = pthread_condattr_init(&ca); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to initialize the cond attribute object"); } /* Set the mutex type */ ret = pthread_mutexattr_settype(&ma, scenarii[i].m_type); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to set mutex type"); } #if VERBOSE > 1 output("[parent] Mutex type : %i\n", scenarii[i].m_type); #endif /********** * Initialize the testdata_t structure with the previously defined attributes */ /* Initialize the mutex */ ret = pthread_mutex_init(&(td->mtx), &ma); if (ret != 0) { UNRESOLVED(ret, "[parent] Mutex init failed"); } /* initialize the condvar */ ret = pthread_cond_init(&(td->cnd), &ca); if (ret != 0) { UNRESOLVED(ret, "[parent] Cond init failed"); } ret = pthread_mutexattr_gettype(&ma, &(td->type)); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to read mutex type attribute"); } td->ctrl=0; td->boolean=0; td->status=0; /********** * Proceed to the actual testing */ /* Create the child */ /* We are testing across two threads */ ret = pthread_create(&child_th, NULL, tf, td); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to create the child thread."); } /* Note: in case of an error, the child process will be alive for 10 sec then exit. */ /* Child is now running and will enter the timedwait */ /* We are waiting for this; and we have to monitor the status value as well. */ ret = pthread_mutex_lock(&(td->mtx)); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to lock the mutex"); } while ((td->ctrl == 0) && (td->status == 0)) { ret = pthread_mutex_unlock(&(td->mtx)); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to unlock the mutex"); } sched_yield(); ret = pthread_mutex_lock(&(td->mtx)); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to lock the mutex"); } } if ((td->ctrl == 2) && (td->status == 0)) /* Spurious wakeups hapenned */ { output("Spurious wake ups have happened. Maybe pthread_cond_wait is broken?\n"); td->ctrl = 1; } if (td->ctrl == 1)/* The child is inside the cond wait */ { ret = pthread_cond_signal(&(td->cnd)); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to signal the condition"); } /* Let the child leave the wait function if something is broken */ usleep(100); if (td->ctrl != 1) { FAILED("[parent] Child went out from pthread_cond_wait without locking the mutex"); } /* Allow the child to continue */ td->boolean=1; } /* Let the child do its checking */ ret = pthread_mutex_unlock(&(td->mtx)); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to unlock the mutex"); } /* Wait for the child to terminate */ ret = pthread_join(child_th, NULL); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to join the thread"); } /********** * Destroy the data */ ret = pthread_cond_destroy(&(td->cnd)); if (ret != 0) { UNRESOLVED(ret, "Failed to destroy the cond var"); } ret = pthread_mutex_destroy(&(td->mtx)); if (ret != 0) { UNRESOLVED(ret, "Failed to destroy the mutex"); } ret = pthread_condattr_destroy(&ca); if (ret != 0) { UNRESOLVED(ret, "Failed to destroy the cond var attribute object"); } ret = pthread_mutexattr_destroy(&ma); if (ret != 0) { UNRESOLVED(ret, "Failed to destroy the mutex attribute object"); } } /* Proceed to the next scenario */ #if VERBOSE > 0 output("Test passed\n"); #endif PASSED; return 0; }
int main (int argc, char *argv[]) { int ret=0; pthread_t child; output_init(); scenar_init(); for (sc=0; sc < NSCENAR; sc++) { #if VERBOSE > 0 output("-----\n"); output("Starting test with scenario (%i): %s\n", sc, scenarii[sc].descr); #endif if (scenarii[sc].detached != 0) /* only joinable threads can be detached */ { ret = pthread_attr_setdetachstate(&scenarii[sc].ta, PTHREAD_CREATE_JOINABLE); if (ret != 0) { UNRESOLVED(ret, "Unable to set detachstate back to joinable"); } } /* for detached scenarii, we will call pthread_detach from inside the thread. for joinable scenarii, we'll call pthread_detach from this thread. */ ret = pthread_create(&child, &scenarii[sc].ta, threaded, (scenarii[sc].detached != 0)?&ret:NULL); switch (scenarii[sc].result) { case 0: /* Operation was expected to succeed */ if (ret != 0) { UNRESOLVED(ret, "Failed to create this thread"); } break; case 1: /* Operation was expected to fail */ if (ret == 0) { UNRESOLVED(-1, "An error was expected but the thread creation succeeded"); } #if VERBOSE > 0 break; case 2: /* We did not know the expected result */ default: if (ret == 0) { output("Thread has been created successfully for this scenario\n"); } else { output("Thread creation failed with the error: %s\n", strerror(ret)); } #endif } if (ret == 0) /* The new thread is running */ { /* Just wait for the thread to terminate */ do { ret = sem_wait(&(scenarii[sc].sem)); } while ((ret == -1) && (errno == EINTR)); if (ret == -1) { UNRESOLVED(errno, "Failed to post the semaphore"); } /* If we must detach from here, we do it now. */ if (scenarii[sc].detached == 0) { ret = pthread_detach(child); if (ret != 0) { UNRESOLVED(ret, "Failed to detach the child thread."); } } /* now check that the thread resources are freed. */ ret = pthread_join(child, NULL); if (ret == 0) { FAILED("We were able to join a detached thread."); } /* Let the thread an additionnal row to cleanup */ sched_yield(); } } scenar_fini(); #if VERBOSE > 0 output("-----\n"); output("All test data destroyed\n"); output("Test PASSED\n"); #endif PASSED; }
/* main function */ int main() { int ret; long rts; struct sigaction sa; /* Initialize output */ output_init(); /* Test the RTS extension */ rts = sysconf(_SC_REALTIME_SIGNALS); if (rts < 0L) { UNTESTED("This test needs the RTS extension"); } /* Set the signal handler */ sa.sa_flags = SA_SIGINFO; sa.sa_sigaction = handler; ret = sigemptyset(&sa.sa_mask); if (ret != 0) { UNRESOLVED(ret, "Failed to empty signal set"); } /* Install the signal handler for SIGXCPU */ ret = sigaction(SIGNAL, &sa, 0); if (ret != 0) { UNRESOLVED(ret, "Failed to set signal handler"); } if (called) { FAILED("The signal handler has been called when no signal was raised"); } ret = raise(SIGNAL); if (ret != 0) { UNRESOLVED(ret, "Failed to raise SIGXCPU"); } if (!called) { FAILED("the sa_handler was not called whereas SA_SIGINFO was not set"); } /* Test passed */ #if VERBOSE > 0 output("Test passed\n"); #endif PASSED; }
/********* main ********/ int main(int argc, char *argv[]) { int ret, i, j; pthread_t th; pthread_mutexattr_t ma[4], *pma[5]; pma[4] = NULL; output_init(); /* Initialize the mutex attributes */ for (i = 0; i < 4; i++) { pma[i] = &ma[i]; if ((ret = pthread_mutexattr_init(pma[i]))) { UNRESOLVED(ret, "pthread_mutexattr_init"); } } #ifndef WITHOUT_XOPEN if ((ret = pthread_mutexattr_settype(pma[0], PTHREAD_MUTEX_NORMAL))) { UNRESOLVED(ret, "pthread_mutexattr_settype (normal)"); } if ((ret = pthread_mutexattr_settype(pma[1], PTHREAD_MUTEX_ERRORCHECK))) { UNRESOLVED(ret, "pthread_mutexattr_settype (errorcheck)"); } if ((ret = pthread_mutexattr_settype(pma[2], PTHREAD_MUTEX_RECURSIVE))) { UNRESOLVED(ret, "pthread_mutexattr_settype (recursive)"); } if ((ret = pthread_mutexattr_settype(pma[3], PTHREAD_MUTEX_DEFAULT))) { UNRESOLVED(ret, "pthread_mutexattr_settype (default)"); } #if VERBOSE >1 output ("Mutex attributes NORMAL,ERRORCHECK,RECURSIVE,DEFAULT initialized\n"); #endif #else #if VERBOSE > 0 output ("Mutex attributes NORMAL,ERRORCHECK,RECURSIVE,DEFAULT unavailable\n"); #endif #endif /* Initialize the 5 mutex */ for (i = 0; i < 5; i++) { if ((ret = pthread_mutex_init(&mtx[i], pma[i]))) { UNRESOLVED(ret, "pthread_mutex_init failed")} if ((ret = pthread_mutex_lock(&mtx[i]))) { UNRESOLVED(ret, "Initial pthread_mutex_lock failed")} } #if VERBOSE >1 output("Mutex objects are initialized\n"); #endif /* We don't need the mutex attribute objects anymore */ for (i = 0; i < 4; i++) { if ((ret = pthread_mutexattr_destroy(pma[i]))) { UNRESOLVED(ret, "pthread_mutexattr_destroy"); } } /* Initialize the semaphores */ if (sem_init(&semsig, 0, 1)) { UNRESOLVED(errno, "Sem init (1) failed"); } if (sem_init(&semstart, 0, 0)) { UNRESOLVED(errno, "Sem init (0) failed"); } #if VERBOSE >1 output("Going to create the child thread\n"); #endif /* Start the child */ if ((ret = pthread_create(&th, NULL, threaded, NULL))) { UNRESOLVED(ret, "Unable to create the thread"); } #if VERBOSE >1 output("Child created\n"); #endif /* Monitor the child */ for (i = 0; i < 5; i++) { /* We will do this for the 5 kinds of mutex */ if (sem_wait(&semstart)) { /* Wait for the thread to be ready */ UNRESOLVED(errno, "Unable to wait for the child"); } #if VERBOSE >1 output("Child is ready for iteration %i\n", i + 1); #endif ctrl = 0; /* init the ctrl var */ /* Send some signals to the thread */ for (j = 0; j < 10; j++) { if ((ret = sem_wait(&semsig))) { UNRESOLVED(errno, "Sem_wait failed from the signal handler"); } sched_yield(); /* Let the child do its stuff - might be a nanosleep here */ if ((ret = pthread_kill(th, SIGUSR1))) { UNRESOLVED(ret, "Pthread_kill failed"); } } #if VERBOSE >1 output("Child was killed 10 times\n"); #endif /* Now check the thread is still waiting for the mutex */ if (ctrl != 0) { FAILED ("Killed child passed the pthread_mutex_lock without owning it"); } #if VERBOSE >1 output("Control was OK\n"); #endif /* Unlock the mutex so the thread can proceed to the next one */ if ((ret = pthread_mutex_unlock(&mtx[i]))) { UNRESOLVED(ret, "Mutex unlock in main failed"); } } #if VERBOSE >1 output ("The test has passed, we are now going to clean up everything.\n"); #endif /* Clean everything: the test has passed */ if ((ret = pthread_join(th, NULL))) { UNRESOLVED(ret, "Unable to join the child"); } for (i = 0; i < 5; i++) { if ((ret = pthread_mutex_destroy(&mtx[i]))) { UNRESOLVED(ret, "Unable to finally destroy a mutex"); } } if (sem_destroy(&semstart)) { UNRESOLVED(errno, "Unable to destroy semstart semaphore"); } if (sem_destroy(&semsig)) { UNRESOLVED(errno, "Unable to destroy semsig semaphore"); } PASSED; }
static void init_machine(running_machine *machine) { mame_private *mame = machine->mame_data; int num; /* initialize basic can't-fail systems here */ cpuintrf_init(machine); sndintrf_init(machine); fileio_init(machine); config_init(machine); output_init(machine); state_init(machine); state_save_allow_registration(TRUE); drawgfx_init(machine); palette_init(machine); render_init(machine); ui_init(machine); generic_machine_init(machine); generic_video_init(machine); mame->rand_seed = 0x9d14abd7; /* initialize the base time (if not doing record/playback) */ if (!Machine->record_file && !Machine->playback_file) time(&mame->base_time); else mame->base_time = 0; /* init the osd layer */ if (osd_init(machine) != 0) fatalerror("osd_init failed"); /* initialize the input system */ /* this must be done before the input ports are initialized */ if (code_init(machine) != 0) fatalerror("code_init failed"); /* initialize the input ports for the game */ /* this must be done before memory_init in order to allow specifying */ /* callbacks based on input port tags */ if (input_port_init(machine, machine->gamedrv->ipt) != 0) fatalerror("input_port_init failed"); /* load the ROMs if we have some */ /* this must be done before memory_init in order to allocate memory regions */ rom_init(machine, machine->gamedrv->rom); /* initialize the timers and allocate a soft_reset timer */ /* this must be done before cpu_init so that CPU's can allocate timers */ timer_init(machine); mame->soft_reset_timer = timer_alloc(soft_reset); /* initialize the memory system for this game */ /* this must be done before cpu_init so that set_context can look up the opcode base */ if (memory_init(machine) != 0) fatalerror("memory_init failed"); /* now set up all the CPUs */ if (cpuexec_init(machine) != 0) fatalerror("cpuexec_init failed"); if (cpuint_init(machine) != 0) fatalerror("cpuint_init failed"); #ifdef MESS /* initialize the devices */ devices_init(machine); #endif /* start the save/load system */ saveload_init(machine); /* call the game driver's init function */ /* this is where decryption is done and memory maps are altered */ /* so this location in the init order is important */ ui_set_startup_text("Initializing...", TRUE); if (machine->gamedrv->driver_init != NULL) (*machine->gamedrv->driver_init)(machine); /* start the audio system */ if (sound_init(machine) != 0) fatalerror("sound_init failed"); /* start the video hardware */ if (video_init(machine) != 0) fatalerror("video_init failed"); /* start the cheat engine */ if (options.cheat) cheat_init(machine); /* call the driver's _START callbacks */ if (machine->drv->machine_start != NULL && (*machine->drv->machine_start)(machine) != 0) fatalerror("Unable to start machine emulation"); if (machine->drv->sound_start != NULL && (*machine->drv->sound_start)(machine) != 0) fatalerror("Unable to start sound emulation"); if (machine->drv->video_start != NULL && (*machine->drv->video_start)(machine) != 0) fatalerror("Unable to start video emulation"); /* free memory regions allocated with REGIONFLAG_DISPOSE (typically gfx roms) */ for (num = 0; num < MAX_MEMORY_REGIONS; num++) if (mame->mem_region[num].flags & ROMREGION_DISPOSE) free_memory_region(machine, num); #ifdef MAME_DEBUG /* initialize the debugger */ if (machine->debug_mode) mame_debug_init(machine); #endif }
/* We can now free the memory */ while (ptr_prev != NULL) { ptr = ptr_prev; ptr_prev = *(void **)ptr; free(ptr); } #if VERBOSE > 1 output("Memory is released\n"); #endif for (i=0; i<4; i++) pthread_mutexattr_destroy(pma[i]); for (i=0; i<5; i++) { if (retini[i] != 0 && retini[i] !=ENOMEM) { FAILED("Mutex init returned a wrong error code when no memory was left"); } if (retini[i] == 0) { #if VERBOSE > 0 output("Mutex initialization for attribute %d succeeds when memory is full\n", i); #endif if (retdtr[i] != 0) { UNRESOLVED(retdtr[i], "Mutex destroy failed on mutex inilialized under heavy loaded memory"); } } #if VERBOSE > 0 else { output("Mutex initialization for attribute %d fails with ENOMEM when memory is full\n", i); } #endif } PASSED; } #else /* WITHOUT_XOPEN */ int main(int argc, char * argv[]) { output_init(); UNTESTED("This test requires XSI features"); }
/* The main test function. */ int main( int argc, char * argv[] ) { int ret, i; pthread_t ch; /* Initialize output */ output_init(); ret = pthread_mutex_lock( &mtx ); if ( ret != 0 ) { UNRESOLVED( ret, "Failed to lock mutex" ); } ret = pthread_create( &ch, NULL, threaded, NULL ); if ( ret != 0 ) { UNRESOLVED( ret, "Failed to create a thread" ); } /* Register the handlers */ for ( i = 0; i < 10000; i++ ) { ret = pthread_atfork( prepare, parent, child ); if ( ret == ENOMEM ) { output( "ENOMEM returned after %i iterations\n", i ); break; } if ( ret != 0 ) { UNRESOLVED( ret, "Failed to register the atfork handlers" ); } } if ( ret == 0 ) { /* Let the child go on */ ret = pthread_mutex_unlock( &mtx ); if ( ret != 0 ) { UNRESOLVED( ret, "Failed to unlock mutex" ); } ret = pthread_join( ch, NULL ); if ( ret != 0 ) { UNRESOLVED( ret, "Failed to join the thread" ); } } /* Test passed */ #if VERBOSE > 0 output( "Test passed\n" ); #endif PASSED; }
/* The main test function. */ int main( int argc, char * argv[] ) { int ret, status; pid_t child, ctl; struct itimerval it; /* Initialize output */ output_init(); /* Create the interval timer */ it.it_interval.tv_sec = 15; it.it_interval.tv_usec = 0; it.it_value.tv_sec = 10; it.it_value.tv_usec = 0; ret = setitimer( ITIMER_REAL, &it, NULL ); if ( ret != 0 ) { UNRESOLVED( errno, "Failed to set interval timer for ITIMER_REAL" ); } ret = setitimer( ITIMER_VIRTUAL, &it, NULL ); if ( ret != 0 ) { UNRESOLVED( errno, "Failed to set interval timer for ITIMER_VIRTUAL" ); } ret = setitimer( ITIMER_PROF, &it, NULL ); if ( ret != 0 ) { UNRESOLVED( errno, "Failed to set interval timer for ITIMER_PROF" ); } #if VERBOSE > 0 output( "All interval timers are set.\n" ); #endif /* Create the child */ child = fork(); if ( child == ( pid_t ) - 1 ) { UNRESOLVED( errno, "Failed to fork" ); } /* child */ if ( child == ( pid_t ) 0 ) { /* Check we get the correct information: timer is reset */ ret = getitimer( ITIMER_REAL, &it ); if ( ret != 0 ) { UNRESOLVED( errno, "Failed to read ITIMER_REAL in child" ); } if ( it.it_value.tv_sec != 0 ) { FAILED( "Timer ITIMER_REAL was not reset in child" ); } ret = getitimer( ITIMER_VIRTUAL, &it ); if ( ret != 0 ) { UNRESOLVED( errno, "Failed to read ITIMER_VIRTUAL in child" ); } if ( it.it_value.tv_sec != 0 ) { FAILED( "Timer ITIMER_VIRTUAL was not reset in child" ); } ret = getitimer( ITIMER_PROF, &it ); if ( ret != 0 ) { UNRESOLVED( errno, "Failed to read ITIMER_PROF in child" ); } if ( it.it_value.tv_sec != 0 ) { FAILED( "Timer ITIMER_PROF was not reset in child" ); } /* We're done */ exit( PTS_PASS ); } /* Parent joins the child */ ctl = waitpid( child, &status, 0 ); if ( ctl != child ) { UNRESOLVED( errno, "Waitpid returned the wrong PID" ); } if ( ( !WIFEXITED( status ) ) || ( WEXITSTATUS( status ) != PTS_PASS ) ) { FAILED( "Child exited abnormally" ); } /* Test passed */ #if VERBOSE > 0 output( "Test passed\n" ); #endif PASSED; }
/* Main function */ int main (int argc, char * argv[]) { int ret; pthread_t th_work, th_sig1, th_sig2; thestruct arg1, arg2; output_init(); #ifdef WITH_SYNCHRO if (sem_init(&semsig1, 0, 1)) { UNRESOLVED(errno, "Semsig1 init"); } if (sem_init(&semsig2, 0, 1)) { UNRESOLVED(errno, "Semsig2 init"); } #endif if (sem_init(&semsync, 0, 0)) { UNRESOLVED(errno, "semsync init"); } if ((ret = pthread_create(&th_work, NULL, threaded, NULL))) { UNRESOLVED(ret, "Worker thread creation failed"); } arg1.thr = &th_work; arg2.thr = &th_work; arg1.sig = SIGUSR1; arg2.sig = SIGUSR2; #ifdef WITH_SYNCHRO arg1.sem = &semsig1; arg2.sem = &semsig2; #endif if ((ret = pthread_create(&th_sig1, NULL, sendsig, (void *)&arg1))) { UNRESOLVED(ret, "Signal 1 sender thread creation failed"); } if ((ret = pthread_create(&th_sig2, NULL, sendsig, (void *)&arg2))) { UNRESOLVED(ret, "Signal 2 sender thread creation failed"); } /* Let's wait for a while now */ sleep(1); /* Now stop the threads and join them */ do { do_it=0; } while (do_it); if ((ret = pthread_join(th_sig1, NULL))) { UNRESOLVED(ret, "Signal 1 sender thread join failed"); } if ((ret = pthread_join(th_sig2, NULL))) { UNRESOLVED(ret, "Signal 2 sender thread join failed"); } if (sem_post(&semsync)) { UNRESOLVED(errno, "could not post semsync"); } if ((ret = pthread_join(th_work, NULL))) { UNRESOLVED(ret, "Worker thread join failed"); } #if VERBOSE > 0 output("Test executed successfully.\n"); output(" %d condvars initialization and destruction were done.\n", count_ope); #ifdef WITH_SYNCHRO output(" %d signals were sent meanwhile.\n", count_sig); #endif #endif PASSED; }
/* The main test function. */ int main(void) { int ret, value; sem_t *sem; /* Initialize output */ output_init(); /* Create the semaphore */ sem = sem_open(SEM_NAME, O_CREAT | O_EXCL, 0777, 2); if (sem == SEM_FAILED && errno == EEXIST) { sem_unlink(SEM_NAME); sem = sem_open(SEM_NAME, O_CREAT | O_EXCL, 0777, 2); } if (sem == SEM_FAILED) { UNRESOLVED(errno, "Failed to create the semaphore"); } /* Use the semaphore to change its value. */ do { ret = sem_wait(sem); } while (ret != 0 && errno == EINTR); if (ret != 0) { UNRESOLVED(errno, "Failed to wait for the semaphore"); } /* Here, count is 1. Now, close the semaphore */ ret = sem_close(sem); if (ret != 0) { UNRESOLVED(errno, "Failed to close the semaphore"); } /* Open the semaphore again */ sem = sem_open(SEM_NAME, O_CREAT, 0777, 3); if (sem == SEM_FAILED) { UNRESOLVED(errno, "Failed to re-open the semaphore"); } /* Check current semaphore count */ ret = sem_getvalue(sem, &value); if (ret != 0) { UNRESOLVED(errno, "Failed to get semaphore value"); } if (value != 1) { output("Got value: %d\n", value); FAILED("The semaphore count has changed after sem_close"); } /* Now, we can destroy all */ ret = sem_close(sem); if (ret != 0) { UNRESOLVED(errno, "Failed to close the semaphore"); } ret = sem_unlink(SEM_NAME); if (ret != 0) { UNRESOLVED(errno, "Failed to unlink the semaphore"); } /* Test passed */ #if VERBOSE > 0 output("Test passed\n"); #endif PASSED; }
/** The main entry point of GridLAB-D @returns Exit codes XC_SUCCESS, etc. (see gridlabd.h) **/ int main(int argc, /**< the number entries on command-line argument list \p argv */ char *argv[]) /**< a list of pointers to the command-line arguments */ { char *pd1, *pd2; int i, pos=0; char *browser = getenv("GLBROWSER"); /* set the default timezone */ timestamp_set_tz(NULL); exec_clock(); /* initialize the wall clock */ realtime_starttime(); /* mark start */ /* set the process info */ global_process_id = getpid(); /* specify the default browser */ if ( browser!= NULL ) strncpy(global_browser,browser,sizeof(global_browser)-1); #if defined WIN32 && _DEBUG atexit(pause_at_exit); #endif #ifdef WIN32 kill_starthandler(); atexit(kill_stophandler); #endif /* capture the execdir */ strcpy(global_execname,argv[0]); strcpy(global_execdir,argv[0]); pd1 = strrchr(global_execdir,'/'); pd2 = strrchr(global_execdir,'\\'); if (pd1>pd2) *pd1='\0'; else if (pd2>pd1) *pd2='\0'; /* determine current working directory */ getcwd(global_workdir,1024); /* capture the command line */ for (i=0; i<argc; i++) { if (pos < (int)(sizeof(global_command_line)-strlen(argv[i]))) pos += sprintf(global_command_line+pos,"%s%s",pos>0?" ":"",argv[i]); } /* main initialization */ if (!output_init(argc,argv) || !exec_init()) exit(XC_INIERR); /* set thread count equal to processor count if not passed on command-line */ if (global_threadcount == 0) global_threadcount = processor_count(); output_verbose("detected %d processor(s)", processor_count()); output_verbose("using %d helper thread(s)", global_threadcount); /* process command line arguments */ if (cmdarg_load(argc,argv)==FAILED) { output_fatal("shutdown after command line rejected"); /* TROUBLESHOOT The command line is not valid and the system did not complete its startup procedure. Correct the problem with the command line and try again. */ exit(XC_ARGERR); } /* stitch clock */ global_clock = global_starttime; /* initialize scheduler */ sched_init(0); /* recheck threadcount in case user set it 0 */ if (global_threadcount == 0) { global_threadcount = processor_count(); output_verbose("using %d helper thread(s)", global_threadcount); } /* see if newer version is available */ if ( global_check_version ) check_version(1); /* setup the random number generator */ random_init(); /* pidfile */ if (strcmp(global_pidfile,"")!=0) { FILE *fp = fopen(global_pidfile,"w"); if (fp==NULL) { output_fatal("unable to create pidfile '%s'", global_pidfile); /* TROUBLESHOOT The system must allow creation of the process id file at the location indicated in the message. Create and/or modify access rights to the path for that file and try again. */ exit(XC_PRCERR); } #ifdef WIN32 #define getpid _getpid #endif fprintf(fp,"%d\n",getpid()); output_verbose("process id %d written to %s", getpid(), global_pidfile); fclose(fp); atexit(delete_pidfile); } /* do legal stuff */ #ifdef LEGAL_NOTICE if (strcmp(global_pidfile,"")==0 && legal_notice()==FAILED) exit(XC_USRERR); #endif /* start the processing environment */ output_verbose("load time: %d sec", realtime_runtime()); output_verbose("starting up %s environment", global_environment); if (environment_start(argc,argv)==FAILED) { output_fatal("environment startup failed: %s", strerror(errno)); /* TROUBLESHOOT The requested environment could not be started. This usually follows a more specific message regarding the startup problem. Follow the recommendation for the indicated problem. */ if ( exec_getexitcode()==XC_SUCCESS ) exec_setexitcode(XC_ENVERR); } /* save the model */ if (strcmp(global_savefile,"")!=0) { if (saveall(global_savefile)==FAILED) output_error("save to '%s' failed", global_savefile); } /* do module dumps */ if (global_dumpall!=FALSE) { output_verbose("dumping module data"); module_dumpall(); } /* KML output */ if (strcmp(global_kmlfile,"")!=0) kml_dump(global_kmlfile); /* terminate */ module_termall(); /* wrap up */ output_verbose("shutdown complete"); /* profile results */ if (global_profiler) { class_profiles(); module_profiles(); } #ifdef DUMP_SCHEDULES /* dump a copy of the schedules for reference */ schedule_dumpall("schedules.txt"); #endif /* restore locale */ locale_pop(); /* if pause enabled */ #ifndef WIN32 if (global_pauseatexit) { output_verbose("pausing at exit"); while (true) { sleep(5); } } #endif /* compute elapsed runtime */ output_verbose("elapsed runtime %d seconds", realtime_runtime()); output_verbose("exit code %d", exec_getexitcode()); exit(exec_getexitcode()); }
/* Main entry point. */ int main(int argc, char * argv[]) { int ret; int sc; pthread_mutexattr_t ma; testdata_t * td; testdata_t alternativ; pid_t child_pr=0, chkpid; int status; pthread_t child_th; /* Initialize output */ output_init(); /********** * Allocate space for the testdata structure */ /* Cannot mmap a file (or not interested in this), we use an alternative method */ td = &alternativ; #if VERBOSE > 0 output("Testdata allocated in the process memory.\n"); #endif /********** * For each test scenario, initialize the attributes and other variables. * Do the whole thing for each time to test. */ for ( sc=0; sc < NSCENAR ; sc++) { #if VERBOSE > 1 output("[parent] Preparing attributes for: %s\n", scenarii[sc].descr); #endif /* set / reset everything */ ret = pthread_mutexattr_init(&ma); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to initialize the mutex attribute object"); } #ifndef WITHOUT_XOPEN /* Set the mutex type */ ret = pthread_mutexattr_settype(&ma, scenarii[sc].m_type); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to set mutex type"); } #if VERBOSE > 1 output("[parent] Mutex type : %i\n", scenarii[sc].m_type); #endif #endif /********** * Initialize the testdata_t structure with the previously defined attributes */ /* Initialize the mutex */ ret = pthread_mutex_init(&(td->mtx), &ma); if (ret != 0) { UNRESOLVED(ret, "[parent] Mutex init failed"); } /* Initialize the other datas from the test structure */ td->status=0; /********** * Proceed to the actual testing */ /* Trylock the mutex twice before creating children */ ret = pthread_mutex_trylock(&(td->mtx)); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to trylock the mutex"); } ret = pthread_mutex_trylock(&(td->mtx)); #ifndef WITHOUT_XOPEN if (scenarii[sc].m_type == PTHREAD_MUTEX_RECURSIVE) { if (ret != 0) { UNRESOLVED(ret, "Failed to pthread_mutex_trylock() twice a recursive mutex"); } /* Unlock once so the count is "1" */ ret = pthread_mutex_unlock(&(td->mtx)); if (ret != 0) { UNRESOLVED(ret, "Failed to unlock the mutex"); } } else #endif if (ret == 0) { FAILED("Main was able to pthread_mutex_trylock() twice without error"); } /* Create the children */ /* We are testing across two threads */ ret = pthread_create(&child_th, NULL, tf, td); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to create the child thread."); } /* Wait for the child to terminate */ ret = pthread_join(child_th, NULL); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to join the thread"); } /* Check the child status */ if (td->status != EBUSY) { output("Unexpected return value: %d (%s)\n", td->status, strerror(td->status)); FAILED("pthread_mutex_trylock() did not return EBUSY in the child"); } /* Unlock the mutex */ ret= pthread_mutex_unlock(&(td->mtx)); if (ret != 0) { UNRESOLVED(ret, "Failed to unlock the mutex"); } /********** * Destroy the data */ ret = pthread_mutex_destroy(&(td->mtx)); if (ret != 0) { UNRESOLVED(ret, "Failed to destroy the mutex"); } ret = pthread_mutexattr_destroy(&ma); if (ret != 0) { UNRESOLVED(ret, "Failed to destroy the mutex attribute object"); } } /* Proceed to the next scenario */ #if VERBOSE > 0 output("Test passed\n"); #endif PASSED; return 0; }
/* The main test function. */ int main(void) { int ret, i; sem_t *sems; sem_t sem_last; long max; /* Initialize output */ output_init(); max = sysconf(_SC_SEM_NSEMS_MAX); if (max <= 0) { output("sysconf(_SC_SEM_NSEMS_MAX) = %ld\n", max); UNTESTED("There is no constraint on SEM_NSEMS_MAX"); } sems = (sem_t *) calloc(max, sizeof(sem_t)); if (sems == NULL) { UNRESOLVED(errno, "Failed to alloc space"); } for (i = 0; i < max; i++) { ret = sem_init(&sems[i], 0, 0); if (ret != 0) { output ("sem_init failed to initialize the %d nth semaphore.\n", i); output("Tryed to initialize %ld.\n", max); output("Error is %d: %s\n", errno, strerror(errno)); for (; i > 0; i--) sem_destroy(&sems[i - 1]); free(sems); PASSED; } } ret = sem_init(&sem_last, 0, 1); if (ret == 0) { FAILED ("We were able to sem_init more than SEM_NSEMS_MAX semaphores"); } if (errno != ENOSPC) { output("Error is %d: %s\n", errno, strerror(errno)); } for (i = 0; i < max; i++) sem_destroy(&sems[i]); free(sems); /* Test passed */ #if VERBOSE > 0 output("Test passed\n"); #endif PASSED; }
int main (int argc, char * argv[]) { int ret; pthread_mutexattr_t ma; pthread_condattr_t ca; int scenar; int child_count; pid_t pid; int status; pthread_attr_t ta; testdata_t alternativ; output_init(); /********** * Allocate space for the testdata structure */ /* Cannot mmap a file, we use an alternative method */ td = &alternativ; #if VERBOSE > 0 output("Testdata allocated in the process memory.\n"); #endif /* Initialize the thread attribute object */ ret = pthread_attr_init(&ta); if (ret != 0) { UNRESOLVED(ret, "[parent] Failed to initialize a thread attribute object"); } ret = pthread_attr_setstacksize(&ta, PTHREAD_STACK_MIN); if (ret != 0) { UNRESOLVED(ret, "[parent] Failed to set thread stack size"); } /* Do the test for each test scenario */ for (scenar=0; scenar < NSCENAR; scenar++) { /* set / reset everything */ ret = pthread_mutexattr_init(&ma); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to initialize the mutex attribute object"); } ret = pthread_condattr_init(&ca); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to initialize the cond attribute object"); } #ifndef WITHOUT_XOPEN /* Set the mutex type */ ret = pthread_mutexattr_settype(&ma, scenarii[scenar].m_type); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to set mutex type"); } #endif td->mtype=scenarii[scenar].m_type; /* initialize the condvar */ ret = pthread_cond_init(&td->cnd, &ca); if (ret != 0) { UNRESOLVED(ret, "Cond init failed"); } /* initialize the mutex */ ret = pthread_mutex_init(&td->mtx, &ma); if (ret != 0) { UNRESOLVED(ret, "Mutex init failed"); } /* Destroy the attributes */ ret = pthread_condattr_destroy(&ca); if (ret != 0) { UNRESOLVED(ret, "Failed to destroy the cond var attribute object"); } ret = pthread_mutexattr_destroy(&ma); if (ret != 0) { UNRESOLVED(ret, "Failed to destroy the mutex attribute object"); } #if VERBOSE > 2 output("[parent] Starting test %s\n", scenarii[scenar].descr); #endif td->count=0; /* Create all the children */ for (children.nb=0; children.nb<NCHILDREN; children.nb++) { ret = pthread_create(&(children.ch[children.nb].t), &ta, child, NULL); if (ret != 0) { UNRESOLVED(ret, "Failed to create enough threads"); } } #if VERBOSE > 4 output("[parent] Created %i children\n", NCHILDREN); #endif /* Make sure all children are waiting */ ret = pthread_mutex_lock(&td->mtx); if (ret != 0) { UNRESOLVED_KILLALL(ret, "Failed to lock mutex"); } child_count = td->count; while (child_count < NCHILDREN) { ret = pthread_mutex_unlock(&td->mtx); if (ret != 0) { UNRESOLVED_KILLALL(ret, "Failed to unlock mutex"); } sched_yield(); ret = pthread_mutex_lock(&td->mtx); if (ret != 0) { UNRESOLVED_KILLALL(ret, "Failed to lock mutex"); } child_count = td->count; } #if VERBOSE > 4 output("[parent] All children are waiting\n"); #endif /* Wakeup the children */ td->predicate=1; ret = pthread_cond_broadcast(&td->cnd); if (ret != 0) { UNRESOLVED_KILLALL(ret, "Failed to broadcast the condition."); } #if VERBOSE > 4 output("[parent] Condition was signaled\n"); #endif ret = pthread_mutex_unlock(&td->mtx); if (ret != 0) { UNRESOLVED_KILLALL(ret, "Failed to unlock mutex"); } #if VERBOSE > 4 output("[parent] Joining the children\n"); #endif /* join the children */ for (--children.nb; children.nb >= 0; children.nb--) { ret = pthread_join(children.ch[children.nb].t, NULL); if (ret != 0) { UNRESOLVED(ret, "Failed to join a child thread"); } } if (ret != 0) { output_fini(); exit(ret); } #if VERBOSE > 4 output("[parent] All children terminated\n"); #endif /* Destroy the datas */ ret = pthread_cond_destroy(&td->cnd); if (ret != 0) { UNRESOLVED(ret, "Failed to destroy the condvar"); } ret = pthread_mutex_destroy(&td->mtx); if (ret != 0) { UNRESOLVED(ret, "Failed to destroy the mutex"); } } /* Destroy global data */ ret = pthread_attr_destroy(&ta); if (ret != 0) { UNRESOLVED(ret, "Final thread attr destroy failed"); } /* exit */ PASSED; return 0; }
/* main routine */ int main(int argc, char * argv[]) { int ret, i; void * rval; struct sigaction sa; pthread_t threads[NSCENAR * SCALABILITY_FACTOR * FACTOR]; int rets[NSCENAR * SCALABILITY_FACTOR * FACTOR]; /* Initialize output */ output_init(); /* Initialize scenarii table */ scenar_init(); /* Register the signal handler for SIGUSR1 */ sigemptyset (&sa.sa_mask); sa.sa_flags = 0; sa.sa_handler = sighdl; if ((ret = sigaction (SIGUSR1, &sa, NULL))) { UNRESOLVED(ret, "Unable to register signal handler"); } if ((ret = sigaction (SIGALRM, &sa, NULL))) { UNRESOLVED(ret, "Unable to register signal handler"); } #if VERBOSE > 1 output("[parent] Signal handler registered\n"); #endif while (do_it) { /* Create all the threads */ for (i=0; i<SCALABILITY_FACTOR * FACTOR; i++) { for (sc=0; sc<NSCENAR; sc++) { /* Skip the alternative stack threads */ if (scenarii[sc].altstack != 0) continue; rets[i*NSCENAR + sc] = pthread_create(&threads[i*NSCENAR + sc], &scenarii[sc].ta, threaded, &threads[i*NSCENAR + sc]); switch (scenarii[sc].result) { case 0: /* Operation was expected to succeed */ if (rets[i*NSCENAR + sc] != 0) { UNRESOLVED(rets[i*NSCENAR + sc], "Failed to create this thread"); } break; case 1: /* Operation was expected to fail */ if (rets[i*NSCENAR + sc] == 0) { UNRESOLVED(-1, "An error was expected but the thread creation succeeded"); } break; case 2: /* We did not know the expected result */ default: #if VERBOSE > 5 if (rets[i*NSCENAR + sc] == 0) { output("Thread has been created successfully for this scenario\n"); } else { output("Thread creation failed with the error: %s\n", strerror(rets[i*NSCENAR + sc])); } #endif ; } if (rets[i*NSCENAR + sc] == 0) { /* Just wait for the thread to terminate */ do { ret = sem_wait(&scenarii[sc].sem); } while ((ret == -1) && (errno == EINTR)); if (ret == -1) { UNRESOLVED(errno, "Failed to wait for the semaphore"); } } } } /* Join all the joinable threads and check the value */ for (i=0; i<SCALABILITY_FACTOR * FACTOR; i++) { for (sc=0; sc<NSCENAR; sc++) { if ((scenarii[sc].altstack == 0) && (scenarii[sc].detached == 0) && (rets[i*NSCENAR + sc] == 0)) { ret = pthread_join(threads[i*NSCENAR + sc], &rval); if (ret != 0) { UNRESOLVED(ret, "Unable to join a thread"); } if (rval != (void *)&threads[i*NSCENAR + sc]) { output("arg: %p -- got %p -- NULL=%p\n", &threads[i*NSCENAR + sc], rval, NULL); FAILED("The retrieved error value is corrupted"); } } } } iterations++; } /* Destroy scenarii attributes */ scenar_fini(); /* Test passed */ output("pthread_exit stress test PASSED -- %llu iterations\n",iterations); PASSED; }
/* The main test function. */ int main(int argc, char * argv[]) { int ret, param, status; pid_t child, ctl; struct sched_param sp; /* Initialize output */ output_init(); /* Change process policy and parameters */ sp.sched_priority = param = sched_get_priority_max(POLICY); if (sp.sched_priority == -1) { UNRESOLVED(errno, "Failed to get max priority value"); } ret = sched_setscheduler(0, POLICY, &sp); if (ret == -1) { UNRESOLVED(errno, "Failed to change process scheduling policy"); } /* Create the child */ child = fork(); if (child == -1) { UNRESOLVED(errno, "Failed to fork"); } /* child */ if (child == 0) { /* Check the scheduling policy */ ret = sched_getscheduler(0); if (ret == -1) { UNRESOLVED(errno, "Failed to read scheduling policy in child"); } if (ret != POLICY) { FAILED("The scheduling policy was not inherited"); } ret = sched_getparam(0, &sp); if (ret != 0) { UNRESOLVED(errno, "Failed to read scheduling parameter in child"); } if (sp.sched_priority != param) { FAILED("The scheduling parameter was not inherited"); } /* We're done */ exit(PTS_PASS); } /* Parent joins the child */ ctl = waitpid(child, &status, 0); if (ctl != child) { UNRESOLVED(errno, "Waitpid returned the wrong PID"); } if (!WIFEXITED(status) || (WEXITSTATUS(status) != PTS_PASS)) { FAILED("Child exited abnormally"); } /* Test passed */ #if VERBOSE > 0 output("Test passed\n"); #endif PASSED; }
/* main function */ int main() { int ret; struct sigaction sa, save; /* Initialize output */ output_init(); /* Register the signal handler with signal */ if (SIG_ERR == signal(SIGNAL, handler_1)) { UNRESOLVED(errno, "Failed to register signal handler with signal()"); } /* As whether signal handler is restored to default when executed is implementation defined, we cannot check it was registered here. */ /* Set the new signal handler with sigaction*/ sa.sa_flags = 0; sa.sa_handler = handler_2; ret = sigemptyset(&sa.sa_mask); if (ret != 0) { UNRESOLVED(ret, "Failed to empty signal set"); } /* Install the signal handler for SIGXFSZ */ ret = sigaction(SIGNAL, &sa, &save); if (ret != 0) { UNRESOLVED(ret, "Failed to set signal handler"); } /* Check the signal handler has been set up */ ret = raise(SIGNAL); if (ret != 0) { UNRESOLVED(ret , "Failed to raise the signal"); } if (called != 0) { FAILED("handler not executed"); } /* Restore the first signal handler */ ret = sigaction(SIGNAL, &save, 0); if (ret != 0) { UNRESOLVED(ret, "Failed to set signal handler"); } /* Check the signal handler has been set up */ ret = raise(SIGNAL); if (ret != 0) { UNRESOLVED(ret , "Failed to raise the signal"); } if (called != 1) { FAILED("handler not executed"); } /* Test passed */ #if VERBOSE > 0 output("Test passed\n"); #endif PASSED; }
/* The main test function. */ int main( int argc, char * argv[] ) { int ret, error; sem_t * sem; #undef PATH_MAX #undef NAME_MAX long PATH_MAX, NAME_MAX; char * sem_name; /* Initialize output */ output_init(); /* Get PATH_MAX value */ PATH_MAX = pathconf( "/", _PC_PATH_MAX ); #if VERBOSE > 0 output( "PATH_MAX: %ld\n", PATH_MAX ); #endif if ( PATH_MAX > 0 ) { /* create a semaphore with a name longer than PATH_MAX */ sem_name = calloc( PATH_MAX + 1, sizeof( char ) ); if ( sem_name == NULL ) { UNRESOLVED( errno, "Failed to allocate space for the semaphore name" ); } /* the space was allocated */ sem_name[ 0 ] = '/'; sem_name[ PATH_MAX ] = '\0'; memset( sem_name + 1, 'P', PATH_MAX - 1 ); /* Create the semaphore */ sem = sem_open( sem_name, O_CREAT, 0777, 1 ); if ( sem != SEM_FAILED ) { ret = sem_unlink( sem_name ); error = errno; free( sem_name ); if ( ret == 0 ) { FAILED( "The function did not return ENAMETOOLONG as expected" ); } else { output( "Error was %d: %s\n", error, strerror( error ) ); FAILED( "Unable to unlink a semaphore which we just created" ); } } #if VERBOSE > 0 else { output( "Creation of the semaphore failed with error %d: %s\n", errno, strerror( errno ) ); } #endif } /* Get NAME_MAX value */ NAME_MAX = pathconf( "/", _PC_NAME_MAX ); #if VERBOSE > 0 output( "NAME_MAX: %ld\n", NAME_MAX ); #endif if ( NAME_MAX > 0 ) { /* create a semaphore with a name longer than NAME_MAX */ sem_name = calloc( NAME_MAX + 2, sizeof( char ) ); if ( sem_name == NULL ) { UNRESOLVED( errno, "Failed to allocate space for the semaphore name" ); } /* the space was allocated */ sem_name[ 0 ] = '/'; sem_name[ NAME_MAX + 1 ] = '\0'; memset( sem_name + 1, 'N', NAME_MAX ); /* Create the semaphore */ sem = sem_open( sem_name, O_CREAT , 0777, 1 ); if ( sem != SEM_FAILED ) { ret = sem_unlink( sem_name ); error = errno; free( sem_name ); if ( ret == 0 ) { FAILED( "The function did not return ENAMETOOLONG as expected" ); } else { output( "Error was %d: %s\n", error, strerror( error ) ); FAILED( "Unable to unlink a semaphore which we just created" ); } } #if VERBOSE > 0 else { output( "Creation of the semaphore failed with error %d: %s\n", errno, strerror( errno ) ); } #endif } /* Test passed */ #if VERBOSE > 0 output( "Test passed\n" ); #endif PASSED; }
int main(int argc, char * argv[]) { pid_t child; pthread_mutex_t mtx; pthread_mutexattr_t ma[4]; pthread_mutexattr_t *pma[5]; int ret=0; int i; int retini[5] = {-1,-1,-1,-1,-1}; int retdtr[5]= {-1,-1,-1,-1,-1}; void * ptr, *ptr_prev=NULL; int sz = 0; struct rlimit rl; int status=0; output_init(); child = fork(); if (child == (pid_t)-1) { UNRESOLVED(errno, "Fork failed"); } if (child != 0) /* We are the father */ { if (child != waitpid(child, &status, 0)) { UNRESOLVED(errno, "Waitpid failed"); } if (WIFSIGNALED(status)) { UNRESOLVED(WTERMSIG(status), "The child process was killed."); } if (WIFEXITED(status)) return WEXITSTATUS(status); UNRESOLVED(0, "Child process neither returned nor was killed."); } /* Only the child goes further */ /* We initialize the different mutex attributes */ for (i=0; (i<4) && (ret == 0); i++) { pma[i] = &ma[i]; ret = pthread_mutexattr_init(pma[i]); } if (ret) { UNRESOLVED(ret, "Mutex attribute init failed"); } pma[4] = (pthread_mutexattr_t *) NULL; if ((ret = pthread_mutexattr_settype(pma[0], PTHREAD_MUTEX_NORMAL))) { UNRESOLVED(ret, "Mutex attribute NORMAL failed"); } if ((ret = pthread_mutexattr_settype(pma[0], PTHREAD_MUTEX_DEFAULT))) { UNRESOLVED(ret, "Mutex attribute DEFAULT failed"); } if ((ret = pthread_mutexattr_settype(pma[0], PTHREAD_MUTEX_RECURSIVE))) { UNRESOLVED(ret, "Mutex attribute RECURSIVE failed"); } if ((ret = pthread_mutexattr_settype(pma[0], PTHREAD_MUTEX_ERRORCHECK))) { UNRESOLVED(ret, "Mutex attribute ERRORCHECK failed"); } sz = sysconf(_SC_PAGESIZE); /* Limit the process memory to a small value (64Mb for example). */ rl.rlim_max=1024*1024*64; rl.rlim_cur=1024*1024*64; if ((ret = setrlimit(RLIMIT_AS, &rl))) { UNRESOLVED(ret, "Memory limitation failed"); } #if VERBOSE > 1 output("Ready to take over memory. Page size is %d\n", sz); #endif /* Allocate all available memory */ while (1) { ptr = malloc(sz); /* Allocate one page of memory */ if (ptr == NULL) break; #if VERBOSE > 1 ret++; #endif *(void **)ptr = ptr_prev; /* Write into the allocated page */ ptr_prev = ptr; } #if VERBOSE > 1 output("%d pages were allocated before failure\n", ret); ret = 0; #endif while (1) { ptr = malloc(sizeof(void*)); /* Allocate every remaining bits of memory */ if (ptr == NULL) break; #if VERBOSE > 1 ret++; #endif *(void **)ptr = ptr_prev; /* Keep track of allocated memory */ ptr_prev = ptr; } #if VERBOSE > 1 output("%d additional spaces were allocated before failure\n", ret); ret = 0; #endif if (errno != ENOMEM) UNRESOLVED(errno, "Memory not full"); /* Now that memory is full, we try to initialize a mutex */ for (i=0; i<5; i++) { retini[i] = pthread_mutex_init(&mtx, pma[i]); if (!retini[i]) /* If mutex has been initialized, we destroy it */ retdtr[i] = pthread_mutex_destroy(&mtx); } /* We can now free the memory */ while (ptr_prev != NULL) { ptr = ptr_prev; ptr_prev = *(void **)ptr; free(ptr); } #if VERBOSE > 1 output("Memory is released\n"); #endif for (i=0; i<4; i++) pthread_mutexattr_destroy(pma[i]); for (i=0; i<5; i++) { if (retini[i] != 0 && retini[i] !=ENOMEM) { FAILED("Mutex init returned a wrong error code when no memory was left"); } if (retini[i] == 0) { #if VERBOSE > 0 output("Mutex initialization for attribute %d succeeds when memory is full\n", i); #endif if (retdtr[i] != 0) { UNRESOLVED(retdtr[i], "Mutex destroy failed on mutex inilialized under heavy loaded memory"); } } #if VERBOSE > 0 else { output("Mutex initialization for attribute %d fails with ENOMEM when memory is full\n", i); } #endif } PASSED; }
/* The main test function. */ int main( int argc, char * argv[] ) { int ret, status; pid_t child, ctl; timer_t tmr; struct sigevent se; struct itimerspec it; /* Initialize output */ output_init(); notified = 0; /* Create the timer */ se.sigev_notify = SIGEV_THREAD; se.sigev_signo = 0; se.sigev_value.sival_int = SIGUSR1; se.sigev_notify_function = ¬ification; se.sigev_notify_attributes = NULL; /* default detached thread */ ret = timer_create( CLOCK_REALTIME, &se, &tmr ); if ( ret != 0 ) { UNRESOLVED( errno, "Failed to create a timer" ); } /* Arm the timer */ it.it_interval.tv_nsec = 0; it.it_interval.tv_sec = 0; it.it_value.tv_sec = 0; it.it_value.tv_nsec = 500000000; /* 0.5 sec */ ret = timer_settime( tmr, 0, &it, NULL ); /* Create the child */ child = fork(); if ( child == ( pid_t ) - 1 ) { UNRESOLVED( errno, "Failed to fork" ); } /* child */ if ( child == ( pid_t ) 0 ) { sleep( 1 ); if ( notified != 0 ) { if ( notified == ( int ) getpid() ) { FAILED( "Per-Process Timer was inherited in child" ); } else { output( "Notification occured before the child forked" ); } } /* We're done */ exit( PTS_PASS ); } /* Parent joins the child */ ctl = waitpid( child, &status, 0 ); if ( ctl != child ) { UNRESOLVED( errno, "Waitpid returned the wrong PID" ); } if ( ( !WIFEXITED( status ) ) || ( WEXITSTATUS( status ) != PTS_PASS ) ) { FAILED( "Child exited abnormally" ); } if ( notified != ( int ) getpid() ) { output( "Notified value: %d\n", notified ); UNRESOLVED( -1, "No notification occured -- per process timers do not work?" ); } /* Test passed */ #if VERBOSE > 0 output( "Test passed\n" ); #endif PASSED; }
int main(int argc, char *argv[]) { int ret; pthread_t th_work, th_sig1, th_sig2; struct thestruct arg1, arg2; struct sigaction sa; output_init(); sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sa.sa_handler = sighdl1; ret = sigaction(SIGUSR1, &sa, NULL); if (ret == -1) UNRESOLVED(ret, "Unable to register signal handler1"); sa.sa_handler = sighdl2; ret = sigaction(SIGUSR2, &sa, NULL); if (ret == -1) UNRESOLVED(ret, "Unable to register signal handler2"); /* We prepare a signal set which includes SIGUSR1 and SIGUSR2 */ sigemptyset(&usersigs); ret = sigaddset(&usersigs, SIGUSR1); ret |= sigaddset(&usersigs, SIGUSR2); if (ret != 0) UNRESOLVED(ret, "Unable to add SIGUSR1 or 2 to a signal set"); /* We now block the signals SIGUSR1 and SIGUSR2 for this THREAD */ ret = pthread_sigmask(SIG_BLOCK, &usersigs, NULL); if (ret != 0) UNRESOLVED(ret, "Unable to block SIGUSR1 and SIGUSR2 " "in main thread"); #ifdef WITH_SYNCHRO if (sem_init(&semsig1, 0, 1)) UNRESOLVED(errno, "Semsig1 init"); if (sem_init(&semsig2, 0, 1)) UNRESOLVED(errno, "Semsig2 init"); #endif /* Initialize thread attribute objects */ scenar_init(); ret = pthread_create(&th_work, NULL, test, NULL); if (ret) UNRESOLVED(ret, "Worker thread creation failed"); arg1.sig = SIGUSR1; arg2.sig = SIGUSR2; #ifdef WITH_SYNCHRO arg1.sem = &semsig1; arg2.sem = &semsig2; #endif ret = pthread_create(&th_sig1, NULL, sendsig, (void *) &arg1); if (ret) UNRESOLVED(ret, "Signal 1 sender thread creation failed"); ret = pthread_create(&th_sig2, NULL, sendsig, (void *) &arg2); if (ret) UNRESOLVED(ret, "Signal 2 sender thread creation failed"); /* Now stop the threads and join them */ do { do_it1 = 0; } while (do_it1); sleep(1); do { do_it2 = 0; } while (do_it2); ret = pthread_join(th_sig1, NULL); if (ret) UNRESOLVED(ret, "Signal 1 sender thread join failed"); ret = pthread_join(th_sig2, NULL); if (ret) UNRESOLVED(ret, "Signal 2 sender thread join failed"); ret = pthread_join(th_work, NULL); if (ret) UNRESOLVED(ret, "Worker thread join failed"); scenar_fini(); #if VERBOSE > 0 output("Test executed successfully.\n"); output(" %d pthread_join calls.\n", count_ope); #ifdef WITH_SYNCHRO output(" %d signals were sent meanwhile.\n", count_sig); #endif #endif PASSED; }
int main(void) { int ret, j; unsigned int i; pthread_mutexattr_t ma; pthread_condattr_t ca; pthread_t th[NTHREADS]; int loc_started, loc_stopped; long altclk_ok, pshared_ok; struct { char altclk; /* Want to use alternative clock */ char pshared; /* Want to use process-shared primitives */ int type; /* mutex type */ char *descr; /* Description of the case */ } scenar[] = { { 0, 0, PTHREAD_MUTEX_RECURSIVE, "Recursive mutex" }, { 0, 0, PTHREAD_MUTEX_ERRORCHECK, "Errorcheck mutex" }, #ifdef USE_ALTCLK { 1, 0, PTHREAD_MUTEX_RECURSIVE, "Recursive mutex + altclock cond" }, { 1, 0, PTHREAD_MUTEX_ERRORCHECK, "Errorcheck mutex + altclock cond" }, { 1, 1, PTHREAD_MUTEX_RECURSIVE, "Recursive pshared mutex + altclock cond" }, { 1, 1, PTHREAD_MUTEX_ERRORCHECK, "Errorcheck pshared mutex + altclock cond" }, #endif { 0, 1, PTHREAD_MUTEX_RECURSIVE, "Recursive pshared mutex" }, { 0, 1, PTHREAD_MUTEX_ERRORCHECK, "Errorcheck pshared mutex" }, }; output_init(); /* Initialize the constants */ altclk_ok = sysconf(_SC_CLOCK_SELECTION); if (altclk_ok > 0) altclk_ok = sysconf(_SC_MONOTONIC_CLOCK); #ifndef USE_ALTCLK if (altclk_ok > 0) output("Implementation supports the MONOTONIC CLOCK " "but option is disabled in test.\n"); #endif pshared_ok = sysconf(_SC_THREAD_PROCESS_SHARED); #if VERBOSE > 0 output("Test starting\n"); output(" Process-shared primitive %s be tested\n", (pshared_ok > 0) ? "will" : "won't"); output(" Alternative clock for cond %s be tested\n", (altclk_ok > 0) ? "will" : "won't"); #endif for (i = 0; i < (sizeof(scenar) / sizeof(scenar[0])); i++) { #if VERBOSE > 1 output("Starting test for %s\n", scenar[i].descr); #endif /* Initialize the data structure */ ret = pthread_mutexattr_init(&ma); if (ret != 0) UNRESOLVED(ret, "Mutex attribute object init failed"); ret = pthread_mutexattr_settype(&ma, scenar[i].type); if (ret != 0) UNRESOLVED(ret, "Unable to set mutex type"); if ((pshared_ok > 0) && (scenar[i].pshared != 0)) { ret = pthread_mutexattr_setpshared(&ma, PTHREAD_PROCESS_SHARED); if (ret != 0) UNRESOLVED(ret, "Unable to set mutex process-shared"); } ret = pthread_condattr_init(&ca); if (ret != 0) UNRESOLVED(ret, "Cond attribute object init failed"); if ((pshared_ok > 0) && (scenar[i].pshared != 0)) { ret = pthread_condattr_setpshared(&ca, PTHREAD_PROCESS_SHARED); if (ret != 0) UNRESOLVED(ret, "Unable to set cond process-shared"); } #ifdef USE_ALTCLK if ((altclk_ok > 0) && (scenar[i].altclk != 0)) { ret = pthread_condattr_setclock(&ca, CLOCK_MONOTONIC); if (ret != 0) UNRESOLVED(ret, "Unable to set alternative (monotonic) clock for cond"); } #endif ret = pthread_mutex_init(&(data.mtx1), &ma); if (ret != 0) UNRESOLVED(ret, "Unable to init mutex 1"); ret = pthread_mutex_init(&(data.mtx2), &ma); if (ret != 0) UNRESOLVED(ret, "Unable to init mutex 2"); ret = pthread_cond_init(&(data.cnd), &ca); if (ret != 0) UNRESOLVED(ret, "Unable to initialize condvar"); data.boolcnd = 0; ret = pthread_mutexattr_gettype(&ma, &(data.type)); if (ret != 0) UNRESOLVED(ret, "Unable to get type from mutex attr"); #ifdef USE_ALTCLK ret = pthread_condattr_getclock(&ca, &(data.cid)); if (ret != 0) UNRESOLVED(ret, "Unable to get clock ID from cond attr"); #else data.cid = CLOCK_REALTIME; #endif data.started = 0; data.stopped = 0; /* Start the threads */ #if VERBOSE > 1 output("Initialization OK, starting threads\n"); #endif for (j = 0; j < NTHREADS; j++) { ret = pthread_create(&th[j], NULL, threaded, (void *)(long)(j & 1)); if (ret != 0) UNRESOLVED(ret, "Thread creation failed"); } /* Wait for the threads to be started */ do { ret = pthread_mutex_lock(&(data.mtx1)); if (ret != 0) UNRESOLVED(ret, "Unable to lock m1 in parent"); loc_started = data.started; ret = pthread_mutex_unlock(&(data.mtx1)); if (ret != 0) UNRESOLVED(ret, "Unable to unlock m1 in parent"); } while (loc_started < NTHREADS); /* Broadcast the condition until all threads are terminated */ data.boolcnd = 1; do { ret = pthread_cond_broadcast(&(data.cnd)); if (ret != 0) UNRESOLVED(ret, "Unable to broadcast cnd"); sched_yield(); ret = pthread_mutex_lock(&(data.mtx2)); if (ret != 0) UNRESOLVED(ret, "Unable to lock m2 in parent"); loc_stopped = data.stopped; ret = pthread_mutex_unlock(&(data.mtx2)); if (ret != 0) UNRESOLVED(ret, "Unable to unlock m2 in parent"); } while (loc_stopped < NTHREADS); /* Join the threads */ for (j = 0; j < NTHREADS; j++) { ret = pthread_join(th[j], NULL); if (ret != 0) UNRESOLVED(ret, "Thread join failed"); } #if VERBOSE > 1 output("Test passed for %s\n", scenar[i].descr); #endif /* Destroy data */ ret = pthread_cond_destroy(&(data.cnd)); if (ret != 0) UNRESOLVED(ret, "Cond destroy failed"); ret = pthread_mutex_destroy(&(data.mtx1)); if (ret != 0) UNRESOLVED(ret, "Mutex 1 destroy failed"); ret = pthread_mutex_destroy(&(data.mtx2)); if (ret != 0) UNRESOLVED(ret, "Mutex 2 destroy failed"); ret = pthread_condattr_destroy(&ca); if (ret != 0) UNRESOLVED(ret, "Cond attribute destroy failed"); ret = pthread_mutexattr_destroy(&ma); if (ret != 0) UNRESOLVED(ret, "Mutex attr destroy failed"); } /* Proceed to next case */ PASSED; }
/* Main routine */ int main (int argc, char *argv[]) { int ret=0; int ctl=0; void * rval; pthread_t child; int i,j; output_init(); scenar_init(); for (j=0; j<3; j++) { ret = pthread_key_create(&tld[j], destructor); if (ret != 0) { UNRESOLVED(ret, "Failed to create a TLD key"); } } for (i=0; i < NSCENAR; i++) { if (scenarii[i].detached == 0) { #if VERBOSE > 0 output("-----\n"); output("Starting test with scenario (%i): %s\n", i, scenarii[i].descr); #endif ctl=0; ret = pthread_create(&child, &scenarii[i].ta, threaded, &ctl); switch (scenarii[i].result) { case 0: /* Operation was expected to succeed */ if (ret != 0) { UNRESOLVED(ret, "Failed to create this thread"); } break; case 1: /* Operation was expected to fail */ if (ret == 0) { UNRESOLVED(-1, "An error was expected but the thread creation succeeded"); } break; case 2: /* We did not know the expected result */ default: #if VERBOSE > 0 if (ret == 0) { output("Thread has been created successfully for this scenario\n"); } else { output("Thread creation failed with the error: %s\n", strerror(ret)); } #endif } if (ret == 0) /* The new thread is running */ { ret = pthread_join(child, &rval); if (ret != 0) { UNRESOLVED(ret, "Unable to join a thread"); } if (rval != (NULL+1)) { FAILED("pthread_join() did not retrieve the pthread_exit() param"); } if (atctl != 0) { FAILED("The function registered with atexit() executed"); } if (ctl != 3) { FAILED("The TLD destructors were not called"); } } } } for (j=0; j<3; j++) { ret = pthread_key_delete(tld[j]); if (ret != 0) { UNRESOLVED(ret, "Failed to delete a TLD key"); } } scenar_fini(); #if VERBOSE > 0 output("-----\n"); output("All test data destroyed\n"); output("Test PASSED\n"); #endif PASSED; }
/* Destroy data */ ret = pthread_cond_destroy(&(data.cnd)); if (ret != 0) UNRESOLVED(ret, "Cond destroy failed"); ret = pthread_mutex_destroy(&(data.mtx1)); if (ret != 0) UNRESOLVED(ret, "Mutex 1 destroy failed"); ret = pthread_mutex_destroy(&(data.mtx2)); if (ret != 0) UNRESOLVED(ret, "Mutex 2 destroy failed"); ret = pthread_condattr_destroy(&ca); if (ret != 0) UNRESOLVED(ret, "Cond attribute destroy failed"); ret = pthread_mutexattr_destroy(&ma); if (ret != 0) UNRESOLVED(ret, "Mutex attr destroy failed"); } /* Proceed to next case */ PASSED; } #else /* WITHOUT_XOPEN */ int main(void) { output_init(); UNTESTED("This test requires XSI features"); }
/* Main entry point. */ int main(int argc, char * argv[]) { int ret; int sc; pthread_mutexattr_t ma; testdata_t * td; testdata_t alternativ; int do_fork; pid_t child_pr=0, chkpid; int status; pthread_t child_th; long pshared, mf; /* Initialize output */ output_init(); /* Initialize the timeout */ alarm(30); /* Test system abilities */ pshared = sysconf(_SC_THREAD_PROCESS_SHARED); mf =sysconf(_SC_MAPPED_FILES); #if VERBOSE > 0 output("Test starting\n"); output("System abilities:\n"); output(" TSH : %li\n", pshared); output(" MF : %li\n", mf); if ((mf < 0) || (pshared < 0)) output("Process-shared attributes won't be tested\n"); #endif #ifdef WITHOUT_XOPEN #if VERBOSE > 0 output("As XSI extension is disabled, we won't test the feature across process\n"); #endif mf = -1; #endif /********** * Allocate space for the testdata structure */ if (mf < 0) { /* Cannot mmap a file (or not interested in this), we use an alternative method */ td = &alternativ; pshared = -1; /* We won't do this testing anyway */ #if VERBOSE > 0 output("Testdata allocated in the process memory.\n"); #endif } #ifndef WITHOUT_XOPEN else { /* We will place the test data in a mmaped file */ char filename[] = "/tmp/mutex_trylock_4-2-XXXXXX"; size_t sz; void * mmaped; int fd; char * tmp; /* We now create the temp files */ fd = mkstemp(filename); if (fd == -1) { UNRESOLVED(errno, "Temporary file could not be created"); } /* and make sure the file will be deleted when closed */ unlink(filename); #if VERBOSE > 1 output("Temp file created (%s).\n", filename); #endif sz= (size_t)sysconf(_SC_PAGESIZE); tmp = calloc(1, sz); if (tmp == NULL) { UNRESOLVED(errno, "Memory allocation failed"); } /* Write the data to the file. */ if (write (fd, tmp, sz) != (ssize_t) sz) { UNRESOLVED(sz, "Writting to the file failed"); } free(tmp); /* Now we can map the file in memory */ mmaped = mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (mmaped == MAP_FAILED) { UNRESOLVED(errno, "mmap failed"); } td = (testdata_t *) mmaped; /* Our datatest structure is now in shared memory */ #if VERBOSE > 1 output("Testdata allocated in shared memory.\n"); #endif } #endif /********** * For each test scenario, initialize the attributes and other variables. * Do the whole thing for each time to test. */ for (sc=0; sc < NSCENAR ; sc++) { #if VERBOSE > 1 output("[parent] Preparing attributes for: %s\n", scenarii[sc].descr); #endif /* set / reset everything */ do_fork=0; ret = pthread_mutexattr_init(&ma); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to initialize the mutex attribute object"); } #ifndef WITHOUT_XOPEN /* Set the mutex type */ ret = pthread_mutexattr_settype(&ma, scenarii[sc].m_type); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to set mutex type"); } #if VERBOSE > 1 output("[parent] Mutex type : %i\n", scenarii[sc].m_type); #endif #endif /* Set the pshared attributes, if supported */ if ((pshared > 0) && (scenarii[sc].m_pshared != 0)) { ret = pthread_mutexattr_setpshared(&ma, PTHREAD_PROCESS_SHARED); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to set the mutex process-shared"); } #if VERBOSE > 1 output("[parent] Mutex is process-shared\n"); #endif } #if VERBOSE > 1 else { output("[parent] Mutex is process-private\n"); } #endif /* Tell whether the test will be across processes */ if ((pshared > 0) && (scenarii[sc].fork != 0)) { do_fork = 1; #if VERBOSE > 1 output("[parent] Child will be a new process\n"); #endif } #if VERBOSE > 1 else { output("[parent] Child will be a new thread\n"); } #endif /********** * Initialize the testdata_t structure with the previously defined attributes */ /* Initialize the mutex */ ret = pthread_mutex_init(&(td->mtx), &ma); if (ret != 0) { UNRESOLVED(ret, "[parent] Mutex init failed"); } /* Initialize the other datas from the test structure */ td->status=0; /********** * Proceed to the actual testing */ /* Trylock the mutex twice before creating children */ ret = pthread_mutex_lock(&(td->mtx)); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to lock the mutex"); } /* Create the children */ if (do_fork != 0) { /* We are testing across processes */ child_pr = fork(); if (child_pr == -1) { UNRESOLVED(errno, "[parent] Fork failed"); } if (child_pr == 0) { #if VERBOSE > 3 output("[child] Child process is starting...\n"); #endif if (tf((void *)td) != NULL) { UNRESOLVED(-1, "[child] Got an unexpected return value from test function"); } else { /* We cannot use the PASSED macro here since it would terminate the output */ exit (0); } } /* Only the parent process goes further */ } else /* do_fork == 0 */ { /* We are testing across two threads */ ret = pthread_create(&child_th, NULL, tf, td); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to create the child thread."); } } /* Wait for the child to terminate */ if (do_fork != 0) { /* We were testing across processes */ ret = 0; chkpid = waitpid(child_pr, &status, 0); if (chkpid != child_pr) { output("Expected pid: %i. Got %i\n", (int)child_pr, (int)chkpid); UNRESOLVED(errno, "Waitpid failed"); } if (WIFSIGNALED(status)) { output("Child process killed with signal %d\n",WTERMSIG(status)); UNRESOLVED(-1 , "Child process was killed"); } if (WIFEXITED(status)) { ret = WEXITSTATUS(status); } else { UNRESOLVED(-1, "Child process was neither killed nor exited"); } if (ret != 0) { exit(ret); /* Output has already been closed in child */ } } else /* child was a thread */ { ret = pthread_join(child_th, NULL); if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to join the thread"); } } /* Check the child status */ if (td->status != EBUSY) { output("Unexpected return value: %d (%s)\n", td->status, strerror(td->status)); FAILED("pthread_mutex_trylock() did not return EBUSY in the child"); } /* Unlock the mutex */ ret= pthread_mutex_unlock(&(td->mtx)); if (ret != 0) { UNRESOLVED(ret, "Failed to unlock the mutex"); } /********** * Destroy the data */ ret = pthread_mutex_destroy(&(td->mtx)); if (ret != 0) { UNRESOLVED(ret, "Failed to destroy the mutex"); } ret = pthread_mutexattr_destroy(&ma); if (ret != 0) { UNRESOLVED(ret, "Failed to destroy the mutex attribute object"); } } /* Proceed to the next scenario */ #if VERBOSE > 0 output("Test passed\n"); #endif PASSED; }
int main(void) { int ret, status; pid_t child, ctl; long ctp, ctt; clockid_t clp, clt; struct timespec tp; output_init(); ctp = sysconf(_SC_CPUTIME); ctt = sysconf(_SC_THREAD_CPUTIME); if ((ctp == -1) && (ctt == -1)) { UNTESTED ("The testcase needs CPUTIME or THREAD_CPUTIME support"); } #if VERBOSE > 0 output("System abilities:\n"); output(" _POSIX_CPUTIME : %ld\n", ctp); output(" _POSIX_THREAD_CPUTIME : %ld\n", ctt); #endif if (ctp > 0) { ret = clock_getcpuclockid(0, &clp); if (ret != 0) { UNRESOLVED(ret, "Unable to get cpu-time clock id of the process"); } do { ret = clock_gettime(clp, &tp); if (ret != 0) { UNRESOLVED(errno, "Failed to read CPU time clock"); } } while (tp.tv_sec < 1); } if (ctt > 0) { ret = pthread_getcpuclockid(pthread_self(), &clt); if (ret != 0) { UNRESOLVED(ret, "Unable to get cpu-time clock id of the thread"); } do { ret = clock_gettime(clt, &tp); if (ret != 0) { UNRESOLVED(errno, "Failed to read thread CPU time clock"); } } while (tp.tv_sec < 1); } /* Create the child */ child = fork(); if (child == -1) { UNRESOLVED(errno, "Failed to fork"); } /* child */ if (child == 0) { if (ctp > 0) { ret = clock_getcpuclockid(0, &clp); if (ret != 0) { UNRESOLVED(ret, "Unable to get cpu-time clock id of the process"); } ret = clock_gettime(clp, &tp); if (ret != 0) { UNRESOLVED(errno, "Failed to read CPU time clock"); } if (tp.tv_sec > 0) { FAILED ("The process CPU-time clock was not reset in child\n"); } } if (ctt > 0) { ret = pthread_getcpuclockid(pthread_self(), &clt); if (ret != 0) { UNRESOLVED(ret, "Unable to get cpu-time clock id of the thread"); } ret = clock_gettime(clt, &tp); if (ret != 0) { UNRESOLVED(errno, "Failed to read thread CPU time clock"); } if (tp.tv_sec > 0) { FAILED ("The thread CPU-time clock was not reset in child\n"); } } exit(PTS_PASS); } /* Parent joins the child */ ctl = waitpid(child, &status, 0); if (ctl != child) { UNRESOLVED(errno, "Waitpid returned the wrong PID"); } if (!WIFEXITED(status) || (WEXITSTATUS(status) != PTS_PASS)) { FAILED("Child exited abnormally"); } #if VERBOSE > 0 output("Test passed\n"); #endif PASSED; }
/* Main function */ int main ( int argc, char * argv[] ) { int ret; pthread_t th_work, th_sig1, th_sig2, me; thestruct arg1, arg2; struct sigaction sa; /* Initialize output routine */ output_init(); /* We need to register the signal handlers for the PROCESS */ sigemptyset ( &sa.sa_mask ); sa.sa_flags = 0; sa.sa_handler = sighdl1; if ( ( ret = sigaction ( SIGUSR1, &sa, NULL ) ) ) { UNRESOLVED( ret, "Unable to register signal handler1" ); } sa.sa_handler = sighdl2; if ( ( ret = sigaction ( SIGUSR2, &sa, NULL ) ) ) { UNRESOLVED( ret, "Unable to register signal handler2" ); } /* We prepare a signal set which includes SIGUSR1 and SIGUSR2 */ sigemptyset( &usersigs ); ret = sigaddset( &usersigs, SIGUSR1 ); ret |= sigaddset( &usersigs, SIGUSR2 ); if ( ret != 0 ) { UNRESOLVED( ret, "Unable to add SIGUSR1 or 2 to a signal set" ); } /* We now block the signals SIGUSR1 and SIGUSR2 for this THREAD */ ret = pthread_sigmask( SIG_BLOCK, &usersigs, NULL ); if ( ret != 0 ) { UNRESOLVED( ret, "Unable to block SIGUSR1 and SIGUSR2 in main thread" ); } #ifdef WITH_SYNCHRO if ( sem_init( &semsig1, 0, 1 ) ) { UNRESOLVED( errno, "Semsig1 init" ); } if ( sem_init( &semsig2, 0, 1 ) ) { UNRESOLVED( errno, "Semsig2 init" ); } #endif me = pthread_self(); if ( ( ret = pthread_create( &th_work, NULL, test, &me ) ) ) { UNRESOLVED( ret, "Worker thread creation failed" ); } arg1.sig = SIGUSR1; arg2.sig = SIGUSR2; #ifdef WITH_SYNCHRO arg1.sem = &semsig1; arg2.sem = &semsig2; #endif if ( ( ret = pthread_create( &th_sig1, NULL, sendsig, ( void * ) & arg1 ) ) ) { UNRESOLVED( ret, "Signal 1 sender thread creation failed" ); } if ( ( ret = pthread_create( &th_sig2, NULL, sendsig, ( void * ) & arg2 ) ) ) { UNRESOLVED( ret, "Signal 2 sender thread creation failed" ); } /* Let's wait for a while now */ sleep( 1 ); /* Now stop the threads and join them */ do { do_it = 0; } while ( do_it ); if ( ( ret = pthread_join( th_sig1, NULL ) ) ) { UNRESOLVED( ret, "Signal 1 sender thread join failed" ); } if ( ( ret = pthread_join( th_sig2, NULL ) ) ) { UNRESOLVED( ret, "Signal 2 sender thread join failed" ); } if ( ( ret = pthread_join( th_work, NULL ) ) ) { UNRESOLVED( ret, "Worker thread join failed" ); } #if VERBOSE > 0 output( "Test executed successfully.\n" ); output( " %d operations.\n", count_ope ); #ifdef WITH_SYNCHRO output( " %d signals were sent meanwhile.\n", count_sig ); #endif #endif PASSED; }
/* The main test function. */ int main( int argc, char * argv[] ) { int ret, value; sem_t * sem1, * sem2; /* Initialize output */ output_init(); /* Create the semaphore */ sem1 = sem_open( SEM_NAME, O_CREAT | O_EXCL, 0777, 1 ); if ( ( sem1 == SEM_FAILED ) && ( errno == EEXIST ) ) { sem_unlink( SEM_NAME ); sem1 = sem_open( SEM_NAME, O_CREAT | O_EXCL, 0777, 1 ); } if ( sem1 == SEM_FAILED ) { UNRESOLVED( errno, "Failed to create the semaphore" ); } /* Unlink */ ret = sem_unlink( SEM_NAME ); if ( ret != 0 ) { UNRESOLVED( errno, "Failed to unlink the semaphore" ); } /* Try reconnect */ sem2 = sem_open( SEM_NAME, 0 ); if ( sem2 != SEM_FAILED ) { FAILED( "Reconnecting the unlinked semaphore did not failed" ); } if ( errno != ENOENT ) { output( "Error %d: %s\n", errno, strerror( errno ) ); FAILED( "Reconnecting the unlinked semaphore failed with a wrong error" ); } /* Reopen the semaphore */ sem2 = sem_open( SEM_NAME, O_CREAT | O_EXCL, 0777, 3 ); if ( sem2 == SEM_FAILED ) { output( "Gor error %d: %s\n", errno, strerror( errno ) ); FAILED( "Failed to recreate the semaphore" ); } /* Check the semaphore have different values */ ret = sem_getvalue( sem1, &value ); if ( ret != 0 ) { UNRESOLVED( errno, "Failed to read sem1 value" ); } if ( value != 1 ) { output( "Read: %d\n", value ); FAILED( "Semaphore value is not as expected" ); } ret = sem_getvalue( sem2, &value ); if ( ret != 0 ) { UNRESOLVED( errno, "Failed to read sem1 value" ); } if ( value != 3 ) { output( "Read: %d\n", value ); FAILED( "Semaphore value is not as expected" ); } /* Unlink */ ret = sem_unlink( SEM_NAME ); if ( ret != 0 ) { UNRESOLVED( errno, "Failed to unlink the semaphore" ); } /* close both */ ret = sem_close( sem1 ); if ( ret != 0 ) { UNRESOLVED( errno, "Failed to close the semaphore" ); } ret = sem_close( sem2 ); if ( ret != 0 ) { UNRESOLVED( errno, "Failed to close the semaphore" ); } /* Test passed */ #if VERBOSE > 0 output( "Test passed\n" ); #endif PASSED; }
/* Main function */ int main (int argc, char * argv[]) { int ret = 0, i; struct sigaction sa; pthread_barrier_t bar; pthread_attr_t ta[ 4 ]; pthread_t th[ NTHREADS ]; struct sched_param sp; /* Initialize output routine */ output_init(); /* Initialize barrier */ ret = pthread_barrier_init(&bar, NULL, 5); if (ret != 0) { UNRESOLVED(ret, "Failed to init barrier"); } /* Register the signal handler for SIGUSR1 */ sigemptyset (&sa.sa_mask); sa.sa_flags = 0; sa.sa_handler = sighdl; if ((ret = sigaction (SIGUSR1, &sa, NULL))) { UNRESOLVED(ret, "Unable to register signal handler"); } if ((ret = sigaction (SIGALRM, &sa, NULL))) { UNRESOLVED(ret, "Unable to register signal handler"); } #if VERBOSE > 1 output("[parent] Signal handler registered\n"); #endif td[ 0 ].policy = td[ 1 ].policy = SCHED_FIFO; td[ 2 ].policy = td[ 3 ].policy = SCHED_RR; td[ 0 ].prio = sched_get_priority_min(SCHED_FIFO); if (td[ 0 ].prio == -1) { UNRESOLVED(errno, "Failed to get scheduler range value"); } td[ 1 ].prio = sched_get_priority_max(SCHED_FIFO); if (td[ 1 ].prio == -1) { UNRESOLVED(errno, "Failed to get scheduler range value"); } td[ 2 ].prio = sched_get_priority_min(SCHED_RR); if (td[ 2 ].prio == -1) { UNRESOLVED(errno, "Failed to get scheduler range value"); } td[ 3 ].prio = sched_get_priority_max(SCHED_RR); if (td[ 3 ].prio == -1) { UNRESOLVED(errno, "Failed to get scheduler range value"); } /* Initialize the threads attributes and create the RT threads */ for (i = 0; i < 4; i++) { ret = pthread_attr_init(&ta[ i ]); if (ret != 0) { UNRESOLVED(ret, "Failed to initialize thread attribute"); } ret = pthread_attr_setinheritsched(&ta[ i ], PTHREAD_EXPLICIT_SCHED); if (ret != 0) { UNRESOLVED(ret, "Failed to set explicit scheduling attribute"); } sp.sched_priority = td[ i ].prio; ret = pthread_attr_setschedparam(&ta[ i ], &sp); if (ret != 0) { UNRESOLVED(ret, "failed to set thread attribute sched param"); } ret = pthread_attr_setschedpolicy(&ta[ i ], td[ i ].policy); if (ret != 0) { UNRESOLVED(ret, "failed to set thread attribute sched prio"); } ret = pthread_create(&td[ i ].thread, &ta[ i ], rt_thread, &bar); if (ret != 0) { UNRESOLVED(ret, "Failed to create a RT thread -- need more privilege?"); } } /* Create the worker threads */ for (i = 0; i < NTHREADS; i++) { ret = pthread_create(&th[ i ], NULL, threaded, NULL); if (ret != 0) { UNRESOLVED(ret, "failed to create a worker thread"); } } /* Wait for the worker threads to finish */ for (i = 0; i < NTHREADS; i++) { ret = pthread_join(th[ i ], NULL); if (ret != 0) { UNRESOLVED(ret, "failed to join a worker thread"); } } /* Join the barrier to terminate the RT threads */ ret = pthread_barrier_wait(&bar); if ((ret != 0) && (ret != PTHREAD_BARRIER_SERIAL_THREAD)) { UNRESOLVED(ret, "Failed to wait for the barrier"); } /* Join the RT threads */ for (i = 0; i < 4; i++) { ret = pthread_join(td[ i ].thread, NULL); if (ret != 0) { UNRESOLVED(ret, "Failed to join a thread"); } } /* Done! */ output("pthread_getschedparam stress test PASSED -- %llu iterations\n", iterations); ret = pthread_barrier_destroy(&bar); if (ret != 0) { UNRESOLVED(ret, "Failed to destroy the barrier"); } PASSED; }
/* The main test function. */ int main(int argc, char *argv[]) { int ret = 0; pthread_t child; pthread_t joiner; /* Initialize output routine */ output_init(); /* Initialize thread attribute objects */ scenar_init(); for (sc = 0; sc < NSCENAR; sc++) { if (scenarii[sc].detached == 1) continue; #if VERBOSE > 0 output("-----\n"); output("Starting test with scenario (%i): %s\n", sc, scenarii[sc].descr); #endif /* Lock the mutex */ ret = pthread_mutex_lock(&mtx); if (ret != 0) UNRESOLVED(ret, "failed to lock the mutex"); ret = pthread_create(&child, &scenarii[sc].ta, threaded, NULL); switch (scenarii[sc].result) { /* Operation was expected to succeed */ case 0: if (ret != 0) UNRESOLVED(ret, "Failed to create this thread"); break; /* Operation was expected to fail */ case 1: if (ret == 0) UNRESOLVED(-1, "An error was expected " "but the thread creation succeeded"); break; /* We did not know the expected result */ case 2: default: #if VERBOSE > 0 if (ret == 0) output("Thread has been created " "successfully for this scenario\n"); else output("Thread creation failed with the error: " "%s\n", strerror(ret)); #endif } /* The new thread is running */ if (ret == 0) { /* Now create the joiner thread */ ret = pthread_create(&joiner, NULL, joiner_func, &child); if (ret != 0) UNRESOLVED(ret, "Failed to create the " "joiner thread"); /* Let it enter pthread_join */ sched_yield(); /* Cancel the joiner thread */ ret = pthread_cancel(joiner); if (ret != 0) UNRESOLVED(ret, "Failed to cancel the thread"); /* Join the canceled thread */ ret = pthread_join(joiner, NULL); if (ret != 0) UNRESOLVED(ret, "Failed to join the " "canceled thread"); /* Unblock the child thread */ ret = pthread_mutex_unlock(&mtx); if (ret != 0) UNRESOLVED(ret, "Failed to unlock the mutex"); /* Check the first thread is still joinable */ ret = pthread_join(child, NULL); if (ret != 0) { output("Error returned: %d\n"); FAILED("The thread is no more joinable"); } } else { ret = pthread_mutex_unlock(&mtx); if (ret != 0) UNRESOLVED(ret, "Failed to unlock the mutex"); } } scenar_fini(); #if VERBOSE > 0 output("-----\n"); output("All test data destroyed\n"); output("Test PASSED\n"); #endif PASSED; }