Path: blob/main/sys/contrib/openzfs/lib/libspl/mutex.c
96339 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.23* Copyright (c) 2012, 2018 by Delphix. All rights reserved.24* Copyright (c) 2016 Actifio, Inc. All rights reserved.25* Copyright (c) 2025, Klara, Inc.26*/2728#include <assert.h>29#include <pthread.h>30#include <string.h>31#include <errno.h>32#include <sys/mutex.h>3334/*35* =========================================================================36* mutexes37* =========================================================================38*/3940void41mutex_init(kmutex_t *mp, char *name, int type, void *cookie)42{43(void) name, (void) type, (void) cookie;44VERIFY0(pthread_mutex_init(&mp->m_lock, NULL));45memset(&mp->m_owner, 0, sizeof (pthread_t));46}4748void49mutex_destroy(kmutex_t *mp)50{51VERIFY0(pthread_mutex_destroy(&mp->m_lock));52}5354void55mutex_enter(kmutex_t *mp)56{57VERIFY0(pthread_mutex_lock(&mp->m_lock));58mp->m_owner = pthread_self();59}6061int62mutex_enter_check_return(kmutex_t *mp)63{64int error = pthread_mutex_lock(&mp->m_lock);65if (error == 0)66mp->m_owner = pthread_self();67return (error);68}6970int71mutex_tryenter(kmutex_t *mp)72{73int error = pthread_mutex_trylock(&mp->m_lock);74if (error == 0) {75mp->m_owner = pthread_self();76return (1);77} else {78VERIFY3S(error, ==, EBUSY);79return (0);80}81}8283void84mutex_exit(kmutex_t *mp)85{86memset(&mp->m_owner, 0, sizeof (pthread_t));87VERIFY0(pthread_mutex_unlock(&mp->m_lock));88}899091