Home | History | Annotate | Download | only in pthread_cond_wait
      1 /*
      2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
      3  * Created by:  bing.wei.liu REMOVE-THIS AT intel DOT com
      4  * This file is licensed under the GPL license.  For the full content
      5  * of this license, see the COPYING file at the top level of this
      6  * source tree.
      7 
      8  * Test that pthread_cond_wait()
      9  *   Upon successful completion, a value of zero shall be returned.
     10  */
     11 
     12 #include <pthread.h>
     13 #include <stdio.h>
     14 #include <stdlib.h>
     15 #include <unistd.h>
     16 #include <errno.h>
     17 #include <signal.h>
     18 #include "posixtest.h"
     19 
     20 struct testdata {
     21 	pthread_mutex_t mutex;
     22 	pthread_cond_t cond;
     23 } td;
     24 
     25 pthread_t thread1;
     26 
     27 int t1_start = 0;
     28 int signaled = 0;
     29 
     30 /* Alarm handler */
     31 void alarm_handler(int signo)
     32 {
     33 	printf("Error: failed to wakeup thread\n");
     34 	pthread_cancel(thread1);
     35 	exit(PTS_UNRESOLVED);
     36 }
     37 
     38 void *t1_func(void *arg)
     39 {
     40 	int rc;
     41 
     42 	if (pthread_mutex_lock(&td.mutex) != 0) {
     43 		fprintf(stderr, "Thread1 failed to acquire the mutex\n");
     44 		exit(PTS_UNRESOLVED);
     45 	}
     46 	fprintf(stderr, "Thread1 started\n");
     47 	t1_start = 1;		/* let main thread continue */
     48 
     49 	fprintf(stderr, "Thread1 is waiting for the cond\n");
     50 	rc = pthread_cond_wait(&td.cond, &td.mutex);
     51 	if (rc != 0) {
     52 		if (rc == EINVAL) {
     53 			fprintf(stderr, "pthread_cond_wait returns EINVAL\n");
     54 			exit(PTS_UNRESOLVED);
     55 		} else if (rc == EPERM) {
     56 			fprintf(stderr, "pthread_cond_wait returns EPERM\n");
     57 			exit(PTS_UNRESOLVED);
     58 		}
     59 		fprintf(stderr, "pthread_cond_wait returns %d\n", rc);
     60 		exit(PTS_UNRESOLVED);
     61 	}
     62 
     63 	if (signaled == 0) {
     64 		fprintf(stderr, "Thread1 waked up before being notified\n");
     65 		exit(PTS_UNRESOLVED);
     66 	}
     67 	fprintf(stderr, "Thread1 wakened up and got returned value 0\n");
     68 
     69 	if (pthread_mutex_unlock(&td.mutex) != 0) {
     70 		fprintf(stderr, "Thread1 failed to release the mutex\n");
     71 		exit(PTS_UNRESOLVED);
     72 	}
     73 	fprintf(stderr, "Thread1 released the mutex\n");
     74 	return NULL;
     75 }
     76 
     77 int main(void)
     78 {
     79 
     80 	struct sigaction act;
     81 
     82 	if (pthread_mutex_init(&td.mutex, NULL) != 0) {
     83 		fprintf(stderr, "Fail to initialize mutex\n");
     84 		return PTS_UNRESOLVED;
     85 	}
     86 	if (pthread_cond_init(&td.cond, NULL) != 0) {
     87 		fprintf(stderr, "Fail to initialize cond\n");
     88 		return PTS_UNRESOLVED;
     89 	}
     90 
     91 	if (pthread_create(&thread1, NULL, t1_func, NULL) != 0) {
     92 		fprintf(stderr, "Fail to create thread 1\n");
     93 		return PTS_UNRESOLVED;
     94 	}
     95 
     96 	sleep(2);
     97 
     98 	/* Setup alarm handler */
     99 	act.sa_handler = alarm_handler;
    100 	act.sa_flags = 0;
    101 	sigemptyset(&act.sa_mask);
    102 	sigaction(SIGALRM, &act, 0);
    103 	alarm(5);
    104 
    105 	fprintf(stderr,
    106 		"To wake up thread1 by broadcasting its waited condition\n");
    107 	signaled = 1;
    108 	if (pthread_cond_broadcast(&td.cond) != 0) {
    109 		fprintf(stderr, "Main failed to broadcast the condition\n");
    110 		return PTS_UNRESOLVED;
    111 	}
    112 
    113 	pthread_join(thread1, NULL);
    114 	printf("Test PASSED\n");
    115 	return PTS_PASS;
    116 }
    117