]>
Commit | Line | Data |
---|---|---|
74971473 JG |
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 | ||
1f8031f7 DL |
24 | #ifdef HAVE_CONFIG_H |
25 | #include "config.h" | |
26 | #endif | |
27 | ||
74971473 JG |
28 | #include <errno.h> |
29 | #include <stddef.h> | |
30 | #include <stdbool.h> | |
31 | #include <stdio.h> | |
32 | #include <sys/select.h> | |
33 | ||
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" | |
39 | ||
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) | |
43 | { | |
44 | pthread_mutex_lock(&timers_context->timer_list_lock); | |
45 | ||
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; | |
50 | ||
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); | |
63 | } else { | |
64 | keep_walking = false; | |
65 | } | |
66 | } | |
67 | ||
68 | pthread_mutex_unlock(&timers_context->timer_list_lock); | |
69 | } | |
70 | ||
71 | ||
72 | /* pcep_timers::initialize() will create a thread and invoke this method */ | |
73 | void *event_loop(void *context) | |
74 | { | |
75 | if (context == NULL) { | |
76 | pcep_log( | |
77 | LOG_WARNING, | |
78 | "%s: pcep_timers_event_loop cannot start event_loop with NULL data", | |
79 | __func__); | |
80 | return NULL; | |
81 | } | |
82 | ||
83 | pcep_log(LOG_NOTICE, "%s: [%ld-%ld] Starting timers_event_loop thread", | |
84 | __func__, time(NULL), pthread_self()); | |
85 | ||
86 | pcep_timers_context *timers_context = (pcep_timers_context *)context; | |
87 | struct timeval timer; | |
88 | int retval; | |
89 | ||
90 | while (timers_context->active) { | |
91 | /* check the timers every half second */ | |
92 | timer.tv_sec = 0; | |
93 | timer.tv_usec = 500000; | |
94 | ||
95 | do { | |
96 | /* if the select() call gets interrupted, select() will | |
97 | * set the remaining time in timer, so we need to call | |
98 | * it again. | |
99 | */ | |
100 | retval = select(0, NULL, NULL, NULL, &timer); | |
101 | } while (retval != 0 && errno == EINTR); | |
102 | ||
103 | walk_and_process_timers(timers_context); | |
104 | } | |
105 | ||
106 | pcep_log(LOG_WARNING, "%s: [%ld-%ld] Finished timers_event_loop thread", | |
107 | __func__, time(NULL), pthread_self()); | |
108 | ||
109 | return NULL; | |
110 | } |