]> git.proxmox.com Git - mirror_frr.git/blob - pceplib/pcep_timers_event_loop.c
Merge pull request #8257 from donaldsharp/fix_bgp_metric
[mirror_frr.git] / pceplib / pcep_timers_event_loop.c
1 /*
2 * This file is part of the PCEPlib, a PCEP protocol library.
3 *
4 * Copyright (C) 2020 Volta Networks https://voltanet.io/
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author : Brady Johnson <brady@voltanet.io>
20 *
21 */
22
23
24 #include <errno.h>
25 #include <stddef.h>
26 #include <stdbool.h>
27 #include <stdio.h>
28 #include <sys/select.h>
29
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"
35
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)
39 {
40 pthread_mutex_lock(&timers_context->timer_list_lock);
41
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;
46
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);
59 } else {
60 keep_walking = false;
61 }
62 }
63
64 pthread_mutex_unlock(&timers_context->timer_list_lock);
65 }
66
67
68 /* pcep_timers::initialize() will create a thread and invoke this method */
69 void *event_loop(void *context)
70 {
71 if (context == NULL) {
72 pcep_log(
73 LOG_WARNING,
74 "%s: pcep_timers_event_loop cannot start event_loop with NULL data",
75 __func__);
76 return NULL;
77 }
78
79 pcep_log(LOG_NOTICE, "%s: [%ld-%ld] Starting timers_event_loop thread",
80 __func__, time(NULL), pthread_self());
81
82 pcep_timers_context *timers_context = (pcep_timers_context *)context;
83 struct timeval timer;
84 int retval;
85
86 while (timers_context->active) {
87 /* check the timers every half second */
88 timer.tv_sec = 0;
89 timer.tv_usec = 500000;
90
91 do {
92 /* if the select() call gets interrupted, select() will
93 * set the remaining time in timer, so we need to call
94 * it again.
95 */
96 retval = select(0, NULL, NULL, NULL, &timer);
97 } while (retval != 0 && errno == EINTR);
98
99 walk_and_process_timers(timers_context);
100 }
101
102 pcep_log(LOG_WARNING, "%s: [%ld-%ld] Finished timers_event_loop thread",
103 __func__, time(NULL), pthread_self());
104
105 return NULL;
106 }