[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [Libevent-users] Disabling callbacks and closing - block until callbacks finished



It occurred to me that it may be due to the fact that I am creating bufferevents using the BEV_OPT_UNLOCK_CALLBACKS flag and not locking the bufferevents when executing the tasks. Perhaps if the close task does the following then the problem will disappear:

1. Lock the buffer event (http://www.wangafu.net/~nickm/libevent-2.0/doxygen/html/bufferevent_8h.html#a56a61802037478591048b4967fa15599)
2. Disable all callbacks
3. Unlock the buffer event
4. Close the connection
5. Remove all pending tasks in the task queue for this connection

Nick.


Quoting nick@xxxxxxxx:

Situation:
----------

I am building a multithreaded socket server built with Libevent. The event loop is run in its own thread and each callback creates a task into a queue. A pool of worker threads then processes the tasks, (of course ensuring that tasks for any given connection are processed in the correct order).

When a close event arrives, a task is created to close the connection and free all related structures. When this task is processed by a worker thread, it does the following things:

1. Disable all callbacks
2. Close the connection
3. Remove all pending tasks in the task queue for this connection

Problem:
--------

The problem is that after the connection is closed, there could still be events in the Libevent event loop either waiting to be processed in a callback or actually being processed by the event thread (i.e. callback running). This could lead to tasks being created for the connection _after_ step 3 which would result in tasks in the task queue for a closed connection - which I want to avoid.

Solution?
---------

I could use a mutex to lock the connection in the event callbacks, but I do not want to introduce blocking methods in the event loop thread.

I could ensure that a connection is valid when I take a task.

But ideally I would like to the bufferevent close method, or the callback disable methods to block until all curently running callbacks for that bufferevent have finished. This way I could be sure that no tasks can be created after the connection is closed.

Question:
---------

Do the methods work like this, or is there a way to achieve this?

Many thanks in advance.

Nick.


***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.



***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.