• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pybricks / pybricks-micropython / 14329424534

31 Mar 2025 01:50PM UTC coverage: 56.638% (+0.2%) from 56.477%
14329424534

push

github

laurensvalk
pbio/os: Move IRQ hooks to platform.

Proceed to make pbio more independent from MicroPython.

Also restore them as static inline as they used to be
prior to https://github.com/pybricks/pybricks-micropython/pull/298
which reduces build size.

0 of 6 new or added lines in 1 file covered. (0.0%)

204 existing lines in 11 files now uncovered.

3878 of 6847 relevant lines covered (56.64%)

20623019.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.38
/lib/pbio/drv/clock/clock_linux.c
1
// SPDX-License-Identifier: MIT
2
// Copyright (c) 2018-2022 The Pybricks Authors
3

4
#include <pbdrv/config.h>
5

6
#if PBDRV_CONFIG_CLOCK_LINUX
7

8
#include <stdint.h>
9
#include <time.h>
10
#include <unistd.h>
11

12
#include <pbio/os.h>
13

14
// The SIGNAL option adds a timer that acts as the 1ms tick on embedded systems.
15

16
#if PBDRV_CONFIG_CLOCK_LINUX_SIGNAL
17

18
#include <errno.h>
19
#include <pthread.h>
20
#include <signal.h>
21
#include <stdio.h>
22

23
#include <contiki.h>
24

25
#define NSEC_PER_MSEC       1000000
26

27
#define TIMER_SIGNAL        SIGRTMIN
28
#define TIMER_INTERVAL      (1 * NSEC_PER_MSEC)
29

30
static pthread_t main_thread;
31

32
static void handle_signal(int sig) {
51,982✔
33
    // since signals can occur on any thread, we need to ensure
34
    // that we interrupt the main thread. This is needed, e.g.
35
    // for MicroPython to ensure that any blocking syscall on the
36
    // main thread is interrupted and the event poll hook runs.
37
    if (pthread_self() == main_thread) {
51,982✔
38
        etimer_request_poll();
51,982✔
39
        pbio_os_request_poll();
51,982✔
40
    } else {
UNCOV
41
        pthread_kill(main_thread, TIMER_SIGNAL);
×
42
    }
43
}
51,982✔
44

45
void pbdrv_clock_init(void) {
24✔
46
    static timer_t clock_timer;
24✔
47
    int err;
24✔
48

49
    main_thread = pthread_self();
24✔
50

51
    // set up 1ms tick using signal
52

53
    struct sigaction sa = {
24✔
54
        .sa_handler = handle_signal,
55
    };
56

57
    sigemptyset(&sa.sa_mask);
24✔
58

59
    err = sigaction(TIMER_SIGNAL, &sa, NULL);
24✔
60

61
    if (err == -1) {
24✔
UNCOV
62
        perror("sigaction");
×
63
    }
64

65
    struct sigevent se = {
48✔
66
        .sigev_notify = SIGEV_SIGNAL,
67
        .sigev_signo = TIMER_SIGNAL,
24✔
68
    };
69

70
    err = timer_create(CLOCK_REALTIME, &se, &clock_timer);
24✔
71

72
    if (err == -1) {
24✔
UNCOV
73
        perror("timer_create");
×
74
    }
75

76
    struct itimerspec its = {
24✔
77
        .it_interval.tv_sec = 0,
78
        .it_interval.tv_nsec = TIMER_INTERVAL,
79
        .it_value.tv_sec = 0,
80
        .it_value.tv_nsec = TIMER_INTERVAL,
81
    };
82

83
    err = timer_settime(clock_timer, 0, &its, NULL);
24✔
84

85
    if (err == -1) {
24✔
UNCOV
86
        perror("timer_settime");
×
87
    }
88
}
24✔
89

90
#else // PBDRV_CONFIG_CLOCK_LINUX_SIGNAL
91

92
void pbdrv_clock_init(void) {
93
}
94

95
#endif // PBDRV_CONFIG_CLOCK_LINUX_SIGNAL
96

97
uint32_t pbdrv_clock_get_ms(void) {
727,098✔
98
    struct timespec time_val;
727,098✔
99
    clock_gettime(CLOCK_MONOTONIC_RAW, &time_val);
727,098✔
100
    return time_val.tv_sec * 1000 + time_val.tv_nsec / 1000000;
727,098✔
101
}
102

103
uint32_t pbdrv_clock_get_100us(void) {
32,291✔
104
    struct timespec time_val;
32,291✔
105
    clock_gettime(CLOCK_MONOTONIC_RAW, &time_val);
32,291✔
106
    return time_val.tv_sec * 10000 + time_val.tv_nsec / 100000;
32,291✔
107
}
108

109
uint32_t pbdrv_clock_get_us(void) {
×
UNCOV
110
    struct timespec time_val;
×
UNCOV
111
    clock_gettime(CLOCK_MONOTONIC_RAW, &time_val);
×
UNCOV
112
    return time_val.tv_sec * 1000000 + time_val.tv_nsec / 1000;
×
113
}
114

115
#endif // PBDRV_CONFIG_CLOCK_LINUX
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc