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
77 #include <rte_common.h>
79 #include "lthread_api.h"
80 #include "lthread_diag_api.h"
81 #include "lthread_diag.h"
82 #include "lthread_int.h"
83 #include "lthread_sched.h"
84 #include "lthread_queue.h"
85 #include "lthread_objcache.h"
86 #include "lthread_timer.h"
87 #include "lthread_mutex.h"
88 #include "lthread_cond.h"
91 * Create a condition variable
94 lthread_cond_init(char *name, struct lthread_cond **cond,
95 __rte_unused const struct lthread_condattr *attr)
97 struct lthread_cond *c;
100 return POSIX_ERRNO(EINVAL);
102 /* allocate a condition variable from cache */
103 c = _lthread_objcache_alloc((THIS_SCHED)->cond_cache);
106 return POSIX_ERRNO(EAGAIN);
108 c->blocked = _lthread_queue_create("blocked");
109 if (c->blocked == NULL) {
110 _lthread_objcache_free((THIS_SCHED)->cond_cache, (void *)c);
111 return POSIX_ERRNO(EAGAIN);
115 strncpy(c->name, "no name", sizeof(c->name));
117 strncpy(c->name, name, sizeof(c->name));
118 c->name[sizeof(c->name)-1] = 0;
120 c->root_sched = THIS_SCHED;
123 DIAG_CREATE_EVENT((*cond), LT_DIAG_COND_CREATE);
128 * Destroy a condition variable
130 int lthread_cond_destroy(struct lthread_cond *c)
133 DIAG_EVENT(c, LT_DIAG_COND_DESTROY, c, POSIX_ERRNO(EINVAL));
134 return POSIX_ERRNO(EINVAL);
138 if (_lthread_queue_destroy(c->blocked) < 0) {
140 DIAG_EVENT(c, LT_DIAG_COND_DESTROY, c, POSIX_ERRNO(EBUSY));
141 return POSIX_ERRNO(EBUSY);
145 _lthread_objcache_free(c->root_sched->cond_cache, c);
146 DIAG_EVENT(c, LT_DIAG_COND_DESTROY, c, 0);
151 * Wait on a condition variable
153 int lthread_cond_wait(struct lthread_cond *c, __rte_unused uint64_t reserved)
155 struct lthread *lt = THIS_LTHREAD;
158 DIAG_EVENT(c, LT_DIAG_COND_WAIT, c, POSIX_ERRNO(EINVAL));
159 return POSIX_ERRNO(EINVAL);
163 DIAG_EVENT(c, LT_DIAG_COND_WAIT, c, 0);
165 /* queue the current thread in the blocked queue
166 * this will be written when we return to the scheduler
167 * to ensure that the current thread context is saved
168 * before any signal could result in it being dequeued and
171 lt->pending_wr_queue = c->blocked;
174 /* the condition happened */
179 * Signal a condition variable
180 * attempt to resume any blocked thread
182 int lthread_cond_signal(struct lthread_cond *c)
187 DIAG_EVENT(c, LT_DIAG_COND_SIGNAL, c, POSIX_ERRNO(EINVAL));
188 return POSIX_ERRNO(EINVAL);
191 lt = _lthread_queue_remove(c->blocked);
194 /* okay wake up this thread */
195 DIAG_EVENT(c, LT_DIAG_COND_SIGNAL, c, lt);
196 _ready_queue_insert((struct lthread_sched *)lt->sched, lt);
202 * Broadcast a condition variable
204 int lthread_cond_broadcast(struct lthread_cond *c)
209 DIAG_EVENT(c, LT_DIAG_COND_BROADCAST, c, POSIX_ERRNO(EINVAL));
210 return POSIX_ERRNO(EINVAL);
213 DIAG_EVENT(c, LT_DIAG_COND_BROADCAST, c, 0);
215 /* drain the queue waking everybody */
216 lt = _lthread_queue_remove(c->blocked);
219 DIAG_EVENT(c, LT_DIAG_COND_BROADCAST, c, lt);
221 _ready_queue_insert((struct lthread_sched *)lt->sched,
224 } while (!_lthread_queue_empty(c->blocked));
226 DIAG_EVENT(c, LT_DIAG_COND_BROADCAST, c, 0);
231 * return the diagnostic ref val stored in a condition var
234 lthread_cond_diag_ref(struct lthread_cond *c)