2 * SPDX-License-Identifier: BSD-3-Clause
3 * Copyright 2015 Intel Corporation.
4 * Copyright 2012 Hasan Alayli <halayli@gmail.com>
22 #include <rte_common.h>
23 #include <rte_string_fns.h>
25 #include "lthread_api.h"
26 #include "lthread_diag_api.h"
27 #include "lthread_diag.h"
28 #include "lthread_int.h"
29 #include "lthread_sched.h"
30 #include "lthread_queue.h"
31 #include "lthread_objcache.h"
32 #include "lthread_timer.h"
33 #include "lthread_mutex.h"
34 #include "lthread_cond.h"
37 * Create a condition variable
40 lthread_cond_init(char *name, struct lthread_cond **cond,
41 __rte_unused const struct lthread_condattr *attr)
43 struct lthread_cond *c;
46 return POSIX_ERRNO(EINVAL);
48 /* allocate a condition variable from cache */
49 c = _lthread_objcache_alloc((THIS_SCHED)->cond_cache);
52 return POSIX_ERRNO(EAGAIN);
54 c->blocked = _lthread_queue_create("blocked");
55 if (c->blocked == NULL) {
56 _lthread_objcache_free((THIS_SCHED)->cond_cache, (void *)c);
57 return POSIX_ERRNO(EAGAIN);
61 strlcpy(c->name, "no name", sizeof(c->name));
63 strlcpy(c->name, name, sizeof(c->name));
65 c->root_sched = THIS_SCHED;
68 DIAG_CREATE_EVENT((*cond), LT_DIAG_COND_CREATE);
73 * Destroy a condition variable
75 int lthread_cond_destroy(struct lthread_cond *c)
78 DIAG_EVENT(c, LT_DIAG_COND_DESTROY, c, POSIX_ERRNO(EINVAL));
79 return POSIX_ERRNO(EINVAL);
83 if (_lthread_queue_destroy(c->blocked) < 0) {
85 DIAG_EVENT(c, LT_DIAG_COND_DESTROY, c, POSIX_ERRNO(EBUSY));
86 return POSIX_ERRNO(EBUSY);
90 _lthread_objcache_free(c->root_sched->cond_cache, c);
91 DIAG_EVENT(c, LT_DIAG_COND_DESTROY, c, 0);
96 * Wait on a condition variable
98 int lthread_cond_wait(struct lthread_cond *c, __rte_unused uint64_t reserved)
100 struct lthread *lt = THIS_LTHREAD;
103 DIAG_EVENT(c, LT_DIAG_COND_WAIT, c, POSIX_ERRNO(EINVAL));
104 return POSIX_ERRNO(EINVAL);
108 DIAG_EVENT(c, LT_DIAG_COND_WAIT, c, 0);
110 /* queue the current thread in the blocked queue
111 * this will be written when we return to the scheduler
112 * to ensure that the current thread context is saved
113 * before any signal could result in it being dequeued and
116 lt->pending_wr_queue = c->blocked;
119 /* the condition happened */
124 * Signal a condition variable
125 * attempt to resume any blocked thread
127 int lthread_cond_signal(struct lthread_cond *c)
132 DIAG_EVENT(c, LT_DIAG_COND_SIGNAL, c, POSIX_ERRNO(EINVAL));
133 return POSIX_ERRNO(EINVAL);
136 lt = _lthread_queue_remove(c->blocked);
139 /* okay wake up this thread */
140 DIAG_EVENT(c, LT_DIAG_COND_SIGNAL, c, lt);
141 _ready_queue_insert((struct lthread_sched *)lt->sched, lt);
147 * Broadcast a condition variable
149 int lthread_cond_broadcast(struct lthread_cond *c)
154 DIAG_EVENT(c, LT_DIAG_COND_BROADCAST, c, POSIX_ERRNO(EINVAL));
155 return POSIX_ERRNO(EINVAL);
158 DIAG_EVENT(c, LT_DIAG_COND_BROADCAST, c, 0);
160 /* drain the queue waking everybody */
161 lt = _lthread_queue_remove(c->blocked);
164 DIAG_EVENT(c, LT_DIAG_COND_BROADCAST, c, lt);
166 _ready_queue_insert((struct lthread_sched *)lt->sched,
169 } while (!_lthread_queue_empty(c->blocked));
171 DIAG_EVENT(c, LT_DIAG_COND_BROADCAST, c, 0);
176 * return the diagnostic ref val stored in a condition var
179 lthread_cond_diag_ref(struct lthread_cond *c)