Path: blob/main/tools/regression/pthread/mutex_isowned_np/mutex_isowned_np.c
39491 views
/*-1* Copyright (c) 2008 Dag-Erling Smørgrav2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer9* in this position and unchanged.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <pthread.h>28#include <pthread_np.h>29#include <stdio.h>30#include <stdlib.h>3132static void *33thread(void *arg)34{35pthread_mutex_t *mtx = arg;3637if (pthread_mutex_isowned_np(mtx) != 0) {38printf("pthread_mutex_isowned_np() returned non-zero\n"39"for a mutex held by another thread\n");40exit(1);41}42return (NULL);43}4445int46main(void)47{48pthread_t thr;49pthread_mutex_t mtx;5051pthread_mutex_init(&mtx, NULL);52if (pthread_mutex_isowned_np(&mtx) != 0) {53printf("pthread_mutex_isowned_np() returned non-zero\n"54"for a mutex that is not held\n");55exit(1);56}57pthread_mutex_lock(&mtx);58if (pthread_mutex_isowned_np(&mtx) == 0) {59printf("pthread_mutex_isowned_np() returned zero\n"60"for a mutex we hold ourselves\n");61exit(1);62}63pthread_create(&thr, NULL, thread, &mtx);64pthread_join(thr, NULL);65pthread_mutex_unlock(&mtx);66if (pthread_mutex_isowned_np(&mtx) != 0) {67printf("pthread_mutex_isowned_np() returned non-zero\n"68"for a mutex that is not held\n");69exit(1);70}7172printf("OK\n");73exit(0);74}757677