Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 Wind River Systems, Inc. |
| 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * @file |
| 9 | * |
| 10 | * @brief Kernel asynchronous event polling interface. |
| 11 | * |
| 12 | * This polling mechanism allows waiting on multiple events concurrently, |
| 13 | * either events triggered directly, or from kernel objects or other kernel |
| 14 | * constructs. |
| 15 | */ |
| 16 | |
| 17 | #include <kernel.h> |
| 18 | #include <kernel_structs.h> |
Andy Ross | 245b54e | 2018-02-08 09:10:46 -0800 | [diff] [blame] | 19 | #include <kernel_internal.h> |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 20 | #include <wait_q.h> |
| 21 | #include <ksched.h> |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 22 | #include <syscall_handler.h> |
Anas Nashif | 536dd5a | 2019-06-26 10:33:52 -0400 | [diff] [blame] | 23 | #include <sys/slist.h> |
Anas Nashif | ee9dd1a | 2019-06-26 10:33:41 -0400 | [diff] [blame] | 24 | #include <sys/dlist.h> |
Anas Nashif | a2fd7d7 | 2019-06-26 10:33:55 -0400 | [diff] [blame] | 25 | #include <sys/util.h> |
Anas Nashif | 5eb90ec | 2019-06-26 10:33:39 -0400 | [diff] [blame] | 26 | #include <sys/__assert.h> |
Flavio Ceolin | 4f2e9a7 | 2018-12-16 14:27:10 -0800 | [diff] [blame] | 27 | #include <stdbool.h> |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 28 | |
Andy Ross | f2b1a4b | 2018-07-25 09:40:32 -0700 | [diff] [blame] | 29 | /* Single subsystem lock. Locking per-event would be better on highly |
| 30 | * contended SMP systems, but the original locking scheme here is |
| 31 | * subtle (it relies on releasing/reacquiring the lock in areas for |
| 32 | * latency control and it's sometimes hard to see exactly what data is |
| 33 | * "inside" a given critical section). Do the synchronization port |
| 34 | * later as an optimization. |
| 35 | */ |
| 36 | static struct k_spinlock lock; |
| 37 | |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 38 | void k_poll_event_init(struct k_poll_event *event, u32_t type, |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 39 | int mode, void *obj) |
| 40 | { |
| 41 | __ASSERT(mode == K_POLL_MODE_NOTIFY_ONLY, |
| 42 | "only NOTIFY_ONLY mode is supported\n"); |
Flavio Ceolin | 8aec087 | 2018-08-15 11:52:00 -0700 | [diff] [blame] | 43 | __ASSERT(type < (BIT(_POLL_NUM_TYPES)), "invalid type\n"); |
Flavio Ceolin | d8837c6 | 2018-09-18 12:40:54 -0700 | [diff] [blame] | 44 | __ASSERT(obj != NULL, "must provide an object\n"); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 45 | |
| 46 | event->poller = NULL; |
Benjamin Walsh | 969d4a7 | 2017-02-02 11:25:11 -0500 | [diff] [blame] | 47 | /* event->tag is left uninitialized: the user will set it if needed */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 48 | event->type = type; |
| 49 | event->state = K_POLL_STATE_NOT_READY; |
| 50 | event->mode = mode; |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 51 | event->unused = 0U; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 52 | event->obj = obj; |
| 53 | } |
| 54 | |
| 55 | /* must be called with interrupts locked */ |
Flavio Ceolin | 4f2e9a7 | 2018-12-16 14:27:10 -0800 | [diff] [blame] | 56 | static inline bool is_condition_met(struct k_poll_event *event, u32_t *state) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 57 | { |
| 58 | switch (event->type) { |
| 59 | case K_POLL_TYPE_SEM_AVAILABLE: |
| 60 | if (k_sem_count_get(event->sem) > 0) { |
| 61 | *state = K_POLL_STATE_SEM_AVAILABLE; |
Flavio Ceolin | 4f2e9a7 | 2018-12-16 14:27:10 -0800 | [diff] [blame] | 62 | return true; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 63 | } |
| 64 | break; |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 65 | case K_POLL_TYPE_DATA_AVAILABLE: |
| 66 | if (!k_queue_is_empty(event->queue)) { |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 67 | *state = K_POLL_STATE_FIFO_DATA_AVAILABLE; |
Flavio Ceolin | 4f2e9a7 | 2018-12-16 14:27:10 -0800 | [diff] [blame] | 68 | return true; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 69 | } |
| 70 | break; |
| 71 | case K_POLL_TYPE_SIGNAL: |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 72 | if (event->signal->signaled != 0U) { |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 73 | *state = K_POLL_STATE_SIGNALED; |
Flavio Ceolin | 4f2e9a7 | 2018-12-16 14:27:10 -0800 | [diff] [blame] | 74 | return true; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 75 | } |
| 76 | break; |
| 77 | case K_POLL_TYPE_IGNORE: |
Flavio Ceolin | d7271ec | 2018-11-15 09:42:14 -0800 | [diff] [blame] | 78 | break; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 79 | default: |
Flavio Ceolin | 6fdc56d | 2018-09-18 12:32:27 -0700 | [diff] [blame] | 80 | __ASSERT(false, "invalid event type (0x%x)\n", event->type); |
Flavio Ceolin | a3cea50 | 2018-09-10 22:54:55 -0700 | [diff] [blame] | 81 | break; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 82 | } |
| 83 | |
Flavio Ceolin | 4f2e9a7 | 2018-12-16 14:27:10 -0800 | [diff] [blame] | 84 | return false; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 85 | } |
| 86 | |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 87 | static inline void add_event(sys_dlist_t *events, struct k_poll_event *event, |
| 88 | struct _poller *poller) |
| 89 | { |
| 90 | struct k_poll_event *pending; |
| 91 | |
| 92 | pending = (struct k_poll_event *)sys_dlist_peek_tail(events); |
Flavio Ceolin | 4218d5f | 2018-09-17 09:39:51 -0700 | [diff] [blame] | 93 | if ((pending == NULL) || |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 94 | z_is_t1_higher_prio_than_t2(pending->poller->thread, |
| 95 | poller->thread)) { |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 96 | sys_dlist_append(events, &event->_node); |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | SYS_DLIST_FOR_EACH_CONTAINER(events, pending, _node) { |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 101 | if (z_is_t1_higher_prio_than_t2(poller->thread, |
| 102 | pending->poller->thread)) { |
Andy Ross | eda4c02 | 2019-01-28 09:35:27 -0800 | [diff] [blame] | 103 | sys_dlist_insert(&pending->_node, &event->_node); |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 104 | return; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | sys_dlist_append(events, &event->_node); |
| 109 | } |
| 110 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 111 | /* must be called with interrupts locked */ |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 112 | static inline int register_event(struct k_poll_event *event, |
| 113 | struct _poller *poller) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 114 | { |
| 115 | switch (event->type) { |
| 116 | case K_POLL_TYPE_SEM_AVAILABLE: |
Flavio Ceolin | d8837c6 | 2018-09-18 12:40:54 -0700 | [diff] [blame] | 117 | __ASSERT(event->sem != NULL, "invalid semaphore\n"); |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 118 | add_event(&event->sem->poll_events, event, poller); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 119 | break; |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 120 | case K_POLL_TYPE_DATA_AVAILABLE: |
Flavio Ceolin | d8837c6 | 2018-09-18 12:40:54 -0700 | [diff] [blame] | 121 | __ASSERT(event->queue != NULL, "invalid queue\n"); |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 122 | add_event(&event->queue->poll_events, event, poller); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 123 | break; |
| 124 | case K_POLL_TYPE_SIGNAL: |
Flavio Ceolin | d8837c6 | 2018-09-18 12:40:54 -0700 | [diff] [blame] | 125 | __ASSERT(event->signal != NULL, "invalid poll signal\n"); |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 126 | add_event(&event->signal->poll_events, event, poller); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 127 | break; |
| 128 | case K_POLL_TYPE_IGNORE: |
| 129 | /* nothing to do */ |
| 130 | break; |
| 131 | default: |
Flavio Ceolin | 6fdc56d | 2018-09-18 12:32:27 -0700 | [diff] [blame] | 132 | __ASSERT(false, "invalid event type\n"); |
Flavio Ceolin | a3cea50 | 2018-09-10 22:54:55 -0700 | [diff] [blame] | 133 | break; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 134 | } |
| 135 | |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 136 | event->poller = poller; |
| 137 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | /* must be called with interrupts locked */ |
| 142 | static inline void clear_event_registration(struct k_poll_event *event) |
| 143 | { |
Peter A. Bigot | 4863aa8 | 2018-12-30 06:38:53 -0600 | [diff] [blame] | 144 | bool remove = false; |
| 145 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 146 | event->poller = NULL; |
| 147 | |
| 148 | switch (event->type) { |
| 149 | case K_POLL_TYPE_SEM_AVAILABLE: |
Flavio Ceolin | d8837c6 | 2018-09-18 12:40:54 -0700 | [diff] [blame] | 150 | __ASSERT(event->sem != NULL, "invalid semaphore\n"); |
Peter A. Bigot | 4863aa8 | 2018-12-30 06:38:53 -0600 | [diff] [blame] | 151 | remove = true; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 152 | break; |
Luiz Augusto von Dentz | e5ed88f | 2017-02-21 15:27:20 +0200 | [diff] [blame] | 153 | case K_POLL_TYPE_DATA_AVAILABLE: |
Flavio Ceolin | d8837c6 | 2018-09-18 12:40:54 -0700 | [diff] [blame] | 154 | __ASSERT(event->queue != NULL, "invalid queue\n"); |
Peter A. Bigot | 4863aa8 | 2018-12-30 06:38:53 -0600 | [diff] [blame] | 155 | remove = true; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 156 | break; |
| 157 | case K_POLL_TYPE_SIGNAL: |
Flavio Ceolin | d8837c6 | 2018-09-18 12:40:54 -0700 | [diff] [blame] | 158 | __ASSERT(event->signal != NULL, "invalid poll signal\n"); |
Peter A. Bigot | 4863aa8 | 2018-12-30 06:38:53 -0600 | [diff] [blame] | 159 | remove = true; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 160 | break; |
| 161 | case K_POLL_TYPE_IGNORE: |
| 162 | /* nothing to do */ |
| 163 | break; |
| 164 | default: |
Flavio Ceolin | 6fdc56d | 2018-09-18 12:32:27 -0700 | [diff] [blame] | 165 | __ASSERT(false, "invalid event type\n"); |
Flavio Ceolin | a3cea50 | 2018-09-10 22:54:55 -0700 | [diff] [blame] | 166 | break; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 167 | } |
Peter A. Bigot | 4863aa8 | 2018-12-30 06:38:53 -0600 | [diff] [blame] | 168 | if (remove && sys_dnode_is_linked(&event->_node)) { |
| 169 | sys_dlist_remove(&event->_node); |
| 170 | } |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /* must be called with interrupts locked */ |
| 174 | static inline void clear_event_registrations(struct k_poll_event *events, |
Piotr Zięcik | 1c4177d | 2019-08-27 12:19:26 +0200 | [diff] [blame] | 175 | int num_events, |
Andy Ross | f2b1a4b | 2018-07-25 09:40:32 -0700 | [diff] [blame] | 176 | k_spinlock_key_t key) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 177 | { |
Piotr Zięcik | 1c4177d | 2019-08-27 12:19:26 +0200 | [diff] [blame] | 178 | while (num_events--) { |
| 179 | clear_event_registration(&events[num_events]); |
Andy Ross | f2b1a4b | 2018-07-25 09:40:32 -0700 | [diff] [blame] | 180 | k_spin_unlock(&lock, key); |
| 181 | key = k_spin_lock(&lock); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 185 | static inline void set_event_ready(struct k_poll_event *event, u32_t state) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 186 | { |
| 187 | event->poller = NULL; |
| 188 | event->state |= state; |
| 189 | } |
| 190 | |
Piotr Zięcik | 1c4177d | 2019-08-27 12:19:26 +0200 | [diff] [blame] | 191 | static inline int register_events(struct k_poll_event *events, |
| 192 | int num_events, |
| 193 | struct _poller *poller, |
| 194 | bool just_check) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 195 | { |
Piotr Zięcik | 1c4177d | 2019-08-27 12:19:26 +0200 | [diff] [blame] | 196 | int events_registered = 0; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 197 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 198 | for (int ii = 0; ii < num_events; ii++) { |
Piotr Zięcik | 1c4177d | 2019-08-27 12:19:26 +0200 | [diff] [blame] | 199 | k_spinlock_key_t key; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 200 | u32_t state; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 201 | |
Andy Ross | f2b1a4b | 2018-07-25 09:40:32 -0700 | [diff] [blame] | 202 | key = k_spin_lock(&lock); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 203 | if (is_condition_met(&events[ii], &state)) { |
| 204 | set_event_ready(&events[ii], state); |
Piotr Zięcik | 1c4177d | 2019-08-27 12:19:26 +0200 | [diff] [blame] | 205 | poller->is_polling = false; |
| 206 | } else if (!just_check && poller->is_polling) { |
| 207 | int rc = register_event(&events[ii], poller); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 208 | if (rc == 0) { |
Piotr Zięcik | 1c4177d | 2019-08-27 12:19:26 +0200 | [diff] [blame] | 209 | events_registered += 1; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 210 | } else { |
Flavio Ceolin | 6fdc56d | 2018-09-18 12:32:27 -0700 | [diff] [blame] | 211 | __ASSERT(false, "unexpected return code\n"); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 212 | } |
| 213 | } |
Andy Ross | f2b1a4b | 2018-07-25 09:40:32 -0700 | [diff] [blame] | 214 | k_spin_unlock(&lock, key); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 215 | } |
| 216 | |
Piotr Zięcik | 1c4177d | 2019-08-27 12:19:26 +0200 | [diff] [blame] | 217 | return events_registered; |
| 218 | } |
| 219 | |
| 220 | static int k_poll_poller_cb(struct k_poll_event *event, u32_t state) |
| 221 | { |
| 222 | struct k_thread *thread = event->poller->thread; |
| 223 | |
| 224 | __ASSERT(thread != NULL, "poller should have a thread\n"); |
| 225 | |
| 226 | if (!z_is_thread_pending(thread)) { |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | if (z_is_thread_timeout_expired(thread)) { |
| 231 | return -EAGAIN; |
| 232 | } |
| 233 | |
| 234 | z_unpend_thread(thread); |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 235 | arch_thread_return_value_set(thread, |
Piotr Zięcik | 1c4177d | 2019-08-27 12:19:26 +0200 | [diff] [blame] | 236 | state == K_POLL_STATE_CANCELLED ? -EINTR : 0); |
| 237 | |
| 238 | if (!z_is_thread_ready(thread)) { |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | z_ready_thread(thread); |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | int z_impl_k_poll(struct k_poll_event *events, int num_events, s32_t timeout) |
| 248 | { |
| 249 | int events_registered; |
| 250 | k_spinlock_key_t key; |
| 251 | struct _poller poller = { .is_polling = true, |
| 252 | .thread = _current, |
| 253 | .cb = k_poll_poller_cb }; |
| 254 | |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 255 | __ASSERT(!arch_is_in_isr(), ""); |
Piotr Zięcik | 1c4177d | 2019-08-27 12:19:26 +0200 | [diff] [blame] | 256 | __ASSERT(events != NULL, "NULL events\n"); |
Jukka Rissanen | cc6317d | 2019-11-01 14:03:32 +0200 | [diff] [blame^] | 257 | __ASSERT(num_events >= 0, "<0 events\n"); |
Piotr Zięcik | 1c4177d | 2019-08-27 12:19:26 +0200 | [diff] [blame] | 258 | |
| 259 | events_registered = register_events(events, num_events, &poller, |
| 260 | (timeout == K_NO_WAIT)); |
| 261 | |
Andy Ross | f2b1a4b | 2018-07-25 09:40:32 -0700 | [diff] [blame] | 262 | key = k_spin_lock(&lock); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 263 | |
| 264 | /* |
| 265 | * If we're not polling anymore, it means that at least one event |
| 266 | * condition is met, either when looping through the events here or |
Luiz Augusto von Dentz | 8786244 | 2017-09-07 10:57:27 +0300 | [diff] [blame] | 267 | * because one of the events registered has had its state changed. |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 268 | */ |
Andy Ross | 55a7e46 | 2018-05-31 11:58:09 -0700 | [diff] [blame] | 269 | if (!poller.is_polling) { |
Piotr Zięcik | 1c4177d | 2019-08-27 12:19:26 +0200 | [diff] [blame] | 270 | clear_event_registrations(events, events_registered, key); |
Andy Ross | f2b1a4b | 2018-07-25 09:40:32 -0700 | [diff] [blame] | 271 | k_spin_unlock(&lock, key); |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 272 | return 0; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 273 | } |
| 274 | |
Flavio Ceolin | 76b3518 | 2018-12-16 12:48:29 -0800 | [diff] [blame] | 275 | poller.is_polling = false; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 276 | |
| 277 | if (timeout == K_NO_WAIT) { |
Andy Ross | f2b1a4b | 2018-07-25 09:40:32 -0700 | [diff] [blame] | 278 | k_spin_unlock(&lock, key); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 279 | return -EAGAIN; |
| 280 | } |
| 281 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 282 | _wait_q_t wait_q = Z_WAIT_Q_INIT(&wait_q); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 283 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 284 | int swap_rc = z_pend_curr(&lock, key, &wait_q, timeout); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 285 | |
| 286 | /* |
| 287 | * Clear all event registrations. If events happen while we're in this |
| 288 | * loop, and we already had one that triggered, that's OK: they will |
| 289 | * end up in the list of events that are ready; if we timed out, and |
| 290 | * events happen while we're in this loop, that is OK as well since |
| 291 | * we've already know the return code (-EAGAIN), and even if they are |
| 292 | * added to the list of events that occurred, the user has to check the |
| 293 | * return code first, which invalidates the whole list of event states. |
| 294 | */ |
Andy Ross | f2b1a4b | 2018-07-25 09:40:32 -0700 | [diff] [blame] | 295 | key = k_spin_lock(&lock); |
Piotr Zięcik | 1c4177d | 2019-08-27 12:19:26 +0200 | [diff] [blame] | 296 | clear_event_registrations(events, events_registered, key); |
Andy Ross | f2b1a4b | 2018-07-25 09:40:32 -0700 | [diff] [blame] | 297 | k_spin_unlock(&lock, key); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 298 | |
| 299 | return swap_rc; |
| 300 | } |
| 301 | |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 302 | #ifdef CONFIG_USERSPACE |
Andy Ross | 643701a | 2019-08-13 12:58:38 -0700 | [diff] [blame] | 303 | static inline int z_vrfy_k_poll(struct k_poll_event *events, |
| 304 | int num_events, s32_t timeout) |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 305 | { |
Andy Ross | f2b1a4b | 2018-07-25 09:40:32 -0700 | [diff] [blame] | 306 | int ret; |
| 307 | k_spinlock_key_t key; |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 308 | struct k_poll_event *events_copy = NULL; |
Jakob Olesen | c8708d9 | 2019-05-07 10:17:35 -0700 | [diff] [blame] | 309 | u32_t bounds; |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 310 | |
| 311 | /* Validate the events buffer and make a copy of it in an |
| 312 | * allocated kernel-side buffer. |
| 313 | */ |
Jukka Rissanen | cc6317d | 2019-11-01 14:03:32 +0200 | [diff] [blame^] | 314 | if (Z_SYSCALL_VERIFY(num_events >= 0)) { |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 315 | ret = -EINVAL; |
| 316 | goto out; |
| 317 | } |
Jakob Olesen | c8708d9 | 2019-05-07 10:17:35 -0700 | [diff] [blame] | 318 | if (Z_SYSCALL_VERIFY_MSG(!u32_mul_overflow(num_events, |
| 319 | sizeof(struct k_poll_event), |
| 320 | &bounds), |
| 321 | "num_events too large")) { |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 322 | ret = -EINVAL; |
| 323 | goto out; |
| 324 | } |
| 325 | events_copy = z_thread_malloc(bounds); |
| 326 | if (!events_copy) { |
| 327 | ret = -ENOMEM; |
| 328 | goto out; |
| 329 | } |
| 330 | |
Andy Ross | f2b1a4b | 2018-07-25 09:40:32 -0700 | [diff] [blame] | 331 | key = k_spin_lock(&lock); |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 332 | if (Z_SYSCALL_MEMORY_WRITE(events, bounds)) { |
Andy Ross | f2b1a4b | 2018-07-25 09:40:32 -0700 | [diff] [blame] | 333 | k_spin_unlock(&lock, key); |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 334 | goto oops_free; |
| 335 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 336 | (void)memcpy(events_copy, events, bounds); |
Andy Ross | f2b1a4b | 2018-07-25 09:40:32 -0700 | [diff] [blame] | 337 | k_spin_unlock(&lock, key); |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 338 | |
| 339 | /* Validate what's inside events_copy */ |
| 340 | for (int i = 0; i < num_events; i++) { |
| 341 | struct k_poll_event *e = &events_copy[i]; |
| 342 | |
| 343 | if (Z_SYSCALL_VERIFY(e->mode == K_POLL_MODE_NOTIFY_ONLY)) { |
| 344 | ret = -EINVAL; |
| 345 | goto out_free; |
| 346 | } |
| 347 | |
| 348 | switch (e->type) { |
| 349 | case K_POLL_TYPE_IGNORE: |
| 350 | break; |
| 351 | case K_POLL_TYPE_SIGNAL: |
| 352 | Z_OOPS(Z_SYSCALL_OBJ(e->signal, K_OBJ_POLL_SIGNAL)); |
| 353 | break; |
| 354 | case K_POLL_TYPE_SEM_AVAILABLE: |
| 355 | Z_OOPS(Z_SYSCALL_OBJ(e->sem, K_OBJ_SEM)); |
| 356 | break; |
| 357 | case K_POLL_TYPE_DATA_AVAILABLE: |
| 358 | Z_OOPS(Z_SYSCALL_OBJ(e->queue, K_OBJ_QUEUE)); |
| 359 | break; |
| 360 | default: |
| 361 | ret = -EINVAL; |
| 362 | goto out_free; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | ret = k_poll(events_copy, num_events, timeout); |
Flavio Ceolin | 6699423 | 2018-08-13 15:17:04 -0700 | [diff] [blame] | 367 | (void)memcpy((void *)events, events_copy, bounds); |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 368 | out_free: |
| 369 | k_free(events_copy); |
| 370 | out: |
| 371 | return ret; |
| 372 | oops_free: |
| 373 | k_free(events_copy); |
| 374 | Z_OOPS(1); |
| 375 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 376 | #include <syscalls/k_poll_mrsh.c> |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 377 | #endif |
| 378 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 379 | /* must be called with interrupts locked */ |
Andy Ross | 8606fab | 2018-03-26 10:54:40 -0700 | [diff] [blame] | 380 | static int signal_poll_event(struct k_poll_event *event, u32_t state) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 381 | { |
Piotr Zięcik | 1c4177d | 2019-08-27 12:19:26 +0200 | [diff] [blame] | 382 | struct _poller *poller = event->poller; |
| 383 | int retcode = 0; |
| 384 | |
| 385 | if (poller) { |
| 386 | if (poller->cb != NULL) { |
| 387 | retcode = poller->cb(event, state); |
| 388 | } |
| 389 | |
| 390 | poller->is_polling = false; |
| 391 | |
| 392 | if (retcode < 0) { |
| 393 | return retcode; |
| 394 | } |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 395 | } |
| 396 | |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 397 | set_event_ready(event, state); |
Piotr Zięcik | 1c4177d | 2019-08-27 12:19:26 +0200 | [diff] [blame] | 398 | return retcode; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 399 | } |
| 400 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 401 | void z_handle_obj_poll_events(sys_dlist_t *events, u32_t state) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 402 | { |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 403 | struct k_poll_event *poll_event; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 404 | |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 405 | poll_event = (struct k_poll_event *)sys_dlist_get(events); |
Flavio Ceolin | 4218d5f | 2018-09-17 09:39:51 -0700 | [diff] [blame] | 406 | if (poll_event != NULL) { |
Andy Ross | 8606fab | 2018-03-26 10:54:40 -0700 | [diff] [blame] | 407 | (void) signal_poll_event(poll_event, state); |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 408 | } |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 409 | } |
| 410 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 411 | void z_impl_k_poll_signal_init(struct k_poll_signal *signal) |
Benjamin Walsh | a304f16 | 2017-02-02 16:46:09 -0500 | [diff] [blame] | 412 | { |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 413 | sys_dlist_init(&signal->poll_events); |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 414 | signal->signaled = 0U; |
Benjamin Walsh | a304f16 | 2017-02-02 16:46:09 -0500 | [diff] [blame] | 415 | /* signal->result is left unitialized */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 416 | z_object_init(signal); |
Benjamin Walsh | a304f16 | 2017-02-02 16:46:09 -0500 | [diff] [blame] | 417 | } |
| 418 | |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 419 | #ifdef CONFIG_USERSPACE |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 420 | static inline void z_vrfy_k_poll_signal_init(struct k_poll_signal *signal) |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 421 | { |
| 422 | Z_OOPS(Z_SYSCALL_OBJ_INIT(signal, K_OBJ_POLL_SIGNAL)); |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 423 | z_impl_k_poll_signal_init(signal); |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 424 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 425 | #include <syscalls/k_poll_signal_init_mrsh.c> |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 426 | #endif |
| 427 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 428 | void z_impl_k_poll_signal_check(struct k_poll_signal *signal, |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 429 | unsigned int *signaled, int *result) |
| 430 | { |
| 431 | *signaled = signal->signaled; |
| 432 | *result = signal->result; |
| 433 | } |
| 434 | |
| 435 | #ifdef CONFIG_USERSPACE |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 436 | void z_vrfy_k_poll_signal_check(struct k_poll_signal *signal, |
| 437 | unsigned int *signaled, int *result) |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 438 | { |
| 439 | Z_OOPS(Z_SYSCALL_OBJ(signal, K_OBJ_POLL_SIGNAL)); |
| 440 | Z_OOPS(Z_SYSCALL_MEMORY_WRITE(signaled, sizeof(unsigned int))); |
| 441 | Z_OOPS(Z_SYSCALL_MEMORY_WRITE(result, sizeof(int))); |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 442 | z_impl_k_poll_signal_check(signal, signaled, result); |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 443 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 444 | #include <syscalls/k_poll_signal_check_mrsh.c> |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 445 | #endif |
| 446 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 447 | int z_impl_k_poll_signal_raise(struct k_poll_signal *signal, int result) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 448 | { |
Andy Ross | f2b1a4b | 2018-07-25 09:40:32 -0700 | [diff] [blame] | 449 | k_spinlock_key_t key = k_spin_lock(&lock); |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 450 | struct k_poll_event *poll_event; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 451 | |
| 452 | signal->result = result; |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 453 | signal->signaled = 1U; |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 454 | |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 455 | poll_event = (struct k_poll_event *)sys_dlist_get(&signal->poll_events); |
Flavio Ceolin | 4218d5f | 2018-09-17 09:39:51 -0700 | [diff] [blame] | 456 | if (poll_event == NULL) { |
Andy Ross | f2b1a4b | 2018-07-25 09:40:32 -0700 | [diff] [blame] | 457 | k_spin_unlock(&lock, key); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 458 | return 0; |
| 459 | } |
| 460 | |
Andy Ross | 8606fab | 2018-03-26 10:54:40 -0700 | [diff] [blame] | 461 | int rc = signal_poll_event(poll_event, K_POLL_STATE_SIGNALED); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 462 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 463 | z_reschedule(&lock, key); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 464 | return rc; |
| 465 | } |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 466 | |
| 467 | #ifdef CONFIG_USERSPACE |
Andy Ross | 643701a | 2019-08-13 12:58:38 -0700 | [diff] [blame] | 468 | static inline int z_vrfy_k_poll_signal_raise(struct k_poll_signal *signal, |
| 469 | int result) |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 470 | { |
| 471 | Z_OOPS(Z_SYSCALL_OBJ(signal, K_OBJ_POLL_SIGNAL)); |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 472 | return z_impl_k_poll_signal_raise(signal, result); |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 473 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 474 | #include <syscalls/k_poll_signal_raise_mrsh.c> |
| 475 | |
| 476 | static inline void z_vrfy_k_poll_signal_reset(struct k_poll_signal *signal) |
| 477 | { |
| 478 | Z_OOPS(Z_SYSCALL_OBJ(signal, K_OBJ_POLL_SIGNAL)); |
| 479 | z_impl_k_poll_signal_reset(signal); |
| 480 | } |
| 481 | #include <syscalls/k_poll_signal_reset_mrsh.c> |
| 482 | |
Andrew Boie | 3772f77 | 2018-05-07 16:52:57 -0700 | [diff] [blame] | 483 | #endif |
| 484 | |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 485 | static void triggered_work_handler(struct k_work *work) |
| 486 | { |
| 487 | k_work_handler_t handler; |
| 488 | struct k_work_poll *twork = |
| 489 | CONTAINER_OF(work, struct k_work_poll, work); |
| 490 | |
| 491 | /* |
| 492 | * If callback is not set, the k_work_poll_submit_to_queue() |
| 493 | * already cleared event registrations. |
| 494 | */ |
| 495 | if (twork->poller.cb != NULL) { |
| 496 | k_spinlock_key_t key; |
| 497 | |
| 498 | key = k_spin_lock(&lock); |
| 499 | clear_event_registrations(twork->events, |
| 500 | twork->num_events, key); |
| 501 | k_spin_unlock(&lock, key); |
| 502 | } |
| 503 | |
| 504 | /* Drop work ownership and execute real handler. */ |
| 505 | handler = twork->real_handler; |
| 506 | twork->poller.thread = NULL; |
| 507 | handler(work); |
| 508 | } |
| 509 | |
| 510 | static void triggered_work_expiration_handler(struct _timeout *timeout) |
| 511 | { |
| 512 | struct k_work_poll *twork = |
| 513 | CONTAINER_OF(timeout, struct k_work_poll, timeout); |
| 514 | struct k_work_q *work_q = |
| 515 | CONTAINER_OF(twork->poller.thread, struct k_work_q, thread); |
| 516 | |
| 517 | twork->poller.is_polling = false; |
| 518 | twork->poll_result = -EAGAIN; |
| 519 | |
| 520 | k_work_submit_to_queue(work_q, &twork->work); |
| 521 | } |
| 522 | |
| 523 | static int triggered_work_poller_cb(struct k_poll_event *event, u32_t status) |
| 524 | { |
| 525 | struct _poller *poller = event->poller; |
| 526 | |
| 527 | if (poller->is_polling && poller->thread) { |
| 528 | struct k_work_poll *twork = |
| 529 | CONTAINER_OF(poller, struct k_work_poll, poller); |
| 530 | struct k_work_q *work_q = |
| 531 | CONTAINER_OF(poller->thread, struct k_work_q, thread); |
| 532 | |
| 533 | z_abort_timeout(&twork->timeout); |
| 534 | twork->poll_result = 0; |
| 535 | k_work_submit_to_queue(work_q, &twork->work); |
| 536 | } |
| 537 | |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | static int triggered_work_cancel(struct k_work_poll *work, |
| 542 | k_spinlock_key_t key) |
| 543 | { |
| 544 | /* Check if the work waits for event. */ |
| 545 | if (work->poller.is_polling && work->poller.cb != NULL) { |
| 546 | /* Remove timeout associated with the work. */ |
| 547 | z_abort_timeout(&work->timeout); |
| 548 | |
| 549 | /* |
| 550 | * Prevent work execution if event arrives while we will be |
| 551 | * clearing registrations. |
| 552 | */ |
| 553 | work->poller.cb = NULL; |
| 554 | |
| 555 | /* Clear registrations and work ownership. */ |
| 556 | clear_event_registrations(work->events, work->num_events, key); |
| 557 | work->poller.thread = NULL; |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | /* |
| 562 | * If we reached here, the work is either being registered in |
| 563 | * the k_work_poll_submit_to_queue(), executed or is pending. |
| 564 | * Only in the last case we have a chance to cancel it, but |
| 565 | * unfortunately there is no public API performing this task. |
| 566 | */ |
| 567 | |
| 568 | return -EINVAL; |
| 569 | } |
| 570 | |
| 571 | void k_work_poll_init(struct k_work_poll *work, |
| 572 | k_work_handler_t handler) |
| 573 | { |
| 574 | k_work_init(&work->work, triggered_work_handler); |
| 575 | work->events = NULL; |
| 576 | work->poller.thread = NULL; |
| 577 | work->real_handler = handler; |
| 578 | z_init_timeout(&work->timeout); |
| 579 | } |
| 580 | |
| 581 | int k_work_poll_submit_to_queue(struct k_work_q *work_q, |
| 582 | struct k_work_poll *work, |
| 583 | struct k_poll_event *events, |
| 584 | int num_events, |
| 585 | s32_t timeout) |
| 586 | { |
| 587 | int events_registered; |
| 588 | k_spinlock_key_t key; |
| 589 | |
| 590 | __ASSERT(work_q != NULL, "NULL work_q\n"); |
| 591 | __ASSERT(work != NULL, "NULL work\n"); |
| 592 | __ASSERT(events != NULL, "NULL events\n"); |
| 593 | __ASSERT(num_events > 0, "zero events\n"); |
| 594 | |
| 595 | /* Take overship of the work if it is possible. */ |
| 596 | key = k_spin_lock(&lock); |
| 597 | if (work->poller.thread != NULL) { |
| 598 | if (work->poller.thread == &work_q->thread) { |
| 599 | int retval; |
| 600 | |
| 601 | retval = triggered_work_cancel(work, key); |
| 602 | if (retval < 0) { |
| 603 | k_spin_unlock(&lock, key); |
| 604 | return retval; |
| 605 | } |
| 606 | } else { |
| 607 | k_spin_unlock(&lock, key); |
| 608 | return -EADDRINUSE; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | work->poller.is_polling = true; |
| 613 | work->poller.thread = &work_q->thread; |
| 614 | work->poller.cb = NULL; |
| 615 | k_spin_unlock(&lock, key); |
| 616 | |
| 617 | /* Save list of events. */ |
| 618 | work->events = events; |
| 619 | work->num_events = num_events; |
| 620 | |
| 621 | /* Clear result */ |
| 622 | work->poll_result = -EINPROGRESS; |
| 623 | |
| 624 | /* Register events */ |
| 625 | events_registered = register_events(events, num_events, |
| 626 | &work->poller, false); |
| 627 | |
| 628 | key = k_spin_lock(&lock); |
| 629 | if (work->poller.is_polling && timeout != K_NO_WAIT) { |
| 630 | /* |
| 631 | * Poller is still polling. |
| 632 | * No event is ready and all are watched. |
| 633 | */ |
| 634 | __ASSERT(num_events == events_registered, |
| 635 | "Some events were not registered!\n"); |
| 636 | |
| 637 | /* Setup timeout if such action is requested */ |
| 638 | if (timeout != K_FOREVER) { |
| 639 | z_add_timeout(&work->timeout, |
| 640 | triggered_work_expiration_handler, |
Andy Ross | 8892406 | 2019-10-03 11:43:10 -0700 | [diff] [blame] | 641 | k_ms_to_ticks_ceil32(timeout)); |
Piotr Zięcik | 19d8349 | 2019-09-27 09:16:25 +0200 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | /* From now, any event will result in submitted work. */ |
| 645 | work->poller.cb = triggered_work_poller_cb; |
| 646 | k_spin_unlock(&lock, key); |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | * The K_NO_WAIT timeout was specified or at least one event was ready |
| 652 | * at registration time or changed state since registration. Hopefully, |
| 653 | * the poller->cb was not set, so work was not submitted to workqueue. |
| 654 | */ |
| 655 | |
| 656 | /* |
| 657 | * If poller is still polling, no watched event occurred. This means |
| 658 | * we reached here due to K_NO_WAIT timeout "expiration". |
| 659 | */ |
| 660 | if (work->poller.is_polling) { |
| 661 | work->poller.is_polling = false; |
| 662 | work->poll_result = -EAGAIN; |
| 663 | } else { |
| 664 | work->poll_result = 0; |
| 665 | } |
| 666 | |
| 667 | /* Clear registrations. */ |
| 668 | clear_event_registrations(events, events_registered, key); |
| 669 | k_spin_unlock(&lock, key); |
| 670 | |
| 671 | /* Submit work. */ |
| 672 | k_work_submit_to_queue(work_q, &work->work); |
| 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | int k_work_poll_cancel(struct k_work_poll *work) |
| 678 | { |
| 679 | k_spinlock_key_t key; |
| 680 | int retval; |
| 681 | |
| 682 | /* Check if the work was submitted. */ |
| 683 | if (work == NULL || work->poller.thread == NULL) { |
| 684 | return -EINVAL; |
| 685 | } |
| 686 | |
| 687 | key = k_spin_lock(&lock); |
| 688 | retval = triggered_work_cancel(work, key); |
| 689 | k_spin_unlock(&lock, key); |
| 690 | |
| 691 | return retval; |
| 692 | } |