Asterisk - The Open Source Telephony Project  GIT-master-44aef04
astobj2_global.c
Go to the documentation of this file.
1 /*
2  * astobj2_global - global containers for AO2 objects.
3  *
4  * Copyright (C) 2006 Marta Carbone, Luigi Rizzo - Univ. di Pisa, Italy
5  *
6  * See http://www.asterisk.org for more information about
7  * the Asterisk project. Please do not directly contact
8  * any of the maintainers of this project for assistance;
9  * the project provides a web site, mailing lists and IRC
10  * channels for your use.
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License Version 2. See the LICENSE file
14  * at the top of the source tree.
15  */
16 
17 /*! \file
18  *
19  * \brief Functions implementing ao2_global_obj routines.
20  *
21  * \author Richard Mudgett <rmudgett@digium.com>
22  */
23 
24 /*** MODULEINFO
25  <support_level>core</support_level>
26  ***/
27 
28 #include "asterisk.h"
29 
30 #include "asterisk/astobj2.h"
31 #include "asterisk/utils.h"
32 
33 void *__ao2_global_obj_replace(struct ao2_global_obj *holder, void *obj, const char *tag, const char *file, int line, const char *func, const char *name)
34 {
35  void *obj_old;
36 
37  if (!holder) {
38  /* For sanity */
39  ast_log(LOG_ERROR, "Must be called with a global object!\n");
40  ast_assert(0);
41  return NULL;
42  }
43  if (__ast_rwlock_wrlock(file, line, func, &holder->lock, name)) {
44  /* Could not get the write lock. */
45  ast_assert(0);
46  return NULL;
47  }
48 
49  if (obj) {
50  __ao2_ref(obj, +1, tag, file, line, func);
51  }
52  obj_old = holder->obj;
53  holder->obj = obj;
54 
55  __ast_rwlock_unlock(file, line, func, &holder->lock, name);
56 
57  return obj_old;
58 }
59 
60 int __ao2_global_obj_replace_unref(struct ao2_global_obj *holder, void *obj, const char *tag, const char *file, int line, const char *func, const char *name)
61 {
62  void *obj_old;
63 
64  obj_old = __ao2_global_obj_replace(holder, obj, tag, file, line, func, name);
65  if (obj_old) {
66  __ao2_ref(obj_old, -1, tag, file, line, func);
67  return 1;
68  }
69  return 0;
70 }
71 
72 void *__ao2_global_obj_ref(struct ao2_global_obj *holder, const char *tag, const char *file, int line, const char *func, const char *name)
73 {
74  void *obj;
75 
76  if (!holder) {
77  /* For sanity */
78  ast_log(LOG_ERROR, "Must be called with a global object!\n");
79  ast_assert(0);
80  return NULL;
81  }
82 
83  if (__ast_rwlock_rdlock(file, line, func, &holder->lock, name)) {
84  /* Could not get the read lock. */
85  ast_assert(0);
86  return NULL;
87  }
88 
89  obj = holder->obj;
90  if (obj) {
91  __ao2_ref(obj, +1, tag, file, line, func);
92  }
93 
94  __ast_rwlock_unlock(file, line, func, &holder->lock, name);
95 
96  return obj;
97 }
Asterisk main include file. File version handling, generic pbx functions.
int __ast_rwlock_rdlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name)
Definition: lock.c:819
#define ast_assert(a)
Definition: utils.h:650
#define NULL
Definition: resample.c:96
int __ast_rwlock_unlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name)
Definition: lock.c:748
Utility functions.
int __ao2_ref(void *o, int delta, const char *tag, const char *file, int line, const char *func)
Definition: astobj2.c:498
#define ast_log
Definition: astobj2.c:42
void * __ao2_global_obj_ref(struct ao2_global_obj *holder, const char *tag, const char *file, int line, const char *func, const char *name)
#define LOG_ERROR
Definition: logger.h:285
void * obj
Definition: astobj2.h:808
static const char name[]
Definition: cdr_mysql.c:74
ast_rwlock_t lock
Definition: astobj2.h:806
int __ast_rwlock_wrlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name)
Definition: lock.c:917
void * __ao2_global_obj_replace(struct ao2_global_obj *holder, void *obj, const char *tag, const char *file, int line, const char *func, const char *name)
int __ao2_global_obj_replace_unref(struct ao2_global_obj *holder, void *obj, const char *tag, const char *file, int line, const char *func, const char *name)