Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 1 | /* |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 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 |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 5 | * of this license, see the COPYING file at the top level of this |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 6 | * source tree. |
| 7 | |
| 8 | * Test that pthread_mutexattr_setpshared() |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 9 | * |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 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 | * |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 16 | * If the process-shared attribute is PTHREAD_PROCESS_PRIVATE, the mutex shall only be |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 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. |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 33 | * |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 34 | */ |
| 35 | |
| 36 | #include <pthread.h> |
| 37 | #include <stdio.h> |
| 38 | #include "posixtest.h" |
| 39 | |
| 40 | pthread_mutex_t new_mutex; /* The mutex. */ |
| 41 | |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 42 | int main() |
| 43 | { |
| 44 | pthread_mutexattr_t mta; |
| 45 | int ret; |
| 46 | |
| 47 | /* Initialize a mutex attributes object */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame^] | 48 | if (pthread_mutexattr_init(&mta) != 0) { |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 49 | perror("Error at pthread_mutexattr_init()\n"); |
| 50 | return PTS_UNRESOLVED; |
| 51 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 52 | |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 53 | /* Set the 'pshared' attribute to PTHREAD_PROCESS_PRIVATE */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame^] | 54 | 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); |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 60 | return PTS_FAIL; |
| 61 | } |
| 62 | |
| 63 | /* Set the 'pshared' attribute to PTHREAD_PROCESS_SHARED */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame^] | 64 | 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); |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 69 | return PTS_FAIL; |
| 70 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 71 | |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 72 | printf("Test PASSED\n"); |
| 73 | return PTS_PASS; |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 74 | } |