Asterisk - The Open Source Telephony Project  18.5.0
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.

Referenced by aor_alloc(), AST_TEST_DEFINE(), expire_contact(), and lock_thread().

◆ 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.

Referenced by AST_TEST_DEFINE(), expire_contact(), and lock_thread().

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.

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.

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(), NULL, OBJ_NOLOCK, and OBJ_SEARCH_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 
82  __PRETTY_FUNCTION__, filename, lineno, func);
83  if (lock) {
84  ast_assert((ao2_options_get(lock) & AO2_ALLOC_OPT_LOCK_MASK) == lock_type);
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 
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 
116 failure_cleanup:
118 
119  ao2_cleanup(proxy);
120  ao2_cleanup(lock);
121 
122  return NULL;
123 }
#define ao2_t_ref(o, delta, tag)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:463
static void named_lock_proxy_cb(void *weakproxy, void *data)
Definition: named_locks.c:65
#define ao2_weakproxy_set_object(weakproxy, obj, flags)
Definition: astobj2.h:583
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
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:765
Assume that the ao2_container is already locked.
Definition: astobj2.h:1067
#define ast_assert(a)
Definition: utils.h:695
#define ao2_link_flags(container, obj, flags)
Definition: astobj2.h:1572
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:931
#define ao2_unlock(a)
Definition: astobj2.h:730
#define NULL
Definition: resample.c:96
struct ao2_container * named_locks
Definition: named_locks.c:33
ast_mutex_t lock
Definition: app_meetme.c:1091
#define ao2_lock(a)
Definition: astobj2.h:718
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)
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:290
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
unsigned int ao2_options_get(void *obj)
Retrieve the ao2 options used to create the object.
Definition: astobj2.c:778
#define ao2_t_weakproxy_alloc(data_size, destructor_fn, tag)
Definition: astobj2.h:557