Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | /* * Copyright (c) 2017 Wind River Systems, Inc. * * SPDX-License-Identifier: Apache-2.0 */ /** * @file * * @brief Kernel asynchronous event polling interface. * * This polling mechanism allows waiting on multiple events concurrently, * either events triggered directly, or from kernel objects or other kernel * constructs. */ #include <kernel.h> #include <kernel_structs.h> #include <wait_q.h> #include <ksched.h> #include <misc/slist.h> #include <misc/dlist.h> #include <misc/__assert.h> void k_poll_event_init(struct k_poll_event *event, u32_t type, int mode, void *obj) { __ASSERT(mode == K_POLL_MODE_NOTIFY_ONLY, "only NOTIFY_ONLY mode is supported\n"); __ASSERT(type < (1 << _POLL_NUM_TYPES), "invalid type\n"); __ASSERT(obj, "must provide an object\n"); event->poller = NULL; /* event->tag is left uninitialized: the user will set it if needed */ event->type = type; event->state = K_POLL_STATE_NOT_READY; event->mode = mode; event->unused = 0; event->obj = obj; } /* must be called with interrupts locked */ static inline void set_polling_state(struct k_thread *thread) { _mark_thread_as_polling(thread); } /* must be called with interrupts locked */ static inline void clear_polling_state(struct k_thread *thread) { _mark_thread_as_not_polling(thread); } /* must be called with interrupts locked */ static inline int is_polling(void) { return _is_thread_polling(_current); } /* must be called with interrupts locked */ static inline int is_condition_met(struct k_poll_event *event, u32_t *state) { switch (event->type) { case K_POLL_TYPE_SEM_AVAILABLE: if (k_sem_count_get(event->sem) > 0) { *state = K_POLL_STATE_SEM_AVAILABLE; return 1; } break; case K_POLL_TYPE_DATA_AVAILABLE: if (!k_queue_is_empty(event->queue)) { *state = K_POLL_STATE_FIFO_DATA_AVAILABLE; return 1; } break; case K_POLL_TYPE_SIGNAL: if (event->signal->signaled) { *state = K_POLL_STATE_SIGNALED; return 1; } break; case K_POLL_TYPE_IGNORE: return 0; default: __ASSERT(0, "invalid event type (0x%x)\n", event->type); break; } return 0; } /* must be called with interrupts locked */ static inline int register_event(struct k_poll_event *event) { switch (event->type) { case K_POLL_TYPE_SEM_AVAILABLE: __ASSERT(event->sem, "invalid semaphore\n"); if (event->sem->poll_event) { return -EADDRINUSE; } event->sem->poll_event = event; break; case K_POLL_TYPE_DATA_AVAILABLE: __ASSERT(event->queue, "invalid queue\n"); if (event->queue->poll_event) { return -EADDRINUSE; } event->queue->poll_event = event; break; case K_POLL_TYPE_SIGNAL: __ASSERT(event->queue, "invalid poll signal\n"); if (event->signal->poll_event) { return -EADDRINUSE; } event->signal->poll_event = event; break; case K_POLL_TYPE_IGNORE: /* nothing to do */ break; default: __ASSERT(0, "invalid event type\n"); break; } return 0; } /* must be called with interrupts locked */ static inline void clear_event_registration(struct k_poll_event *event) { event->poller = NULL; switch (event->type) { case K_POLL_TYPE_SEM_AVAILABLE: __ASSERT(event->sem, "invalid semaphore\n"); event->sem->poll_event = NULL; break; case K_POLL_TYPE_DATA_AVAILABLE: __ASSERT(event->queue, "invalid queue\n"); event->queue->poll_event = NULL; break; case K_POLL_TYPE_SIGNAL: __ASSERT(event->signal, "invalid poll signal\n"); event->signal->poll_event = NULL; break; case K_POLL_TYPE_IGNORE: /* nothing to do */ break; default: __ASSERT(0, "invalid event type\n"); break; } } /* must be called with interrupts locked */ static inline void clear_event_registrations(struct k_poll_event *events, int last_registered, unsigned int key) { for (; last_registered >= 0; last_registered--) { clear_event_registration(&events[last_registered]); irq_unlock(key); key = irq_lock(); } } static inline void set_event_ready(struct k_poll_event *event, u32_t state) { event->poller = NULL; event->state |= state; } int k_poll(struct k_poll_event *events, int num_events, s32_t timeout) { __ASSERT(!_is_in_isr(), ""); __ASSERT(events, "NULL events\n"); __ASSERT(num_events > 0, "zero events\n"); int last_registered = -1, in_use = 0, rc; unsigned int key; key = irq_lock(); set_polling_state(_current); irq_unlock(key); /* * We can get by with one poller structure for all events for now: * if/when we allow multiple threads to poll on the same object, we * will need one per poll event associated with an object. */ struct _poller poller = { .thread = _current }; /* find events whose condition is already fulfilled */ for (int ii = 0; ii < num_events; ii++) { u32_t state; key = irq_lock(); if (is_condition_met(&events[ii], &state)) { set_event_ready(&events[ii], state); clear_polling_state(_current); } else if (timeout != K_NO_WAIT && is_polling() && !in_use) { rc = register_event(&events[ii]); if (rc == 0) { events[ii].poller = &poller; ++last_registered; } else if (rc == -EADDRINUSE) { /* setting in_use also prevents any further * registrations by the current thread */ in_use = -EADDRINUSE; events[ii].state = K_POLL_STATE_EADDRINUSE; clear_polling_state(_current); } else { __ASSERT(0, "unexpected return code\n"); } } irq_unlock(key); } key = irq_lock(); /* * If we're not polling anymore, it means that at least one event * condition is met, either when looping through the events here or * because one of the events registered has had its state changed, or * that one of the objects we wanted to poll on already had a thread * polling on it. We can remove all registrations and return either * success or a -EADDRINUSE error. In the case of a -EADDRINUSE error, * the events that were available are still flagged as such, and it is * valid for the caller to consider them available, as if this function * returned success. */ if (!is_polling()) { clear_event_registrations(events, last_registered, key); irq_unlock(key); return in_use; } clear_polling_state(_current); if (timeout == K_NO_WAIT) { irq_unlock(key); return -EAGAIN; } _wait_q_t wait_q = _WAIT_Q_INIT(&wait_q); _pend_current_thread(&wait_q, timeout); int swap_rc = _Swap(key); /* * Clear all event registrations. If events happen while we're in this * loop, and we already had one that triggered, that's OK: they will * end up in the list of events that are ready; if we timed out, and * events happen while we're in this loop, that is OK as well since * we've already know the return code (-EAGAIN), and even if they are * added to the list of events that occurred, the user has to check the * return code first, which invalidates the whole list of event states. */ key = irq_lock(); clear_event_registrations(events, last_registered, key); irq_unlock(key); return swap_rc; } /* must be called with interrupts locked */ static int _signal_poll_event(struct k_poll_event *event, u32_t state, int *must_reschedule) { *must_reschedule = 0; if (!event->poller) { goto ready_event; } struct k_thread *thread = event->poller->thread; __ASSERT(event->poller->thread, "poller should have a thread\n"); clear_polling_state(thread); if (!_is_thread_pending(thread)) { goto ready_event; } if (_is_thread_timeout_expired(thread)) { return -EAGAIN; } _unpend_thread(thread); _abort_thread_timeout(thread); _set_thread_return_value(thread, 0); if (!_is_thread_ready(thread)) { goto ready_event; } _add_thread_to_ready_q(thread); *must_reschedule = !_is_in_isr() && _must_switch_threads(); ready_event: set_event_ready(event, state); return 0; } /* returns 1 if a reschedule must take place, 0 otherwise */ /* *obj_poll_event is guaranteed to not be NULL */ int _handle_obj_poll_event(struct k_poll_event **obj_poll_event, u32_t state) { struct k_poll_event *poll_event = *obj_poll_event; int must_reschedule; *obj_poll_event = NULL; (void)_signal_poll_event(poll_event, state, &must_reschedule); return must_reschedule; } void k_poll_signal_init(struct k_poll_signal *signal) { signal->poll_event = NULL; signal->signaled = 0; /* signal->result is left unitialized */ } int k_poll_signal(struct k_poll_signal *signal, int result) { unsigned int key = irq_lock(); int must_reschedule; signal->result = result; signal->signaled = 1; if (!signal->poll_event) { irq_unlock(key); return 0; } int rc = _signal_poll_event(signal->poll_event, K_POLL_STATE_SIGNALED, &must_reschedule); if (must_reschedule) { (void)_Swap(key); } else { irq_unlock(key); } return rc; } |