]> git.proxmox.com Git - mirror_frr.git/blob - lib/qobj.c
Merge pull request #5628 from donaldsharp/rtm_getneigh
[mirror_frr.git] / lib / qobj.c
1 /*
2 * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc.
3 *
4 * This file is part of Quagga
5 *
6 * Quagga is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * Quagga is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "thread.h"
24 #include "memory.h"
25 #include "hash.h"
26 #include "log.h"
27 #include "qobj.h"
28 #include "jhash.h"
29
30 static uint32_t qobj_hash(const struct qobj_node *node)
31 {
32 return (uint32_t)node->nid;
33 }
34
35 static int qobj_cmp(const struct qobj_node *na, const struct qobj_node *nb)
36 {
37 if (na->nid < nb->nid)
38 return -1;
39 if (na->nid > nb->nid)
40 return 1;
41 return 0;
42 }
43
44 DECLARE_HASH(qobj_nodes, struct qobj_node, nodehash,
45 qobj_cmp, qobj_hash)
46
47 static pthread_rwlock_t nodes_lock;
48 static struct qobj_nodes_head nodes = { };
49
50
51 void qobj_reg(struct qobj_node *node, const struct qobj_nodetype *type)
52 {
53 node->type = type;
54 pthread_rwlock_wrlock(&nodes_lock);
55 do {
56 node->nid = (uint64_t)random();
57 node->nid ^= (uint64_t)random() << 32;
58 } while (!node->nid || qobj_nodes_find(&nodes, node));
59 qobj_nodes_add(&nodes, node);
60 pthread_rwlock_unlock(&nodes_lock);
61 }
62
63 void qobj_unreg(struct qobj_node *node)
64 {
65 pthread_rwlock_wrlock(&nodes_lock);
66 qobj_nodes_del(&nodes, node);
67 pthread_rwlock_unlock(&nodes_lock);
68 }
69
70 struct qobj_node *qobj_get(uint64_t id)
71 {
72 struct qobj_node dummy = {.nid = id}, *rv;
73 pthread_rwlock_rdlock(&nodes_lock);
74 rv = qobj_nodes_find(&nodes, &dummy);
75 pthread_rwlock_unlock(&nodes_lock);
76 return rv;
77 }
78
79 void *qobj_get_typed(uint64_t id, const struct qobj_nodetype *type)
80 {
81 struct qobj_node dummy = {.nid = id};
82 struct qobj_node *node;
83 void *rv;
84
85 pthread_rwlock_rdlock(&nodes_lock);
86 node = qobj_nodes_find(&nodes, &dummy);
87
88 /* note: we explicitly hold the lock until after we have checked the
89 * type.
90 * if the caller holds a lock that for example prevents the deletion of
91 * route-maps, we can still race against a delete of something that
92 * isn't
93 * a route-map. */
94 if (!node || node->type != type)
95 rv = NULL;
96 else
97 rv = (char *)node - node->type->node_member_offset;
98
99 pthread_rwlock_unlock(&nodes_lock);
100 return rv;
101 }
102
103 void qobj_init(void)
104 {
105 pthread_rwlock_init(&nodes_lock, NULL);
106 qobj_nodes_init(&nodes);
107 }
108
109 void qobj_finish(void)
110 {
111 struct qobj_node *node;
112 while ((node = qobj_nodes_pop(&nodes)))
113 qobj_nodes_del(&nodes, node);
114 pthread_rwlock_destroy(&nodes_lock);
115 }