3-2.c revision 356187c5bed5241d31ab609b78fc751acc7da651
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_ERRORCHECK
11
12 * Provides errorchecking.  A thread attempting to relock this mutex without unlocking it
13 * first will return with an error.  A thread attempting to unlock a mutex which another
14 * thread has locked will return with an error.  A thread attempting to unlock an unlocked
15 * mutex will return with an error.
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_ERRORCHECK.
20 * 3.  Create a mutex with that mutexattr object.
21 * 4.  Lock the mutex.
22 * 5.  Create a thread, and in that thread, attempt to unlock the mutex. It should return an
23 *     error.
24 *
25 */
26
27#define _XOPEN_SOURCE 600
28
29#include <pthread.h>
30#include <stdio.h>
31#include <errno.h>
32#include "posixtest.h"
33
34pthread_t thread1;
35pthread_mutex_t mutex;
36pthread_mutexattr_t mta;
37
38int ret;			/* Return value of the thread unlocking the mutex. */
39
40void *a_thread_func()
41{
42	/* Try to unlock the mutex that main already locked. */
43	ret=pthread_mutex_unlock(&mutex);
44	pthread_exit((void*)0);
45}
46
47
48int main()
49{
50
51	/* Initialize a mutex attributes object */
52	if(pthread_mutexattr_init(&mta) != 0)
53	{
54		perror("Error at pthread_mutexattr_init()\n");
55		return PTS_UNRESOLVED;
56	}
57
58	 /* Set the 'type' attribute to be PTHREAD_MUTEX_ERRORCHECK  */
59	if(pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK) != 0)
60	{
61		printf("Test FAILED: Error setting the attribute 'type'\n");
62		return PTS_FAIL;
63	}
64
65	/* Initialize the mutex with that attribute obj. */
66	if(pthread_mutex_init(&mutex, &mta) != 0)
67	{
68		perror("Error intializing the mutex.\n");
69		return PTS_UNRESOLVED;
70	}
71
72	/* Lock the mutex. */
73	if(pthread_mutex_lock(&mutex) != 0 )
74	{
75		perror("Error locking the mutex first time around.\n");
76		return PTS_UNRESOLVED;
77	}
78
79	/* Create the thread that will try to unlock the mutex we just locked. */
80	if(pthread_create(&thread1, NULL, a_thread_func, NULL) != 0)
81	{
82		perror("Error creating a thread.\n");
83		return PTS_UNRESOLVED;
84	}
85
86	/* Wait for that thread to end execution */
87	pthread_join(thread1, NULL);
88
89	if(ret == 0)
90	{
91		printf("Test FAILED: Expected an error when trying to unlock a mutex that was locked by another thread.  Returned 0 instead.\n");
92		return PTS_FAIL;
93	}
94
95	/* cleanup */
96	pthread_mutex_unlock(&mutex);
97	pthread_mutex_destroy(&mutex);
98
99	if(pthread_mutexattr_destroy(&mta))
100	{
101		perror("Error at pthread_mutexattr_destory().\n");
102		return PTS_UNRESOLVED;
103	}
104
105	printf("Test PASSED\n");
106	return PTS_PASS;
107}
108