summaryrefslogtreecommitdiffstats
path: root/lib/frr_pthread.c
blob: 17bc82f5da94f9bcc344a085a91a15706da6dc5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Utilities and interfaces for managing POSIX threads
 * Copyright (C) 2017  Cumulus Networks
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; see the file COPYING; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <zebra.h>
#include <pthread.h>

#include "frr_pthread.h"
#include "memory.h"
#include "hash.h"

DEFINE_MTYPE_STATIC(LIB, FRR_PTHREAD, "FRR POSIX Thread");

static unsigned int next_id = 0;

/* Hash table of all frr_pthreads along with synchronization primitive(s) and
 * hash table callbacks.
 * ------------------------------------------------------------------------ */
static struct hash *pthread_table;
static pthread_mutex_t pthread_table_mtx = PTHREAD_MUTEX_INITIALIZER;

/* pthread_table->hash_cmp */
static int pthread_table_hash_cmp(const void *value1, const void *value2)
{
        const struct frr_pthread *tq1 = value1;
        const struct frr_pthread *tq2 = value2;

        return (tq1->id == tq2->id);
}

/* pthread_table->hash_key */
static unsigned int pthread_table_hash_key(void *value)
{
        return ((struct frr_pthread *)value)->id;
}
/* ------------------------------------------------------------------------ */

void frr_pthread_init()
{
        pthread_mutex_lock(&pthread_table_mtx);
        {
                pthread_table =
                    hash_create(pthread_table_hash_key, pthread_table_hash_cmp);
        }
        pthread_mutex_unlock(&pthread_table_mtx);
}

void frr_pthread_finish()
{
        pthread_mutex_lock(&pthread_table_mtx);
        {
                hash_clean(pthread_table, (void (*)(void *))frr_pthread_destroy);
                hash_free(pthread_table);
        }
        pthread_mutex_unlock(&pthread_table_mtx);
}

struct frr_pthread *frr_pthread_new(const char *name, unsigned int id,
                                    void *(*start_routine) (void *),
                                    int (*stop_routine) (void **, struct frr_pthread *))
{
        static struct frr_pthread holder = { 0 };
        struct frr_pthread *fpt = NULL;

        pthread_mutex_lock(&pthread_table_mtx);
        {
                holder.id = id;

                if (!hash_lookup(pthread_table, &holder)) {
                        struct frr_pthread *fpt =
                            XCALLOC(MTYPE_FRR_PTHREAD,
                                    sizeof(struct frr_pthread));
                        fpt->id = id;
                        fpt->master = thread_master_create();
                        fpt->start_routine = start_routine;
                        fpt->stop_routine = stop_routine;
                        fpt->name = XSTRDUP(MTYPE_FRR_PTHREAD, name);

                        hash_get(pthread_table, fpt, hash_alloc_intern);
                }
        }
        pthread_mutex_unlock(&pthread_table_mtx);

        return fpt;
}

void frr_pthread_destroy(struct frr_pthread *fpt)
{
        thread_master_free(fpt->master);
        XFREE(MTYPE_FRR_PTHREAD, fpt->name);
        XFREE(MTYPE_FRR_PTHREAD, fpt);
}

struct frr_pthread *frr_pthread_get(unsigned int id)
{
        static struct frr_pthread holder = { 0 };
        struct frr_pthread *fpt;

        pthread_mutex_lock(&pthread_table_mtx);
        {
                holder.id = id;
                fpt = hash_lookup(pthread_table, &holder);
        }
        pthread_mutex_unlock(&pthread_table_mtx);

        return fpt;
}

int frr_pthread_run(unsigned int id, const pthread_attr_t * attr, void *arg)
{
        struct frr_pthread *fpt = frr_pthread_get(id);
        int ret;

        if (!fpt)
                return -1;

        ret = pthread_create(&fpt->thread, attr, fpt->start_routine, arg);

        /* Per pthread_create(3), the contents of fpt->thread are undefined if
         * pthread_create() did not succeed. Reset this value to zero. */
        if (ret < 0)
                memset(&fpt->thread, 0x00, sizeof(fpt->thread));

        return ret;
}

/**
 * Calls the stop routine for the frr_pthread and resets any relevant fields.
 *
 * @param fpt - the frr_pthread to stop
 * @param result - pointer to result pointer
 * @return the return code from the stop routine
 */
static int frr_pthread_stop_actual(struct frr_pthread *fpt, void **result)
{
        int ret = (*fpt->stop_routine) (result, fpt);
        memset(&fpt->thread, 0x00, sizeof(fpt->thread));
        return ret;
}

int frr_pthread_stop(unsigned int id, void **result)
{
        struct frr_pthread *fpt = frr_pthread_get(id);
        return frr_pthread_stop_actual(fpt, result);
}

/**
 * Callback for hash_iterate to stop all frr_pthread's.
 */
static void frr_pthread_stop_all_iter(struct hash_backet *hb, void *arg)
{
        struct frr_pthread *fpt = hb->data;
        frr_pthread_stop_actual(fpt, NULL);
}

void frr_pthread_stop_all()
{
        pthread_mutex_lock(&pthread_table_mtx);
        {
                hash_iterate(pthread_table, frr_pthread_stop_all_iter, NULL);
        }
        pthread_mutex_unlock(&pthread_table_mtx);
}

unsigned int frr_pthread_get_id()
{
        return next_id++;
}