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_mutexattr_setpshared()
9 *
10 *  It shall set the process-shared attribute in an initialized attributes object 'attr'.
11
12 * The process-shared attribute is set to PTHREAD_PROCESS_SHARED to permit a mutex to be
13 * operated upon by any thread that has access to the memory where the mutex is allocated,
14 * even if the mutex is allocated in memory that is shared by multiple processes.
15 *
16 * If the process-shared attribute is PTHREAD_PROCESS_PRIVATE, the mutex shall only be
17 * operated upon by threads created within the same process as the thread that initialized
18 * the mutex; if threads of differing processes attempt to operate on such a mutex,
19 * the behavior is undefined.
20 *
21 * Steps:
22 *
23 * Explanation:  To share a mutex between 2 processes, you need to map shared memory for
24 * the mutex.  So whether the 'type' of the mutexattr is shared or private, it really will
25 * not make a difference since both processes will always have access to the shared memory
26 * as long as they the pointer to it.  So all we check here is that you can actually call
27 * the pthread_mutexattr_setpshared() function, passing to it PTHREAD_PROCESS_SHARED and
28 * PTHREAD_PROCESS_PRIVATE.
29 *
30 * 1. Initialize a pthread_mutexattr_t object.
31 * 2. Call pthread_mutexattr_getpshared(), passing to it both PTHREAD_PROCESS_SHARE and
32 *    PTHREAD_PROCESS_PRIVATE.
33 *
34 */
35
36#include <pthread.h>
37#include <stdio.h>
38#include "posixtest.h"
39
40pthread_mutex_t new_mutex;	/* The mutex. */
41
42
43int main()
44{
45	pthread_mutexattr_t mta;
46	int ret;
47
48	/* Initialize a mutex attributes object */
49	if(pthread_mutexattr_init(&mta) != 0)
50	{
51		perror("Error at pthread_mutexattr_init()\n");
52		return PTS_UNRESOLVED;
53	}
54
55	/* Set the 'pshared' attribute to PTHREAD_PROCESS_PRIVATE */
56	if((ret=pthread_mutexattr_setpshared(&mta, PTHREAD_PROCESS_PRIVATE)) != 0)
57	{
58		printf("Test FAILED: Cannot set pshared attribute to PTHREAD_PROCESS_PRIVATE. Error: %d\n", ret);
59		return PTS_FAIL;
60	}
61
62	/* Set the 'pshared' attribute to PTHREAD_PROCESS_SHARED */
63	if((ret=pthread_mutexattr_setpshared(&mta, PTHREAD_PROCESS_SHARED)) != 0)
64	{
65		printf("Test FAILED: Cannot set pshared attribute to PTHREAD_PROCESS_SHARED. Error code: %d\n", ret);
66		return PTS_FAIL;
67	}
68
69	printf("Test PASSED\n");
70	return PTS_PASS;
71}
72