2-1.c revision 354ebb48db8e66a853a58379a4808d5dcd1ceac3
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#define _XOPEN_SOURCE 600
26
27#include <pthread.h>
28#include <stdio.h>
29#include <errno.h>
30#include <signal.h>
31#include <unistd.h>
32#include <stdlib.h>
33#include "posixtest.h"
34
35void alarm_handler(int signo)
36{
37	printf("Got SIGALRM after 1 second\n");
38	printf("Test PASSED\n");
39	exit(PTS_PASS);
40}
41
42int main()
43{
44	pthread_mutex_t mutex;
45	pthread_mutexattr_t mta;
46	int ret;
47
48	/* Initialize a mutex attributes object */
49	if (pthread_mutexattr_init(&mta) != 0) {
50		perror("Error at pthread_mutexattr_init()\n");
51		return PTS_UNRESOLVED;
52	}
53
54	/* Set the 'type' attribute to be PTHREAD_MUTEX_NORMAL  */
55	if (pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_NORMAL) != 0) {
56		printf("Test FAILED: Error setting the attribute 'type'\n");
57		return PTS_FAIL;
58	}
59
60	/* Initialize the mutex with that attribute obj. */
61	if (pthread_mutex_init(&mutex, &mta) != 0) {
62		perror("Error intializing the mutex.\n");
63		return PTS_UNRESOLVED;
64	}
65
66	ret = pthread_mutex_lock(&mutex);
67	if (ret != 0) {
68		printf("Test Unresolved: Error at pthread_mutex_lock, "
69		       "error code %d\n", ret);
70		return PTS_UNRESOLVED;
71	}
72
73	signal(SIGALRM, alarm_handler);
74	alarm(1);
75	/* This lock will cause deadlock */
76	ret = pthread_mutex_lock(&mutex);
77	/* We should not get here */
78	printf("Relock the mutex did not get deadlock\n");
79	printf("Test FAILED\n");
80	return PTS_FAIL;
81}
82