examples/performance-thread: add lthread subsystem
[dpdk.git] / examples / performance-thread / common / lthread_cond.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 /*
35  * Some portions of this software may have been derived from the
36  * https://github.com/halayli/lthread which carrys the following license.
37  *
38  * Copyright (C) 2012, Hasan Alayli <halayli@gmail.com>
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
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.
48  *
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
59  * SUCH DAMAGE.
60  */
61
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <stdint.h>
66 #include <stddef.h>
67 #include <limits.h>
68 #include <inttypes.h>
69 #include <unistd.h>
70 #include <pthread.h>
71 #include <fcntl.h>
72 #include <sys/time.h>
73 #include <sys/mman.h>
74 #include <errno.h>
75
76 #include <rte_config.h>
77 #include <rte_log.h>
78 #include <rte_common.h>
79
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"
90
91 /*
92  * Create a condition variable
93  */
94 int
95 lthread_cond_init(char *name, struct lthread_cond **cond,
96                   __rte_unused const struct lthread_condattr *attr)
97 {
98         struct lthread_cond *c;
99
100         if (cond == NULL)
101                 return POSIX_ERRNO(EINVAL);
102
103         /* allocate a condition variable from cache */
104         c = _lthread_objcache_alloc((THIS_SCHED)->cond_cache);
105
106         if (c == NULL)
107                 return POSIX_ERRNO(EAGAIN);
108
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);
113         }
114
115         if (name == NULL)
116                 strncpy(c->name, "no name", sizeof(c->name));
117         else
118                 strncpy(c->name, name, sizeof(c->name));
119         c->name[sizeof(c->name)-1] = 0;
120
121         c->root_sched = THIS_SCHED;
122
123         (*cond) = c;
124         DIAG_CREATE_EVENT((*cond), LT_DIAG_COND_CREATE);
125         return 0;
126 }
127
128 /*
129  * Destroy a condition variable
130  */
131 int lthread_cond_destroy(struct lthread_cond *c)
132 {
133         if (c == NULL) {
134                 DIAG_EVENT(c, LT_DIAG_COND_DESTROY, c, POSIX_ERRNO(EINVAL));
135                 return POSIX_ERRNO(EINVAL);
136         }
137
138         /* try to free it */
139         if (_lthread_queue_destroy(c->blocked) < 0) {
140                 /* queue in use */
141                 DIAG_EVENT(c, LT_DIAG_COND_DESTROY, c, POSIX_ERRNO(EBUSY));
142                 return POSIX_ERRNO(EBUSY);
143         }
144
145         /* okay free it */
146         _lthread_objcache_free(c->root_sched->cond_cache, c);
147         DIAG_EVENT(c, LT_DIAG_COND_DESTROY, c, 0);
148         return 0;
149 }
150
151 /*
152  * Wait on a condition variable
153  */
154 int lthread_cond_wait(struct lthread_cond *c, __rte_unused uint64_t reserved)
155 {
156         struct lthread *lt = THIS_LTHREAD;
157
158         if (c == NULL) {
159                 DIAG_EVENT(c, LT_DIAG_COND_WAIT, c, POSIX_ERRNO(EINVAL));
160                 return POSIX_ERRNO(EINVAL);
161         }
162
163
164         DIAG_EVENT(c, LT_DIAG_COND_WAIT, c, 0);
165
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
170          * resumed
171          */
172         lt->pending_wr_queue = c->blocked;
173         _suspend();
174
175         /* the condition happened */
176         return 0;
177 }
178
179 /*
180  * Signal a condition variable
181  * attempt to resume any blocked thread
182  */
183 int lthread_cond_signal(struct lthread_cond *c)
184 {
185         struct lthread *lt;
186
187         if (c == NULL) {
188                 DIAG_EVENT(c, LT_DIAG_COND_SIGNAL, c, POSIX_ERRNO(EINVAL));
189                 return POSIX_ERRNO(EINVAL);
190         }
191
192         lt = _lthread_queue_remove(c->blocked);
193
194         if (lt != NULL) {
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);
198         }
199         return 0;
200 }
201
202 /*
203  * Broadcast a condition variable
204  */
205 int lthread_cond_broadcast(struct lthread_cond *c)
206 {
207         struct lthread *lt;
208
209         if (c == NULL) {
210                 DIAG_EVENT(c, LT_DIAG_COND_BROADCAST, c, POSIX_ERRNO(EINVAL));
211                 return POSIX_ERRNO(EINVAL);
212         }
213
214         DIAG_EVENT(c, LT_DIAG_COND_BROADCAST, c, 0);
215         do {
216                 /* drain the queue waking everybody */
217                 lt = _lthread_queue_remove(c->blocked);
218
219                 if (lt != NULL) {
220                         DIAG_EVENT(c, LT_DIAG_COND_BROADCAST, c, lt);
221                         /* wake up */
222                         _ready_queue_insert((struct lthread_sched *)lt->sched,
223                                             lt);
224                 }
225         } while (!_lthread_queue_empty(c->blocked));
226         _reschedule();
227         DIAG_EVENT(c, LT_DIAG_COND_BROADCAST, c, 0);
228         return 0;
229 }
230
231 /*
232  * return the diagnostic ref val stored in a condition var
233  */
234 uint64_t
235 lthread_cond_diag_ref(struct lthread_cond *c)
236 {
237         if (c == NULL)
238                 return 0;
239         return c->diag_ref;
240 }