2 * Utilities and interfaces for managing POSIX threads within FRR.
3 * Copyright (C) 2017 Cumulus Networks
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.
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.
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
24 #include "frr_pthread.h"
28 DEFINE_MTYPE(LIB
, FRR_PTHREAD
, "FRR POSIX Thread");
29 DEFINE_MTYPE(LIB
, PTHREAD_PRIM
, "POSIX synchronization primitives");
31 /* id for next created pthread */
32 static unsigned int next_id
= 0;
34 /* default frr_pthread start/stop routine prototypes */
35 static void *fpt_run(void *arg
);
36 static int fpt_halt(struct frr_pthread
*fpt
, void **res
);
38 /* default frr_pthread attributes */
39 struct frr_pthread_attr frr_pthread_attr_default
= {
46 /* hash table to keep track of all frr_pthreads */
47 static struct hash
*frr_pthread_hash
;
48 static pthread_mutex_t frr_pthread_hash_mtx
= PTHREAD_MUTEX_INITIALIZER
;
50 /* frr_pthread_hash->hash_cmp */
51 static int frr_pthread_hash_cmp(const void *value1
, const void *value2
)
53 const struct frr_pthread
*tq1
= value1
;
54 const struct frr_pthread
*tq2
= value2
;
56 return (tq1
->attr
.id
== tq2
->attr
.id
);
59 /* frr_pthread_hash->hash_key */
60 static unsigned int frr_pthread_hash_key(void *value
)
62 return ((struct frr_pthread
*)value
)->attr
.id
;
65 /* ------------------------------------------------------------------------ */
67 void frr_pthread_init()
69 pthread_mutex_lock(&frr_pthread_hash_mtx
);
71 frr_pthread_hash
= hash_create(frr_pthread_hash_key
,
72 frr_pthread_hash_cmp
, NULL
);
74 pthread_mutex_unlock(&frr_pthread_hash_mtx
);
77 void frr_pthread_finish()
79 pthread_mutex_lock(&frr_pthread_hash_mtx
);
81 hash_clean(frr_pthread_hash
,
82 (void (*)(void *))frr_pthread_destroy
);
83 hash_free(frr_pthread_hash
);
85 pthread_mutex_unlock(&frr_pthread_hash_mtx
);
88 struct frr_pthread
*frr_pthread_new(struct frr_pthread_attr
*attr
)
90 static struct frr_pthread holder
= {0};
91 struct frr_pthread
*fpt
= NULL
;
93 attr
= attr
? attr
: &frr_pthread_attr_default
;
95 pthread_mutex_lock(&frr_pthread_hash_mtx
);
97 holder
.attr
.id
= attr
->id
;
99 if (!hash_lookup(frr_pthread_hash
, &holder
)) {
100 fpt
= XCALLOC(MTYPE_FRR_PTHREAD
,
101 sizeof(struct frr_pthread
));
102 /* create new thread master */
103 fpt
->master
= thread_master_create(attr
->name
);
106 if (attr
== &frr_pthread_attr_default
)
107 fpt
->attr
.id
= frr_pthread_get_id();
108 /* initialize startup synchronization primitives */
109 fpt
->running_cond_mtx
= XCALLOC(
110 MTYPE_PTHREAD_PRIM
, sizeof(pthread_mutex_t
));
111 fpt
->running_cond
= XCALLOC(MTYPE_PTHREAD_PRIM
,
112 sizeof(pthread_cond_t
));
113 pthread_mutex_init(fpt
->running_cond_mtx
, NULL
);
114 pthread_cond_init(fpt
->running_cond
, NULL
);
116 /* insert into global thread hash */
117 hash_get(frr_pthread_hash
, fpt
, hash_alloc_intern
);
120 pthread_mutex_unlock(&frr_pthread_hash_mtx
);
125 void frr_pthread_destroy(struct frr_pthread
*fpt
)
127 thread_master_free(fpt
->master
);
129 pthread_mutex_destroy(fpt
->running_cond_mtx
);
130 pthread_cond_destroy(fpt
->running_cond
);
131 XFREE(MTYPE_PTHREAD_PRIM
, fpt
->running_cond_mtx
);
132 XFREE(MTYPE_PTHREAD_PRIM
, fpt
->running_cond
);
133 XFREE(MTYPE_FRR_PTHREAD
, fpt
);
136 struct frr_pthread
*frr_pthread_get(unsigned int id
)
138 static struct frr_pthread holder
= {0};
139 struct frr_pthread
*fpt
;
141 pthread_mutex_lock(&frr_pthread_hash_mtx
);
144 fpt
= hash_lookup(frr_pthread_hash
, &holder
);
146 pthread_mutex_unlock(&frr_pthread_hash_mtx
);
151 int frr_pthread_run(struct frr_pthread
*fpt
, const pthread_attr_t
*attr
)
155 ret
= pthread_create(&fpt
->thread
, attr
, fpt
->attr
.start
, fpt
);
158 * Per pthread_create(3), the contents of fpt->thread are undefined if
159 * pthread_create() did not succeed. Reset this value to zero.
162 memset(&fpt
->thread
, 0x00, sizeof(fpt
->thread
));
167 void frr_pthread_wait_running(struct frr_pthread
*fpt
)
169 pthread_mutex_lock(fpt
->running_cond_mtx
);
171 while (!fpt
->running
)
172 pthread_cond_wait(fpt
->running_cond
,
173 fpt
->running_cond_mtx
);
175 pthread_mutex_unlock(fpt
->running_cond_mtx
);
178 void frr_pthread_notify_running(struct frr_pthread
*fpt
)
180 pthread_mutex_lock(fpt
->running_cond_mtx
);
183 pthread_cond_signal(fpt
->running_cond
);
185 pthread_mutex_unlock(fpt
->running_cond_mtx
);
188 int frr_pthread_stop(struct frr_pthread
*fpt
, void **result
)
190 int ret
= (*fpt
->attr
.stop
)(fpt
, result
);
191 memset(&fpt
->thread
, 0x00, sizeof(fpt
->thread
));
196 * Callback for hash_iterate to stop all frr_pthread's.
198 static void frr_pthread_stop_all_iter(struct hash_backet
*hb
, void *arg
)
200 struct frr_pthread
*fpt
= hb
->data
;
201 frr_pthread_stop(fpt
, NULL
);
204 void frr_pthread_stop_all()
206 pthread_mutex_lock(&frr_pthread_hash_mtx
);
208 hash_iterate(frr_pthread_hash
, frr_pthread_stop_all_iter
, NULL
);
210 pthread_mutex_unlock(&frr_pthread_hash_mtx
);
213 unsigned int frr_pthread_get_id()
215 /* just a sanity check, this should never happen */
216 assert(next_id
<= INT_MAX
- 1);
220 void frr_pthread_yield(void)
226 * ----------------------------------------------------------------------------
228 * ----------------------------------------------------------------------------
231 /* dummy task for sleeper pipe */
232 static int fpt_dummy(struct thread
*thread
)
237 /* poison pill task to end event loop */
238 static int fpt_finish(struct thread
*thread
)
240 struct frr_pthread
*fpt
= THREAD_ARG(thread
);
241 atomic_store_explicit(&fpt
->running
, false, memory_order_relaxed
);
245 /* stop function, called from other threads to halt this one */
246 static int fpt_halt(struct frr_pthread
*fpt
, void **res
)
248 thread_add_event(fpt
->master
, &fpt_finish
, fpt
, 0, NULL
);
249 pthread_join(fpt
->thread
, res
);
255 /* entry pthread function & main event loop */
256 static void *fpt_run(void *arg
)
258 struct frr_pthread
*fpt
= arg
;
259 fpt
->master
->owner
= pthread_self();
263 thread_add_read(fpt
->master
, &fpt_dummy
, NULL
, sleeper
[0], NULL
);
265 fpt
->master
->handle_signals
= false;
267 frr_pthread_notify_running(fpt
);
270 while (atomic_load_explicit(&fpt
->running
, memory_order_relaxed
)) {
271 if (thread_fetch(fpt
->master
, &task
)) {