Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/tools/regression/pthread/mutex_isowned_np/mutex_isowned_np.c
39491 views
1
/*-
2
* Copyright (c) 2008 Dag-Erling Smørgrav
3
* All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer
10
* in this position and unchanged.
11
* 2. Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
*
15
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
* SUCH DAMAGE.
26
*/
27
28
#include <pthread.h>
29
#include <pthread_np.h>
30
#include <stdio.h>
31
#include <stdlib.h>
32
33
static void *
34
thread(void *arg)
35
{
36
pthread_mutex_t *mtx = arg;
37
38
if (pthread_mutex_isowned_np(mtx) != 0) {
39
printf("pthread_mutex_isowned_np() returned non-zero\n"
40
"for a mutex held by another thread\n");
41
exit(1);
42
}
43
return (NULL);
44
}
45
46
int
47
main(void)
48
{
49
pthread_t thr;
50
pthread_mutex_t mtx;
51
52
pthread_mutex_init(&mtx, NULL);
53
if (pthread_mutex_isowned_np(&mtx) != 0) {
54
printf("pthread_mutex_isowned_np() returned non-zero\n"
55
"for a mutex that is not held\n");
56
exit(1);
57
}
58
pthread_mutex_lock(&mtx);
59
if (pthread_mutex_isowned_np(&mtx) == 0) {
60
printf("pthread_mutex_isowned_np() returned zero\n"
61
"for a mutex we hold ourselves\n");
62
exit(1);
63
}
64
pthread_create(&thr, NULL, thread, &mtx);
65
pthread_join(thr, NULL);
66
pthread_mutex_unlock(&mtx);
67
if (pthread_mutex_isowned_np(&mtx) != 0) {
68
printf("pthread_mutex_isowned_np() returned non-zero\n"
69
"for a mutex that is not held\n");
70
exit(1);
71
}
72
73
printf("OK\n");
74
exit(0);
75
}
76
77