Simple coroutine library integrated with IO event loop (libevent) / objects in plain C Snapshot
Classes | Typedefs | Functions
EVLOOP

event loop object. The event loop wraps a small user mode threading library where a user mode thread is created per connection. More...

Classes

struct  tagEVLOOP

Typedefs

typedef struct tagEVLOOP EVLOOP

Functions

EVLOOPEVLOOP_init (STACKS *stacks)
int EVLOOP_run (EVLOOP *loop)
int EVLOOP_break (EVLOOP *loop)

Detailed Description

event loop object. The event loop wraps a small user mode threading library where a user mode thread is created per connection.

When a new connection is received via accept(2), then an EVSOCKET object is created that encapsulates this socket, and the socket is set to non blocking mode, and a user mode thread EVTHREAD is created that owns the new EVSOCKET object. TCPACCEPTOR is the class that does all that.

A user mode thread can own one or more EVSOCKET objects, it can read or write to an EVSOCKET object which has the API of a blocking socket. when the socket blocks, (i.e. recv or send returns -1 and errno == EWOULDBLOCK) then the attached user mode thread is suspended, and control returns to the event loop, the event loop schedules a different user mode thread, when an IO event occurs on a socket owned by that thread.

A user mode thread (EVTHREAD) can create other sockets (EVSOCKET) that are then attached to the current thread.

A use mode thread also owns a set of timer objects (EVTIMER), when the thread exits, all the timer objects are cleaned up.

The event loop is implemented by libevent.

Unlike other packages, where one can create several user mode thread and then has to cope with the problem of their synchronization, we keep things simple here. There is one thread per connection, that is supposed to do protocol handling and processing, all on condition that processing does not involve heavy CPU intensive tasks. If your problem does have such task, then there is also support for the Half-sync/Half-async pattern.


Typedef Documentation

typedef struct tagEVLOOP EVLOOP

Function Documentation

int EVLOOP_break ( EVLOOP loop)

Definition at line 48 of file evthread.c.

{
  return  event_base_loopbreak( loop->ev_base ); 
}
EVLOOP* EVLOOP_init ( STACKS *  stacks)

Definition at line 19 of file evthread.c.

{
  EVLOOP *loop;

  loop = (EVLOOP *)  malloc( sizeof( EVLOOP ) );
  if (!loop) {
    return 0;
  }

  if (!stacks) {
    return 0;
  }
  loop->stacks = stacks;
 
  disable_sigpipe();

  loop->ev_base = event_init();
  if (!loop->ev_base) {
    return 0;
  }

  return loop;
}
int EVLOOP_run ( EVLOOP loop)

Definition at line 43 of file evthread.c.

{
  return event_base_dispatch( loop->ev_base );
}