2 * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc.
4 * This file is part of Quagga
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
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.
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
30 static pthread_rwlock_t nodes_lock
;
31 static struct hash
*nodes
= NULL
;
33 static unsigned int qobj_key(void *data
)
35 struct qobj_node
*node
= data
;
36 return (unsigned int)node
->nid
;
39 static bool qobj_cmp(const void *a
, const void *b
)
41 const struct qobj_node
*na
= a
, *nb
= b
;
42 return na
->nid
== nb
->nid
;
45 void qobj_reg(struct qobj_node
*node
, struct qobj_nodetype
*type
)
48 pthread_rwlock_wrlock(&nodes_lock
);
50 node
->nid
= (uint64_t)random();
51 node
->nid
^= (uint64_t)random() << 32;
53 || hash_get(nodes
, node
, hash_alloc_intern
) != node
);
54 pthread_rwlock_unlock(&nodes_lock
);
57 void qobj_unreg(struct qobj_node
*node
)
59 pthread_rwlock_wrlock(&nodes_lock
);
60 hash_release(nodes
, node
);
61 pthread_rwlock_unlock(&nodes_lock
);
64 struct qobj_node
*qobj_get(uint64_t id
)
66 struct qobj_node dummy
= {.nid
= id
}, *rv
;
67 pthread_rwlock_rdlock(&nodes_lock
);
68 rv
= hash_lookup(nodes
, &dummy
);
69 pthread_rwlock_unlock(&nodes_lock
);
73 void *qobj_get_typed(uint64_t id
, struct qobj_nodetype
*type
)
75 struct qobj_node dummy
= {.nid
= id
};
76 struct qobj_node
*node
;
79 pthread_rwlock_rdlock(&nodes_lock
);
80 node
= hash_lookup(nodes
, &dummy
);
82 /* note: we explicitly hold the lock until after we have checked the
84 * if the caller holds a lock that for example prevents the deletion of
85 * route-maps, we can still race against a delete of something that
88 if (!node
|| node
->type
!= type
)
91 rv
= (char *)node
- node
->type
->node_member_offset
;
93 pthread_rwlock_unlock(&nodes_lock
);
100 pthread_rwlock_init(&nodes_lock
, NULL
);
101 nodes
= hash_create_size(16, qobj_key
, qobj_cmp
, "QOBJ Hash");
105 void qobj_finish(void)
107 hash_clean(nodes
, NULL
);
110 pthread_rwlock_destroy(&nodes_lock
);