]> git.proxmox.com Git - mirror_frr.git/blame - lib/frr_pthread.h
*: reindent
[mirror_frr.git] / lib / frr_pthread.h
CommitLineData
98f14af8 1/*
896014f4
DL
2 * Utilities and interfaces for managing POSIX threads
3 * Copyright (C) 2017 Cumulus Networks
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#ifndef _FRR_PTHREAD_H
21#define _FRR_PTHREAD_H
22
23#include <pthread.h>
24#include "thread.h"
25
26struct frr_pthread {
27
d62a17ae 28 /* pthread id */
29 pthread_t thread;
98f14af8 30
d62a17ae 31 /* frr thread identifier */
32 unsigned int id;
98f14af8 33
d62a17ae 34 /* thread master for this pthread's thread.c event loop */
35 struct thread_master *master;
98f14af8 36
d62a17ae 37 /* start routine */
38 void *(*start_routine)(void *);
98f14af8 39
d62a17ae 40 /* stop routine */
41 int (*stop_routine)(void **, struct frr_pthread *);
98f14af8 42
d62a17ae 43 /* the (hopefully descriptive) name of this thread */
44 char *name;
98f14af8
QY
45};
46
47/* Initializes this module.
48 *
49 * Must be called before using any of the other functions.
50 */
51void frr_pthread_init(void);
52
53/* Uninitializes this module.
54 *
55 * Destroys all registered frr_pthread's and internal data structures.
56 *
57 * It is safe to call frr_pthread_init() after this function to reinitialize
58 * the module.
59 */
60void frr_pthread_finish(void);
61
62/* Creates a new frr_pthread.
63 *
64 * If the provided ID is already assigned to an existing frr_pthread, the
65 * return value will be NULL.
66 *
67 * @param name - the name of the thread. Doesn't have to be unique, but it
68 * probably should be. This value is copied and may be safely free'd upon
69 * return.
70 *
71 * @param id - the integral ID of the thread. MUST be unique. The caller may
72 * use this id to retrieve the thread.
73 *
74 * @param start_routine - start routine for the pthread, will be passed to
75 * pthread_create (see those docs for details)
76 *
77 * @param stop_routine - stop routine for the pthread, called to terminate the
78 * thread. This function should gracefully stop the pthread and clean up any
79 * thread-specific resources. The passed pointer is used to return a data
80 * result.
81 *
82 * @return the created frr_pthread upon success, or NULL upon failure
83 */
84struct frr_pthread *frr_pthread_new(const char *name, unsigned int id,
d62a17ae 85 void *(*start_routine)(void *),
86 int (*stop_routine)(void **,
87 struct frr_pthread *));
98f14af8
QY
88
89/* Destroys an frr_pthread.
90 *
91 * Assumes that the associated pthread, if any, has already terminated.
92 *
93 * @param fpt - the frr_pthread to destroy
94 */
95void frr_pthread_destroy(struct frr_pthread *fpt);
96
97/* Gets an existing frr_pthread by its id.
98 *
99 * @return frr_thread associated with the provided id, or NULL on error
100 */
101struct frr_pthread *frr_pthread_get(unsigned int id);
102
103/* Creates a new pthread and binds it to a frr_pthread.
104 *
105 * This function is a wrapper for pthread_create. The first parameter is the
106 * frr_pthread to bind the created pthread to. All subsequent arguments are
107 * passed unmodified to pthread_create().
108 *
109 * This function returns the same code as pthread_create(). If the value is
110 * zero, the provided frr_pthread is bound to a running POSIX thread. If the
111 * value is less than zero, the provided frr_pthread is guaranteed to be a
112 * clean instance that may be susbsequently passed to frr_pthread_run().
113 *
114 * @param id - frr_pthread to bind the created pthread to
115 * @param attr - see pthread_create(3)
116 * @param arg - see pthread_create(3)
117 *
118 * @return see pthread_create(3)
119 */
d62a17ae 120int frr_pthread_run(unsigned int id, const pthread_attr_t *attr, void *arg);
98f14af8
QY
121
122/* Stops an frr_pthread with a result.
123 *
124 * @param id - frr_pthread to stop
125 * @param result - where to store the thread's result, if any. May be NULL if a
126 * result is not needed.
127 */
128int frr_pthread_stop(unsigned int id, void **result);
129
130/* Stops all frr_pthread's. */
131void frr_pthread_stop_all(void);
132
133/* Returns a unique identifier for use with frr_pthread_new().
134 *
135 * Internally, this is an integer that increments after each call to this
136 * function. Because the number of pthreads created should never exceed INT_MAX
137 * during the life of the program, there is no overflow protection. If by
138 * chance this function returns an ID which is already in use,
139 * frr_pthread_new() will fail when it is provided.
140 *
141 * @return unique identifier
142 */
143unsigned int frr_pthread_get_id(void);
144
145#endif /* _FRR_PTHREAD_H */