modutimeq.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2014 Damien P. George
  7. * Copyright (c) 2016-2017 Paul Sokolovsky
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include <string.h>
  28. #include <stdio.h>
  29. #include "py/objlist.h"
  30. #include "py/runtime.h"
  31. #include "py/smallint.h"
  32. #if MICROPY_PY_UTIMEQ
  33. #define MODULO MICROPY_PY_UTIME_TICKS_PERIOD
  34. #ifndef DEBUG
  35. #define DEBUG 0
  36. #endif
  37. // the algorithm here is modelled on CPython's heapq.py
  38. struct qentry {
  39. mp_uint_t time;
  40. mp_uint_t id;
  41. mp_obj_t callback;
  42. mp_obj_t args;
  43. };
  44. typedef struct _mp_obj_utimeq_t {
  45. mp_obj_base_t base;
  46. mp_uint_t alloc;
  47. mp_uint_t len;
  48. struct qentry items[];
  49. } mp_obj_utimeq_t;
  50. STATIC mp_uint_t utimeq_id;
  51. STATIC mp_obj_utimeq_t *get_heap(mp_obj_t heap_in) {
  52. return MP_OBJ_TO_PTR(heap_in);
  53. }
  54. STATIC bool time_less_than(struct qentry *item, struct qentry *parent) {
  55. mp_uint_t item_tm = item->time;
  56. mp_uint_t parent_tm = parent->time;
  57. mp_uint_t res = parent_tm - item_tm;
  58. if (res == 0) {
  59. // TODO: This actually should use the same "ring" logic
  60. // as for time, to avoid artifacts when id's overflow.
  61. return item->id < parent->id;
  62. }
  63. if ((mp_int_t)res < 0) {
  64. res += MODULO;
  65. }
  66. return res && res < (MODULO / 2);
  67. }
  68. STATIC mp_obj_t utimeq_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  69. mp_arg_check_num(n_args, n_kw, 1, 1, false);
  70. mp_uint_t alloc = mp_obj_get_int(args[0]);
  71. mp_obj_utimeq_t *o = m_new_obj_var(mp_obj_utimeq_t, struct qentry, alloc);
  72. o->base.type = type;
  73. memset(o->items, 0, sizeof(*o->items) * alloc);
  74. o->alloc = alloc;
  75. o->len = 0;
  76. return MP_OBJ_FROM_PTR(o);
  77. }
  78. STATIC void heap_siftdown(mp_obj_utimeq_t *heap, mp_uint_t start_pos, mp_uint_t pos) {
  79. struct qentry item = heap->items[pos];
  80. while (pos > start_pos) {
  81. mp_uint_t parent_pos = (pos - 1) >> 1;
  82. struct qentry *parent = &heap->items[parent_pos];
  83. bool lessthan = time_less_than(&item, parent);
  84. if (lessthan) {
  85. heap->items[pos] = *parent;
  86. pos = parent_pos;
  87. } else {
  88. break;
  89. }
  90. }
  91. heap->items[pos] = item;
  92. }
  93. STATIC void heap_siftup(mp_obj_utimeq_t *heap, mp_uint_t pos) {
  94. mp_uint_t start_pos = pos;
  95. mp_uint_t end_pos = heap->len;
  96. struct qentry item = heap->items[pos];
  97. for (mp_uint_t child_pos = 2 * pos + 1; child_pos < end_pos; child_pos = 2 * pos + 1) {
  98. // choose right child if it's <= left child
  99. if (child_pos + 1 < end_pos) {
  100. bool lessthan = time_less_than(&heap->items[child_pos], &heap->items[child_pos + 1]);
  101. if (!lessthan) {
  102. child_pos += 1;
  103. }
  104. }
  105. // bubble up the smaller child
  106. heap->items[pos] = heap->items[child_pos];
  107. pos = child_pos;
  108. }
  109. heap->items[pos] = item;
  110. heap_siftdown(heap, start_pos, pos);
  111. }
  112. STATIC mp_obj_t mod_utimeq_heappush(size_t n_args, const mp_obj_t *args) {
  113. (void)n_args;
  114. mp_obj_t heap_in = args[0];
  115. mp_obj_utimeq_t *heap = get_heap(heap_in);
  116. if (heap->len == heap->alloc) {
  117. mp_raise_msg(&mp_type_IndexError, "queue overflow");
  118. }
  119. mp_uint_t l = heap->len;
  120. heap->items[l].time = MP_OBJ_SMALL_INT_VALUE(args[1]);
  121. heap->items[l].id = utimeq_id++;
  122. heap->items[l].callback = args[2];
  123. heap->items[l].args = args[3];
  124. heap_siftdown(heap, 0, heap->len);
  125. heap->len++;
  126. return mp_const_none;
  127. }
  128. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_utimeq_heappush_obj, 4, 4, mod_utimeq_heappush);
  129. STATIC mp_obj_t mod_utimeq_heappop(mp_obj_t heap_in, mp_obj_t list_ref) {
  130. mp_obj_utimeq_t *heap = get_heap(heap_in);
  131. if (heap->len == 0) {
  132. nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "empty heap"));
  133. }
  134. mp_obj_list_t *ret = MP_OBJ_TO_PTR(list_ref);
  135. if (!MP_OBJ_IS_TYPE(list_ref, &mp_type_list) || ret->len < 3) {
  136. mp_raise_TypeError(NULL);
  137. }
  138. struct qentry *item = &heap->items[0];
  139. ret->items[0] = MP_OBJ_NEW_SMALL_INT(item->time);
  140. ret->items[1] = item->callback;
  141. ret->items[2] = item->args;
  142. heap->len -= 1;
  143. heap->items[0] = heap->items[heap->len];
  144. heap->items[heap->len].callback = MP_OBJ_NULL; // so we don't retain a pointer
  145. heap->items[heap->len].args = MP_OBJ_NULL;
  146. if (heap->len) {
  147. heap_siftup(heap, 0);
  148. }
  149. return mp_const_none;
  150. }
  151. STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_utimeq_heappop_obj, mod_utimeq_heappop);
  152. STATIC mp_obj_t mod_utimeq_peektime(mp_obj_t heap_in) {
  153. mp_obj_utimeq_t *heap = get_heap(heap_in);
  154. if (heap->len == 0) {
  155. nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "empty heap"));
  156. }
  157. struct qentry *item = &heap->items[0];
  158. return MP_OBJ_NEW_SMALL_INT(item->time);
  159. }
  160. STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_utimeq_peektime_obj, mod_utimeq_peektime);
  161. #if DEBUG
  162. STATIC mp_obj_t mod_utimeq_dump(mp_obj_t heap_in) {
  163. mp_obj_utimeq_t *heap = get_heap(heap_in);
  164. for (int i = 0; i < heap->len; i++) {
  165. printf(UINT_FMT "\t%p\t%p\n", heap->items[i].time,
  166. MP_OBJ_TO_PTR(heap->items[i].callback), MP_OBJ_TO_PTR(heap->items[i].args));
  167. }
  168. return mp_const_none;
  169. }
  170. STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_utimeq_dump_obj, mod_utimeq_dump);
  171. #endif
  172. STATIC mp_obj_t utimeq_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
  173. mp_obj_utimeq_t *self = MP_OBJ_TO_PTR(self_in);
  174. switch (op) {
  175. case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->len != 0);
  176. case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len);
  177. default: return MP_OBJ_NULL; // op not supported
  178. }
  179. }
  180. STATIC const mp_rom_map_elem_t utimeq_locals_dict_table[] = {
  181. { MP_ROM_QSTR(MP_QSTR_push), MP_ROM_PTR(&mod_utimeq_heappush_obj) },
  182. { MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&mod_utimeq_heappop_obj) },
  183. { MP_ROM_QSTR(MP_QSTR_peektime), MP_ROM_PTR(&mod_utimeq_peektime_obj) },
  184. #if DEBUG
  185. { MP_ROM_QSTR(MP_QSTR_dump), MP_ROM_PTR(&mod_utimeq_dump_obj) },
  186. #endif
  187. };
  188. STATIC MP_DEFINE_CONST_DICT(utimeq_locals_dict, utimeq_locals_dict_table);
  189. STATIC const mp_obj_type_t utimeq_type = {
  190. { &mp_type_type },
  191. .name = MP_QSTR_utimeq,
  192. .make_new = utimeq_make_new,
  193. .unary_op = utimeq_unary_op,
  194. .locals_dict = (void*)&utimeq_locals_dict,
  195. };
  196. STATIC const mp_rom_map_elem_t mp_module_utimeq_globals_table[] = {
  197. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utimeq) },
  198. { MP_ROM_QSTR(MP_QSTR_utimeq), MP_ROM_PTR(&utimeq_type) },
  199. };
  200. STATIC MP_DEFINE_CONST_DICT(mp_module_utimeq_globals, mp_module_utimeq_globals_table);
  201. const mp_obj_module_t mp_module_utimeq = {
  202. .base = { &mp_type_module },
  203. .globals = (mp_obj_dict_t*)&mp_module_utimeq_globals,
  204. };
  205. #endif //MICROPY_PY_UTIMEQ