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 pthread_mutexattr_settype() 9 * 10 * PTHREAD_MUTEX_NORMAL 11 12 * This type of mutex doesn't detect deadlock. So a thread attempting to relock this mutex 13 * without unlocking it first will not return an error. Attempting to unlock a mutex locked 14 * by a different thread results in undefined behavior. Attemping to unlock an unlocked mutex 15 * results in undefined behavior. 16 * 17 * Steps: 18 * 1. Initialize a pthread_mutexattr_t object with pthread_mutexattr_init() 19 * 2 Set the 'type' of the mutexattr object to PTHREAD_MUTEX_NORMAL. 20 * 3. Create a mutex with that mutexattr object. 21 * 4. Lock the mutex, then relock it. Expect dead lock. Timer will be use 22 * to interrupt the deadlock. 23 */ 24 25 26 #include <pthread.h> 27 #include <stdio.h> 28 #include <errno.h> 29 #include <signal.h> 30 #include <unistd.h> 31 #include <stdlib.h> 32 #include "posixtest.h" 33 34 void alarm_handler(int signo) 35 { 36 printf("Got SIGALRM after 1 second\n"); 37 printf("Test PASSED\n"); 38 exit(PTS_PASS); 39 } 40 41 int main(void) 42 { 43 pthread_mutex_t mutex; 44 pthread_mutexattr_t mta; 45 int ret; 46 47 /* Initialize a mutex attributes object */ 48 if (pthread_mutexattr_init(&mta) != 0) { 49 perror("Error at pthread_mutexattr_init()\n"); 50 return PTS_UNRESOLVED; 51 } 52 53 /* Set the 'type' attribute to be PTHREAD_MUTEX_NORMAL */ 54 if (pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_NORMAL) != 0) { 55 printf("Test FAILED: Error setting the attribute 'type'\n"); 56 return PTS_FAIL; 57 } 58 59 /* Initialize the mutex with that attribute obj. */ 60 if (pthread_mutex_init(&mutex, &mta) != 0) { 61 perror("Error initializing the mutex.\n"); 62 return PTS_UNRESOLVED; 63 } 64 65 ret = pthread_mutex_lock(&mutex); 66 if (ret != 0) { 67 printf("Test Unresolved: Error at pthread_mutex_lock, " 68 "error code %d\n", ret); 69 return PTS_UNRESOLVED; 70 } 71 72 signal(SIGALRM, alarm_handler); 73 alarm(1); 74 /* This lock will cause deadlock */ 75 ret = pthread_mutex_lock(&mutex); 76 /* We should not get here */ 77 printf("Relock the mutex did not get deadlock\n"); 78 printf("Test FAILED\n"); 79 return PTS_FAIL; 80 } 81