int main(int argc, char **argv) { struct sigaction sa; pthread_t tid; void *retval; bzero(&sa, sizeof(sa)); sa.sa_handler = handler; sa.sa_flags = SA_RESTART; CHECKe(sigaction(SIGUSR1, &sa, NULL)); sa.sa_flags = 0; CHECKe(sigaction(SIGUSR2, &sa, NULL)); CHECKr(pthread_create(&tid, NULL, thr_accept, NULL)); sleep(2); /* Should restart it. */ CHECKr(pthread_kill(tid, SIGUSR1)); sleep(1); /* Should interrupt it. */ CHECKr(pthread_kill(tid, SIGUSR2)); sleep(1); CHECKr(pthread_join(tid, &retval)); ASSERT(retval == (void *)EINTR); ASSERT(hits == 2); SUCCEED; }
int main(int argc, char **argv) { pthread_t thread1; pthread_t thread2; struct sigaction act; act.sa_sigaction = act_handler; sigemptyset(&act.sa_mask); act.sa_flags = SA_SIGINFO | SA_RESETHAND | SA_NODEFER; CHECKe(sigaction(SIGUSR1, &act, NULL)); CHECKr(pthread_create(&thread1, NULL, thread, "T1")); CHECKr(pthread_create(&thread2, NULL, thread, "T2")); sleep(1); /* Signal handler should run once, both threads should awaken */ CHECKe(kill(getpid(), SIGUSR1)); sleep(1); /* Signal handler run once, only T1 should awaken */ CHECKe(sigaction(SIGUSR1, &act, NULL)); CHECKr(pthread_kill(thread1, SIGUSR1)); sleep(1); /* Signal handler run once, only T2 should awaken */ CHECKe(sigaction(SIGUSR1, &act, NULL)); CHECKr(pthread_kill(thread2, SIGUSR1)); sleep(1); SUCCEED; }
int main(int argc, char **argv) { pthread_t d, t, self = pthread_self(); pid_t pid; switch ((pid = fork())) { case -1: PANIC("cannot fork"); /* NOTREACHED */ case 0: /* child */ break; default: CHECKr(waitpid(pid, NULL, 0)); _exit(0); /* NOTREACHED */ } CHECKr(pthread_create(&d, NULL, deadlock_detector, NULL)); CHECKr(pthread_create(&t, NULL, joiner, &self)); CHECKr(pthread_join(t, NULL)); SUCCEED; }
/* * Test that closefrom does the right thing in a threaded programs, * specifically that it doesn't kill the thread kernel signal pipe. */ int main(int argc, char *argv[]) { pthread_t thread; void *status; int fd; int result; /* close files above stderr. The kernel pipes shouldn't be touched */ fd = STDERR_FILENO + 1; result = closefrom(fd); printf("closefrom(%d) == %d/%d\n", fd, result, errno); /* do it again: make sure that the result is -1/EBADF */ result = closefrom(fd); printf("closefrom(%d) == %d/%d\n", fd, result, errno); ASSERT(result == -1 && errno == EBADF); /* start a thread to verify the thread kernel is working */ CHECKr(pthread_create(&thread, NULL, dummy_thread, NULL)); CHECKr(pthread_join(thread, &status)); printf("dummy thread exited with status %p\n", status); SUCCEED; return 0; }
static void test_nocontention_lock(pthread_mutex_t *mutex) { printf(" test_nocontention_lock()\n"); CHECKr(pthread_mutex_lock(mutex)); CHECKr(pthread_mutex_unlock(mutex)); }
static void test_debug_double_lock(pthread_mutex_t *mutex) { printf(" test_debug_double_lock()\n"); CHECKr(pthread_mutex_lock(mutex)); ASSERTe(pthread_mutex_lock(mutex), == EDEADLK); CHECKr(pthread_mutex_unlock(mutex)); }
int main(int argc, char **argv) { pthread_t accept_thread; CHECKr(pthread_create(&accept_thread, NULL, sock_accept, NULL)); CHECKr(pthread_join(accept_thread, NULL)); SUCCEED; }
static void test_debug_double_unlock(pthread_mutex_t *mutex) { printf(" test_debug_double_unlock()\n"); CHECKr(pthread_mutex_lock(mutex)); CHECKr(pthread_mutex_unlock(mutex)); /* Posix D10 says undefined behaviour? */ ASSERTe(pthread_mutex_unlock(mutex), != 0); }
/* * Install a signal handler for sigusr1 and then wait for it to * occur. */ static void * do_nothing (void *arg) { SET_NAME("nothing"); ASSERT(signal(SIGUSR1, sighandler) != SIG_ERR); CHECKr(pthread_mutex_lock(&sync_mutex)); ASSERT(got_signal != 0); CHECKr(pthread_mutex_unlock(&sync_mutex)); return 0; }
static void test_mutex_fast(void) { pthread_mutex_t mutex_fast; printf("test_mutex_fast()\n"); CHECKr(pthread_mutex_init(&mutex_fast, NULL)); test_nocontention_lock(&mutex_fast); test_nocontention_trylock(&mutex_fast); test_contention_lock(&mutex_fast); CHECKr(pthread_mutex_destroy(&mutex_fast)); }
static void * thread_contention(void *arg) { pthread_mutex_t *mutex = arg; SET_NAME("cntntn"); CHECKr(pthread_mutex_lock(mutex)); ASSERT(contention_variable == 1); contention_variable = 2; CHECKr(pthread_mutex_unlock(mutex)); pthread_exit(NULL); }
int main(int argc, char *argv[]) { pthread_t thread; int pipe_fd[2]; CHECKe(pipe(pipe_fd)); CHECKr(pthread_create(&thread, NULL, select_thread, pipe_fd)); sleep(2); CHECKr(pthread_cancel(thread)); CHECKr(pthread_join(thread, NULL)); SUCCEED; }
int main(int argc, char **argv) { pthread_t tid; CHECKr(pthread_create(&tid, NULL, forker1, NULL)); CHECKr(pthread_join(tid, NULL)); ASSERT(cnt == 1); CHECKr(pthread_create(&tid, NULL, forker2, NULL)); CHECKr(pthread_join(tid, NULL)); ASSERT(cnt == 0); SUCCEED; }
/* * Test that masked signals with a default action of terminate process * do NOT terminate the process. */ int main (int argc, char *argv[]) { sigset_t mask; int sig; int r; /* any two (or more) command line args should cause the program to die */ if (argc > 2) { printf("trigger sigalrm[1] [test should die]\n"); ualarm(100000, 0); CHECKe(sleep(1)); } /* mask sigalrm */ CHECKe(sigemptyset(&mask)); CHECKe(sigaddset(&mask, SIGALRM)); CHECKr(pthread_sigmask(SIG_BLOCK, &mask, NULL)); /* make sure pthread_sigmask() returns the right value on failure */ r = pthread_sigmask(-1, &mask, NULL); ASSERTe(r, == EINVAL); /* now trigger sigalrm and wait for it */ printf("trigger sigalrm[2] [masked, test should not die]\n"); ualarm(100000, 0); CHECKe(sleep(1)); /* sigwait for sigalrm, it should be pending. If it is not the test will hang. */ CHECKr(sigwait(&mask, &sig)); ASSERT(sig == SIGALRM); /* make sure sigwait didn't muck with the mask by triggering sigalrm, again */ printf("trigger sigalrm[3] after sigwait [masked, test should not die]\n"); ualarm(100000, 0); CHECKe(sleep(1)); /* any single command line arg will run this code wich unmasks the signal and then makes sure the program terminates when sigalrm is triggered. */ if (argc > 1) { printf("trigger sigalrm[4] [unmasked, test should die]\n"); CHECKr(pthread_sigmask(SIG_UNBLOCK, &mask, NULL)); ualarm(100000, 0); CHECKe(sleep(1)); } SUCCEED; }
static void test_mutex_recursive_trylock(pthread_mutex_t *mutex) { int i; int j = 9; printf(" %s()\n", __func__); CHECKr(pthread_mutex_trylock(mutex)); for (i = 0; i < j; i++) CHECKr(pthread_mutex_trylock(mutex)); for (i = 0; i < j; i++) CHECKr(pthread_mutex_unlock(mutex)); CHECKr(pthread_mutex_unlock(mutex)); }
int main(int argc, char **argv) { pthread_t prod_th, cons_th; long counter = 4; CHECKn(sem_destroy(&cons_sem)); ASSERT(errno == EINVAL); val = 0; CHECKr(sem_init(&cons_sem, 0, 0)); CHECKr(sem_init(&prod_sem, 0, 1)); CHECKr(pthread_create(&prod_th, NULL, producer, &counter)); CHECKr(pthread_create(&cons_th, NULL, consumer, &counter)); CHECKr(pthread_join(prod_th, NULL)); CHECKr(pthread_join(cons_th, NULL)); pthread_exit(NULL); CHECKr(sem_destroy(&prod_sem)); CHECKr(sem_destroy(&cons_sem)); SUCCEED; }
int main(int argc, char *argv[]) { pthread_t thread; setbuf(stdout, NULL); setbuf(stderr, NULL); CHECKr(pthread_create(&thread, NULL, sock_accept, (void *)0xdeadbeaf)); CHECKr(pthread_join(thread, NULL)); SUCCEED; }
static void * sock_connect(void *arg) { struct timeval to; pid_t child_pid; int status; int s; CHECKe(s = socket(AF_INET, SOCK_STREAM, 0)); to.tv_sec = 2; to.tv_usec = 0.5 * 1e6; CHECKe(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to))); CHECKe(child_pid = fork()); if (child_pid == 0) { char *argv[3]; char fdstr[3]; snprintf(fdstr, sizeof(fdstr), "%d", s); argv[0] = "setsockopt3a"; argv[1] = fdstr; argv[2] = NULL; execv(argv[0], argv); _exit(NOTOK); } ASSERTe(wait(&status), == child_pid); ASSERT(WIFEXITED(status)); CHECKr(WEXITSTATUS(status)); return (NULL); }
static void * waiter(void *arg) { int status; pid_t pid; SET_NAME("waiter"); CHECKr(pthread_mutex_lock(&waiter_mutex)); printf("waiting for child\n"); CHECKe(pid = wait(&status)); ASSERT(WIFEXITED(status)); ASSERT(WEXITSTATUS(status) == 0); printf("child exited\n"); CHECKr(pthread_mutex_unlock(&waiter_mutex)); return (NULL); }
static void test_contention_lock(pthread_mutex_t *mutex) { pthread_t thread; printf(" test_contention_lock()\n"); CHECKr(pthread_mutex_lock(mutex)); contention_variable = 0; CHECKr(pthread_create(&thread, NULL, thread_contention, mutex)); pthread_yield(); contention_variable = 1; CHECKr(pthread_mutex_unlock(mutex)); pthread_yield(); CHECKr(pthread_mutex_lock(mutex)); ASSERT(contention_variable == 2); CHECKr(pthread_mutex_unlock(mutex)); }
static void test_mutex_recursive(void) { pthread_mutexattr_t mutex_recursive_attr; pthread_mutex_t mutex_recursive; printf("test_mutex_recursive()\n"); CHECKr(pthread_mutexattr_init(&mutex_recursive_attr)); CHECKr(pthread_mutexattr_settype(&mutex_recursive_attr, PTHREAD_MUTEX_RECURSIVE)); CHECKr(pthread_mutex_init(&mutex_recursive, &mutex_recursive_attr)); test_mutex_recursive_lock(&mutex_recursive); test_mutex_recursive_trylock(&mutex_recursive); /* Posix D10 says undefined behaviour? */ ASSERTe(pthread_mutex_unlock(&mutex_recursive), != 0); CHECKr(pthread_mutex_destroy(&mutex_recursive)); }
static void test_mutex_debug(void) { pthread_mutexattr_t mutex_debug_attr; pthread_mutex_t mutex_debug; printf("test_mutex_debug()\n"); CHECKr(pthread_mutexattr_init(&mutex_debug_attr)); CHECKr(pthread_mutexattr_settype(&mutex_debug_attr, PTHREAD_MUTEX_ERRORCHECK)); CHECKr(pthread_mutex_init(&mutex_debug, &mutex_debug_attr)); test_nocontention_lock(&mutex_debug); test_nocontention_trylock(&mutex_debug); test_contention_lock(&mutex_debug); test_debug_double_lock(&mutex_debug); test_debug_double_unlock(&mutex_debug); CHECKr(pthread_mutex_destroy(&mutex_debug)); }
void * thr_join(void *arg) { pthread_t tid = *(pthread_t *)arg; void *retval; CHECKr(pthread_join(tid, &retval)); return (retval); }
int main(int argc, char *argv[]) { pthread_t thread; pthread_attr_t attr; struct sockaddr_in addr; int ret; /* fork and have the child open a listener */ signal(SIGCHLD, SIG_IGN); switch (fork()) { case 0: server(); exit(0); case -1: exit(errno); default: sleep(2); } /* Open up a TCP connection to the local discard port */ addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); addr.sin_port = htons(TEST_PORT); CHECKe(fd = socket(AF_INET, SOCK_STREAM, 0)); printf("main: connecting to test port with fd %d\n", fd); ret = connect(fd, (struct sockaddr *)&addr, sizeof addr); if (ret == -1) fprintf(stderr, "connect() failed\n"); CHECKe(ret); printf("main: connected on fd %d\n", fd); CHECKr(pthread_attr_init(&attr)); CHECKr(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)); printf("starting child thread\n"); CHECKr(pthread_create(&thread, &attr, new_thread, NULL)); sleep(1); printf("main: closing fd %d\n", fd); CHECKe(close(fd)); printf("main: closed\n"); sleep(1); SUCCEED; }
static void * sock_accept(void *arg) { pthread_t connect_thread; struct sockaddr_in sin; int s; CHECKe(s = socket(AF_INET, SOCK_STREAM, 0)); bzero(&sin, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_len = sizeof(sin); sin.sin_port = htons(6543); sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); CHECKe(bind(s, (struct sockaddr *)&sin, sizeof(sin))); CHECKe(listen(s, 2)); CHECKr(pthread_create(&connect_thread, NULL, sock_connect, NULL)); CHECKr(pthread_join(connect_thread, NULL)); return (NULL); }
int main(int argc, char *argv[]) { pthread_t waiting_threads[WAITING_THREADS]; pthread_t deadlock_thread; struct sockaddr_in addr; int fd, newfd, i, j; void *value_ptr; struct timespec rqtp; rqtp.tv_sec = 0; rqtp.tv_nsec = 1000000; bzero((char *) &addr, sizeof addr); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); addr.sin_port = htons(0); CHECKr(pthread_create(&deadlock_thread, NULL, deadlock_detector, NULL)); CHECKe(fd = socket(AF_INET, SOCK_DGRAM, 0)); CHECKr(bind(fd, (struct sockaddr *)&addr, sizeof(addr))); CHECKe(newfd = socket(AF_INET, SOCK_DGRAM, 0)); CHECKr(bind(newfd, (struct sockaddr *)&addr, sizeof(addr))); for (i = 0; i < ITERATIONS; i++) { for (j = 0; j < WAITING_THREADS; j++) CHECKr(pthread_create(&waiting_threads[j], NULL, waiting_read, (void *)&newfd)); nanosleep(&rqtp, NULL); CHECKe(dup2(fd, newfd)); for (j = 0; j < WAITING_THREADS; j++) { CHECKr(pthread_join(waiting_threads[j], &value_ptr)); ASSERT(value_ptr == (void *)EBADF); } } SUCCEED; }
int main(int argc, char **argv) { struct sigaction sa; pthread_t tid[2]; void *retval; bzero(&sa, sizeof(sa)); sa.sa_handler = handler; CHECKe(sigaction(SIGUSR1, &sa, NULL)); CHECKr(pthread_create(&tid[0], NULL, thr_sleep, NULL)); CHECKr(pthread_create(&tid[1], NULL, thr_join, &tid[0])); sleep(2); CHECKr(pthread_kill(tid[1], SIGUSR1)); sleep(2); ASSERT(hits == 1); CHECKr(pthread_join(tid[1], &retval)); ASSERT(retval == (void *)0); SUCCEED; }
static void * sigwaiter (void *arg) { int signo; SET_NAME("sigwaiter"); /* Block all of the signals that the function will wait for */ CHECKe(sigprocmask (SIG_BLOCK, &wait_mask, NULL)); while (sigcounts[SIGINT] == 0) { printf("Sigwait waiting (thread %p)\n", pthread_self()); CHECKe(sigwait (&wait_mask, &signo)); sigcounts[signo]++; printf ("Sigwait caught signal %d (%s)\n", signo, strsignal(signo)); /* Allow the main thread to prevent the sigwait. */ CHECKr(pthread_mutex_lock (&waiter_mutex)); CHECKr(pthread_mutex_unlock (&waiter_mutex)); } return (arg); }
int main(int argc, char *argv[]) { pthread_t slpr; ASSERT(signal(SIGALRM, handler) != SIG_ERR); CHECKe(alarm(1)); CHECKr(pthread_create(&slpr, NULL, sleeper, NULL)); /* ASSERT(sleep(1) == 0); */ for (;;) { if (alarmed) { alarmed = 0; CHECKe(write(STDOUT_FILENO, "!", 1)); } } }
int main(int argc, char *argv[]) { pthread_t thread; int ch, count = 4; long i; /* Getopt variables. */ extern char *optarg; while ((ch = getopt(argc, argv, "c:?")) != -1) switch (ch) { case 'c': count = atoi(optarg); if ((count > 26) || (count < 2)) { count = 2; } break; case '?': usage(); return(OK); default: usage(); return(NOTOK); } /* create the threads */ for (i = 0; i < count; i++) CHECKr(pthread_create(&thread, NULL, new_thread, (void*)(buf+i))); /* give all threads a chance to run */ sleep (2); ending = 1; for (i = 0; i < count; i++) ASSERT(x[i]); /* make sure each thread ran */ CHECKe(write(STDOUT_FILENO, "\n", 1)); SUCCEED; }