Home | History | Annotate | Download | only in pthread_condattr_setpshared
      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_condattr_setpshared()
      9  *
     10  *  It shall obtain the value of the process-shared attribute from 'attr'.
     11  *
     12  * Explanation:  To share a mutex between 2 processes, you need to map shared memory for
     13  * the mutex.  So whether the 'type' of the condattr is shared or private, it really will
     14  * not make a difference since both processes will always have access to the shared memory
     15  * as long as they the pointer to it.  So all we check here is that you can actually call
     16  * the pthread_condattr_setpshared() function, passing to it PTHREAD_PROCESS_SHARED and
     17  * PTHREAD_PROCESS_PRIVATE.
     18  *
     19  * Steps:
     20  * 1.  In a loop, initialize a pthread_condattr_t object with pthread_condattr_init()
     21  * 2.  Set 'pshared' of the object to PTHREAD_PROCESS_PRIVATE using pthread_condattr_setpshared
     22  * 3.  Call pthread_condattr_getpshared() to check if the process-shared
     23  *     attribute is set as PTHREAD_PROCESS_PRIVATE.
     24  *
     25  */
     26 
     27 #include <pthread.h>
     28 #include <stdio.h>
     29 #include <errno.h>
     30 #include "posixtest.h"
     31 
     32 #define NUM_OF_CONDATTR 10
     33 
     34 int main(void)
     35 {
     36 
     37 	/* Make sure there is process-shared capability. */
     38 #ifndef PTHREAD_PROCESS_SHARED
     39 	fprintf(stderr,
     40 		"process-shared attribute is not available for testing\n");
     41 	return PTS_UNRESOLVED;
     42 #endif
     43 
     44 	pthread_condattr_t attr[NUM_OF_CONDATTR];
     45 	int ret, i, pshared;
     46 
     47 	for (i = 0; i < NUM_OF_CONDATTR; i++) {
     48 		/* Initialize a cond attributes object */
     49 		if (pthread_condattr_init(&attr[i]) != 0) {
     50 			perror("Error at pthread_condattr_init()\n");
     51 			return PTS_UNRESOLVED;
     52 		}
     53 
     54 		/* Set 'pshared' to PTHREAD_PROCESS_PRIVATE. */
     55 		ret =
     56 		    pthread_condattr_setpshared(&attr[i],
     57 						PTHREAD_PROCESS_PRIVATE);
     58 		if (ret != 0) {
     59 			printf
     60 			    ("Test FAILED: Could not set pshared to PTHREAD_PROCESS_PRIVATE, error: %d\n",
     61 			     ret);
     62 			return PTS_FAIL;
     63 		}
     64 
     65 		/* Get 'pshared'.  It should be PTHREAD_PROCESS_PRIVATE. */
     66 		if (pthread_condattr_getpshared(&attr[i], &pshared) != 0) {
     67 			printf
     68 			    ("Test FAILED: obtaining the wrong process-shared attribute, expected PTHREAD_PROCESS_PRIVATE, but got: %d\n",
     69 			     pshared);
     70 			return PTS_FAIL;
     71 		}
     72 
     73 		if (pshared != PTHREAD_PROCESS_PRIVATE) {
     74 			printf("Test FAILED: Incorrect pshared value: %d\n",
     75 			       pshared);
     76 			return PTS_FAIL;
     77 		}
     78 
     79 		/* Destory the cond attributes object */
     80 		if (pthread_condattr_destroy(&attr[i]) != 0) {
     81 			perror("Error at pthread_condattr_destroy()\n");
     82 			return PTS_UNRESOLVED;
     83 		}
     84 	}
     85 
     86 	printf("Test PASSED\n");
     87 	return PTS_PASS;
     88 }
     89