include/event2/thread.h File Reference

Functions for multi-threaded applications using libevent. More...

#include <event-config.h>

Go to the source code of this file.

Defines

#define EVTHREAD_LOCK   0x01
#define EVTHREAD_READ   0x08
#define EVTHREAD_UNLOCK   0x02
#define EVTHREAD_WRITE   0x04

Functions

int evthread_make_base_notifiable (struct event_base *base)
 Make sure it's safe to tell an event base to wake up from another thread.
void evthread_set_id_callback (unsigned long(*id_fn)(void))
 Sets the function for derminting the thread id.
void evthread_set_lock_create_callbacks (void *(*alloc_fn)(void), void(*free_fn)(void *))
 Sets the functions libevent should use for allocating and freeing locks.
void evthread_set_locking_callback (void(*locking_fn)(int mode, void *lock))
 Sets the function libevent should use for locking.
int evthread_use_pthreads (void)
 Sets up libevent for use with Pthreadsn locking and thread ID functions.


Detailed Description

Functions for multi-threaded applications using libevent.

When using a multi-threaded application in which multiple threads add and delete events from a single event base, libevent needs to lock its data structures.

Like the memory-management function hooks, all of the threading functions _must_ be set up before an event_base is created if you want the base to use them.

A multi-threaded application must provide locking functions to libevent via evthread_set_locking_callback(). Libevent will invoke this callback whenever a lock needs to be acquired or released.

The total number of locks employed by libevent can be determined via the evthread_num_locks() function. An application must provision that many locks.

If the owner of an event base is waiting for events to happen, libevent may signal the thread via a special file descriptor to wake up. To enable this feature, an application needs to provide a thread identity function via evthread_set_id_callback().


Function Documentation

int evthread_make_base_notifiable ( struct event_base *  base  ) 

Make sure it's safe to tell an event base to wake up from another thread.

Returns:
0 on success, -1 on failure.

void evthread_set_id_callback ( unsigned long(*)(void)  id_fn  ) 

Sets the function for derminting the thread id.

Parameters:
base the event base for which to set the id function
id_fn the identify function libevent should invoke to determine the identity of a thread.

void evthread_set_lock_create_callbacks ( void *(*)(void)  alloc_fn,
void(*)(void *)  free_fn 
)

Sets the functions libevent should use for allocating and freeing locks.

This needs to be called in addition to evthread_set_locking_callback() before using libevent in a multi-threaded application.

Locks must be recursive. That is, it must be safe for a thread to acquire a lock that it already holds.

Parameters:
alloc_fn function to be called when allocating a new lock
free_fn function to be called to a free a lock

void evthread_set_locking_callback ( void(*)(int mode, void *lock)  locking_fn  ) 

Sets the function libevent should use for locking.

Parameters:
locking_fn the function that libevent should invoke to acquire or release a lock. mode has either EVTHREAD_LOCK or EVTHREAD_UNLOCK set, and in addition, either EVHTREAD_WRITE or EVTREAD_READ.

int evthread_use_pthreads ( void   ) 

Sets up libevent for use with Pthreadsn locking and thread ID functions.

Unavailable if libevent is not build for use with pthreads. Requires libraries to link against libevent_pthreads as well as libevent.

Returns:
0 on success, -1 on failure.


Generated on Fri Apr 17 21:33:56 2009 for libevent by  doxygen 1.5.7