Home | History | Annotate | Download | only in pthread_setcancelstate
      1 /*
      2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
      3  * Created by:  rolla.n.selbak 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 pthread_cancelstate
      9  * It returns a 0 on success.  It may return an error code of:
     10  *
     11  * [EINVAL] The specified state is not PTHREAD_CANCEL_ENABLE or
     12  * PTHREAD_CANCEL_DISABLE.
     13 
     14  * It will not return EINTR.
     15  *
     16  * STEPS:
     17  * 1. Create a thread.
     18  * 2. In the thread function, set the state to an invalid value -100;
     19  * 3. Check the return value.
     20  */
     21 
     22 #include <pthread.h>
     23 #include <stdio.h>
     24 #include <errno.h>
     25 #include <unistd.h>
     26 #include "posixtest.h"
     27 
     28 int ret;			/* Return value of pthread_setcancelstate(). */
     29 
     30 /* Function that the thread executes upon its creation */
     31 void *a_thread_func()
     32 {
     33 	/* Set cancel state to an invalid integer and save the return value. */
     34 	ret = pthread_setcancelstate(-100, NULL);
     35 
     36 	pthread_exit(0);
     37 	return NULL;
     38 }
     39 
     40 int main(void)
     41 {
     42 	pthread_t new_th;
     43 
     44 	/* Initializing value */
     45 	ret = 0;
     46 
     47 	/* Create a new thread. */
     48 	if (pthread_create(&new_th, NULL, a_thread_func, NULL) != 0) {
     49 		perror("Error creating thread\n");
     50 		return PTS_UNRESOLVED;
     51 	}
     52 
     53 	/* Wait for thread to end execution. */
     54 	if (pthread_join(new_th, NULL) != 0) {
     55 		perror("Error in pthread_join()\n");
     56 		return PTS_UNRESOLVED;
     57 	}
     58 
     59 	/* This means that pthread_setcancelstate() did not give an error when passed an
     60 	 * invalid state value of -100. */
     61 	if (ret != EINVAL) {
     62 		if (ret == 0) {
     63 			printf
     64 			    ("Test PASSED: *NOTE: Returned 0 on error, though standard states 'may' fail.\n");
     65 			return PTS_PASS;
     66 		}
     67 
     68 		printf("Test FAILED: returned invalid error code of %d.\n",
     69 		       ret);
     70 		return PTS_FAIL;
     71 	}
     72 
     73 	printf("Test PASSED\n");
     74 	return PTS_PASS;
     75 }
     76