Skip to content

Commit d9d592d

Browse files
committed
posix: timer: support other clocks
There is no requirement that says e.g. CLOCK_REALTIME cannot be used for timer_create(). In fact, the spec explicitly requires it. It might not be ideal, but users should still be able to use it. Signed-off-by: Christopher Friedt <[email protected]>
1 parent 31c4e34 commit d9d592d

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

lib/posix/timer.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ static void zephyr_timer_wrapper(struct k_timer *ztimer);
1818

1919
struct timer_obj {
2020
struct k_timer ztimer;
21-
void (*sigev_notify_function)(union sigval val);
22-
union sigval val;
23-
int sigev_notify;
21+
struct sigevent sev;
2422
struct k_sem sem_cond;
2523
pthread_t thread;
2624
struct timespec interval; /* Reload value */
@@ -41,7 +39,7 @@ static void zephyr_timer_wrapper(struct k_timer *ztimer)
4139
timer->status = NOT_ACTIVE;
4240
}
4341

44-
(timer->sigev_notify_function)(timer->val);
42+
(timer->sev.sigev_notify_function)(timer->sev.sigev_value);
4543
}
4644

4745
static void *zephyr_thread_wrapper(void *arg)
@@ -55,7 +53,7 @@ static void *zephyr_thread_wrapper(void *arg)
5553

5654
k_sem_take(&timer->sem_cond, K_FOREVER);
5755

58-
(timer->sigev_notify_function)(timer->val);
56+
(timer->sev.sigev_notify_function)(timer->sev.sigev_value);
5957
}
6058

6159
return NULL;
@@ -82,10 +80,8 @@ int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
8280
struct timer_obj *timer;
8381
const k_timeout_t alloc_timeout = K_MSEC(CONFIG_TIMER_CREATE_WAIT);
8482

85-
if (clockid != CLOCK_MONOTONIC || evp == NULL ||
86-
(evp->sigev_notify != SIGEV_NONE &&
87-
evp->sigev_notify != SIGEV_SIGNAL &&
88-
evp->sigev_notify != SIGEV_THREAD)) {
83+
if (evp == NULL || (evp->sigev_notify != SIGEV_NONE && evp->sigev_notify != SIGEV_SIGNAL &&
84+
evp->sigev_notify != SIGEV_THREAD)) {
8985
errno = EINVAL;
9086
return -1;
9187
}
@@ -97,8 +93,8 @@ int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
9793
return -1;
9894
}
9995

100-
timer->sigev_notify_function = evp->sigev_notify_function;
101-
timer->val = evp->sigev_value;
96+
timer->sev.sigev_notify_function = evp->sigev_notify_function;
97+
timer->sev.sigev_value = evp->sigev_value;
10298
timer->interval.tv_sec = 0;
10399
timer->interval.tv_nsec = 0;
104100
timer->reload = 0U;
@@ -109,7 +105,7 @@ int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
109105
} else if (evp->sigev_notify == SIGEV_THREAD) {
110106
int ret;
111107

112-
timer->sigev_notify = SIGEV_THREAD;
108+
timer->sev.sigev_notify = SIGEV_THREAD;
113109
k_sem_init(&timer->sem_cond, 0, 1);
114110
ret = pthread_create(&timer->thread, evp->sigev_notify_attributes,
115111
zephyr_thread_wrapper, timer);
@@ -266,8 +262,9 @@ int timer_delete(timer_t timerid)
266262
k_timer_stop(&timer->ztimer);
267263
}
268264

269-
if (timer->sigev_notify == SIGEV_THREAD)
265+
if (timer->sev.sigev_notify == SIGEV_THREAD) {
270266
pthread_cancel(timer->thread);
267+
}
271268

272269
k_mem_slab_free(&posix_timer_slab, (void *)timer);
273270

0 commit comments

Comments
 (0)