Asterisk - The Open Source Telephony Project GIT-master-f36a736
Macros | Enumerations | Functions
Named mutex and read-write locks

Macros

#define ast_named_lock_get(lock_type, keyspace, key)
 Geta named lock handle. More...
 
#define ast_named_lock_put(lock)   ao2_cleanup(lock)
 Put a named lock handle away. More...
 

Enumerations

enum  ast_named_lock_type { AST_NAMED_LOCK_TYPE_MUTEX = AO2_ALLOC_OPT_LOCK_MUTEX , AST_NAMED_LOCK_TYPE_RWLOCK = AO2_ALLOC_OPT_LOCK_RWLOCK }
 Which type of lock to request. More...
 

Functions

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)
 

Detailed Description

Named mutex and read-write locks

Since
13.9.0

Locking some objects like sorcery objects can be tricky because the underlying ao2 object may not be the same for all callers. For instance, two threads that call ast_sorcery_retrieve_by_id on the same aor name might actually get 2 different ao2 objects if the underlying wizard had to rehydrate the aor from a database. Locking one ao2 object doesn't have any effect on the other even if those objects had locks in the first placeNamed locks allow access control by name. Now an aor named "1000" can be locked and any other thread attempting to lock the aor named "1000" will wait regardless of whether the underlying ao2 object is the same or not.To use a named lock: Call ast_named_lock_get with the appropriate keyspace and key. Use the standard ao2 lock/unlock functions as needed. Call ao2_cleanup when you're finished with it.

Macro Definition Documentation

◆ ast_named_lock_get

#define ast_named_lock_get (   lock_type,
  keyspace,
  key 
)
Value:
__ast_named_lock_get(__FILE__, __LINE__, __PRETTY_FUNCTION__, lock_type, \
keyspace, key)
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

Geta named lock handle.

Since
13.9.0
Parameters
lock_typeOne of ast_named_lock_type
keyspace
key
Return values
Apointer to an ast_named_lock structure
NULLon error
Note
keyspace and key can be anything. For sorcery objects, keyspace could be the object type and key could be the object id.

Definition at line 83 of file named_locks.h.

◆ ast_named_lock_put

#define ast_named_lock_put (   lock)    ao2_cleanup(lock)

Put a named lock handle away.

Since
13.9.0
Parameters
lockThe pointer to the ast_named_lock structure returned by ast_named_lock_get

Definition at line 93 of file named_locks.h.

Enumeration Type Documentation

◆ ast_named_lock_type

Which type of lock to request.

Enumerator
AST_NAMED_LOCK_TYPE_MUTEX 

Request a named mutex.

AST_NAMED_LOCK_TYPE_RWLOCK 

Request a named read/write lock.

Definition at line 57 of file named_locks.h.

57 {
58 /*! Request a named mutex. */
60 /*! Request a named read/write lock. */
62};
@ AO2_ALLOC_OPT_LOCK_RWLOCK
Definition: astobj2.h:365
@ AO2_ALLOC_OPT_LOCK_MUTEX
Definition: astobj2.h:363
@ AST_NAMED_LOCK_TYPE_RWLOCK
Definition: named_locks.h:61
@ AST_NAMED_LOCK_TYPE_MUTEX
Definition: named_locks.h:59

Function Documentation

◆ __ast_named_lock_get()

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 at line 70 of file named_locks.c.

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
82 __PRETTY_FUNCTION__, filename, lineno, func);
83 if (lock) {
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. */
102 goto failure_cleanup;
103 }
104
106 goto failure_cleanup;
107 }
108
109 strcpy(proxy->key, concat_key); /* Safe */
112 ao2_t_ref(proxy, -1, "Release allocation reference");
113
114 return lock;
115
116failure_cleanup:
118
119 ao2_cleanup(proxy);
121
122 return NULL;
123}
ast_mutex_t lock
Definition: app_sla.c:331
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
#define ao2_weakproxy_set_object(weakproxy, obj, flags)
Associate weakproxy with obj.
Definition: astobj2.h:579
int ao2_weakproxy_subscribe(void *weakproxy, ao2_weakproxy_notification_cb cb, void *data, int flags)
Request notification when weakproxy points to NULL.
Definition: astobj2.c:934
#define ao2_t_ref(o, delta, tag)
Definition: astobj2.h:460
unsigned int ao2_options_get(void *obj)
Retrieve the ao2 options used to create the object.
Definition: astobj2.c:781
@ AO2_ALLOC_OPT_LOCK_MASK
Definition: astobj2.h:369
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_link_flags(container, obj, flags)
Add an object to a container.
Definition: astobj2.h:1554
#define ao2_t_weakproxy_alloc(data_size, destructor_fn, tag)
Definition: astobj2.h:553
#define ao2_unlock(a)
Definition: astobj2.h:729
#define ao2_lock(a)
Definition: astobj2.h:717
void * __ao2_weakproxy_find(struct ao2_container *c, const void *arg, enum search_flags flags, const char *tag, const char *file, int line, const char *func)
@ OBJ_NOLOCK
Assume that the ao2_container is already locked.
Definition: astobj2.h:1063
@ OBJ_SEARCH_KEY
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1101
void * __ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn, unsigned int options, const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result
Definition: astobj2.c:768
static void named_lock_proxy_cb(void *weakproxy, void *data)
Definition: named_locks.c:65
struct ao2_container * named_locks
Definition: named_locks.c:33
#define NULL
Definition: resample.c:96
#define ast_assert(a)
Definition: utils.h:739

References __ao2_alloc(), __ao2_weakproxy_find(), AO2_ALLOC_OPT_LOCK_MASK, ao2_cleanup, ao2_link_flags, ao2_lock, ao2_options_get(), ao2_t_ref, ao2_t_weakproxy_alloc, ao2_unlock, ao2_weakproxy_set_object, ao2_weakproxy_subscribe(), ast_alloca, ast_assert, named_lock_proxy::key, lock, named_lock_proxy_cb(), named_locks, NULL, OBJ_NOLOCK, and OBJ_SEARCH_KEY.