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
34int main()
35{
36
37	/* Make sure there is process-shared capability. */
38	#ifndef PTHREAD_PROCESS_SHARED
39	  fprintf(stderr,"process-shared attribute is not available for testing\n");
40	  return PTS_UNRESOLVED;
41	#endif
42
43	pthread_condattr_t attr[NUM_OF_CONDATTR];
44	int ret, i, pshared;
45
46	for(i=0;i<NUM_OF_CONDATTR;i++)
47	{
48		/* Initialize a cond attributes object */
49		if(pthread_condattr_init(&attr[i]) != 0)
50		{
51			perror("Error at pthread_condattr_init()\n");
52			return PTS_UNRESOLVED;
53		}
54
55		/* Set 'pshared' to PTHREAD_PROCESS_PRIVATE. */
56		ret=pthread_condattr_setpshared(&attr[i], PTHREAD_PROCESS_PRIVATE);
57		if(ret != 0)
58		{
59			printf("Test FAILED: Could not set pshared to PTHREAD_PROCESS_PRIVATE, error: %d\n", ret);
60			return PTS_FAIL;
61		}
62
63		/* Get 'pshared'.  It should be PTHREAD_PROCESS_PRIVATE. */
64		if(pthread_condattr_getpshared(&attr[i], &pshared) != 0)
65		{
66			printf("Test FAILED: obtaining the wrong process-shared attribute, expected PTHREAD_PROCESS_PRIVATE, but got: %d\n", pshared);
67			return PTS_FAIL;
68		}
69
70		if(pshared != PTHREAD_PROCESS_PRIVATE)
71		{
72			printf("Test FAILED: Incorrect pshared value: %d\n", pshared);
73			return PTS_FAIL;
74		}
75
76		/* Destory the cond attributes object */
77		if(pthread_condattr_destroy(&attr[i]) != 0)
78		{
79			perror("Error at pthread_condattr_destroy()\n");
80			return PTS_UNRESOLVED;
81		}
82	}
83
84	printf("Test PASSED\n");
85	return PTS_PASS;
86}
87