1176050Sdes/*-
2228953Suqs * Copyright (c) 2008 Dag-Erling Co��dan Sm��rgrav
3176050Sdes * All rights reserved.
4176050Sdes *
5176050Sdes * Redistribution and use in source and binary forms, with or without
6176050Sdes * modification, are permitted provided that the following conditions
7176050Sdes * are met:
8176050Sdes * 1. Redistributions of source code must retain the above copyright
9176050Sdes *    notice, this list of conditions and the following disclaimer
10176050Sdes *    in this position and unchanged.
11176050Sdes * 2. Redistributions in binary form must reproduce the above copyright
12176050Sdes *    notice, this list of conditions and the following disclaimer in the
13176050Sdes *    documentation and/or other materials provided with the distribution.
14176050Sdes *
15176050Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16176050Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17176050Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18176050Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19176050Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20176050Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21176050Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22176050Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23176050Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24176050Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25176050Sdes * SUCH DAMAGE.
26176050Sdes *
27176050Sdes * $FreeBSD: releng/10.3/tools/regression/pthread/mutex_isowned_np/mutex_isowned_np.c 228953 2011-12-29 12:33:27Z uqs $
28176050Sdes */
29176050Sdes
30176050Sdes#include <pthread.h>
31176050Sdes#include <pthread_np.h>
32176050Sdes#include <stdio.h>
33176050Sdes#include <stdlib.h>
34176050Sdes
35176050Sdesstatic void *
36176050Sdesthread(void *arg)
37176050Sdes{
38176050Sdes	pthread_mutex_t *mtx = arg;
39176050Sdes
40176050Sdes	if (pthread_mutex_isowned_np(mtx) != 0) {
41176050Sdes		printf("pthread_mutex_isowned_np() returned non-zero\n"
42176050Sdes		    "for a mutex held by another thread\n");
43176050Sdes		exit(1);
44176050Sdes	}
45176050Sdes	return (NULL);
46176050Sdes}
47176050Sdes
48176050Sdesint
49176050Sdesmain(int argc, char *argv[])
50176050Sdes{
51176050Sdes	pthread_t thr;
52176050Sdes	pthread_mutex_t mtx;
53176050Sdes
54176050Sdes	pthread_mutex_init(&mtx, NULL);
55176050Sdes	if (pthread_mutex_isowned_np(&mtx) != 0) {
56176050Sdes		printf("pthread_mutex_isowned_np() returned non-zero\n"
57176050Sdes		    "for a mutex that is not held\n");
58176050Sdes		exit(1);
59176050Sdes	}
60176050Sdes	pthread_mutex_lock(&mtx);
61176050Sdes	if (pthread_mutex_isowned_np(&mtx) == 0) {
62176050Sdes		printf("pthread_mutex_isowned_np() returned zero\n"
63176050Sdes		    "for a mutex we hold ourselves\n");
64176050Sdes		exit(1);
65176050Sdes	}
66176050Sdes	pthread_create(&thr, NULL, thread, &mtx);
67176050Sdes	pthread_join(thr, NULL);
68176050Sdes	pthread_mutex_unlock(&mtx);
69176050Sdes	if (pthread_mutex_isowned_np(&mtx) != 0) {
70176050Sdes		printf("pthread_mutex_isowned_np() returned non-zero\n"
71176050Sdes		    "for a mutex that is not held\n");
72176050Sdes		exit(1);
73176050Sdes	}
74176050Sdes
75176050Sdes	printf("OK\n");
76176050Sdes	exit(0);
77176050Sdes}
78