xref: /f-stack/lib/include/sys/condvar.h (revision 1f5a5310)
1 /*
2  * Copyright (c) 2000 Jake Burkholder <[email protected]>.All rights reserved.
3  * Copyright (c) 2010 Kip Macy All rights reserved.
4  * Copyright (c) 2013 Patrick Kelsey. All rights reserved.
5  * Copyright (C) 2017-2021 THL A29 Limited, a Tencent company.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice, this
12  *   list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  *   this list of conditions and the following disclaimer in the documentation
15  *   and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef _FSTACK_SYS_CONDVAR_H_
31 #define _FSTACK_SYS_CONDVAR_H_
32 
33 #ifndef LOCORE
34 #include <sys/queue.h>
35 
36 #include "ff_host_interface.h"
37 
38 struct lock_object;
39 struct thread;
40 
41 TAILQ_HEAD(cv_waitq, thread);
42 
43 /*
44  * Condition variable.  The waiters count is protected by the mutex that
45  * protects the condition; that is, the mutex that is passed to cv_wait*()
46  * and is held across calls to cv_signal() and cv_broadcast().  It is an
47  * optimization to avoid looking up the sleep queue if there are no waiters.
48  */
49 struct cv {
50     const char *cv_description;
51     ff_cond_t cv_cond;
52 };
53 
54 #ifdef _KERNEL
55 void cv_init(struct cv *cvp, const char *desc);
56 void cv_destroy(struct cv *cvp);
57 
58 void _cv_wait(struct cv *cvp, struct lock_object *lock);
59 void _cv_wait_unlock(struct cv *cvp, struct lock_object *lock);
60 int _cv_wait_sig(struct cv *cvp, struct lock_object *lock);
61 int _cv_timedwait(struct cv *cvp, struct lock_object *lock, int timo);
62 int _cv_timedwait_sig(struct cv *cvp, struct lock_object *lock, int timo);
63 int _cv_timedwait_sig_sbt(struct cv *cvp, struct lock_object *lock,
64     sbintime_t sbt, sbintime_t pr, int flags);
65 
66 void cv_signal(struct cv *cvp);
67 void cv_broadcastpri(struct cv *cvp, int pri);
68 
69 #define cv_wait(cvp, lock)                        \
70     _cv_wait((cvp), &(lock)->lock_object)
71 #define cv_wait_unlock(cvp, lock)                    \
72     _cv_wait_unlock((cvp), &(lock)->lock_object)
73 #define cv_wait_sig(cvp, lock)                        \
74     _cv_wait_sig((cvp), &(lock)->lock_object)
75 #define cv_timedwait(cvp, lock, timo)                    \
76     _cv_timedwait((cvp), &(lock)->lock_object, (timo))
77 #define cv_timedwait_sig(cvp, lock, timo)                \
78     _cv_timedwait_sig((cvp), &(lock)->lock_object, (timo))
79 #define cv_timedwait_sig_sbt(cvp, lock, sbt, pr, flags)            \
80     _cv_timedwait_sig_sbt((cvp), &(lock)->lock_object, (sbt), (pr), (flags))
81 
82 #define cv_broadcast(cvp) cv_broadcastpri(cvp, 0)
83 
84 #define cv_wmesg(cvp) ((cvp)->cv_description)
85 
86 #endif    /* _KERNEL */
87 #endif    /* !LOCORE */
88 #endif    /* _FSTACK_SYS_CONDVAR_H_ */
89