4 * Copyright(c) 2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * 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
17 * * Neither the name of Intel Corporation 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 FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Some portions of this software may have been derived from the
36 * https://github.com/halayli/lthread which carrys the following license.
38 * Copyright (C) 2012, Hasan Alayli <halayli@gmail.com>
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
49 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
76 #include <rte_config.h>
78 #include <rte_common.h>
80 #include "lthread_api.h"
81 #include "lthread_diag_api.h"
82 #include "lthread_diag.h"
83 #include "lthread_int.h"
84 #include "lthread_sched.h"
85 #include "lthread_queue.h"
86 #include "lthread_objcache.h"
87 #include "lthread_timer.h"
88 #include "lthread_mutex.h"
89 #include "lthread_cond.h"
92 * Create a condition variable
95 lthread_cond_init(char *name, struct lthread_cond **cond,
96 __rte_unused const struct lthread_condattr *attr)
98 struct lthread_cond *c;
101 return POSIX_ERRNO(EINVAL);
103 /* allocate a condition variable from cache */
104 c = _lthread_objcache_alloc((THIS_SCHED)->cond_cache);
107 return POSIX_ERRNO(EAGAIN);
109 c->blocked = _lthread_queue_create("blocked");
110 if (c->blocked == NULL) {
111 _lthread_objcache_free((THIS_SCHED)->cond_cache, (void *)c);
112 return POSIX_ERRNO(EAGAIN);
116 strncpy(c->name, "no name", sizeof(c->name));
118 strncpy(c->name, name, sizeof(c->name));
119 c->name[sizeof(c->name)-1] = 0;
121 c->root_sched = THIS_SCHED;
124 DIAG_CREATE_EVENT((*cond), LT_DIAG_COND_CREATE);
129 * Destroy a condition variable
131 int lthread_cond_destroy(struct lthread_cond *c)
134 DIAG_EVENT(c, LT_DIAG_COND_DESTROY, c, POSIX_ERRNO(EINVAL));
135 return POSIX_ERRNO(EINVAL);
139 if (_lthread_queue_destroy(c->blocked) < 0) {
141 DIAG_EVENT(c, LT_DIAG_COND_DESTROY, c, POSIX_ERRNO(EBUSY));
142 return POSIX_ERRNO(EBUSY);
146 _lthread_objcache_free(c->root_sched->cond_cache, c);
147 DIAG_EVENT(c, LT_DIAG_COND_DESTROY, c, 0);
152 * Wait on a condition variable
154 int lthread_cond_wait(struct lthread_cond *c, __rte_unused uint64_t reserved)
156 struct lthread *lt = THIS_LTHREAD;
159 DIAG_EVENT(c, LT_DIAG_COND_WAIT, c, POSIX_ERRNO(EINVAL));
160 return POSIX_ERRNO(EINVAL);
164 DIAG_EVENT(c, LT_DIAG_COND_WAIT, c, 0);
166 /* queue the current thread in the blocked queue
167 * this will be written when we return to the scheduler
168 * to ensure that the current thread context is saved
169 * before any signal could result in it being dequeued and
172 lt->pending_wr_queue = c->blocked;
175 /* the condition happened */
180 * Signal a condition variable
181 * attempt to resume any blocked thread
183 int lthread_cond_signal(struct lthread_cond *c)
188 DIAG_EVENT(c, LT_DIAG_COND_SIGNAL, c, POSIX_ERRNO(EINVAL));
189 return POSIX_ERRNO(EINVAL);
192 lt = _lthread_queue_remove(c->blocked);
195 /* okay wake up this thread */
196 DIAG_EVENT(c, LT_DIAG_COND_SIGNAL, c, lt);
197 _ready_queue_insert((struct lthread_sched *)lt->sched, lt);
203 * Broadcast a condition variable
205 int lthread_cond_broadcast(struct lthread_cond *c)
210 DIAG_EVENT(c, LT_DIAG_COND_BROADCAST, c, POSIX_ERRNO(EINVAL));
211 return POSIX_ERRNO(EINVAL);
214 DIAG_EVENT(c, LT_DIAG_COND_BROADCAST, c, 0);
216 /* drain the queue waking everybody */
217 lt = _lthread_queue_remove(c->blocked);
220 DIAG_EVENT(c, LT_DIAG_COND_BROADCAST, c, lt);
222 _ready_queue_insert((struct lthread_sched *)lt->sched,
225 } while (!_lthread_queue_empty(c->blocked));
227 DIAG_EVENT(c, LT_DIAG_COND_BROADCAST, c, 0);
232 * return the diagnostic ref val stored in a condition var
235 lthread_cond_diag_ref(struct lthread_cond *c)