def __init__(self, runq_len=16, waitq_len=16, ioq_len=0, lp_len=0):
     self.runq = ucollections.deque((), runq_len, True)
     self._max_overdue_ms = 0
     self.lpq = utimeq.utimeq(lp_len) if lp_len else None
     self.ioq_len = ioq_len
     if ioq_len:
         self.ioq = ucollections.deque((), ioq_len, True)
         self._call_io = self._call_now
     else:
         self._call_io = self.call_soon
     self.waitq = utimeq.utimeq(waitq_len)
     # Current task being run. Task is a top-level coroutine scheduled
     # in the event loop (sub-coroutines executed transparently by
     # yield from/await, event loop "doesn't see" them).
     self.cur_task = None
Exemple #2
0
 def __init__(self, runq_len=16, waitq_len=16):
     self.runq = ucollections.deque((), runq_len, True)
     self.waitq = utimeq.utimeq(waitq_len)
     # Current task being run. Task is a top-level coroutine scheduled
     # in the event loop (sub-coroutines executed transparently by
     # yield from/await, event loop "doesn't see" them).
     self.cur_task = None
 def __init__(self, len=42, lpqlen=42, max_overdue_ms=0, hpqlen=0):
     super().__init__(len)
     self._max_overdue_ms = max_overdue_ms
     self.lpq = utimeq.utimeq(lpqlen)
     if hpqlen:
         self.hpq = [[0,0,0] for _ in range(hpqlen)]
     else:
         self.hpq = None
Exemple #4
0
def edge_case(edge, offset):
    h = utimeq(10)
    add(h, ticks_add(0, offset))
    add(h, ticks_add(edge, offset))
    dprint(h)
    l = pop_all(h)
    diff = ticks_diff(l[1][0], l[0][0])
    dprint(diff, diff > 0)
    return diff
def edge_case(edge, offset):
    h = utimeq(10)
    add(h, ticks_add(0, offset))
    add(h, ticks_add(edge, offset))
    dprint(h)
    l = pop_all(h)
    diff = ticks_diff(l[1][0], l[0][0])
    dprint(diff, diff > 0)
    return diff
Exemple #6
0
def unschedule_task(task):
    '''
    Remove task from the time queue.  Cancels previous `schedule_task`.
    '''
    global _scheduled_tasks
    task_entry = [0, 0, 0]  # deadline, task, value
    queue_copy = utimeq.utimeq(_MAX_QUEUE_SIZE)
    while _scheduled_tasks:
        _scheduled_tasks.pop(task_entry)
        if task_entry[1] is not task:
            queue_copy.push(task_entry[0], task_entry[1], task_entry[2])
    _scheduled_tasks = queue_copy
Exemple #7
0
def unschedule(task):
    '''
    Remove task from the time queue.  Cancels previous `schedule`.
    '''
    global _queue
    task_entry = [0, 0, 0]  # deadline, task, value
    queue_copy = utimeq.utimeq(_QUEUE_SIZE)
    while _queue:
        _queue.pop(task_entry)
        d, t, v = task_entry
        if t is not task:
            queue_copy.push(d, t, v)
    _queue = queue_copy
Exemple #8
0
stepped through until completion, and can get asynchronously blocked by
`yield`ing or `await`ing a syscall.

See `schedule`, `run`, and syscalls `sleep`, `wait`, `signal` and `spawn`.
"""

import utime
import utimeq
from micropython import const

from trezor import io, log

after_step_hook = None  # function, called after each task step

_QUEUE_SIZE = const(64)  # maximum number of scheduled tasks
_queue = utimeq.utimeq(_QUEUE_SIZE)
_paused = {}

if __debug__:
    # for performance stats
    import array

    log_delay_pos = 0
    log_delay_rb_len = const(10)
    log_delay_rb = array.array("i", [0] * log_delay_rb_len)


def schedule(task, value=None, deadline=None):
    """
    Schedule task to be executed with `value` on given `deadline` (in
    microseconds).  Does not start the event loop itself, see `run`.
Exemple #9
0
 def __init__(self, len=42):
     self.q = utimeq.utimeq(len)
     # Current task being run. Task is a top-level coroutine scheduled
     # in the event loop (sub-coroutines executed transparently by
     # yield from/await, event loop "doesn't see" them).
     self.cur_task = None
Exemple #10
0
 def __init__(self, len=42):
     self.q = utimeq.utimeq(len)
Exemple #11
0
        Any,
        Awaitable,
        Callable,
        Coroutine,
        Generator,
    )

    Task = Coroutine | Generator
    AwaitableTask = Task | Awaitable
    Finalizer = Callable[[Task, Any], None]

# function to call after every task step
after_step_hook: Callable[[], None] | None = None

# tasks scheduled for execution in the future
_queue = utimeq.utimeq(64)

# tasks paused on I/O
_paused: dict[int, set[Task]] = {}

# functions to execute after a task is finished
_finalizers: dict[int, Finalizer] = {}

