]> git.proxmox.com Git - mirror_frr.git/blame - lib/frr_pthread.c
Fix 'make clean' os Mac OS
[mirror_frr.git] / lib / frr_pthread.c
CommitLineData
98f14af8 1/*
a45dc974 2 * Utilities and interfaces for managing POSIX threads within FRR.
d8a8a8de 3 * Copyright (C) 2017 Cumulus Networks, Inc.
896014f4
DL
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
98f14af8
QY
18 */
19
20#include <zebra.h>
21#include <pthread.h>
324be174
DL
22#ifdef HAVE_PTHREAD_NP_H
23#include <pthread_np.h>
24#endif
b2140cb7 25#include <sched.h>
98f14af8
QY
26
27#include "frr_pthread.h"
28#include "memory.h"
1ac267a2 29#include "linklist.h"
98f14af8 30
a45dc974 31DEFINE_MTYPE(LIB, FRR_PTHREAD, "FRR POSIX Thread");
0bbb9e72 32DEFINE_MTYPE(LIB, PTHREAD_PRIM, "POSIX synchronization primitives");
98f14af8 33
a45dc974
QY
34/* default frr_pthread start/stop routine prototypes */
35static void *fpt_run(void *arg);
36static int fpt_halt(struct frr_pthread *fpt, void **res);
98f14af8 37
a45dc974
QY
38/* default frr_pthread attributes */
39struct frr_pthread_attr frr_pthread_attr_default = {
a45dc974
QY
40 .start = fpt_run,
41 .stop = fpt_halt,
a45dc974
QY
42};
43
1ac267a2
DL
44/* list to keep track of all frr_pthreads */
45static pthread_mutex_t frr_pthread_list_mtx = PTHREAD_MUTEX_INITIALIZER;
46static struct list *frr_pthread_list;
a45dc974 47
98f14af8
QY
48/* ------------------------------------------------------------------------ */
49
50void frr_pthread_init()
51{
1ac267a2 52 pthread_mutex_lock(&frr_pthread_list_mtx);
d62a17ae 53 {
1ac267a2
DL
54 frr_pthread_list = list_new();
55 frr_pthread_list->del = (void (*)(void *))&frr_pthread_destroy;
d62a17ae 56 }
1ac267a2 57 pthread_mutex_unlock(&frr_pthread_list_mtx);
98f14af8
QY
58}
59
60void frr_pthread_finish()
61{
1ac267a2 62 pthread_mutex_lock(&frr_pthread_list_mtx);
d62a17ae 63 {
6a154c88 64 list_delete(&frr_pthread_list);
d62a17ae 65 }
1ac267a2 66 pthread_mutex_unlock(&frr_pthread_list_mtx);
98f14af8
QY
67}
68
d8a8a8de 69struct frr_pthread *frr_pthread_new(struct frr_pthread_attr *attr,
57019528 70 const char *name, const char *os_name)
98f14af8 71{
d62a17ae 72 struct frr_pthread *fpt = NULL;
73
a45dc974
QY
74 attr = attr ? attr : &frr_pthread_attr_default;
75
1ac267a2
DL
76 fpt = XCALLOC(MTYPE_FRR_PTHREAD, sizeof(struct frr_pthread));
77 /* initialize mutex */
78 pthread_mutex_init(&fpt->mtx, NULL);
79 /* create new thread master */
80 fpt->master = thread_master_create(name);
81 /* set attributes */
82 fpt->attr = *attr;
83 name = (name ? name : "Anonymous thread");
84 fpt->name = XSTRDUP(MTYPE_FRR_PTHREAD, name);
85 if (os_name)
86 snprintf(fpt->os_name, OS_THREAD_NAMELEN, "%s", os_name);
b8dccd94
DS
87 else
88 snprintf(fpt->os_name, OS_THREAD_NAMELEN, "%s", name);
1ac267a2
DL
89 /* initialize startup synchronization primitives */
90 fpt->running_cond_mtx = XCALLOC(
91 MTYPE_PTHREAD_PRIM, sizeof(pthread_mutex_t));
92 fpt->running_cond = XCALLOC(MTYPE_PTHREAD_PRIM,
93 sizeof(pthread_cond_t));
94 pthread_mutex_init(fpt->running_cond_mtx, NULL);
95 pthread_cond_init(fpt->running_cond, NULL);
96
97 pthread_mutex_lock(&frr_pthread_list_mtx);
d62a17ae 98 {
1ac267a2 99 listnode_add(frr_pthread_list, fpt);
d62a17ae 100 }
1ac267a2 101 pthread_mutex_unlock(&frr_pthread_list_mtx);
d62a17ae 102
103 return fpt;
98f14af8
QY
104}
105
106void frr_pthread_destroy(struct frr_pthread *fpt)
107{
d62a17ae 108 thread_master_free(fpt->master);
a45dc974 109
d8a8a8de 110 pthread_mutex_destroy(&fpt->mtx);
a45dc974
QY
111 pthread_mutex_destroy(fpt->running_cond_mtx);
112 pthread_cond_destroy(fpt->running_cond);
d8a8a8de
QY
113 if (fpt->name)
114 XFREE(MTYPE_FRR_PTHREAD, fpt->name);
a45dc974
QY
115 XFREE(MTYPE_PTHREAD_PRIM, fpt->running_cond_mtx);
116 XFREE(MTYPE_PTHREAD_PRIM, fpt->running_cond);
d62a17ae 117 XFREE(MTYPE_FRR_PTHREAD, fpt);
98f14af8
QY
118}
119
c80bedb8 120int frr_pthread_set_name(struct frr_pthread *fpt)
d8a8a8de 121{
57019528
CS
122 int ret = 0;
123
e9d938b8
DL
124#ifdef HAVE_PTHREAD_SETNAME_NP
125# ifdef GNU_LINUX
c80bedb8 126 ret = pthread_setname_np(fpt->thread, fpt->os_name);
e9d938b8 127# else /* NetBSD */
c80bedb8 128 ret = pthread_setname_np(fpt->thread, fpt->os_name, NULL);
e9d938b8
DL
129# endif
130#elif defined(HAVE_PTHREAD_SET_NAME_NP)
c80bedb8 131 pthread_set_name_np(fpt->thread, fpt->os_name);
57019528 132#endif
57019528
CS
133
134 return ret;
d8a8a8de
QY
135}
136
a45dc974 137int frr_pthread_run(struct frr_pthread *fpt, const pthread_attr_t *attr)
98f14af8 138{
d62a17ae 139 int ret;
98f14af8 140
a45dc974 141 ret = pthread_create(&fpt->thread, attr, fpt->attr.start, fpt);
98f14af8 142
a45dc974
QY
143 /*
144 * Per pthread_create(3), the contents of fpt->thread are undefined if
145 * pthread_create() did not succeed. Reset this value to zero.
146 */
d62a17ae 147 if (ret < 0)
148 memset(&fpt->thread, 0x00, sizeof(fpt->thread));
98f14af8 149
d62a17ae 150 return ret;
98f14af8
QY
151}
152
a45dc974 153void frr_pthread_wait_running(struct frr_pthread *fpt)
98f14af8 154{
a45dc974
QY
155 pthread_mutex_lock(fpt->running_cond_mtx);
156 {
157 while (!fpt->running)
158 pthread_cond_wait(fpt->running_cond,
159 fpt->running_cond_mtx);
160 }
161 pthread_mutex_unlock(fpt->running_cond_mtx);
98f14af8
QY
162}
163
a45dc974 164void frr_pthread_notify_running(struct frr_pthread *fpt)
98f14af8 165{
a45dc974
QY
166 pthread_mutex_lock(fpt->running_cond_mtx);
167 {
168 fpt->running = true;
169 pthread_cond_signal(fpt->running_cond);
170 }
171 pthread_mutex_unlock(fpt->running_cond_mtx);
98f14af8
QY
172}
173
a45dc974
QY
174int frr_pthread_stop(struct frr_pthread *fpt, void **result)
175{
176 int ret = (*fpt->attr.stop)(fpt, result);
177 memset(&fpt->thread, 0x00, sizeof(fpt->thread));
178 return ret;
179}
180
98f14af8
QY
181void frr_pthread_stop_all()
182{
1ac267a2 183 pthread_mutex_lock(&frr_pthread_list_mtx);
d62a17ae 184 {
1ac267a2
DL
185 struct listnode *n;
186 struct frr_pthread *fpt;
187 for (ALL_LIST_ELEMENTS_RO(frr_pthread_list, n, fpt))
188 frr_pthread_stop(fpt, NULL);
d62a17ae 189 }
1ac267a2 190 pthread_mutex_unlock(&frr_pthread_list_mtx);
98f14af8 191}
b2140cb7 192
a45dc974
QY
193/*
194 * ----------------------------------------------------------------------------
195 * Default Event Loop
196 * ----------------------------------------------------------------------------
197 */
198
199/* dummy task for sleeper pipe */
200static int fpt_dummy(struct thread *thread)
201{
202 return 0;
203}
204
205/* poison pill task to end event loop */
206static int fpt_finish(struct thread *thread)
207{
208 struct frr_pthread *fpt = THREAD_ARG(thread);
985e36a6 209
a45dc974
QY
210 atomic_store_explicit(&fpt->running, false, memory_order_relaxed);
211 return 0;
212}
213
214/* stop function, called from other threads to halt this one */
215static int fpt_halt(struct frr_pthread *fpt, void **res)
216{
217 thread_add_event(fpt->master, &fpt_finish, fpt, 0, NULL);
218 pthread_join(fpt->thread, res);
a45dc974
QY
219
220 return 0;
221}
222
a6275055
QY
223/*
224 * Entry pthread function & main event loop.
225 *
226 * Upon thread start the following actions occur:
227 *
228 * - frr_pthread's owner field is set to pthread ID.
229 * - All signals are blocked (except for unblockable signals).
230 * - Pthread's threadmaster is set to never handle pending signals
231 * - Poker pipe for poll() is created and queued as I/O source
232 * - The frr_pthread->running_cond condition variable is signalled to indicate
233 * that the previous actions have completed. It is not safe to assume any of
234 * the above have occurred before receiving this signal.
235 *
236 * After initialization is completed, the event loop begins running. Each tick,
237 * the following actions are performed before running the usual event system
238 * tick function:
239 *
240 * - Verify that the running boolean is set
241 * - Verify that there are no pending cancellation requests
242 * - Verify that there are tasks scheduled
243 *
244 * So long as the conditions are met, the event loop tick is run and the
245 * returned task is executed.
246 *
247 * If any of these conditions are not met, the event loop exits, closes the
248 * pipes and dies without running any cleanup functions.
249 */
a45dc974
QY
250static void *fpt_run(void *arg)
251{
252 struct frr_pthread *fpt = arg;
253 fpt->master->owner = pthread_self();
254
255 int sleeper[2];
256 pipe(sleeper);
257 thread_add_read(fpt->master, &fpt_dummy, NULL, sleeper[0], NULL);
258
259 fpt->master->handle_signals = false;
260
c80bedb8 261 frr_pthread_set_name(fpt);
57019528 262
a45dc974
QY
263 frr_pthread_notify_running(fpt);
264
265 struct thread task;
266 while (atomic_load_explicit(&fpt->running, memory_order_relaxed)) {
a6275055 267 pthread_testcancel();
a45dc974
QY
268 if (thread_fetch(fpt->master, &task)) {
269 thread_call(&task);
270 }
271 }
272
273 close(sleeper[1]);
274 close(sleeper[0]);
275
276 return NULL;
277}