blob: 18352e8e460152f496a9f7c11f01212e4a07c6ea [file] [log] [blame]
Garrett Cooper2c282152010-12-16 00:55:50 -08001/*
robbiew0dc07652005-06-03 16:29:48 +00002 * 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
Garrett Cooper2c282152010-12-16 00:55:50 -08005 * of this license, see the COPYING file at the top level of this
robbiew0dc07652005-06-03 16:29:48 +00006 * source tree.
7
8 * Test that pthread_mutexattr_setpshared()
Garrett Cooper2c282152010-12-16 00:55:50 -08009 *
robbiew0dc07652005-06-03 16:29:48 +000010 * 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 *
Garrett Cooper2c282152010-12-16 00:55:50 -080016 * If the process-shared attribute is PTHREAD_PROCESS_PRIVATE, the mutex shall only be
robbiew0dc07652005-06-03 16:29:48 +000017 * 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.
Garrett Cooper2c282152010-12-16 00:55:50 -080033 *
robbiew0dc07652005-06-03 16:29:48 +000034 */
35
36#include <pthread.h>
37#include <stdio.h>
38#include "posixtest.h"
39
40pthread_mutex_t new_mutex; /* The mutex. */
41
robbiew0dc07652005-06-03 16:29:48 +000042int main()
43{
44 pthread_mutexattr_t mta;
45 int ret;
46
47 /* Initialize a mutex attributes object */
Wanlong Gao354ebb42012-12-07 10:10:04 +080048 if (pthread_mutexattr_init(&mta) != 0) {
robbiew0dc07652005-06-03 16:29:48 +000049 perror("Error at pthread_mutexattr_init()\n");
50 return PTS_UNRESOLVED;
51 }
Garrett Cooper2c282152010-12-16 00:55:50 -080052
robbiew0dc07652005-06-03 16:29:48 +000053 /* Set the 'pshared' attribute to PTHREAD_PROCESS_PRIVATE */
Wanlong Gao354ebb42012-12-07 10:10:04 +080054 if ((ret =
55 pthread_mutexattr_setpshared(&mta,
56 PTHREAD_PROCESS_PRIVATE)) != 0) {
57 printf
58 ("Test FAILED: Cannot set pshared attribute to PTHREAD_PROCESS_PRIVATE. Error: %d\n",
59 ret);
robbiew0dc07652005-06-03 16:29:48 +000060 return PTS_FAIL;
61 }
62
63 /* Set the 'pshared' attribute to PTHREAD_PROCESS_SHARED */
Wanlong Gao354ebb42012-12-07 10:10:04 +080064 if ((ret =
65 pthread_mutexattr_setpshared(&mta, PTHREAD_PROCESS_SHARED)) != 0) {
66 printf
67 ("Test FAILED: Cannot set pshared attribute to PTHREAD_PROCESS_SHARED. Error code: %d\n",
68 ret);
robbiew0dc07652005-06-03 16:29:48 +000069 return PTS_FAIL;
70 }
Garrett Cooper2c282152010-12-16 00:55:50 -080071
robbiew0dc07652005-06-03 16:29:48 +000072 printf("Test PASSED\n");
73 return PTS_PASS;
Chris Dearmanec6edca2012-10-17 19:54:01 -070074}