poll.c 21 KB

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