]>
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>
32 #include <sys/select.h>
34 #include "pcep_timers_event_loop.h"
35 #include "pcep_timer_internals.h"
36 #include "pcep_utils_ordered_list.h"
37 #include "pcep_utils_logging.h"
38 #include "pcep_utils_memory.h"
40 /* For each expired timer: remove the timer from the list, call the
41 * expire_handler, and free the timer. */
42 void walk_and_process_timers(pcep_timers_context
*timers_context
)
44 pthread_mutex_lock(&timers_context
->timer_list_lock
);
46 bool keep_walking
= true;
47 ordered_list_node
*timer_node
= timers_context
->timer_list
->head
;
48 time_t now
= time(NULL
);
49 pcep_timer
*timer_data
;
51 /* the timers are sorted by expire_time, so we will only
52 * remove the top node each time through the loop */
53 while (timer_node
!= NULL
&& keep_walking
) {
54 timer_data
= (pcep_timer
*)timer_node
->data
;
55 if (timer_data
->expire_time
<= now
) {
56 timer_node
= timer_node
->next_node
;
57 ordered_list_remove_first_node(
58 timers_context
->timer_list
);
59 /* call the timer expired handler */
60 timers_context
->expire_handler(timer_data
->data
,
61 timer_data
->timer_id
);
62 pceplib_free(PCEPLIB_INFRA
, timer_data
);
68 pthread_mutex_unlock(&timers_context
->timer_list_lock
);
72 /* pcep_timers::initialize() will create a thread and invoke this method */
73 void *event_loop(void *context
)
75 if (context
== NULL
) {
78 "%s: pcep_timers_event_loop cannot start event_loop with NULL data",
83 pcep_log(LOG_NOTICE
, "%s: [%ld-%ld] Starting timers_event_loop thread",
84 __func__
, time(NULL
), pthread_self());
86 pcep_timers_context
*timers_context
= (pcep_timers_context
*)context
;
90 while (timers_context
->active
) {
91 /* check the timers every half second */
93 timer
.tv_usec
= 500000;
96 /* if the select() call gets interrupted, select() will
97 * set the remaining time in timer, so we need to call
100 retval
= select(0, NULL
, NULL
, NULL
, &timer
);
101 } while (retval
!= 0 && errno
== EINTR
);
103 walk_and_process_timers(timers_context
);
106 pcep_log(LOG_WARNING
, "%s: [%ld-%ld] Finished timers_event_loop thread",
107 __func__
, time(NULL
), pthread_self());