Libevent Source Code Analysis-----More Evbuffer operation functions

Source: Internet
Author: User

Reprint Please specify Source: http://blog.csdn.net/luotuo44/article/details/39325447


Lock Operation:


In the previous blog post you can see that many functions need to lock the evbuffer before operation. Unlike Event_base, if the evbuffer supports locks, the function evbuffer_enable_locking is called explicitly.

[CPP]  View Plain copy//buffer.c file    int//parameter can be a lock variable or null   evbuffer_enable_locking (struct  evbuffer *buf, void *lock)    {   #ifdef  _event_disable_thread_ support       return -1;   #else         if  (buf->lock)            return -1;           if  (!lock)  {            //self-assigning lock variables            evthread_alloc_lock ( lock, evthread_locktype_recursive);           if  (! Lock)                return -1;            buf->lock = lock;             //the Evbuffer has a lock, it needs to release the lock memory             buf->own_lock = 1;       } else {            buf->lock = lock;//lock        with parameters      buf->own_lock = 0;//himself did not own the lock. No need to release lock memory        }          return 0;    #endif   }  

As you can see, the second argument can be null. A lock is requested inside the function. Obviously if you want Evbuffer to be able to use a lock, you have to call Evthread_use_windows_threads () or evthread_use_pthreads () at the beginning, and you can refer to a blog post.

Because the user controls this Evbuffer, Libevent provides a lock and unlock interface for the user to use.[CPP]  View Plain copy//buffer.c file    void   evbuffer_lock (struct evbuffer *buf)     {       evbuffer_lock (buf);  }      void    Evbuffer_unlock (struct evbuffer *buf)    {       evbuffer _unlock (BUF);  }     //evbuffer-internal.h file    #define  evbuffer_ LOCK (buffer)                         \       do {                                  \            evlock_lock ((buffer)->lock, 0);               \  

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.