]> git.proxmox.com Git - mirror_frr.git/blob - lib/frr_pthread.h
zebra, lib: fix the ZEBRA_INTERFACE_VRF_UPDATE zapi message
[mirror_frr.git] / lib / frr_pthread.h
1 /*
2 * Utilities and interfaces for managing POSIX threads within FRR.
3 * Copyright (C) 2017 Cumulus Networks, Inc.
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
18 */
19
20 #ifndef _FRR_PTHREAD_H
21 #define _FRR_PTHREAD_H
22
23 #include <pthread.h>
24 #include "frratomic.h"
25 #include "memory.h"
26 #include "thread.h"
27
28 DECLARE_MTYPE(FRR_PTHREAD);
29 DECLARE_MTYPE(PTHREAD_PRIM);
30
31 #define OS_THREAD_NAMELEN 16
32
33 struct frr_pthread;
34 struct frr_pthread_attr;
35
36 struct frr_pthread_attr {
37 void *(*start)(void *);
38 int (*stop)(struct frr_pthread *, void **);
39 };
40
41 struct frr_pthread {
42
43 /*
44 * Mutex protecting this structure. Must be taken for reading some
45 * fields, denoted by a 'Requires: mtx'.
46 */
47 pthread_mutex_t mtx;
48
49 /* pthread id */
50 pthread_t thread;
51
52 /* thread master for this pthread's thread.c event loop */
53 struct thread_master *master;
54
55 /* caller-specified data; start & stop funcs, name, id */
56 struct frr_pthread_attr attr;
57
58 /*
59 * Notification mechanism for allowing pthreads to notify their parents
60 * when they are ready to do work. This mechanism has two associated
61 * functions:
62 *
63 * - frr_pthread_wait_running()
64 * This function should be called by the spawning thread after
65 * frr_pthread_run(). It safely waits until the spawned thread
66 * indicates that is ready to do work by posting to the condition
67 * variable.
68 *
69 * - frr_pthread_notify_running()
70 * This function should be called by the spawned thread when it is
71 * ready to do work. It will wake up any threads waiting on the
72 * previously described condition.
73 */
74 pthread_cond_t *running_cond;
75 pthread_mutex_t *running_cond_mtx;
76 _Atomic bool running;
77
78 /*
79 * Fake thread-specific storage. No constraints on usage. Helpful when
80 * creating reentrant pthread implementations. Can be used to pass
81 * argument to pthread entry function.
82 *
83 * Requires: mtx
84 */
85 void *data;
86
87 /*
88 * Human-readable thread name.
89 *
90 * Requires: mtx
91 */
92 char *name;
93
94 /* Used in pthread_set_name max 16 characters */
95 char os_name[OS_THREAD_NAMELEN];
96 };
97
98 extern struct frr_pthread_attr frr_pthread_attr_default;
99
100 /*
101 * Initializes this module.
102 *
103 * Must be called before using any of the other functions.
104 */
105 void frr_pthread_init(void);
106
107 /*
108 * Uninitializes this module.
109 *
110 * Destroys all registered frr_pthread's and internal data structures.
111 *
112 * It is safe to call frr_pthread_init() after this function to reinitialize
113 * the module.
114 */
115 void frr_pthread_finish(void);
116
117 /*
118 * Creates a new frr_pthread with the given attributes.
119 *
120 * The 'attr' argument should be filled out with the desired attributes,
121 * including ID, start and stop functions and the desired name. Alternatively,
122 * if attr is NULL, the default attributes will be used. The pthread will be
123 * set up to run a basic threadmaster loop and the name will be "Anonymous".
124 * Scheduling tasks onto the threadmaster in the 'master' field of the returned
125 * frr_pthread will cause them to run on that pthread.
126 *
127 * @param attr - the thread attributes
128 * @param name - Human-readable name
129 * @param os_name - 16 characters (including '\0') thread name to set in os,
130 * @return the created frr_pthread upon success, or NULL upon failure
131 */
132 struct frr_pthread *frr_pthread_new(struct frr_pthread_attr *attr,
133 const char *name, const char *os_name);
134
135 /*
136 * Changes the name of the frr_pthread.
137 *
138 * @param fpt - the frr_pthread to operate on
139 * @param name - Human-readable name
140 * @param os_name - 16 characters thread name , including the null
141 * terminator ('\0') to set in os.
142 * @return - on success returns 0 otherwise nonzero error number.
143 */
144 int frr_pthread_set_name(struct frr_pthread *fpt, const char *name,
145 const char *os_name);
146
147 /*
148 * Destroys an frr_pthread.
149 *
150 * Assumes that the associated pthread, if any, has already terminated.
151 *
152 * @param fpt - the frr_pthread to destroy
153 */
154 void frr_pthread_destroy(struct frr_pthread *fpt);
155
156 /*
157 * Creates a new pthread and binds it to a frr_pthread.
158 *
159 * This function is a wrapper for pthread_create. The first parameter is the
160 * frr_pthread to bind the created pthread to. All subsequent arguments are
161 * passed unmodified to pthread_create(). The frr_pthread * provided will be
162 * used as the argument to the pthread entry function. If it is necessary to
163 * pass additional data, the 'data' field in the frr_pthread may be used.
164 *
165 * This function returns the same code as pthread_create(). If the value is
166 * zero, the provided frr_pthread is bound to a running POSIX thread. If the
167 * value is less than zero, the provided frr_pthread is guaranteed to be a
168 * clean instance that may be susbsequently passed to frr_pthread_run().
169 *
170 * @param fpt - frr_pthread * to run
171 * @param attr - see pthread_create(3)
172 *
173 * @return see pthread_create(3)
174 */
175 int frr_pthread_run(struct frr_pthread *fpt, const pthread_attr_t *attr);
176
177 /*
178 * Waits until the specified pthread has finished setting up and is ready to
179 * begin work.
180 *
181 * If the pthread's code makes use of the startup synchronization mechanism,
182 * this function should be called before attempting to use the functionality
183 * exposed by the pthread. It waits until the 'running' condition is satisfied
184 * (see struct definition of frr_pthread).
185 *
186 * @param fpt - the frr_pthread * to wait on
187 */
188 void frr_pthread_wait_running(struct frr_pthread *fpt);
189
190 /*
191 * Notifies other pthreads that the calling thread has finished setting up and
192 * is ready to begin work.
193 *
194 * This will allow any other pthreads waiting in 'frr_pthread_wait_running' to
195 * proceed.
196 *
197 * @param fpt - the frr_pthread * that has finished setting up
198 */
199 void frr_pthread_notify_running(struct frr_pthread *fpt);
200
201 /*
202 * Stops a frr_pthread with a result.
203 *
204 * @param fpt - frr_pthread * to stop
205 * @param result - where to store the thread's result, if any. May be NULL if a
206 * result is not needed.
207 */
208 int frr_pthread_stop(struct frr_pthread *fpt, void **result);
209
210 /* Stops all frr_pthread's. */
211 void frr_pthread_stop_all(void);
212
213 #ifndef HAVE_PTHREAD_CONDATTR_SETCLOCK
214 #define pthread_condattr_setclock(A, B)
215 #endif
216
217 #endif /* _FRR_PTHREAD_H */