Skip to content

Commit

Permalink
Add more to itimer
Browse files Browse the repository at this point in the history
Signed-off-by: Ioannis Konstantelias <ikonstadel@gmail.com>
  • Loading branch information
gon1332 committed Feb 13, 2025
1 parent d3f1c1d commit 35f7307
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 42 deletions.
5 changes: 5 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
add_library(time_ops OBJECT time_ops.c)
target_include_directories(time_ops PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

add_library(list INTERFACE)
target_include_directories(list INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/lib)

add_executable(ptpd2
arith.c
bmc.c
Expand All @@ -14,6 +17,7 @@ add_executable(ptpd2
signaling.c
time_ops.c
timer.c
timer_itimer.c
timer_posix.c
timingdomain.c
dep/iniparser/dictionary.c
Expand Down Expand Up @@ -47,6 +51,7 @@ target_include_directories(ptpd2 PUBLIC
)
target_link_libraries(ptpd2 PUBLIC
m
list
"$<$<BOOL:${SNMP}>:netsnmp;netsnmpagent;netsnmpmibs>"
)
target_compile_options(ptpd2 PUBLIC
Expand Down
89 changes: 48 additions & 41 deletions src/timer_itimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,77 +19,80 @@

#include "timer_priv.h"
#include "utils.h"
#include "list.h"

#include <string.h>
#define ITIMER_INTERVAL_IN_MICROSECONDS 31250

struct tmr_itimer {
struct tmr timer;
bool expired;
bool running;
unsigned total_timers;
unsigned interval; /**< Interval in counts of itimer expirations */
unsigned time_left; /**< Time left in counts of itimer expirations */
unsigned time_elapsed; /**< Time elapsed in counts of itimer expirations */

struct list list;
};

static struct list *head = NULL;
static unsigned elapsed = 0;

static void
tmr_handler(int sig, siginfo_t *si, void *uc)
tmr_handler(int sig)
{
(void)sig;
(void)uc;
struct tmr_itimer *t_p = si->si_value.sival_ptr;

if (t_p) {
t_p->expired = true;
}
++elapsed;
}

static void
tmr_itimer_start(struct tmr *t, double period)
{
struct tmr_itimer *t_p = CONTAINER_OF(t, struct tmr_itimer, timer);
struct tmr_itimer *t_i = CONTAINER_OF(t, struct tmr_itimer, timer);

const double min_interval_in_seconds = 0.000250;

period = MAX(period, min_interval_in_seconds);

/* FILL ME UP */

t_p->running = true;
const unsigned interval_counts = (period * 1e6) / ITIMER_INTERVAL_IN_MICROSECONDS;
t_i->interval = MAX(interval_counts, 1);
t_i->time_left = t_i->interval;
t_i->running = true;
}

static void
tmr_itimer_stop(struct tmr *t)
{
struct tmr_itimer *t_p = CONTAINER_OF(t, struct tmr_itimer, timer);

/* FILL ME UP */
struct tmr_itimer *t_i = CONTAINER_OF(t, struct tmr_itimer, timer);

t_p->running = false;
t_i->interval = 0;
t_i->running = false;
}

static bool
tmr_itimer_running(struct tmr *t)
{
struct tmr_itimer *t_p = CONTAINER_OF(t, struct tmr_itimer, timer);
struct tmr_itimer *t_i = CONTAINER_OF(t, struct tmr_itimer, timer);

return t_p->running;
return t_i->running;
}

static bool
tmr_itimer_expired(struct tmr *t)
{
struct tmr_itimer *t_p = CONTAINER_OF(t, struct tmr_itimer, timer);
struct tmr_itimer *t_i = CONTAINER_OF(t, struct tmr_itimer, timer);

return t_p->expired;
return t_i->expired;
}

static void
tmr_itimer_destroy(struct tmr *t)
{
struct tmr_itimer *t_p = CONTAINER_OF(t, struct tmr_itimer, timer);
struct tmr_itimer *t_i = CONTAINER_OF(t, struct tmr_itimer, timer);

t_p->total_timers--;
t_i->total_timers--;

/* The last timer, closes the door */
if (--t_p->total_timers == 0) {
if (--t_i->total_timers == 0) {
struct sigaction sa = {0};
sa.sa_handler = SIG_DFL;
sigemptyset(&sa.sa_mask);
Expand All @@ -99,15 +102,15 @@ tmr_itimer_destroy(struct tmr *t)
}
}

free(t_p);
t_p = NULL;
free(t_i);
t_i = NULL;
}

struct tmr *
tmr_itimer_create(void)
{
struct tmr_itimer *t_p = calloc(1, sizeof *t_p);
if (!t_p) {
struct tmr_itimer *t_i = calloc(1, sizeof *t_i);
if (!t_i) {
return NULL;
}

Expand All @@ -124,30 +127,34 @@ tmr_itimer_create(void)

struct itimerval itimer;
itimer.it_interval.tv_sec = 0;
itimer.it_interval.tv_usec = 31250;
itimer.it_interval.tv_usec = ITIMER_INTERVAL_IN_MICROSECONDS;
itimer.it_value = itimer.it_interval;

if (setitimer(ITIMER_REAL, &itimer, NULL) == -1) {
perror("setitimer");
goto fail;
}

t_p->total_timers = 0;
t_i->total_timers = 0;
first_timer_init = true;

list_create(&head);
}

t_p->timer.destroy = tmr_itimer_destroy;
t_p->timer.start = tmr_itimer_start;
t_p->timer.stop = tmr_itimer_stop;
t_p->timer.running = tmr_itimer_running;
t_p->timer.expired = tmr_itimer_expired;
t_p->running = false;
t_p->expired = false;
t_p->total_timers++;
t_i->timer.destroy = tmr_itimer_destroy;
t_i->timer.start = tmr_itimer_start;
t_i->timer.stop = tmr_itimer_stop;
t_i->timer.running = tmr_itimer_running;
t_i->timer.expired = tmr_itimer_expired;
t_i->running = false;
t_i->expired = false;
t_i->total_timers++;

list_add(&head, &t_i->list);

return &t_p->timer;
return &t_i->timer;
fail:
free(t_p);
t_p = NULL;
free(t_i);
t_i = NULL;
return NULL;
}
5 changes: 4 additions & 1 deletion src/unit_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# SPDX-License-Identifier: BSD-2-Clause

add_executable(test_time_ops test_time_ops.c)
target_include_directories(test_time_ops PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(test_time_ops unity time_ops)
add_test(time_ops_suite test_time_ops)

add_executable(test_list test_list.c)
target_link_libraries(test_list unity list)
add_test(list_suite test_list)
6 changes: 6 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
*/
#define CONTAINER_OF(ptr, type, member) ((type *)((char *)(ptr)-offsetof(type, member)))

/**
* @brief Evaluates to the maximum number of the two provided
*
* @param a First number to compare
* @param b Second number to compare
*/
#define MAX(a, b) (((a) > (b)) ? (a) : (b))

#endif /* UTILS_H_ */

0 comments on commit 35f7307

Please sign in to comment.