int main(void) { pthread_setschedparam_np(0, SCHED_FIFO, 0, 0x1, PTHREAD_HARD_REAL_TIME_NP); mlockall(MCL_CURRENT | MCL_FUTURE); RT_SET_REAL_TIME_MODE(); pthread_cleanup_push(main_exit_handler, 0); start_rt_timer(nano2count(TICK)); DISPLAY("User space POSIX test program.\n"); pthread_cond_init(cond = &conds, NULL); pthread_mutex_init(mtx = &mtxs, NULL); pthread_barrier_init(barrier = &barriers, NULL, 5); pthread_create(&thread1, NULL, task_func1, NULL); pthread_create(&thread2, NULL, task_func2, NULL); pthread_create(&thread3, NULL, task_func3, NULL); pthread_create(&thread4, NULL, task_func4, NULL); pthread_barrier_wait(barrier); DISPLAY("\nDo not panic, wait 2 s, till task3 times out.\n\n"); pthread_barrier_wait(barrier); pthread_join(thread1, NULL); pthread_join(thread2, NULL); pthread_join(thread3, NULL); pthread_join(thread4, NULL); pthread_cond_destroy(cond); pthread_mutex_destroy(mtx); pthread_cleanup_pop(1); DISPLAY("User space POSIX test program removed.\n"); pthread_exit(0); return 0; }
static void *task_func2(void *dummy) { pthread_setschedparam_np(2, SCHED_FIFO, 0, 0x1, PTHREAD_HARD_REAL_TIME_NP); mlockall(MCL_CURRENT | MCL_FUTURE); RT_SET_REAL_TIME_MODE(); pthread_cleanup_push(task_exit_handler, 0); pthread_barrier_wait(barrier); DISPLAY("Starting task2, waiting on the conditional variable to be 2.\n"); pthread_mutex_lock(mtx); while(cond_data < 2) { pthread_cond_wait(cond, mtx); } pthread_mutex_unlock(mtx); if(cond_data == 2) { DISPLAY("task2, conditional variable signalled, value: %d.\n", cond_data); } DISPLAY("task2 waits for a broadcast.\n"); pthread_mutex_lock(mtx); while(cond_data < 3) { pthread_cond_wait(cond, mtx); } pthread_mutex_unlock(mtx); if(cond_data == 3) { DISPLAY("task2, conditional variable broadcasted, value: %d.\n", cond_data); } pthread_cleanup_pop(1); DISPLAY("Ending task2.\n"); pthread_exit(0); return 0; }
void * tf (void *dummy) { bool loop = true; pthread_setschedparam_np(0, SCHED_FIFO, 0, 0xF, PTHREAD_HARD_REAL_TIME); pthread_barrier_wait(&br); while (loop) { pthread_mutex_lock (&lock); while (n && !exiting) pthread_cond_wait (&cv, &lock); n = true; pthread_mutex_unlock (&lock); fputs (".", f); pthread_mutex_lock (&lock); n = false; if (exiting) loop = false; #ifdef UNLOCK_AFTER_BROADCAST pthread_cond_broadcast (&cv); pthread_mutex_unlock (&lock); #else pthread_mutex_unlock (&lock); pthread_cond_broadcast (&cv); #endif } return NULL; }
static void *task_func3(void *dummy) { struct timespec abstime; pthread_setschedparam_np(3, SCHED_FIFO, 0, 0x1, PTHREAD_HARD_REAL_TIME_NP); mlockall(MCL_CURRENT | MCL_FUTURE); RT_SET_REAL_TIME_MODE(); pthread_cleanup_push(task_exit_handler, 0); pthread_barrier_wait(barrier); DISPLAY("Starting task3, waiting on the conditional variable to be 3 with a 2 s timeout.\n"); pthread_mutex_lock(mtx); while(cond_data < 3) { clock_gettime(CLOCK_MONOTONIC, &abstime); abstime.tv_sec += 2; if (pthread_cond_timedwait(cond, mtx, &abstime) != 0) { break; } } pthread_mutex_unlock(mtx); if(cond_data < 3) { DISPLAY("task3, timed out, conditional variable value: %d.\n", cond_data); } pthread_mutex_lock(mtx); cond_data = 3; pthread_mutex_unlock(mtx); DISPLAY("task3 broadcasts after setting data to 3.\n"); pthread_cond_broadcast(cond); pthread_cleanup_pop(1); DISPLAY("Ending task3.\n"); pthread_exit(0); return 0; }
int main(void) { pthread_setschedparam_np(0, SCHED_FIFO, 0, 0xF, PTHREAD_HARD_REAL_TIME); start_rt_timer(0); do_test(); return 0; }
int main(void) { pthread_setschedparam_np(0, SCHED_FIFO, 0, 0xF, PTHREAD_HARD_REAL_TIME_NP); start_rt_timer(0); pthread_mutex_init(&m, NULL); pthread_cond_init(&c, NULL); do_test(); return 0; }
int main(void) { #ifdef ORIGINAL_TEST printf("ORIGINAL_TEST\n"); #endif pthread_setschedparam_np(0, SCHED_FIFO, 0, 0xF, PTHREAD_HARD_REAL_TIME_NP); do_test(); return 0; }
static void * tf (void *a) { pthread_setschedparam_np(0, SCHED_FIFO, 0, 0xF, PTHREAD_HARD_REAL_TIME); pid_t pid = fork (); if (pid == -1) { puts ("fork failed"); exit (1); } if (pid == 0) { pthread_setschedparam_np(0, SCHED_FIFO, 0, 0xF, PTHREAD_HARD_REAL_TIME); atexit (final_test); pthread_exit (NULL); } int r; int e = TEMP_FAILURE_RETRY (waitpid (pid, &r, 0)); if (e != pid) { puts ("waitpid failed"); exit (1); } if (! WIFSIGNALED (r)) { puts ("child not signled"); exit (1); } if (WTERMSIG (r) != THE_SIGNAL) { puts ("child's termination signal wrong"); exit (1); } return NULL; }
static void * tf (void *a) { pthread_setschedparam_np(0, SCHED_FIFO, 0, 0xF, PTHREAD_HARD_REAL_TIME); if (pthread_join ((pthread_t) a, NULL) != 0) { printf ("join failed while %d are running\n", nrunning); _exit (1); } printf ("%2d left\n", --nrunning); return NULL; }
int do_test (void) { f = fopen ("/dev/null", "w"); if (f == NULL) { printf ("couldn't open /dev/null, %m\n"); return 1; } count = sysconf (_SC_NPROCESSORS_ONLN); if (count <= 0) count = 1; count *= 4; pthread_barrier_init(&br, NULL, count); pthread_t th[count]; int i, ret; for (i = 0; i < count; ++i) if ((ret = pthread_create (&th[i], NULL, tf, NULL)) != 0) { errno = ret; printf ("pthread_create %d failed: %m\n", i); return 1; } struct timespec ts = { .tv_sec = 5, .tv_nsec = 0 }; while (nanosleep (&ts, &ts) != 0); pthread_mutex_lock (&lock); exiting = true; pthread_mutex_unlock (&lock); for (i = 0; i < count; ++i) pthread_join (th[i], NULL); fclose (f); return 0; } #define TIMEOUT 40 int main(void) { pthread_setschedparam_np(0, SCHED_FIFO, 0, 0xF, PTHREAD_HARD_REAL_TIME); start_rt_timer(0); pthread_cond_init(&cv, NULL); pthread_mutex_init(&lock, NULL); do_test(); return 0; }
static void * tf (void *a) { /* Block SIGUSR1. */ sigset_t ss; pthread_setschedparam_np(0, SCHED_FIFO, 0, 0xF, PTHREAD_HARD_REAL_TIME_NP); sigemptyset (&ss); sigaddset (&ss, SIGUSR1); if (BLOCK_SIG && pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0) { puts ("child: sigmask failed"); exit (1); } if (pthread_mutex_lock (&m) != 0) { puts ("child: mutex_lock failed"); exit (1); } int e = pthread_barrier_wait (&b); if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) { puts ("child: barrier_wait failed"); exit (1); } /* Compute timeout. */ struct timeval tv; (void) gettimeofday (&tv, NULL); struct timespec ts; TIMEVAL_TO_TIMESPEC (&tv, &ts); // RTAI clock_gettime(CLOCK_MONOTONIC, &ts); /* Timeout: 1sec. */ ts.tv_sec += 1; /* This call should never return. */ if (pthread_cond_timedwait (&c, &m, &ts) != ETIMEDOUT) { puts ("cond_timedwait didn't time out"); exit (1); } return NULL; }
void * tf (void *id) { pthread_setschedparam_np(0, SCHED_FIFO, 0, 0xF, PTHREAD_HARD_REAL_TIME); pthread_barrier_wait(&br); pthread_mutex_lock (&lock); if ((long) id == 0) { while (!exiting) { if ((spins++ % 1000) == 0) write (fd, ".", 1); pthread_mutex_unlock (&lock); pthread_mutex_lock (&lock); int njobs = rand () % (count + 1); nn = njobs; if ((rand () % 30) == 0) pthread_cond_broadcast (&cv); else while (njobs--) pthread_cond_signal (&cv); } pthread_cond_broadcast (&cv); } else { while (!exiting) { while (!nn && !exiting) pthread_cond_wait (&cv, &lock); --nn; pthread_mutex_unlock (&lock); pthread_mutex_lock (&lock); } } pthread_mutex_unlock (&lock); return NULL; }
static void * tf (void *arg) { pthread_setschedparam_np(0, SCHED_FIFO, 0, 0xF, PTHREAD_HARD_REAL_TIME_NP); int err = pthread_cond_wait (&cond, &mut); if (err == 0) { puts ("cond_wait did not fail"); exit (1); } if (err != EPERM) { printf ("cond_wait didn't return EPERM but %d\n", err); exit (1); } /* Current time. */ struct timeval tv; (void) gettimeofday (&tv, NULL); /* +1000 seconds in correct format. */ struct timespec ts; TIMEVAL_TO_TIMESPEC (&tv, &ts); ts.tv_sec += 1000; err = pthread_cond_timedwait (&cond, &mut, &ts); if (err == 0) { puts ("cond_timedwait did not fail"); exit (1); } if (err != EPERM) { printf ("cond_timedwait didn't return EPERM but %d\n", err); exit (1); } return (void *) 1l; }
static void * tf (void *a) { pthread_setschedparam_np(0, SCHED_FIFO, 0, 0xF, PTHREAD_HARD_REAL_TIME_NP); if (pthread_mutex_lock (&m) != 0) { puts ("child: mutex_lock failed"); exit (1); } int e = pthread_barrier_wait (&b); if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) { puts ("child: barrier_wait failed"); exit (1); } /* This call should never return. */ pthread_cond_wait (&c, &m); return NULL; }
int main(void) { int i; thread = (void *)malloc(NTASKS*sizeof(pthread_t)); task = (void *)malloc(NTASKS*sizeof(RT_TASK *)); pthread_setschedparam_np(2*NTASKS, SCHED_FIFO, 0, 0x1, PTHREAD_HARD_REAL_TIME_NP); pthread_rwlock_init(rwl = &rwls, 0); pthread_barrier_init(&barrier, NULL, NTASKS + 1); rt_set_oneshot_mode(); start_rt_timer(0); for (i = 0; i < NTASKS; i++) { pthread_create(&thread[i], NULL, (void *)thread_fun, (void *)(i + 1)); } pthread_barrier_wait(&barrier); pthread_rwlock_destroy(rwl); pthread_barrier_destroy(&barrier); stop_rt_timer(); free(thread); free(task); return 0; }
static void *thread_fun(int idx) { unsigned int loops = LOOPS; struct timespec abstime; char name[7]; sprintf(name, "TASK%d", idx); pthread_setschedparam_np(NTASKS - idx + 1, SCHED_FIFO, 0, 0x1, PTHREAD_HARD_REAL_TIME_NP); mlockall(MCL_CURRENT | MCL_FUTURE); RT_SET_REAL_TIME_MODE(); while(loops--) { DISPLAY("TASK %d 1 COND/TIMED PREWLOCKED\n", idx); clock_gettime(0, &abstime); abstime.tv_sec += 2; if (idx%2) { if (pthread_rwlock_trywrlock(rwl)) { DISPLAY("TASK %d 1 COND PREWLOCKED FAILED GO UNCOND\n", idx); pthread_rwlock_wrlock(rwl); } } else if (pthread_rwlock_timedwrlock(rwl, &abstime) >= SEM_TIMOUT) { DISPLAY("TASK %d 1 TIMED PREWLOCKED FAILED GO UNCOND\n", idx); pthread_rwlock_wrlock(rwl); } DISPLAY("TASK %d 1 WLOCKED\n", idx); rt_busy_sleep(100000); DISPLAY("TASK %d 2 COND PREWLOCK\n", idx); if (pthread_rwlock_trywrlock(rwl)) { DISPLAY("TASK %d 2 COND PREWLOCK FAILED GO UNCOND\n", idx); pthread_rwlock_wrlock(rwl); } DISPLAY("TASK %d 2 WLOCK\n", idx); rt_busy_sleep(100000); DISPLAY("TASK %d 3 PREWLOCK\n", idx); pthread_rwlock_wrlock(rwl); DISPLAY("TASK %d 3 WLOCK\n", idx); rt_busy_sleep(100000); DISPLAY("TASK %d 3 PREWUNLOCK\n", idx); pthread_rwlock_unlock(rwl); DISPLAY("TASK %d 3 WUNLOCK\n", idx); rt_busy_sleep(100000); DISPLAY("TASK %d 2 PREWUNLOCK\n", idx); pthread_rwlock_unlock(rwl); DISPLAY("TASK %d 2 WUNLOCK\n", idx); rt_busy_sleep(100000); DISPLAY("TASK %d 1 PREWUNLOCKED\n", idx); pthread_rwlock_unlock(rwl); DISPLAY("TASK %d 1 WUNLOCKED\n", idx); rt_busy_sleep(100000); DISPLAY("TASK %d 1 COND/TIMED PRERDLOCKED\n", idx); clock_gettime(0, &abstime); abstime.tv_sec += 2; if (idx%2) { if (pthread_rwlock_tryrdlock(rwl)) { DISPLAY("TASK %d 1 COND PRERDLOCKED FAILED GO UNCOND\n", idx); pthread_rwlock_rdlock(rwl); } } else if (pthread_rwlock_timedrdlock(rwl, &abstime) >= SEM_TIMOUT) { DISPLAY("TASK %d 1 TIMED PRERDLOCKED FAILED GO UNCOND\n", idx); pthread_rwlock_rdlock(rwl); } DISPLAY("TASK %d 1 RDLOCKED\n", idx); rt_busy_sleep(100000); DISPLAY("TASK %d 2 COND PRERDLOCK\n", idx); if (pthread_rwlock_tryrdlock(rwl)) { DISPLAY("TASK %d 2 COND PRERDLOCK FAILED GO UNCOND\n", idx); pthread_rwlock_rdlock(rwl); } DISPLAY("TASK %d 2 RDLOCK\n", idx); rt_busy_sleep(100000); DISPLAY("TASK %d 3 PRERDLOCK\n", idx); pthread_rwlock_rdlock(rwl); DISPLAY("TASK %d 3 RDLOCK\n", idx); rt_busy_sleep(100000); DISPLAY("TASK %d 3 PRERDUNLOCK\n", idx); pthread_rwlock_unlock(rwl); DISPLAY("TASK %d 3 RDUNLOCK\n", idx); rt_busy_sleep(100000); DISPLAY("TASK %d 2 PRERDUNLOCK\n", idx); pthread_rwlock_unlock(rwl); DISPLAY("TASK %d 2 RDUNLOCK\n", idx); rt_busy_sleep(100000); DISPLAY("TASK %d 1 PRERDUNLOCK\n", idx); pthread_rwlock_unlock(rwl); DISPLAY("TASK %d 1 RDUNLOCK\n", idx); rt_busy_sleep(100000); } rt_make_soft_real_time(); pthread_barrier_wait(&barrier); DISPLAY("TASK %d EXITED\n", idx); return NULL; }