aboutsummaryrefslogtreecommitdiff
path: root/testcases/open_posix_testsuite/conformance/interfaces/pthread_mutexattr_settype/2-1.c
blob: c73d7cd81547dbd517f3a910ac895205370e9daa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Copyright (c) 2002, Intel Corporation. All rights reserved.
 * Created by:  bing.wei.liu REMOVE-THIS AT intel DOT com
 * This file is licensed under the GPL license.  For the full content
 * of this license, see the COPYING file at the top level of this
 * source tree.

 * Test pthread_mutexattr_settype()
 *
 *  PTHREAD_MUTEX_NORMAL

 *  This type of mutex doesn't detect deadlock.  So a thread attempting to relock this mutex
 *  without unlocking it first will not return an error.  Attempting to unlock a mutex locked
 *  by a different thread results in undefined behavior.  Attemping to unlock an unlocked mutex
 *  results in undefined behavior.
 *
 * Steps:
 * 1.  Initialize a pthread_mutexattr_t object with pthread_mutexattr_init()
 * 2   Set the 'type' of the mutexattr object to PTHREAD_MUTEX_NORMAL.
 * 3.  Create a mutex with that mutexattr object.
 * 4.  Lock the mutex, then relock it. Expect dead lock. Timer will be use
 *     to interrupt the deadlock.
 */


#include <pthread.h>
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include "posixtest.h"

void alarm_handler(int signo)
{
	printf("Got SIGALRM after 1 second\n");
	printf("Test PASSED\n");
	exit(PTS_PASS);
}

int main(void)
{
	pthread_mutex_t mutex;
	pthread_mutexattr_t mta;
	int ret;

	/* Initialize a mutex attributes object */
	if (pthread_mutexattr_init(&mta) != 0) {
		perror("Error at pthread_mutexattr_init()\n");
		return PTS_UNRESOLVED;
	}

	/* Set the 'type' attribute to be PTHREAD_MUTEX_NORMAL  */
	if (pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_NORMAL) != 0) {
		printf("Test FAILED: Error setting the attribute 'type'\n");
		return PTS_FAIL;
	}

	/* Initialize the mutex with that attribute obj. */
	if (pthread_mutex_init(&mutex, &mta) != 0) {
		perror("Error initializing the mutex.\n");
		return PTS_UNRESOLVED;
	}

	ret = pthread_mutex_lock(&mutex);
	if (ret != 0) {
		printf("Test Unresolved: Error at pthread_mutex_lock, "
		       "error code %d\n", ret);
		return PTS_UNRESOLVED;
	}

	signal(SIGALRM, alarm_handler);
	alarm(1);
	/* This lock will cause deadlock */
	ret = pthread_mutex_lock(&mutex);
	/* We should not get here */
	printf("Relock the mutex did not get deadlock\n");
	printf("Test FAILED\n");
	return PTS_FAIL;
}