]>
git.proxmox.com Git - mirror_frr.git/blob - pceplib/pcep_timers_event_loop.c
2 * This file is part of the PCEPlib, a PCEP protocol library.
4 * Copyright (C) 2020 Volta Networks https://voltanet.io/
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * Author : Brady Johnson <brady@voltanet.io>
28 #include <sys/select.h>
30 #include "pcep_timers_event_loop.h"
31 #include "pcep_timer_internals.h"
32 #include "pcep_utils_ordered_list.h"
33 #include "pcep_utils_logging.h"
34 #include "pcep_utils_memory.h"
36 /* For each expired timer: remove the timer from the list, call the
37 * expire_handler, and free the timer. */
38 void walk_and_process_timers(pcep_timers_context
*timers_context
)
40 pthread_mutex_lock(&timers_context
->timer_list_lock
);
42 bool keep_walking
= true;
43 ordered_list_node
*timer_node
= timers_context
->timer_list
->head
;
44 time_t now
= time(NULL
);
45 pcep_timer
*timer_data
;
47 /* the timers are sorted by expire_time, so we will only
48 * remove the top node each time through the loop */
49 while (timer_node
!= NULL
&& keep_walking
) {
50 timer_data
= (pcep_timer
*)timer_node
->data
;
51 if (timer_data
->expire_time
<= now
) {
52 timer_node
= timer_node
->next_node
;
53 ordered_list_remove_first_node(
54 timers_context
->timer_list
);
55 /* call the timer expired handler */
56 timers_context
->expire_handler(timer_data
->data
,
57 timer_data
->timer_id
);
58 pceplib_free(PCEPLIB_INFRA
, timer_data
);
64 pthread_mutex_unlock(&timers_context
->timer_list_lock
);
68 /* pcep_timers::initialize() will create a thread and invoke this method */
69 void *event_loop(void *context
)
71 if (context
== NULL
) {
74 "%s: pcep_timers_event_loop cannot start event_loop with NULL data",
79 pcep_log(LOG_NOTICE
, "%s: [%ld-%ld] Starting timers_event_loop thread",
80 __func__
, time(NULL
), pthread_self());
82 pcep_timers_context
*timers_context
= (pcep_timers_context
*)context
;
86 while (timers_context
->active
) {
87 /* check the timers every half second */
89 timer
.tv_usec
= 500000;
92 /* if the select() call gets interrupted, select() will
93 * set the remaining time in timer, so we need to call
96 retval
= select(0, NULL
, NULL
, NULL
, &timer
);
97 } while (retval
!= 0 && errno
== EINTR
);
99 walk_and_process_timers(timers_context
);
102 pcep_log(LOG_WARNING
, "%s: [%ld-%ld] Finished timers_event_loop thread",
103 __func__
, time(NULL
), pthread_self());