📄 linkedlists.h
字号:
/* * Asterisk -- An open source telephony toolkit. * * Copyright (C) 1999 - 2006, Digium, Inc. * * Mark Spencer <markster@digium.com> * Kevin P. Fleming <kpfleming@digium.com> * * See http://www.asterisk.org for more information about * the Asterisk project. Please do not directly contact * any of the maintainers of this project for assistance; * the project provides a web site, mailing lists and IRC * channels for your use. * * This program is free software, distributed under the terms of * the GNU General Public License Version 2. See the LICENSE file * at the top of the source tree. */#ifndef ASTERISK_LINKEDLISTS_H#define ASTERISK_LINKEDLISTS_H#include "asterisk/lock.h"/*! \file linkedlists.h \brief A set of macros to manage forward-linked lists.*//*! \brief Locks a list. \param head This is a pointer to the list head structure This macro attempts to place an exclusive lock in the list head structure pointed to by head. \retval 0 on success \retval non-zero on failure*/#define AST_LIST_LOCK(head) \ ast_mutex_lock(&(head)->lock) /*! \brief Write locks a list. \param head This is a pointer to the list head structure This macro attempts to place an exclusive write lock in the list head structure pointed to by head. \retval 0 on success \retval non-zero on failure*/#define AST_RWLIST_WRLOCK(head) \ ast_rwlock_wrlock(&(head)->lock)/*! \brief Read locks a list. \param head This is a pointer to the list head structure This macro attempts to place a read lock in the list head structure pointed to by head. \retval 0 on success \retval non-zero on failure*/#define AST_RWLIST_RDLOCK(head) \ ast_rwlock_rdlock(&(head)->lock) /*! \brief Locks a list, without blocking if the list is locked. \param head This is a pointer to the list head structure This macro attempts to place an exclusive lock in the list head structure pointed to by head. \retval 0 on success \retval non-zero on failure*/#define AST_LIST_TRYLOCK(head) \ ast_mutex_trylock(&(head)->lock) /*! \brief Write locks a list, without blocking if the list is locked. \param head This is a pointer to the list head structure This macro attempts to place an exclusive write lock in the list head structure pointed to by head. \retval 0 on success \retval non-zero on failure*/#define AST_RWLIST_TRYWRLOCK(head) \ ast_rwlock_trywrlock(&(head)->lock)/*! \brief Read locks a list, without blocking if the list is locked. \param head This is a pointer to the list head structure This macro attempts to place a read lock in the list head structure pointed to by head. \retval 0 on success \retval non-zero on failure*/#define AST_RWLIST_TRYRDLOCK(head) \ ast_rwlock_tryrdlock(&(head)->lock) /*! \brief Attempts to unlock a list. \param head This is a pointer to the list head structure This macro attempts to remove an exclusive lock from the list head structure pointed to by head. If the list was not locked by this thread, this macro has no effect.*/#define AST_LIST_UNLOCK(head) \ ast_mutex_unlock(&(head)->lock)/*! \brief Attempts to unlock a read/write based list. \param head This is a pointer to the list head structure This macro attempts to remove a read or write lock from the list head structure pointed to by head. If the list was not locked by this thread, this macro has no effect.*/#define AST_RWLIST_UNLOCK(head) \ ast_rwlock_unlock(&(head)->lock)/*! \brief Defines a structure to be used to hold a list of specified type. \param name This will be the name of the defined structure. \param type This is the type of each list entry. This macro creates a structure definition that can be used to hold a list of the entries of type \a type. It does not actually declare (allocate) a structure; to do that, either follow this macro with the desired name of the instance you wish to declare, or use the specified \a name to declare instances elsewhere. Example usage: \code static AST_LIST_HEAD(entry_list, entry) entries; \endcode This would define \c struct \c entry_list, and declare an instance of it named \a entries, all intended to hold a list of type \c struct \c entry.*/#define AST_LIST_HEAD(name, type) \struct name { \ struct type *first; \ struct type *last; \ ast_mutex_t lock; \}/*! \brief Defines a structure to be used to hold a read/write list of specified type. \param name This will be the name of the defined structure. \param type This is the type of each list entry. This macro creates a structure definition that can be used to hold a list of the entries of type \a type. It does not actually declare (allocate) a structure; to do that, either follow this macro with the desired name of the instance you wish to declare, or use the specified \a name to declare instances elsewhere. Example usage: \code static AST_RWLIST_HEAD(entry_list, entry) entries; \endcode This would define \c struct \c entry_list, and declare an instance of it named \a entries, all intended to hold a list of type \c struct \c entry.*/#define AST_RWLIST_HEAD(name, type) \struct name { \ struct type *first; \ struct type *last; \ ast_rwlock_t lock; \}/*! \brief Defines a structure to be used to hold a list of specified type (with no lock). \param name This will be the name of the defined structure. \param type This is the type of each list entry. This macro creates a structure definition that can be used to hold a list of the entries of type \a type. It does not actually declare (allocate) a structure; to do that, either follow this macro with the desired name of the instance you wish to declare, or use the specified \a name to declare instances elsewhere. Example usage: \code static AST_LIST_HEAD_NOLOCK(entry_list, entry) entries; \endcode This would define \c struct \c entry_list, and declare an instance of it named \a entries, all intended to hold a list of type \c struct \c entry.*/#define AST_LIST_HEAD_NOLOCK(name, type) \struct name { \ struct type *first; \ struct type *last; \}/*! \brief Defines initial values for a declaration of AST_LIST_HEAD*/#define AST_LIST_HEAD_INIT_VALUE { \ .first = NULL, \ .last = NULL, \ .lock = AST_MUTEX_INIT_VALUE, \ }/*! \brief Defines initial values for a declaration of AST_RWLIST_HEAD*/#define AST_RWLIST_HEAD_INIT_VALUE { \ .first = NULL, \ .last = NULL, \ .lock = AST_RWLOCK_INIT_VALUE, \ }/*! \brief Defines initial values for a declaration of AST_LIST_HEAD_NOLOCK*/#define AST_LIST_HEAD_NOLOCK_INIT_VALUE { \ .first = NULL, \ .last = NULL, \ }/*! \brief Defines a structure to be used to hold a list of specified type, statically initialized. \param name This will be the name of the defined structure. \param type This is the type of each list entry. This macro creates a structure definition that can be used to hold a list of the entries of type \a type, and allocates an instance of it, initialized to be empty. Example usage: \code static AST_LIST_HEAD_STATIC(entry_list, entry); \endcode This would define \c struct \c entry_list, intended to hold a list of type \c struct \c entry.*/#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)#define AST_LIST_HEAD_STATIC(name, type) \struct name { \ struct type *first; \ struct type *last; \ ast_mutex_t lock; \} name; \static void __attribute__((constructor)) __init_##name(void) \{ \ AST_LIST_HEAD_INIT(&name); \} \static void __attribute__((destructor)) __fini_##name(void) \{ \ AST_LIST_HEAD_DESTROY(&name); \} \struct __dummy_##name#else#define AST_LIST_HEAD_STATIC(name, type) \struct name { \ struct type *first; \ struct type *last; \ ast_mutex_t lock; \} name = AST_LIST_HEAD_INIT_VALUE#endif/*! \brief Defines a structure to be used to hold a read/write list of specified type, statically initialized. \param name This will be the name of the defined structure. \param type This is the type of each list entry. This macro creates a structure definition that can be used to hold a list of the entries of type \a type, and allocates an instance of it, initialized to be empty. Example usage: \code static AST_RWLIST_HEAD_STATIC(entry_list, entry); \endcode This would define \c struct \c entry_list, intended to hold a list of type \c struct \c entry.*/#ifndef HAVE_PTHREAD_RWLOCK_INITIALIZER#define AST_RWLIST_HEAD_STATIC(name, type) \struct name { \ struct type *first; \ struct type *last; \ ast_rwlock_t lock; \} name; \static void __attribute__((constructor)) __init_##name(void) \{ \ AST_RWLIST_HEAD_INIT(&name); \} \static void __attribute__((destructor)) __fini_##name(void) \{ \ AST_RWLIST_HEAD_DESTROY(&name); \} \struct __dummy_##name#else#define AST_RWLIST_HEAD_STATIC(name, type) \struct name { \ struct type *first; \ struct type *last; \ ast_rwlock_t lock; \} name = AST_RWLIST_HEAD_INIT_VALUE#endif/*! \brief Defines a structure to be used to hold a list of specified type, statically initialized. This is the same as AST_LIST_HEAD_STATIC, except without the lock included.*/#define AST_LIST_HEAD_NOLOCK_STATIC(name, type) \struct name { \ struct type *first; \ struct type *last; \} name = AST_LIST_HEAD_NOLOCK_INIT_VALUE/*! \brief Initializes a list head structure with a specified first entry. \param head This is a pointer to the list head structure \param entry pointer to the list entry that will become the head of the list This macro initializes a list head structure by setting the head entry to the supplied value and recreating the embedded lock.*/#define AST_LIST_HEAD_SET(head, entry) do { \ (head)->first = (entry); \ (head)->last = (entry); \ ast_mutex_init(&(head)->lock); \} while (0)/*! \brief Initializes an rwlist head structure with a specified first entry. \param head This is a pointer to the list head structure \param entry pointer to the list entry that will become the head of the list This macro initializes a list head structure by setting the head entry to the supplied value and recreating the embedded lock.*/#define AST_RWLIST_HEAD_SET(head, entry) do { \ (head)->first = (entry); \ (head)->last = (entry); \ ast_rwlock_init(&(head)->lock); \} while (0)/*! \brief Initializes a list head structure with a specified first entry. \param head This is a pointer to the list head structure \param entry pointer to the list entry that will become the head of the list This macro initializes a list head structure by setting the head entry to the supplied value.*/#define AST_LIST_HEAD_SET_NOLOCK(head, entry) do { \ (head)->first = (entry); \ (head)->last = (entry); \} while (0)/*! \brief Declare a forward link structure inside a list entry. \param type This is the type of each list entry. This macro declares a structure to be used to link list entries together. It must be used inside the definition of the structure named in \a type, as follows: \code struct list_entry { ... AST_LIST_ENTRY(list_entry) list; } \endcode The field name \a list here is arbitrary, and can be anything you wish.*/#define AST_LIST_ENTRY(type) \struct { \ struct type *next; \}#define AST_RWLIST_ENTRY AST_LIST_ENTRY /*! \brief Returns the first entry contained in a list.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -