Motr  M0
cond.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2011-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #include "lib/cond.h"
24 #include "lib/mutex.h"
25 #include "lib/assert.h"
26 
40 M0_INTERNAL void m0_cond_init(struct m0_cond *cond, struct m0_mutex *mutex)
41 {
42  m0_chan_init(&cond->c_chan, mutex);
43 }
44 M0_EXPORTED(m0_cond_init);
45 
46 M0_INTERNAL void m0_cond_fini(struct m0_cond *cond)
47 {
48  m0_chan_fini_lock(&cond->c_chan);
49 }
50 M0_EXPORTED(m0_cond_fini);
51 
52 M0_INTERNAL void m0_cond_wait(struct m0_cond *cond)
53 {
54  struct m0_clink clink;
55 
56  /*
57  * First, register the clink with the channel, *then* unlock the
58  * mutex. This guarantees that signals to the condition variable are not
59  * missed, because they are done under the mutex.
60  */
61 
63 
65  m0_clink_add(&cond->c_chan, &clink);
66  m0_chan_unlock(&cond->c_chan);
68  m0_chan_lock(&cond->c_chan);
71 }
72 M0_EXPORTED(m0_cond_wait);
73 
74 M0_INTERNAL bool m0_cond_timedwait(struct m0_cond *cond,
75  const m0_time_t abs_timeout)
76 {
77  struct m0_clink clink;
78  bool retval;
79 
81 
83  m0_clink_add(&cond->c_chan, &clink);
84  m0_chan_unlock(&cond->c_chan);
85  retval = m0_chan_timedwait(&clink, abs_timeout);
86  m0_chan_lock(&cond->c_chan);
89 
90  return retval;
91 }
92 M0_EXPORTED(m0_cond_timedwait);
93 
94 M0_INTERNAL void m0_cond_signal(struct m0_cond *cond)
95 {
96  m0_chan_signal(&cond->c_chan);
97 }
98 M0_EXPORTED(m0_cond_signal);
99 
100 M0_INTERNAL void m0_cond_broadcast(struct m0_cond *cond)
101 {
102  m0_chan_broadcast(&cond->c_chan);
103 }
104 
107 /*
108  * Local variables:
109  * c-indentation-style: "K&R"
110  * c-basic-offset: 8
111  * tab-width: 8
112  * fill-column: 80
113  * scroll-step: 1
114  * End:
115  */
Definition: cond.h:99
M0_INTERNAL void m0_chan_wait(struct m0_clink *link)
Definition: chan.c:336
#define M0_PRE(cond)
M0_INTERNAL bool m0_chan_is_locked(const struct m0_chan *ch)
Definition: chan.c:78
#define NULL
Definition: misc.h:38
M0_INTERNAL void m0_clink_init(struct m0_clink *link, m0_chan_cb_t cb)
Definition: chan.c:201
M0_INTERNAL void m0_clink_del(struct m0_clink *link)
Definition: chan.c:267
uint64_t m0_time_t
Definition: time.h:37
M0_INTERNAL void m0_chan_lock(struct m0_chan *ch)
Definition: chan.c:68
M0_INTERNAL void m0_cond_init(struct m0_cond *cond, struct m0_mutex *mutex)
Definition: cond.c:40
M0_INTERNAL void m0_chan_signal(struct m0_chan *chan)
Definition: chan.c:159
M0_INTERNAL void m0_chan_init(struct m0_chan *chan, struct m0_mutex *ch_guard)
Definition: chan.c:96
M0_INTERNAL void m0_cond_fini(struct m0_cond *cond)
Definition: cond.c:46
M0_INTERNAL void m0_cond_signal(struct m0_cond *cond)
Definition: cond.c:94
static struct m0_clink clink[RDWR_REQUEST_MAX]
M0_INTERNAL void m0_chan_unlock(struct m0_chan *ch)
Definition: chan.c:73
M0_INTERNAL void m0_cond_wait(struct m0_cond *cond)
Definition: cond.c:52
struct m0_chan c_chan
Definition: cond.h:100
M0_INTERNAL void m0_clink_add(struct m0_chan *chan, struct m0_clink *link)
Definition: chan.c:228
M0_INTERNAL bool m0_chan_timedwait(struct m0_clink *link, const m0_time_t abs_timeout)
Definition: chan.c:349
struct m0_mutex mutex
Definition: format.h:170
M0_INTERNAL void m0_clink_fini(struct m0_clink *link)
Definition: chan.c:208
M0_INTERNAL bool m0_cond_timedwait(struct m0_cond *cond, const m0_time_t abs_timeout)
Definition: cond.c:74
M0_INTERNAL void m0_cond_broadcast(struct m0_cond *cond)
Definition: cond.c:100
M0_INTERNAL void m0_chan_fini_lock(struct m0_chan *chan)
Definition: chan.c:112
Definition: mutex.h:47
M0_INTERNAL void m0_chan_broadcast(struct m0_chan *chan)
Definition: chan.c:172