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

timer object atached to event loop, when the timer fires, the timer id is sent to the thread that has set the timer More...

Classes

struct  tagEVTIMER

Typedefs

typedef struct tagEVTIMER EVTIMER

Enumerations

enum  EVTIMERSTATE { EVTIMER_STATE_INIT, EVTIMER_STATE_SCHEDULED }

Functions

EVTIMEREVTIMER_init (EVTHREAD *thread, int timer_id, struct timeval tm)
int EVTIMER_start (EVTIMER *ret)
int EVTIMER_cancel (EVTIMER *timer)
int EVTIMER_free (EVTIMER *timer)

Detailed Description

timer object atached to event loop, when the timer fires, the timer id is sent to the thread that has set the timer


Typedef Documentation

typedef struct tagEVTIMER EVTIMER

Enumeration Type Documentation

Enumerator:
EVTIMER_STATE_INIT 
EVTIMER_STATE_SCHEDULED 

Definition at line 138 of file evthread.h.


Function Documentation

int EVTIMER_cancel ( EVTIMER timer)

Definition at line 201 of file evthread.c.

{
  if (timer->state != EVTIMER_STATE_SCHEDULED) {
    return -1;
  }
  
  MLOG_TRACE( "Timer %p canceled", &timer->timer_event ); 

  event_del( &timer->timer_event );
  timer->state = EVTIMER_STATE_INIT;
  return 0;
}
int EVTIMER_free ( EVTIMER timer)

Definition at line 214 of file evthread.c.

{
  EVTIMER_cancel(timer);
  EVTHREAD_OBJECT_free( &timer->object_base );
  return 0;
}
EVTIMER* EVTIMER_init ( EVTHREAD thread,
int  timer_id,
struct timeval  tm 
)

Definition at line 165 of file evthread.c.

{
  EVTIMER *ret;
 
  ret = (EVTIMER *) malloc( sizeof( EVTIMER ) );
  if (!ret) {
    return 0;
  }

  ret->loop = thread->loop;
  ret->timer_id = timer_id;
  ret->tm = tm;
  ret->state = EVTIMER_STATE_INIT;
 
  EVTHREAD_OBJECT_init( &ret->object_base, EVTHREAD_OBJECT_TIMER , thread );
 
  return ret;
}
int EVTIMER_start ( EVTIMER ret)

Definition at line 185 of file evthread.c.

{
  if (ret->state != EVTIMER_STATE_INIT) {
    return -1;
  }

  ret->state = EVTIMER_STATE_SCHEDULED;
  
  event_set( &ret->timer_event, -1, 0, timer_cb, (void *) ret );
  event_base_set( ret->loop->ev_base, &ret->timer_event );
  event_add( &ret->timer_event, &ret->tm );

  MLOG_TRACE( "Timer %p started %ld:%ld", &ret->timer_event, ret->tm.tv_sec, ret->tm.tv_usec ); 
  return 0;
}