Debian Codegraph @

main/named_locks.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2016, Fairview 5 Engineering, LLC
5 *
6 * George Joseph <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 /*! \file
20 *
21 * \brief Named Locks
22 *
23 * \author George Joseph <[email protected]>
24 */
25
26 #include "asterisk.h"
27
28 #include "asterisk/_private.h"
29 #include "asterisk/astobj2.h"
30 #include "asterisk/named_locks.h"
31 #include "asterisk/utils.h"
32
33 struct ao2_container *named_locks;
34 #define NAMED_LOCKS_BUCKETS 101
35
36 struct named_lock_proxy {
37 AO2_WEAKPROXY();
38 char key[0];
39 };
40
41 struct ast_named_lock {
42 };
43
44 AO2_STRING_FIELD_HASH_FN(named_lock_proxy, key)
45 AO2_STRING_FIELD_CMP_FN(named_lock_proxy, key)
46
47 static void named_locks_shutdown(void)
48 {
49 ao2_cleanup(named_locks);
50 }
51
52 int ast_named_locks_init(void)
53 {
54 named_locks = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
55 NAMED_LOCKS_BUCKETS, named_lock_proxy_hash_fn, NULL, named_lock_proxy_cmp_fn);
56 if (!named_locks) {
57 return -1;
58 }
59
60 ast_register_cleanup(named_locks_shutdown);
61
62 return 0;
63 }
64
65 static void named_lock_proxy_cb(void *weakproxy, void *data)
66 {
67 ao2_unlink(named_locks, weakproxy);
68 }
69
70 struct ast_named_lock *__ast_named_lock_get(const char *filename, int lineno, const char *func,
71 enum ast_named_lock_type lock_type, const char *keyspace, const char *key)
72 {
73 struct named_lock_proxy *proxy;
74 struct ast_named_lock *lock;
75 int keylen = strlen(keyspace) + strlen(key) + 2;
76 char *concat_key = ast_alloca(keylen);
77
78 sprintf(concat_key, "%s-%s", keyspace, key); /* Safe */
79
80 ao2_lock(named_locks);
81 lock = __ao2_weakproxy_find(named_locks, concat_key, OBJ_SEARCH_KEY | OBJ_NOLOCK,
82 __PRETTY_FUNCTION__, filename, lineno, func);
83 if (lock) {
84 ast_assert((ao2_options_get(lock) & AO2_ALLOC_OPT_LOCK_MASK) == lock_type);
85 ao2_unlock(named_locks);
86
87 return lock;
88 }
89
90 proxy = ao2_t_weakproxy_alloc(sizeof(*proxy) + keylen, NULL, concat_key);
91 if (!proxy) {
92 goto failure_cleanup;
93 }
94
95 lock = __ao2_alloc(sizeof(*lock) + keylen, NULL, lock_type, concat_key, filename, lineno, func);
96 if (!lock) {
97 goto failure_cleanup;
98 }
99
100 /* We have exclusive access to proxy and lock, no need for locking here. */
101 if (ao2_weakproxy_set_object(proxy, lock, OBJ_NOLOCK)) {
102 goto failure_cleanup;
103 }
104
105 if (ao2_weakproxy_subscribe(proxy, named_lock_proxy_cb, NULL, OBJ_NOLOCK)) {
106 goto failure_cleanup;
107 }
108
109 strcpy(proxy->key, concat_key); /* Safe */
110 ao2_link_flags(named_locks, proxy, OBJ_NOLOCK);
111 ao2_unlock(named_locks);
112 ao2_t_ref(proxy, -1, "Release allocation reference");
113
114 return lock;
115
116 failure_cleanup:
117 ao2_unlock(named_locks);
118
119 ao2_cleanup(proxy);
120 ao2_cleanup(lock);
121
122 return NULL;
123 }