The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
thread.h
Go to the documentation of this file.
1#pragma once
2/*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16 */
17
18/**
19 * $Id: 58e4b0f3796b48c488841a74922653cce1cb4cf2 $
20 *
21 * @file io/thread.h
22 * @brief Common thread instantiation and detach for worker and coordinator threads
23 *
24 * @copyright 2026 Network RADIUS SAS (legal@networkradius.com)
25 */
26RCSIDH(thread_h, "$Id: 58e4b0f3796b48c488841a74922653cce1cb4cf2 $")
27
28#include <freeradius-devel/util/event.h>
29#include <freeradius-devel/util/semaphore.h>
30#include <freeradius-devel/util/talloc.h>
31#include <freeradius-devel/util/dlist.h>
32
33#include <pthread.h>
34
35/**
36 * Track the child thread status.
37 */
38typedef enum fr_thread_status_t {
39 FR_THREAD_FREE = 0, //!< child is free
40 FR_THREAD_INITIALIZING, //!< initialized, but not running
41 FR_THREAD_RUNNING, //!< running, and in the running queue
42 FR_THREAD_EXITED, //!< exited, and in the exited queue
43 FR_THREAD_FAIL //!< failed, and in the exited queue
45
46typedef struct {
47 char const *name; //!< of this thread
48 int id; //!< unique ID for this thread
49 fr_thread_status_t status; //!< running, etc.
50 pthread_t pthread_id; //!< of this thread
51
52 TALLOC_CTX *ctx; //!< our allocation ctx
53 fr_event_list_t *el; //!< our event list
54
55 /*
56 * This field is owned and managed by the parent coordinator thread.
57 */
58 fr_dlist_t entry; //!< entry into the parent linked list of threads
60
61typedef void *(*fr_thread_entry_t)(void *);
62
63int fr_thread_create(pthread_t *thread, fr_thread_entry_t func, void *arg) CC_HINT(nonnull(1,2));
64
66
67int fr_thread_setup(fr_thread_t *out, char const *name) CC_HINT(nonnull);
68
69int fr_thread_instantiate(TALLOC_CTX *ctx, fr_event_list_t *el) CC_HINT(nonnull);
70
71void fr_thread_detach(void);
72
73void fr_thread_start(fr_thread_t *thread, fr_sem_t *sem) CC_HINT(nonnull);
74
75void fr_thread_exit(fr_thread_t *thread, fr_thread_status_t status, fr_sem_t *sem) CC_HINT(nonnull);
#define RCSIDH(h, id)
Definition build.h:489
Head of a doubly linked list.
Definition dlist.h:51
Entry in a doubly linked list.
Definition dlist.h:41
Stores all information relating to an event list.
Definition event.c:377
static char const * name
sem_t fr_sem_t
Definition semaphore.h:53
fr_thread_status_t status
running, etc.
Definition thread.h:49
char const * name
of this thread
Definition thread.h:47
void fr_thread_start(fr_thread_t *thread, fr_sem_t *sem)
Signal the parent that we're done.
Definition thread.c:203
int fr_thread_create(pthread_t *thread, fr_thread_entry_t func, void *arg))
Create a joinable thread.
Definition thread.c:46
fr_event_list_t * el
our event list
Definition thread.h:53
int id
unique ID for this thread
Definition thread.h:48
int fr_thread_wait_list(fr_sem_t *sem, fr_dlist_head_t *head)
Wait for multiple threads to signal readiness via a semaphore.
Definition thread.c:79
fr_thread_status_t
Track the child thread status.
Definition thread.h:38
@ FR_THREAD_RUNNING
running, and in the running queue
Definition thread.h:41
@ FR_THREAD_INITIALIZING
initialized, but not running
Definition thread.h:40
@ FR_THREAD_EXITED
exited, and in the exited queue
Definition thread.h:42
@ FR_THREAD_FREE
child is free
Definition thread.h:39
@ FR_THREAD_FAIL
failed, and in the exited queue
Definition thread.h:43
int fr_thread_setup(fr_thread_t *out, char const *name)
Common setup for child threads: block signals, allocate a talloc context, and create an event list.
Definition thread.c:112
fr_dlist_t entry
entry into the parent linked list of threads
Definition thread.h:58
int fr_thread_instantiate(TALLOC_CTX *ctx, fr_event_list_t *el)
Instantiate thread-specific data for modules, virtual servers, xlats, unlang, and TLS.
Definition thread.c:165
void fr_thread_detach(void)
Detach thread-specific data for modules, virtual servers, xlats.
Definition thread.c:190
TALLOC_CTX * ctx
our allocation ctx
Definition thread.h:52
pthread_t pthread_id
of this thread
Definition thread.h:50
void fr_thread_exit(fr_thread_t *thread, fr_thread_status_t status, fr_sem_t *sem)
Signal the parent that we're done.
Definition thread.c:218
void *(* fr_thread_entry_t)(void *)
Definition thread.h:61
static fr_event_list_t * el
static fr_slen_t head
Definition xlat.h:420
int nonnull(2, 5))
static size_t char ** out
Definition value.h:1030