# reference to the task that is currently executing
this_task: Task | None = None

if __debug__:
    # synthetic event queue
    synthetic_events: list[tuple[int, Any]] = []

Exemple #12
0
SIZE = 7

DEBUG = 0

if DEBUG:

    def dprint(*v):
        print(*v)

else:

    def dprint(*v):
        pass


h = utimeq(SIZE)


def push(v):
    return h.push(v, None, None)


def pop():
    l = [0, 0, 0]
    h.pop(l)
    return l[0]


def expect_IndexError(idx, case_id):
    try:
        h.remove(idx)
Exemple #13
0
MAX = ticks_add(0, -1)
MODULO_HALF = MAX // 2 + 1

if DEBUG:

    def dprint(*v):
        print(*v)
else:

    def dprint(*v):
        pass


# Try not to crash on invalid data
h = utimeq(10)
try:
    h.push(1)
    assert False
except TypeError:
    pass

try:
    h.pop(1)
    assert False
except IndexError:
    pass


def pop_all(h):
    l = []
 def __init__(self, len=42, lpqlen=42):
     super().__init__(len)
     self._max_overdue_ms = 0
     self.lpq = utimeq.utimeq(lpqlen)
     self.hp_tasks = None
     self.create_task(self.lp_monitor())
Exemple #15
0
 def __init__(self, runq_len=16, waitq_len=16, lpqlen=42):
     super().__init__(runq_len, waitq_len)
     self._max_overdue_ms = 0
     self.lpq = utimeq.utimeq(lpqlen)
     self.hp_tasks = []
MAX = ticks_add(0, -1)
MODULO_HALF = MAX // 2 + 1

if DEBUG:

    def dprint(*v):
        print(*v)

else:

    def dprint(*v):
        pass


# Try not to crash on invalid data
h = utimeq(10)
try:
    h.push(1)
    assert False
except TypeError:
    pass

try:
    h.pop(1)
    assert False
except IndexError:
    pass

# unsupported unary op
try:
    ~h
Exemple #17
0
TOUCH = io.TOUCH
TOUCH_START = io.TOUCH_START
TOUCH_MOVE = io.TOUCH_MOVE
TOUCH_END = io.TOUCH_END

READ = io.POLL_READ
WRITE = io.POLL_WRITE

after_step_hook = None  # function, called after each task step

_MAX_SELECT_DELAY = const(1000000)  # usec delay if queue is empty
_MAX_QUEUE_SIZE = const(64)  # maximum number of scheduled tasks

_paused_tasks = {}  # {message interface: [task]}
_scheduled_tasks = utimeq.utimeq(_MAX_QUEUE_SIZE)

if __debug__:
    # for performance stats
    import array
    log_delay_pos = 0
    log_delay_rb_len = const(10)
    log_delay_rb = array.array('i', [0] * log_delay_rb_len)


def schedule_task(task, value=None, deadline=None):
    '''
    Schedule task to be executed with `value` on given `deadline` (in
    microseconds).  Does not start the event loop itself, see `run`.
    '''
    if deadline is None:
Exemple #18
0
 def __init__(self, qlen=16):
     self.queue = utimeq.utimeq(qlen)
Exemple #19
0
    sys.exit()

DEBUG = 0

MAX = ticks_add(0, -1)
MODULO_HALF = MAX // 2 + 1

if DEBUG:
    def dprint(*v):
        print(*v)
else:
    def dprint(*v):
        pass

# Try not to crash on invalid data
h = utimeq(10)
try:
    h.push(1)
    assert False
except TypeError:
    pass

try:
    h.pop(1)
    assert False
except IndexError:
    pass


def pop_all(h):
    l = []
Exemple #20
0
    sys.exit()

DEBUG = 0

MAX = ticks_add(0, -1)
MODULO_HALF = MAX // 2 + 1

if DEBUG:
    def dprint(*v):
        print(*v)
else:
    def dprint(*v):
        pass

# Try not to crash on invalid data
h = utimeq(10)
try:
    h.push(1)
    assert False
except TypeError:
    pass

try:
    h.pop(1)
    assert False
except IndexError:
    pass

# unsupported unary op
try:
    ~h
Exemple #21
0
 def __init__(self, len=42):
     self.q = utimeq.utimeq(len)
Exemple #22
0
try:
    from utimeq import utimeq
except ImportError:
    print("SKIP")
    raise SystemExit

h = utimeq(4)

assert h.push(0, 1, 2) == 0
assert h.push(1, 1, 2) == 1
assert h.push(2, 1, 2) == 2
assert h.push(3, 1, 2) == 3

try:
    h.push(0, 1, 2)
    assert False
except IndexError:
    pass

h.pop([0, 0, 0])
assert h.push(4, 1, 2) == 0

h.pop([0, 0, 0])
assert h.push(5, 1, 2) == 1

h.pop([0, 0, 0])
assert h.push(6, 1, 2) == 2

h.pop([0, 0, 0])
assert h.push(7, 1, 2) == 3