Asterisk - The Open Source Telephony Project GIT-master-f36a736
named_locks.h
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2016, Fairview 5 Engineering, LLC
5 *
6 * George Joseph <george.joseph@fairview5.com>
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 <george.joseph@fairview5.com>
24 */
25
26#ifndef INCLUDE_ASTERISK_NAMED_LOCKS_H_
27#define INCLUDE_ASTERISK_NAMED_LOCKS_H_
28
29#include "asterisk/astobj2.h"
30
31/*!
32 * \defgroup named_locks Named mutex and read-write locks
33 * @{
34 * \page NamedLocks Named mutex and read-write locks
35 * \since 13.9.0
36 *
37 * Locking some objects like sorcery objects can be tricky because the underlying
38 * ao2 object may not be the same for all callers. For instance, two threads that
39 * call ast_sorcery_retrieve_by_id on the same aor name might actually get 2 different
40 * ao2 objects if the underlying wizard had to rehydrate the aor from a database.
41 * Locking one ao2 object doesn't have any effect on the other even if those objects
42 * had locks in the first place
43 *
44 * Named locks allow access control by name. Now an aor named "1000" can be locked and
45 * any other thread attempting to lock the aor named "1000" will wait regardless of whether
46 * the underlying ao2 object is the same or not.
47 *
48 * To use a named lock:
49 * Call ast_named_lock_get with the appropriate keyspace and key.
50 * Use the standard ao2 lock/unlock functions as needed.
51 * Call ao2_cleanup when you're finished with it.
52 */
53
54/*!
55 * \brief Which type of lock to request.
56 */
58 /*! Request a named mutex. */
60 /*! Request a named read/write lock. */
62};
63
64struct ast_named_lock;
65
66struct ast_named_lock *__ast_named_lock_get(const char *filename, int lineno, const char *func,
67 enum ast_named_lock_type lock_type, const char *keyspace, const char *key);
68
69/*!
70 * \brief Geta named lock handle
71 * \since 13.9.0
72 *
73 * \param lock_type One of ast_named_lock_type
74 * \param keyspace
75 * \param key
76 * \retval A pointer to an ast_named_lock structure
77 * \retval NULL on error
78 *
79 * \note
80 * keyspace and key can be anything. For sorcery objects, keyspace could be the object type
81 * and key could be the object id.
82 */
83#define ast_named_lock_get(lock_type, keyspace, key) \
84 __ast_named_lock_get(__FILE__, __LINE__, __PRETTY_FUNCTION__, lock_type, \
85 keyspace, key)
86
87/*!
88 * \brief Put a named lock handle away
89 * \since 13.9.0
90 *
91 * \param lock The pointer to the ast_named_lock structure returned by ast_named_lock_get
92 */
93#define ast_named_lock_put(lock) ao2_cleanup(lock)
94
95/*!
96 * @}
97 */
98
99#endif /* INCLUDE_ASTERISK_NAMED_LOCKS_H_ */
@ AO2_ALLOC_OPT_LOCK_RWLOCK
Definition: astobj2.h:365
@ AO2_ALLOC_OPT_LOCK_MUTEX
Definition: astobj2.h:363
struct ast_named_lock * __ast_named_lock_get(const char *filename, int lineno, const char *func, enum ast_named_lock_type lock_type, const char *keyspace, const char *key)
Definition: named_locks.c:70
ast_named_lock_type
Which type of lock to request.
Definition: named_locks.h:57
@ AST_NAMED_LOCK_TYPE_RWLOCK
Definition: named_locks.h:61
@ AST_NAMED_LOCK_TYPE_MUTEX
Definition: named_locks.h:59