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 */
32
33 #ifndef _SYS_MUTEX_H_
34 #define _SYS_MUTEX_H_
35
36 #include <pthread.h>
37
38 /*
39 * Mutex types and options passed to mtx_init(). MTX_QUIET and MTX_DUPOK
40 * can also be passed in.
41 */
42 #define MTX_DEF 0x00000000 /* DEFAULT (sleep) lock */
43 #define MTX_SPIN 0x00000001 /* Spin lock (disables interrupts) */
44 #define MTX_RECURSE 0x00000004 /* Option: lock allowed to recurse */
45 #define MTX_NOWITNESS 0x00000008 /* Don't do any witness checking. */
46 #define MTX_NOPROFILE 0x00000020 /* Don't profile this lock */
47
48 #define MA_OWNED 1
49 #define MA_NOTOWNED 2
50
51 struct pthread_mutex;
52 #define mtx pthread_mutex
53
54
55 static inline void
mtx_init(pthread_mutex_t * mtx,const char * name,const char * type,int opts)56 mtx_init(pthread_mutex_t *mtx, const char *name, const char *type, int opts)
57 {
58 pthread_mutexattr_t attr;
59 (void)pthread_mutexattr_init(&attr);
60 if ((unsigned int)opts & MTX_RECURSE)
61 (void)pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
62 pthread_mutex_init(mtx, &attr);
63 }
64
65 #define mtx_destroy(m) (void)pthread_mutex_destroy((m))
66 #define mtx_lock(m) (void)pthread_mutex_lock((m))
67 #define mtx_unlock(m) (void)pthread_mutex_unlock((m))
68 #define mtx_lock_spin(m)
69 #define mtx_unlock_spin(m)
70 #define mtx_owned(m) (((m)->owner == (void *)OsCurrTaskGet()) ? (1) : (0))
71
72 #define _mtx_assert(m, what) do { \
73 switch (what) { \
74 case MA_OWNED: \
75 /* if (!mtx_owned(m)){ panic("mtx assert at %s:%d\n", __FUNCTION__, __LINE__);}*/ \
76 break; \
77 case MA_NOTOWNED: \
78 if (mtx_owned(m)){ panic("mtx assert at %s:%d\n", __FUNCTION__, __LINE__);} \
79 break; \
80 default: \
81 {panic("mtx assert at %s:%d\n", __FUNCTION__, __LINE__);} \
82 } \
83 } while (0)
84
85 #ifdef INVARIANTS
86 #define mtx_assert_(m, what, file, line) \
87 _mtx_assert((m), (what), (file), (line))
88
89 #define GIANT_REQUIRED mtx_assert_(&Giant, MA_OWNED, __FILE__, __LINE__)
90
91 #else /* INVARIANTS */
92 #define mtx_assert_(m, what, file, line) (void)0
93 #define GIANT_REQUIRED
94 #endif /* INVARIANTS */
95
96 #define mtx_assert(m, what) mtx_assert_(m, what, __FILE__, __LINE__)
97
98 #define thread_lock(td) LOS_TaskLock()
99 #define thread_unlock(td) LOS_TaskUnlock()
100
101 #endif /* _SYS_MUTEX_H_ */
102