mpthreadport.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2016 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. #include <stdio.h>
  27. #include "py/gc.h"
  28. #include "py/mpthread.h"
  29. #include "gccollect.h"
  30. #if MICROPY_PY_THREAD
  31. // the mutex controls access to the linked list
  32. STATIC mp_thread_mutex_t thread_mutex;
  33. void mp_thread_init(void) {
  34. mp_thread_mutex_init(&thread_mutex);
  35. mp_thread_set_state(&mp_state_ctx.thread);
  36. }
  37. void mp_thread_gc_others(void) {
  38. mp_thread_mutex_lock(&thread_mutex, 1);
  39. for (pyb_thread_t *th = pyb_thread_all; th != NULL; th = th->all_next) {
  40. gc_collect_root((void**)&th, 1);
  41. gc_collect_root(&th->arg, 1);
  42. gc_collect_root(&th->stack, 1);
  43. if (th != pyb_thread_cur) {
  44. gc_collect_root(th->stack, th->stack_len);
  45. }
  46. }
  47. mp_thread_mutex_unlock(&thread_mutex);
  48. }
  49. void mp_thread_create(void *(*entry)(void*), void *arg, size_t *stack_size) {
  50. if (*stack_size == 0) {
  51. *stack_size = 4096; // default stack size
  52. } else if (*stack_size < 2048) {
  53. *stack_size = 2048; // minimum stack size
  54. }
  55. // round stack size to a multiple of the word size
  56. size_t stack_len = *stack_size / sizeof(uint32_t);
  57. *stack_size = stack_len * sizeof(uint32_t);
  58. // allocate stack and linked-list node (must be done outside thread_mutex lock)
  59. uint32_t *stack = m_new(uint32_t, stack_len);
  60. pyb_thread_t *th = m_new_obj(pyb_thread_t);
  61. mp_thread_mutex_lock(&thread_mutex, 1);
  62. // create thread
  63. uint32_t id = pyb_thread_new(th, stack, stack_len, entry, arg);
  64. if (id == 0) {
  65. mp_thread_mutex_unlock(&thread_mutex);
  66. nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't create thread"));
  67. }
  68. mp_thread_mutex_unlock(&thread_mutex);
  69. // adjust stack_size to provide room to recover from hitting the limit
  70. *stack_size -= 1024;
  71. }
  72. void mp_thread_start(void) {
  73. }
  74. void mp_thread_finish(void) {
  75. }
  76. #endif // MICROPY_PY_THREAD