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_mutex_init() 9 * Upon successful initialization, the state of the mutex becomes 10 * initialized and unlocked. 11 * 12 */ 13 14 #include <pthread.h> 15 #include <stdio.h> 16 #include <unistd.h> 17 #include "posixtest.h" 18 19 int main(void) 20 { 21 pthread_mutex_t mutex; 22 int rc; 23 24 /* Initialize a mutex object */ 25 if ((rc = pthread_mutex_init(&mutex, NULL)) != 0) { 26 fprintf(stderr, "Fail to initialize mutex, rc=%d\n", rc); 27 printf("Test FAILED\n"); 28 return PTS_FAIL; 29 } 30 31 /* Acquire the mutex object using pthread_mutex_lock */ 32 if ((rc = pthread_mutex_lock(&mutex)) != 0) { 33 fprintf(stderr, "Fail to lock the mutex, rc=%d\n", rc); 34 printf("Test FAILED\n"); 35 return PTS_FAIL; 36 } 37 fprintf(stderr, "Main: hold the mutex for a while\n"); 38 sleep(1); 39 40 /* Release the mutex object using pthread_mutex_unlock */ 41 if ((rc = pthread_mutex_unlock(&mutex)) != 0) { 42 fprintf(stderr, "Fail to unlock the mutex, rc=%d\n", rc); 43 return PTS_UNRESOLVED; 44 } 45 46 /* Destory the mutex object */ 47 if ((rc = pthread_mutex_destroy(&mutex)) != 0) { 48 fprintf(stderr, "Fail to destory the mutex, rc=%d\n", rc); 49 return PTS_UNRESOLVED; 50 } 51 52 printf("Test PASSED\n"); 53 return PTS_PASS; 54 } 55