• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_trylock()
9  *   is equivalent to pthread_mutex_lock() except that if the mutex object
10  *   referenced by 'mutex' is currently locked (by any thread, including the
11  *   current thread), the call shall return immediately.
12 
13  * Steps:
14  *   -- Initilize a mutex object
15  *   -- Create a secondary thread and have it lock the mutex
16  *   -- Within the main thread, try to lock the mutex using
17  	pthread_mutex_trylock() and EBUSY is expected
18  *   -- Have the secondary thread unlock the mutex
19  *   -- Within the main thread, try to lock the mutex again
20  	and expect a successful locking.
21  *
22  */
23 
24 #include <pthread.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include "posixtest.h"
29 
30 static void *func(void *parm);
31 
32 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
33 static int t1_start = 0;
34 static int t1_pause = 1;
35 
main(void)36 int main(void)
37 {
38 	int i, rc;
39 	pthread_t t1;
40 
41 	/* Create a secondary thread and wait until it has locked the mutex */
42 	pthread_create(&t1, NULL, func, NULL);
43 	while (!t1_start)
44 		sleep(1);
45 
46 	/* Trylock the mutex and expect it returns EBUSY */
47 	rc = pthread_mutex_trylock(&mutex);
48 	if (rc != EBUSY) {
49 		fprintf(stderr, "Expected %d(EBUSY), got %d\n", EBUSY, rc);
50 		printf("Test FAILED\n");
51 		return PTS_FAIL;
52 	}
53 
54 	/* Allow the secondary thread to go ahead */
55 	t1_pause = 0;
56 
57 	/* Trylock the mutex for N times */
58 	for (i = 0; i < 5; i++) {
59 		rc = pthread_mutex_trylock(&mutex);
60 		if (rc == 0) {
61 			pthread_mutex_unlock(&mutex);
62 			break;
63 		} else if (rc == EBUSY) {
64 			sleep(1);
65 			continue;
66 		} else {
67 			fprintf(stderr,
68 				"Unexpected error code(%d) for pthread_mutex_lock()\n",
69 				rc);
70 			return PTS_UNRESOLVED;
71 		}
72 	}
73 
74 	/* Clean up */
75 	pthread_join(t1, NULL);
76 	pthread_mutex_destroy(&mutex);
77 
78 	if (i >= 5) {
79 		fprintf(stderr,
80 			"Have tried %d times but failed to get the mutex\n", i);
81 		return PTS_UNRESOLVED;
82 	}
83 	printf("Test PASSED\n");
84 	return PTS_PASS;
85 }
86 
func(void * parm PTS_ATTRIBUTE_UNUSED)87 static void *func(void *parm PTS_ATTRIBUTE_UNUSED)
88 {
89 	int rc;
90 
91 	if ((rc = pthread_mutex_lock(&mutex)) != 0) {
92 		fprintf(stderr, "Error at pthread_mutex_lock(), rc=%d\n", rc);
93 		pthread_exit((void *)PTS_UNRESOLVED);
94 	}
95 	t1_start = 1;
96 
97 	while (t1_pause)
98 		sleep(1);
99 
100 	if ((rc = pthread_mutex_unlock(&mutex)) != 0) {
101 		fprintf(stderr, "Error at pthread_mutex_unlock(), rc=%d\n", rc);
102 		pthread_exit((void *)PTS_UNRESOLVED);
103 	}
104 
105 	pthread_exit(0);
106 	return (void *)(0);
107 }
108