Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
wolfcrypt/src/wolfevent.c@15:117db924cf7c, 2018-08-18 (annotated)
- Committer:
- wolfSSL
- Date:
- Sat Aug 18 22:20:43 2018 +0000
- Revision:
- 15:117db924cf7c
wolfSSL 3.15.3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 15:117db924cf7c | 1 | /* wolfevent.c |
wolfSSL | 15:117db924cf7c | 2 | * |
wolfSSL | 15:117db924cf7c | 3 | * Copyright (C) 2006-2017 wolfSSL Inc. |
wolfSSL | 15:117db924cf7c | 4 | * |
wolfSSL | 15:117db924cf7c | 5 | * This file is part of wolfSSL. |
wolfSSL | 15:117db924cf7c | 6 | * |
wolfSSL | 15:117db924cf7c | 7 | * wolfSSL is free software; you can redistribute it and/or modify |
wolfSSL | 15:117db924cf7c | 8 | * it under the terms of the GNU General Public License as published by |
wolfSSL | 15:117db924cf7c | 9 | * the Free Software Foundation; either version 2 of the License, or |
wolfSSL | 15:117db924cf7c | 10 | * (at your option) any later version. |
wolfSSL | 15:117db924cf7c | 11 | * |
wolfSSL | 15:117db924cf7c | 12 | * wolfSSL is distributed in the hope that it will be useful, |
wolfSSL | 15:117db924cf7c | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wolfSSL | 15:117db924cf7c | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wolfSSL | 15:117db924cf7c | 15 | * GNU General Public License for more details. |
wolfSSL | 15:117db924cf7c | 16 | * |
wolfSSL | 15:117db924cf7c | 17 | * You should have received a copy of the GNU General Public License |
wolfSSL | 15:117db924cf7c | 18 | * along with this program; if not, write to the Free Software |
wolfSSL | 15:117db924cf7c | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
wolfSSL | 15:117db924cf7c | 20 | */ |
wolfSSL | 15:117db924cf7c | 21 | |
wolfSSL | 15:117db924cf7c | 22 | #ifdef HAVE_CONFIG_H |
wolfSSL | 15:117db924cf7c | 23 | #include <config.h> |
wolfSSL | 15:117db924cf7c | 24 | #endif |
wolfSSL | 15:117db924cf7c | 25 | |
wolfSSL | 15:117db924cf7c | 26 | #include <wolfssl/wolfcrypt/settings.h> |
wolfSSL | 15:117db924cf7c | 27 | |
wolfSSL | 15:117db924cf7c | 28 | |
wolfSSL | 15:117db924cf7c | 29 | #ifdef HAVE_WOLF_EVENT |
wolfSSL | 15:117db924cf7c | 30 | |
wolfSSL | 15:117db924cf7c | 31 | #include <wolfssl/internal.h> |
wolfSSL | 15:117db924cf7c | 32 | #include <wolfssl/error-ssl.h> |
wolfSSL | 15:117db924cf7c | 33 | #include <wolfssl/wolfcrypt/error-crypt.h> |
wolfSSL | 15:117db924cf7c | 34 | |
wolfSSL | 15:117db924cf7c | 35 | #include <wolfssl/wolfcrypt/wolfevent.h> |
wolfSSL | 15:117db924cf7c | 36 | |
wolfSSL | 15:117db924cf7c | 37 | |
wolfSSL | 15:117db924cf7c | 38 | int wolfEvent_Init(WOLF_EVENT* event, WOLF_EVENT_TYPE type, void* context) |
wolfSSL | 15:117db924cf7c | 39 | { |
wolfSSL | 15:117db924cf7c | 40 | if (event == NULL) { |
wolfSSL | 15:117db924cf7c | 41 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 42 | } |
wolfSSL | 15:117db924cf7c | 43 | |
wolfSSL | 15:117db924cf7c | 44 | if (event->state == WOLF_EVENT_STATE_PENDING) { |
wolfSSL | 15:117db924cf7c | 45 | WOLFSSL_MSG("Event already pending!"); |
wolfSSL | 15:117db924cf7c | 46 | return BAD_COND_E; |
wolfSSL | 15:117db924cf7c | 47 | } |
wolfSSL | 15:117db924cf7c | 48 | |
wolfSSL | 15:117db924cf7c | 49 | XMEMSET(event, 0, sizeof(WOLF_EVENT)); |
wolfSSL | 15:117db924cf7c | 50 | event->type = type; |
wolfSSL | 15:117db924cf7c | 51 | event->context = context; |
wolfSSL | 15:117db924cf7c | 52 | |
wolfSSL | 15:117db924cf7c | 53 | return 0; |
wolfSSL | 15:117db924cf7c | 54 | } |
wolfSSL | 15:117db924cf7c | 55 | |
wolfSSL | 15:117db924cf7c | 56 | int wolfEvent_Poll(WOLF_EVENT* event, WOLF_EVENT_FLAG flags) |
wolfSSL | 15:117db924cf7c | 57 | { |
wolfSSL | 15:117db924cf7c | 58 | int ret = BAD_COND_E; |
wolfSSL | 15:117db924cf7c | 59 | |
wolfSSL | 15:117db924cf7c | 60 | /* Check hardware */ |
wolfSSL | 15:117db924cf7c | 61 | #ifdef WOLFSSL_ASYNC_CRYPT |
wolfSSL | 15:117db924cf7c | 62 | if (event->type >= WOLF_EVENT_TYPE_ASYNC_FIRST && |
wolfSSL | 15:117db924cf7c | 63 | event->type <= WOLF_EVENT_TYPE_ASYNC_LAST) |
wolfSSL | 15:117db924cf7c | 64 | { |
wolfSSL | 15:117db924cf7c | 65 | ret = wolfAsync_EventPoll(event, flags); |
wolfSSL | 15:117db924cf7c | 66 | } |
wolfSSL | 15:117db924cf7c | 67 | #endif /* WOLFSSL_ASYNC_CRYPT */ |
wolfSSL | 15:117db924cf7c | 68 | |
wolfSSL | 15:117db924cf7c | 69 | return ret; |
wolfSSL | 15:117db924cf7c | 70 | } |
wolfSSL | 15:117db924cf7c | 71 | |
wolfSSL | 15:117db924cf7c | 72 | int wolfEventQueue_Init(WOLF_EVENT_QUEUE* queue) |
wolfSSL | 15:117db924cf7c | 73 | { |
wolfSSL | 15:117db924cf7c | 74 | int ret = 0; |
wolfSSL | 15:117db924cf7c | 75 | |
wolfSSL | 15:117db924cf7c | 76 | if (queue == NULL) { |
wolfSSL | 15:117db924cf7c | 77 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 78 | } |
wolfSSL | 15:117db924cf7c | 79 | |
wolfSSL | 15:117db924cf7c | 80 | XMEMSET(queue, 0, sizeof(WOLF_EVENT_QUEUE)); |
wolfSSL | 15:117db924cf7c | 81 | #ifndef SINGLE_THREADED |
wolfSSL | 15:117db924cf7c | 82 | ret = wc_InitMutex(&queue->lock); |
wolfSSL | 15:117db924cf7c | 83 | #endif |
wolfSSL | 15:117db924cf7c | 84 | return ret; |
wolfSSL | 15:117db924cf7c | 85 | } |
wolfSSL | 15:117db924cf7c | 86 | |
wolfSSL | 15:117db924cf7c | 87 | |
wolfSSL | 15:117db924cf7c | 88 | int wolfEventQueue_Push(WOLF_EVENT_QUEUE* queue, WOLF_EVENT* event) |
wolfSSL | 15:117db924cf7c | 89 | { |
wolfSSL | 15:117db924cf7c | 90 | int ret; |
wolfSSL | 15:117db924cf7c | 91 | |
wolfSSL | 15:117db924cf7c | 92 | if (queue == NULL || event == NULL) { |
wolfSSL | 15:117db924cf7c | 93 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 94 | } |
wolfSSL | 15:117db924cf7c | 95 | |
wolfSSL | 15:117db924cf7c | 96 | #ifndef SINGLE_THREADED |
wolfSSL | 15:117db924cf7c | 97 | if ((ret = wc_LockMutex(&queue->lock)) != 0) { |
wolfSSL | 15:117db924cf7c | 98 | return ret; |
wolfSSL | 15:117db924cf7c | 99 | } |
wolfSSL | 15:117db924cf7c | 100 | #endif |
wolfSSL | 15:117db924cf7c | 101 | |
wolfSSL | 15:117db924cf7c | 102 | ret = wolfEventQueue_Add(queue, event); |
wolfSSL | 15:117db924cf7c | 103 | |
wolfSSL | 15:117db924cf7c | 104 | #ifndef SINGLE_THREADED |
wolfSSL | 15:117db924cf7c | 105 | wc_UnLockMutex(&queue->lock); |
wolfSSL | 15:117db924cf7c | 106 | #endif |
wolfSSL | 15:117db924cf7c | 107 | |
wolfSSL | 15:117db924cf7c | 108 | return ret; |
wolfSSL | 15:117db924cf7c | 109 | } |
wolfSSL | 15:117db924cf7c | 110 | |
wolfSSL | 15:117db924cf7c | 111 | int wolfEventQueue_Pop(WOLF_EVENT_QUEUE* queue, WOLF_EVENT** event) |
wolfSSL | 15:117db924cf7c | 112 | { |
wolfSSL | 15:117db924cf7c | 113 | int ret = 0; |
wolfSSL | 15:117db924cf7c | 114 | |
wolfSSL | 15:117db924cf7c | 115 | if (queue == NULL || event == NULL) { |
wolfSSL | 15:117db924cf7c | 116 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 117 | } |
wolfSSL | 15:117db924cf7c | 118 | |
wolfSSL | 15:117db924cf7c | 119 | #ifndef SINGLE_THREADED |
wolfSSL | 15:117db924cf7c | 120 | /* In single threaded mode "event_queue.lock" doesn't exist */ |
wolfSSL | 15:117db924cf7c | 121 | if ((ret = wc_LockMutex(&queue->lock)) != 0) { |
wolfSSL | 15:117db924cf7c | 122 | return ret; |
wolfSSL | 15:117db924cf7c | 123 | } |
wolfSSL | 15:117db924cf7c | 124 | #endif |
wolfSSL | 15:117db924cf7c | 125 | |
wolfSSL | 15:117db924cf7c | 126 | /* Pop first item off queue */ |
wolfSSL | 15:117db924cf7c | 127 | *event = queue->head; |
wolfSSL | 15:117db924cf7c | 128 | ret = wolfEventQueue_Remove(queue, *event); |
wolfSSL | 15:117db924cf7c | 129 | |
wolfSSL | 15:117db924cf7c | 130 | #ifndef SINGLE_THREADED |
wolfSSL | 15:117db924cf7c | 131 | wc_UnLockMutex(&queue->lock); |
wolfSSL | 15:117db924cf7c | 132 | #endif |
wolfSSL | 15:117db924cf7c | 133 | |
wolfSSL | 15:117db924cf7c | 134 | return ret; |
wolfSSL | 15:117db924cf7c | 135 | } |
wolfSSL | 15:117db924cf7c | 136 | |
wolfSSL | 15:117db924cf7c | 137 | /* assumes queue is locked by caller */ |
wolfSSL | 15:117db924cf7c | 138 | int wolfEventQueue_Add(WOLF_EVENT_QUEUE* queue, WOLF_EVENT* event) |
wolfSSL | 15:117db924cf7c | 139 | { |
wolfSSL | 15:117db924cf7c | 140 | if (queue == NULL || event == NULL) { |
wolfSSL | 15:117db924cf7c | 141 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 142 | } |
wolfSSL | 15:117db924cf7c | 143 | |
wolfSSL | 15:117db924cf7c | 144 | event->next = NULL; /* added to end */ |
wolfSSL | 15:117db924cf7c | 145 | event->prev = NULL; |
wolfSSL | 15:117db924cf7c | 146 | if (queue->tail == NULL) { |
wolfSSL | 15:117db924cf7c | 147 | queue->head = event; |
wolfSSL | 15:117db924cf7c | 148 | } |
wolfSSL | 15:117db924cf7c | 149 | else { |
wolfSSL | 15:117db924cf7c | 150 | queue->tail->next = event; |
wolfSSL | 15:117db924cf7c | 151 | event->prev = queue->tail; |
wolfSSL | 15:117db924cf7c | 152 | } |
wolfSSL | 15:117db924cf7c | 153 | queue->tail = event; /* add to the end either way */ |
wolfSSL | 15:117db924cf7c | 154 | queue->count++; |
wolfSSL | 15:117db924cf7c | 155 | |
wolfSSL | 15:117db924cf7c | 156 | return 0; |
wolfSSL | 15:117db924cf7c | 157 | } |
wolfSSL | 15:117db924cf7c | 158 | |
wolfSSL | 15:117db924cf7c | 159 | /* assumes queue is locked by caller */ |
wolfSSL | 15:117db924cf7c | 160 | int wolfEventQueue_Remove(WOLF_EVENT_QUEUE* queue, WOLF_EVENT* event) |
wolfSSL | 15:117db924cf7c | 161 | { |
wolfSSL | 15:117db924cf7c | 162 | int ret = 0; |
wolfSSL | 15:117db924cf7c | 163 | |
wolfSSL | 15:117db924cf7c | 164 | if (queue == NULL || event == NULL) { |
wolfSSL | 15:117db924cf7c | 165 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 166 | } |
wolfSSL | 15:117db924cf7c | 167 | |
wolfSSL | 15:117db924cf7c | 168 | if (event == queue->head && event == queue->tail) { |
wolfSSL | 15:117db924cf7c | 169 | queue->head = NULL; |
wolfSSL | 15:117db924cf7c | 170 | queue->tail = NULL; |
wolfSSL | 15:117db924cf7c | 171 | } |
wolfSSL | 15:117db924cf7c | 172 | else if (event == queue->head) { |
wolfSSL | 15:117db924cf7c | 173 | queue->head = event->next; |
wolfSSL | 15:117db924cf7c | 174 | queue->head->prev = NULL; |
wolfSSL | 15:117db924cf7c | 175 | } |
wolfSSL | 15:117db924cf7c | 176 | else if (event == queue->tail) { |
wolfSSL | 15:117db924cf7c | 177 | queue->tail = event->prev; |
wolfSSL | 15:117db924cf7c | 178 | queue->tail->next = NULL; |
wolfSSL | 15:117db924cf7c | 179 | } |
wolfSSL | 15:117db924cf7c | 180 | else { |
wolfSSL | 15:117db924cf7c | 181 | WOLF_EVENT* next = event->next; |
wolfSSL | 15:117db924cf7c | 182 | WOLF_EVENT* prev = event->prev; |
wolfSSL | 15:117db924cf7c | 183 | next->prev = prev; |
wolfSSL | 15:117db924cf7c | 184 | prev->next = next; |
wolfSSL | 15:117db924cf7c | 185 | } |
wolfSSL | 15:117db924cf7c | 186 | queue->count--; |
wolfSSL | 15:117db924cf7c | 187 | |
wolfSSL | 15:117db924cf7c | 188 | return ret; |
wolfSSL | 15:117db924cf7c | 189 | } |
wolfSSL | 15:117db924cf7c | 190 | |
wolfSSL | 15:117db924cf7c | 191 | int wolfEventQueue_Poll(WOLF_EVENT_QUEUE* queue, void* context_filter, |
wolfSSL | 15:117db924cf7c | 192 | WOLF_EVENT** events, int maxEvents, WOLF_EVENT_FLAG flags, int* eventCount) |
wolfSSL | 15:117db924cf7c | 193 | { |
wolfSSL | 15:117db924cf7c | 194 | WOLF_EVENT* event; |
wolfSSL | 15:117db924cf7c | 195 | int ret = 0, count = 0; |
wolfSSL | 15:117db924cf7c | 196 | |
wolfSSL | 15:117db924cf7c | 197 | if (queue == NULL) { |
wolfSSL | 15:117db924cf7c | 198 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 199 | } |
wolfSSL | 15:117db924cf7c | 200 | |
wolfSSL | 15:117db924cf7c | 201 | #ifndef SINGLE_THREADED |
wolfSSL | 15:117db924cf7c | 202 | /* In single threaded mode "event_queue.lock" doesn't exist */ |
wolfSSL | 15:117db924cf7c | 203 | if ((ret = wc_LockMutex(&queue->lock)) != 0) { |
wolfSSL | 15:117db924cf7c | 204 | return ret; |
wolfSSL | 15:117db924cf7c | 205 | } |
wolfSSL | 15:117db924cf7c | 206 | #endif |
wolfSSL | 15:117db924cf7c | 207 | |
wolfSSL | 15:117db924cf7c | 208 | /* itterate event queue */ |
wolfSSL | 15:117db924cf7c | 209 | for (event = queue->head; event != NULL; event = event->next) |
wolfSSL | 15:117db924cf7c | 210 | { |
wolfSSL | 15:117db924cf7c | 211 | /* optional filter based on context */ |
wolfSSL | 15:117db924cf7c | 212 | if (context_filter == NULL || event->context == context_filter) { |
wolfSSL | 15:117db924cf7c | 213 | |
wolfSSL | 15:117db924cf7c | 214 | /* poll event */ |
wolfSSL | 15:117db924cf7c | 215 | ret = wolfEvent_Poll(event, flags); |
wolfSSL | 15:117db924cf7c | 216 | if (ret < 0) break; /* exit for */ |
wolfSSL | 15:117db924cf7c | 217 | |
wolfSSL | 15:117db924cf7c | 218 | /* If event is done then process */ |
wolfSSL | 15:117db924cf7c | 219 | if (event->state == WOLF_EVENT_STATE_DONE) { |
wolfSSL | 15:117db924cf7c | 220 | /* remove from queue */ |
wolfSSL | 15:117db924cf7c | 221 | ret = wolfEventQueue_Remove(queue, event); |
wolfSSL | 15:117db924cf7c | 222 | if (ret < 0) break; /* exit for */ |
wolfSSL | 15:117db924cf7c | 223 | |
wolfSSL | 15:117db924cf7c | 224 | /* return pointer in 'events' arg */ |
wolfSSL | 15:117db924cf7c | 225 | if (events) { |
wolfSSL | 15:117db924cf7c | 226 | events[count] = event; /* return pointer */ |
wolfSSL | 15:117db924cf7c | 227 | } |
wolfSSL | 15:117db924cf7c | 228 | count++; |
wolfSSL | 15:117db924cf7c | 229 | |
wolfSSL | 15:117db924cf7c | 230 | /* check to make sure our event list isn't full */ |
wolfSSL | 15:117db924cf7c | 231 | if (events && count >= maxEvents) { |
wolfSSL | 15:117db924cf7c | 232 | break; /* exit for */ |
wolfSSL | 15:117db924cf7c | 233 | } |
wolfSSL | 15:117db924cf7c | 234 | } |
wolfSSL | 15:117db924cf7c | 235 | } |
wolfSSL | 15:117db924cf7c | 236 | } |
wolfSSL | 15:117db924cf7c | 237 | |
wolfSSL | 15:117db924cf7c | 238 | #ifndef SINGLE_THREADED |
wolfSSL | 15:117db924cf7c | 239 | wc_UnLockMutex(&queue->lock); |
wolfSSL | 15:117db924cf7c | 240 | #endif |
wolfSSL | 15:117db924cf7c | 241 | |
wolfSSL | 15:117db924cf7c | 242 | /* return number of properly populated events */ |
wolfSSL | 15:117db924cf7c | 243 | if (eventCount) { |
wolfSSL | 15:117db924cf7c | 244 | *eventCount = count; |
wolfSSL | 15:117db924cf7c | 245 | } |
wolfSSL | 15:117db924cf7c | 246 | |
wolfSSL | 15:117db924cf7c | 247 | return ret; |
wolfSSL | 15:117db924cf7c | 248 | } |
wolfSSL | 15:117db924cf7c | 249 | |
wolfSSL | 15:117db924cf7c | 250 | int wolfEventQueue_Count(WOLF_EVENT_QUEUE* queue) |
wolfSSL | 15:117db924cf7c | 251 | { |
wolfSSL | 15:117db924cf7c | 252 | int ret; |
wolfSSL | 15:117db924cf7c | 253 | |
wolfSSL | 15:117db924cf7c | 254 | if (queue == NULL) { |
wolfSSL | 15:117db924cf7c | 255 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 256 | } |
wolfSSL | 15:117db924cf7c | 257 | |
wolfSSL | 15:117db924cf7c | 258 | #ifndef SINGLE_THREADED |
wolfSSL | 15:117db924cf7c | 259 | /* In single threaded mode "event_queue.lock" doesn't exist */ |
wolfSSL | 15:117db924cf7c | 260 | if ((ret = wc_LockMutex(&queue->lock)) != 0) { |
wolfSSL | 15:117db924cf7c | 261 | return ret; |
wolfSSL | 15:117db924cf7c | 262 | } |
wolfSSL | 15:117db924cf7c | 263 | #endif |
wolfSSL | 15:117db924cf7c | 264 | |
wolfSSL | 15:117db924cf7c | 265 | ret = queue->count; |
wolfSSL | 15:117db924cf7c | 266 | |
wolfSSL | 15:117db924cf7c | 267 | #ifndef SINGLE_THREADED |
wolfSSL | 15:117db924cf7c | 268 | wc_UnLockMutex(&queue->lock); |
wolfSSL | 15:117db924cf7c | 269 | #endif |
wolfSSL | 15:117db924cf7c | 270 | |
wolfSSL | 15:117db924cf7c | 271 | return ret; |
wolfSSL | 15:117db924cf7c | 272 | } |
wolfSSL | 15:117db924cf7c | 273 | |
wolfSSL | 15:117db924cf7c | 274 | void wolfEventQueue_Free(WOLF_EVENT_QUEUE* queue) |
wolfSSL | 15:117db924cf7c | 275 | { |
wolfSSL | 15:117db924cf7c | 276 | if (queue) { |
wolfSSL | 15:117db924cf7c | 277 | #ifndef SINGLE_THREADED |
wolfSSL | 15:117db924cf7c | 278 | wc_FreeMutex(&queue->lock); |
wolfSSL | 15:117db924cf7c | 279 | #endif |
wolfSSL | 15:117db924cf7c | 280 | } |
wolfSSL | 15:117db924cf7c | 281 | } |
wolfSSL | 15:117db924cf7c | 282 | |
wolfSSL | 15:117db924cf7c | 283 | #endif /* HAVE_WOLF_EVENT */ |
wolfSSL | 15:117db924cf7c | 284 |