pybthread.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2017 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #ifndef MICROPY_INCLUDED_STM32_PYBTHREAD_H
  27. #define MICROPY_INCLUDED_STM32_PYBTHREAD_H
  28. typedef struct _pyb_thread_t {
  29. void *sp;
  30. uint32_t local_state;
  31. void *arg; // thread Python args, a GC root pointer
  32. void *stack; // pointer to the stack
  33. size_t stack_len; // number of words in the stack
  34. uint32_t timeslice;
  35. struct _pyb_thread_t *all_next;
  36. struct _pyb_thread_t *run_prev;
  37. struct _pyb_thread_t *run_next;
  38. struct _pyb_thread_t *queue_next;
  39. } pyb_thread_t;
  40. typedef pyb_thread_t *pyb_mutex_t;
  41. extern volatile int pyb_thread_enabled;
  42. extern pyb_thread_t *volatile pyb_thread_all;
  43. extern pyb_thread_t *volatile pyb_thread_cur;
  44. void pyb_thread_init(pyb_thread_t *th);
  45. void pyb_thread_deinit();
  46. uint32_t pyb_thread_new(pyb_thread_t *th, void *stack, size_t stack_len, void *entry, void *arg);
  47. void pyb_thread_dump(void);
  48. static inline uint32_t pyb_thread_get_id(void) {
  49. return (uint32_t)pyb_thread_cur;
  50. }
  51. static inline void pyb_thread_set_local(void *value) {
  52. pyb_thread_cur->local_state = (uint32_t)value;
  53. }
  54. static inline void *pyb_thread_get_local(void) {
  55. return (void*)pyb_thread_cur->local_state;
  56. }
  57. static inline void pyb_thread_yield(void) {
  58. if (pyb_thread_cur->run_next == pyb_thread_cur) {
  59. __WFI();
  60. } else {
  61. SCB->ICSR = SCB_ICSR_PENDSVSET_Msk;
  62. }
  63. }
  64. void pyb_mutex_init(pyb_mutex_t *m);
  65. int pyb_mutex_lock(pyb_mutex_t *m, int wait);
  66. void pyb_mutex_unlock(pyb_mutex_t *m);
  67. #endif // MICROPY_INCLUDED_STM32_PYBTHREAD_H