1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015 Intel Corporation
18 #include <rte_per_lcore.h>
20 #include <rte_spinlock.h>
21 #include <rte_common.h>
23 #include "lthread_api.h"
24 #include "lthread_int.h"
25 #include "lthread_mutex.h"
26 #include "lthread_sched.h"
27 #include "lthread_queue.h"
28 #include "lthread_objcache.h"
29 #include "lthread_diag.h"
35 lthread_mutex_init(char *name, struct lthread_mutex **mutex,
36 __rte_unused const struct lthread_mutexattr *attr)
38 struct lthread_mutex *m;
41 return POSIX_ERRNO(EINVAL);
44 m = _lthread_objcache_alloc((THIS_SCHED)->mutex_cache);
46 return POSIX_ERRNO(EAGAIN);
48 m->blocked = _lthread_queue_create("blocked queue");
49 if (m->blocked == NULL) {
50 _lthread_objcache_free((THIS_SCHED)->mutex_cache, m);
51 return POSIX_ERRNO(EAGAIN);
55 strncpy(m->name, "no name", sizeof(m->name));
57 strncpy(m->name, name, sizeof(m->name));
58 m->name[sizeof(m->name)-1] = 0;
60 m->root_sched = THIS_SCHED;
63 rte_atomic64_init(&m->count);
65 DIAG_CREATE_EVENT(m, LT_DIAG_MUTEX_CREATE);
74 int lthread_mutex_destroy(struct lthread_mutex *m)
76 if ((m == NULL) || (m->blocked == NULL)) {
77 DIAG_EVENT(m, LT_DIAG_MUTEX_DESTROY, m, POSIX_ERRNO(EINVAL));
78 return POSIX_ERRNO(EINVAL);
81 if (m->owner == NULL) {
82 /* try to delete the blocked queue */
83 if (_lthread_queue_destroy(m->blocked) < 0) {
84 DIAG_EVENT(m, LT_DIAG_MUTEX_DESTROY,
85 m, POSIX_ERRNO(EBUSY));
86 return POSIX_ERRNO(EBUSY);
89 /* free the mutex to cache */
90 _lthread_objcache_free(m->root_sched->mutex_cache, m);
91 DIAG_EVENT(m, LT_DIAG_MUTEX_DESTROY, m, 0);
94 /* can't do its still in use */
95 DIAG_EVENT(m, LT_DIAG_MUTEX_DESTROY, m, POSIX_ERRNO(EBUSY));
96 return POSIX_ERRNO(EBUSY);
100 * Try to obtain a mutex
102 int lthread_mutex_lock(struct lthread_mutex *m)
104 struct lthread *lt = THIS_LTHREAD;
106 if ((m == NULL) || (m->blocked == NULL)) {
107 DIAG_EVENT(m, LT_DIAG_MUTEX_LOCK, m, POSIX_ERRNO(EINVAL));
108 return POSIX_ERRNO(EINVAL);
111 /* allow no recursion */
112 if (m->owner == lt) {
113 DIAG_EVENT(m, LT_DIAG_MUTEX_LOCK, m, POSIX_ERRNO(EDEADLK));
114 return POSIX_ERRNO(EDEADLK);
118 rte_atomic64_inc(&m->count);
120 if (rte_atomic64_cmpset
121 ((uint64_t *) &m->owner, 0, (uint64_t) lt)) {
122 /* happy days, we got the lock */
123 DIAG_EVENT(m, LT_DIAG_MUTEX_LOCK, m, 0);
126 /* spin due to race with unlock when
127 * nothing was blocked
129 } while ((rte_atomic64_read(&m->count) == 1) &&
132 /* queue the current thread in the blocked queue
133 * we defer this to after we return to the scheduler
134 * to ensure that the current thread context is saved
135 * before unlock could result in it being dequeued and
138 DIAG_EVENT(m, LT_DIAG_MUTEX_BLOCKED, m, lt);
139 lt->pending_wr_queue = m->blocked;
140 /* now relinquish cpu */
142 /* resumed, must loop and compete for the lock again */
147 /* try to lock a mutex but don't block */
148 int lthread_mutex_trylock(struct lthread_mutex *m)
150 struct lthread *lt = THIS_LTHREAD;
152 if ((m == NULL) || (m->blocked == NULL)) {
153 DIAG_EVENT(m, LT_DIAG_MUTEX_TRYLOCK, m, POSIX_ERRNO(EINVAL));
154 return POSIX_ERRNO(EINVAL);
157 if (m->owner == lt) {
159 DIAG_EVENT(m, LT_DIAG_MUTEX_TRYLOCK, m, POSIX_ERRNO(EDEADLK));
160 return POSIX_ERRNO(EDEADLK);
163 rte_atomic64_inc(&m->count);
164 if (rte_atomic64_cmpset
165 ((uint64_t *) &m->owner, (uint64_t) NULL, (uint64_t) lt)) {
167 DIAG_EVENT(m, LT_DIAG_MUTEX_TRYLOCK, m, 0);
171 /* failed so return busy */
172 rte_atomic64_dec(&m->count);
173 DIAG_EVENT(m, LT_DIAG_MUTEX_TRYLOCK, m, POSIX_ERRNO(EBUSY));
174 return POSIX_ERRNO(EBUSY);
180 int lthread_mutex_unlock(struct lthread_mutex *m)
182 struct lthread *lt = THIS_LTHREAD;
183 struct lthread *unblocked;
185 if ((m == NULL) || (m->blocked == NULL)) {
186 DIAG_EVENT(m, LT_DIAG_MUTEX_UNLOCKED, m, POSIX_ERRNO(EINVAL));
187 return POSIX_ERRNO(EINVAL);
190 /* fail if its owned */
191 if (m->owner != lt || m->owner == NULL) {
192 DIAG_EVENT(m, LT_DIAG_MUTEX_UNLOCKED, m, POSIX_ERRNO(EPERM));
193 return POSIX_ERRNO(EPERM);
196 rte_atomic64_dec(&m->count);
197 /* if there are blocked threads then make one ready */
198 while (rte_atomic64_read(&m->count) > 0) {
199 unblocked = _lthread_queue_remove(m->blocked);
201 if (unblocked != NULL) {
202 rte_atomic64_dec(&m->count);
203 DIAG_EVENT(m, LT_DIAG_MUTEX_UNLOCKED, m, unblocked);
204 RTE_ASSERT(unblocked->sched != NULL);
205 _ready_queue_insert((struct lthread_sched *)
206 unblocked->sched, unblocked);
210 /* release the lock */
216 * return the diagnostic ref val stored in a mutex
219 lthread_mutex_diag_ref(struct lthread_mutex *m)