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>
24 #include "lthread_api.h"
25 #include "lthread_diag_api.h"
26 #include "lthread_diag.h"
27 #include "lthread_int.h"
28 #include "lthread_sched.h"
29 #include "lthread_queue.h"
30 #include "lthread_objcache.h"
31 #include "lthread_timer.h"
32 #include "lthread_mutex.h"
33 #include "lthread_cond.h"
36 * Create a condition variable
39 lthread_cond_init(char *name, struct lthread_cond **cond,
40 __rte_unused const struct lthread_condattr *attr)
42 struct lthread_cond *c;
45 return POSIX_ERRNO(EINVAL);
47 /* allocate a condition variable from cache */
48 c = _lthread_objcache_alloc((THIS_SCHED)->cond_cache);
51 return POSIX_ERRNO(EAGAIN);
53 c->blocked = _lthread_queue_create("blocked");
54 if (c->blocked == NULL) {
55 _lthread_objcache_free((THIS_SCHED)->cond_cache, (void *)c);
56 return POSIX_ERRNO(EAGAIN);
60 strncpy(c->name, "no name", sizeof(c->name));
62 strncpy(c->name, name, sizeof(c->name));
63 c->name[sizeof(c->name)-1] = 0;
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)