Asterisk channel lock and reference

Source: Internet
Author: User

Create Channel

The asterisk code is calledAst_channel_alloc () MacroCreate a channel. After the channel is created, it is automatically inserted into the hash table of the main channel, which is used by the system to track all active channels. The key of the hash table is created based on the channel name. Therefore, if you need to change the channel name, you must callAst_change_name ()Function, but cannot directly modify the corresponding field.Ast_channel_alloc ()Returns a channel pointer, so that the caller holds a channel reference. At most, this reference is directly transmittedAst_pbx_run ()Function.

Channel lock

EachAst_channelEach channel has a lock associated with it. This lock is passed throughAstobj2Internally allocated. To lock or unlock a channel, callAst_channel_lock ()Function.

PreviouslyAst_channelConvertAstobj2Previously, the channel lock was used in other scenarios, but it is no longer needed. Before that, if you need to do some work on the channel outside the channel thread, the only way to ensure that the channel will not disappear in front of you is to lock the channel and hold it. Now, there is no need to do this again. You only need to hold a channel reference to ensure that the channel does not suddenly disappear.

During access to a channel, if you need to ensure that the channel data read is not modified by a third party, you still need to lock the channel. In addition, if you need to make some non-atomic changes to the channel data, you also need to lock the channel.

Channel reference

There are multiple ways to obtain a channel reference. First, you can retain the returned reference after creating the channel. Another method is to call the channel search or channel traversal API. These API functions are ast_channel_get _ * () or ast_channel_iterator _*(). Once channel references are retrieved through these methods, the channel will not disappear. Therefore, channel locking is required only when channel data needs to be accessed or changed. However, after you use the channel reference, you must ensure that the reference is released.

After using the channel reference, you can release it in different ways. First, you can callAst_channel_unref ()The function simply releases the reference. Second, you can callAst_channel_release ()Function to release channel references. In the past, this function was usually used to callAst_channel_free()Function. These release functions release both a reference and the lifecycle of the channel in the global container. In this way, after all references are released, the channel object will be destroyed immediately.

Exceptions in rules

Even ifAst_channelThe reference count has been introduced into the object, and it is still directly stored in some places.Ast_channelPointer, but the reference count does not reflect this content. This is largely due to historical issues. The only thing that can be retained in this case is that the Code process is clear. The main example is the channel driver code. Channel drivers usually store an owner attribute in their PVT struct. The value isAst_channelPointer. At this time, the channel driver knows that this pointer can be promptly cleared, because the hangup callback function will certainly be called before the channel is destroyed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.