autochan.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2009, Digium, Inc.
  5. *
  6. * Mark Michelson <mmichelson@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file
  20. * \brief "smart" channels
  21. *
  22. * \author Mark Michelson <mmichelson@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/autochan.h"
  30. #include "asterisk/utils.h"
  31. #include "asterisk/linkedlists.h"
  32. #include "asterisk/options.h"
  33. #include "asterisk/channel.h"
  34. struct ast_autochan *ast_autochan_setup(struct ast_channel *chan)
  35. {
  36. struct ast_autochan *autochan;
  37. if (!chan) {
  38. return NULL;
  39. }
  40. if (!(autochan = ast_calloc(1, sizeof(*autochan)))) {
  41. return NULL;
  42. }
  43. ast_mutex_init(&autochan->lock);
  44. autochan->chan = ast_channel_ref(chan);
  45. ast_debug(1, "Created autochan %p to hold channel %s (%p)\n",
  46. autochan, ast_channel_name(chan), chan);
  47. /* autochan is still private, no need for ast_autochan_channel_lock() */
  48. ast_channel_lock(autochan->chan);
  49. AST_LIST_INSERT_TAIL(ast_channel_autochans(autochan->chan), autochan, list);
  50. ast_channel_unlock(autochan->chan);
  51. return autochan;
  52. }
  53. void ast_autochan_destroy(struct ast_autochan *autochan)
  54. {
  55. struct ast_autochan *autochan_iter;
  56. ast_autochan_channel_lock(autochan);
  57. AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_autochans(autochan->chan), autochan_iter, list) {
  58. if (autochan_iter == autochan) {
  59. AST_LIST_REMOVE_CURRENT(list);
  60. ast_debug(1, "Removed autochan %p from the list, about to free it\n", autochan);
  61. break;
  62. }
  63. }
  64. AST_LIST_TRAVERSE_SAFE_END;
  65. ast_autochan_channel_unlock(autochan);
  66. autochan->chan = ast_channel_unref(autochan->chan);
  67. ast_mutex_destroy(&autochan->lock);
  68. ast_free(autochan);
  69. }
  70. void ast_autochan_new_channel(struct ast_channel *old_chan, struct ast_channel *new_chan)
  71. {
  72. struct ast_autochan *autochan;
  73. AST_LIST_APPEND_LIST(ast_channel_autochans(new_chan), ast_channel_autochans(old_chan), list);
  74. /* Deadlock avoidance is not needed since the channels are already locked. */
  75. AST_LIST_TRAVERSE(ast_channel_autochans(new_chan), autochan, list) {
  76. ast_mutex_lock(&autochan->lock);
  77. if (autochan->chan == old_chan) {
  78. autochan->chan = ast_channel_ref(new_chan);
  79. ast_channel_unref(old_chan);
  80. ast_debug(1, "Autochan %p used to hold channel %s (%p) but now holds channel %s (%p)\n",
  81. autochan, ast_channel_name(old_chan), old_chan, ast_channel_name(new_chan), new_chan);
  82. }
  83. ast_mutex_unlock(&autochan->lock);
  84. }
  85. }