b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross/*
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * CDDL HEADER START
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross *
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * The contents of this file are subject to the terms of the
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * Common Development and Distribution License (the "License").
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * You may not use this file except in compliance with the License.
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross *
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * or http://www.opensolaris.org/os/licensing.
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * See the License for the specific language governing permissions
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * and limitations under the License.
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross *
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * When distributing Covered Code, include this CDDL HEADER in each
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * If applicable, add the following below this CDDL HEADER, with the
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * fields enclosed by brackets "[]" replaced with your own identifying
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * information: Portions Copyright [yyyy] [name of copyright owner]
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross *
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * CDDL HEADER END
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross */
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross/*
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross */
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross#ifndef _SYS_MUTEX_H
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross#define _SYS_MUTEX_H
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross#include <sys/synch.h> /* lwp_mutex_t */
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross#ifdef __cplusplus
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Rossextern "C" {
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross#endif
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross/*
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * Public interface to mutual exclusion locks. See mutex(9F) for details.
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross *
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * The basic mutex type is MUTEX_ADAPTIVE, which is expected to be used
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * in almost all of the kernel. MUTEX_SPIN provides interrupt blocking
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * and must be used in interrupt handlers above LOCK_LEVEL. The iblock
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * cookie argument to mutex_init() encodes the interrupt level to block.
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * The iblock cookie must be NULL for adaptive locks.
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross *
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * MUTEX_DEFAULT is the type usually specified (except in drivers) to
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * mutex_init(). It is identical to MUTEX_ADAPTIVE.
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross *
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * MUTEX_DRIVER is always used by drivers. mutex_init() converts this to
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * either MUTEX_ADAPTIVE or MUTEX_SPIN depending on the iblock cookie.
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross *
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * Mutex statistics can be gathered on the fly, without rebooting or
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * recompiling the kernel, via the lockstat driver (lockstat(7D)).
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross */
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Rosstypedef enum {
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross MUTEX_ADAPTIVE = 0, /* spin if owner is running, otherwise block */
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross MUTEX_SPIN = 1, /* block interrupts and spin */
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross MUTEX_DRIVER = 4, /* driver (DDI) mutex */
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross MUTEX_DEFAULT = 6 /* kernel default mutex */
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross} kmutex_type_t;
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Rossstruct _kmutex {
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross lwp_mutex_t m_lock;
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross void *m_owner;
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross};
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Rosstypedef struct _kmutex kmutex_t;
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross#if defined(_KERNEL) || defined(_FAKE_KERNEL)
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross/* See the real sys/mutex.h */
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Rosstypedef struct pad_mutex {
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross kmutex_t pad_mutex;
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross#ifdef _LP64
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross char pad_pad[64 - sizeof (kmutex_t)];
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross#endif
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross} pad_mutex_t;
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross#endif /* _KERNEL */
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross#define MUTEX_HELD(x) (mutex_owned(x))
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross#define MUTEX_NOT_HELD(x) (!mutex_owned(x) || panicstr)
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross/*
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * We're simulating the kernel mutex API here, and the
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * user-level has a different signature, so rename.
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross */
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross#define mutex_init kmutex_init
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross#define mutex_destroy kmutex_destroy
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Rossextern void kmutex_init(kmutex_t *, char *, kmutex_type_t, void *);
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Rossextern void kmutex_destroy(kmutex_t *);
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Rossextern void mutex_enter(kmutex_t *);
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Rossextern int mutex_tryenter(kmutex_t *);
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Rossextern void mutex_exit(kmutex_t *);
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Rossextern int mutex_owned(const kmutex_t *);
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Rossextern void *mutex_owner(const kmutex_t *);
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross#ifdef __cplusplus
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross}
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross#endif
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross#endif /* _SYS_MUTEX_H */