MPSAFE - Add a set of general blocking/spinnable mutex functions.
[dragonfly.git] / sys / sys / mutex.h
blob82a43c914beef06f9ddb721a70f0aee197921486
1 /*
2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #ifndef _SYS_MUTEX_H_
36 #define _SYS_MUTEX_H_
38 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
40 #ifndef _SYS_TYPES_H_
41 #include <sys/types.h>
42 #endif
43 #ifndef _MACHINE_ATOMIC_H_
44 #include <machine/atomic.h>
45 #endif
46 #ifndef _MACHINE_CPUFUNC_H_
47 #include <machine/cpufunc.h>
48 #endif
51 * The general mutex structure provides recursive shared and exclusive
52 * locks, downgrade, a non-blocking upgrade, and various other functions.
54 * The structure is 16-byte aligned and either 16 or 32 bytes, designed
55 * for 32 or 64 bit cpus.
57 struct thread;
59 typedef struct mtx {
60 volatile u_int mtx_lock;
61 int mtx_refs;
62 struct thread *mtx_owner;
63 #if LONG_BIT == 32
64 int mtx_unused;
65 #endif
66 } *mtx_t;
68 #define MTX_EXCLUSIVE 0x80000000
69 #define MTX_SHWANTED 0x40000000
70 #define MTX_EXWANTED 0x20000000
71 #define MTX_MASK 0x0FFFFFFF
73 #define MTX_PCATCH 0x00000001
75 #define MTX_OWNER_NONE NULL
76 #define MTX_OWNER_ANON (struct thread *)-2)
78 #endif
81 * See also sys/mutex2.h
83 #ifdef _KERNEL
85 void _mtx_lock_ex(mtx_t mtx, const char *ident, int flags);
86 void _mtx_lock_sh(mtx_t mtx, const char *ident, int flags);
87 void _mtx_spinlock_ex(mtx_t mtx);
88 void _mtx_spinlock_sh(mtx_t mtx);
89 int _mtx_lock_ex_try(mtx_t mtx);
90 int _mtx_lock_sh_try(mtx_t mtx);
91 void _mtx_downgrade(mtx_t mtx);
92 int _mtx_upgrade_try(mtx_t mtx);
93 void _mtx_unlock(mtx_t mtx);
95 #endif
97 #endif