#include <stdio.h>
#include <pthread.h>
#include "local.h"
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int did_test1 = 0;
static void *
test1(void *v)
{
int r;
if (ftrylockfile(stdin) == 0)
errx(1, "Lock on stdin never held");
if ((r = pthread_mutex_lock(&lock)))
errc(1, r, "could not lock lock");
did_test1 = 1;
if ((r = pthread_cond_signal(&cond)))
errc(1, r, "could not signal cond");
if ((r = pthread_mutex_unlock(&lock)))
errc(1, r, "could not unlock lock");
flockfile(stdin);
funlockfile(stdin);
return NULL;
}
int
main(void)
{
int fd, r;
pthread_t thr;
flockfile(stdin);
flockfile(stdin);
if ((r = pthread_create(&thr, NULL, test1, NULL)))
warnc(r, "could not create thread");
if ((r = pthread_mutex_lock(&lock)))
errc(1, r, "could not lock lock");
while (did_test1 == 0)
if ((r = pthread_cond_wait(&cond, &lock)))
errc(1, r, "could not wait on cond");
if ((r = pthread_mutex_unlock(&lock)))
errc(1, r, "could not unlock lock");
funlockfile(stdin);
funlockfile(stdin);
if ((r = pthread_join(thr, NULL)))
warnc(r, "could not join thread");
exit(0);
}