Asterisk - The Open Source Telephony Project  21.4.1
Data Structures | Functions | Variables
test_scoped_lock.c File Reference

SCOPED_LOCK unit tests. More...

#include "asterisk.h"
#include "asterisk/test.h"
#include "asterisk/utils.h"
#include "asterisk/module.h"
#include "asterisk/astobj2.h"

Go to the source code of this file.

Data Structures

struct  test_struct
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_TEST_DEFINE (lock_test)
 
 AST_TEST_DEFINE (cleanup_order)
 
static int load_module (void)
 
static void lock_it (ast_mutex_t *lock)
 
static struct test_structtest_iterator_next (struct ao2_iterator *iter)
 wrapper for ao2_iterator_next More...
 
static void test_lock (struct test_struct *test)
 lock callback function More...
 
static struct test_structtest_ref (struct test_struct *test)
 ref callback function More...
 
static void test_unlock (struct test_struct *test)
 unlock callback function More...
 
static void test_unref (struct test_struct *test)
 unref callback function More...
 
static int unload_module (void)
 
static void unlock_it (ast_mutex_t *lock)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "SCOPED_LOCK test module" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_test * current_test
 
static int indicator
 
static ast_mutex_t the_lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} }
 

Detailed Description

SCOPED_LOCK unit tests.

Author
Mark Michelson mmich.nosp@m.elso.nosp@m.n@dig.nosp@m.ium..nosp@m.com

Definition in file test_scoped_lock.c.

Function Documentation

static struct test_struct* test_iterator_next ( struct ao2_iterator iter)
static

wrapper for ao2_iterator_next

Grabs the next item in the container and replaces the ref acquired from ao2_iterator_next() with a call to test_ref().

Definition at line 179 of file test_scoped_lock.c.

References ao2_ref, and test_ref().

180 {
181  struct test_struct *test = ao2_iterator_next(iter);
182 
183  if (!test) {
184  return NULL;
185  }
186 
187  /* Remove ref from ao2_iterator_next() and replace it with
188  * a test_ref() call. The order here is safe since we can guarantee
189  * the container still has a ref to the test structure.
190  */
191  ao2_ref(test, -1);
192  test_ref(test);
193 
194  return test;
195 }
static struct test_struct * test_ref(struct test_struct *test)
ref callback function
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
static void test_lock ( struct test_struct test)
static

lock callback function

Locks the object passed in. Only sets the locked flag if the object is reffed. This allows us to check that locking is always occurring after reffing.

Definition at line 115 of file test_scoped_lock.c.

116 {
117  ast_test_status_update(current_test, "Lock is occurring\n");
118  ao2_lock(test);
119  if (test->reffed) {
120  test->locked = 1;
121  }
122 }
static struct test_struct* test_ref ( struct test_struct test)
static

ref callback function

Refs the object passed in. Only sets the reffed flag if the object is not locked. This allows us to ensure that reffing always occurs before locking.

Definition at line 147 of file test_scoped_lock.c.

References ao2_ref.

Referenced by test_iterator_next().

148 {
149  ast_test_status_update(current_test, "Ref is occurring\n");
150  ao2_ref(test, +1);
151  if (!test->locked) {
152  test->reffed = 1;
153  }
154  return test;
155 }
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
static void test_unlock ( struct test_struct test)
static

unlock callback function

Unlocks the object passed in. Only clears the locked flag if the object is still reffed. This allows us to ensure that unlocking is always occurring before unreffing.

Definition at line 131 of file test_scoped_lock.c.

132 {
133  ast_test_status_update(current_test, "Unlock is occurring\n");
134  ao2_unlock(test);
135  if (test->reffed) {
136  test->locked = 0;
137  }
138 }
static void test_unref ( struct test_struct test)
static

unref callback function

Unrefs the object passed in. Only sets the unreffed flag if the object is not locked. This allows us to ensure that unreffing always occurs after unlocking.

Definition at line 164 of file test_scoped_lock.c.

References ao2_ref.

165 {
166  ast_test_status_update(current_test, "Unref is occurring\n");
167  ao2_ref(test, -1);
168  if (!test->locked) {
169  test->reffed = 0;
170  }
171 }
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459