1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Berkeley Software Design Inc's name may not be used to endorse or
15 * promote products derived from this software without specific prior
16 * written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * from BSDI $Id: mutex.h,v 2.7.2.35 2000/04/27 03:10:26 cp Exp $
31 * $FreeBSD$
32 */
33
34 #ifndef _SYS_MUTEX_H_
35 #define _SYS_MUTEX_H_
36
37 #include <pthread.h>
38
39 /*
40 * Mutex types and options passed to mtx_init(). MTX_QUIET and MTX_DUPOK
41 * can also be passed in.
42 */
43 #define MTX_DEF 0x00000000 /* DEFAULT (sleep) lock */
44 #define MTX_SPIN 0x00000001 /* Spin lock (disables interrupts) */
45 #define MTX_RECURSE 0x00000004 /* Option: lock allowed to recurse */
46 #define MTX_NOWITNESS 0x00000008 /* Don't do any witness checking. */
47 #define MTX_NOPROFILE 0x00000020 /* Don't profile this lock */
48
49 #define MA_OWNED 1
50 #define MA_NOTOWNED 2
51
52 struct pthread_mutex;
53 #define mtx pthread_mutex
54
55
56 static inline void
mtx_init(pthread_mutex_t * mtx,const char * name,const char * type,int opts)57 mtx_init(pthread_mutex_t *mtx, const char *name, const char *type, int opts)
58 {
59 pthread_mutexattr_t attr;
60 (void)pthread_mutexattr_init(&attr);
61 if ((unsigned int)opts & MTX_RECURSE)
62 (void)pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
63 pthread_mutex_init(mtx, &attr);
64 }
65
66 #define mtx_destroy(m) (void)pthread_mutex_destroy((m))
67 #define mtx_lock(m) (void)pthread_mutex_lock((m))
68 #define mtx_unlock(m) (void)pthread_mutex_unlock((m))
69 #define mtx_lock_spin(m)
70 #define mtx_unlock_spin(m)
71 #define mtx_owned(m) (((m)->owner == (void *)OsCurrTaskGet()) ? (1) : (0))
72
73 #define _mtx_assert(m, what) do { \
74 switch (what) { \
75 case MA_OWNED: \
76 /* if (!mtx_owned(m)){ panic("mtx assert at %s:%d\n", __FUNCTION__, __LINE__);}*/ \
77 break; \
78 case MA_NOTOWNED: \
79 if (mtx_owned(m)){ panic("mtx assert at %s:%d\n", __FUNCTION__, __LINE__);} \
80 break; \
81 default: \
82 {panic("mtx assert at %s:%d\n", __FUNCTION__, __LINE__);} \
83 } \
84 } while (0)
85
86 #ifdef INVARIANTS
87 #define mtx_assert_(m, what, file, line) \
88 _mtx_assert((m), (what), (file), (line))
89
90 #define GIANT_REQUIRED mtx_assert_(&Giant, MA_OWNED, __FILE__, __LINE__)
91
92 #else /* INVARIANTS */
93 #define mtx_assert_(m, what, file, line) (void)0
94 #define GIANT_REQUIRED
95 #endif /* INVARIANTS */
96
97 #define mtx_assert(m, what) mtx_assert_(m, what, __FILE__, __LINE__)
98
99 #define thread_lock(td) LOS_TaskLock()
100 #define thread_unlock(td) LOS_TaskUnlock()
101
102 #endif /* _SYS_MUTEX_H_ */
103