Boost logo

Boost :

Subject: [boost] [timer_scheduler] is there any interest on a timer scheduler library?
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2008-11-03 06:17:48


Hi,

I would like to be able to submit/schedule tasks at a given time (callouts)
which could
be oneshot or periodics. Something like

  the_timer_scheduler::schedule_at(t1_, now()+seconds(10));

Is there any interest on this feature?

I have started to scketch the interface of the library.

Note: I have used timeout because timer already exists in the boost
namespace. callout could be also used if this is more clear.
Note. This interface do not allow arbitrary callbacks functions.

struct timeout;
typedef void (*timeout_callback)(timeout&);

struct timeout_type {
     timeout_type(timeout_callback callback);
     timeout_callback operator()(timeout&);
};

struct timeout {
    timeout(const timeout_type& type);
    const timeout_type& type;
    // ...
};

// template class that store in addition a context
template <typename CTX, timeout_type& TYPE>
struct timeout_ctx : timeout {
    timeout_ctx(CTX* ctx)
        : timeout(TYPE)
        , context_(ctx) {}
    CTX* context() {return context_;}
    CTX* operator CTX*() {return context_;}
protected:
    CTX* context_;
};

// there is a single timer scheduler
namespace the_timer_scheduler {
    void schedule_at(timeout& handle,
        system_time& absolute_time);
    void reschedule_at(timeout& handle,
        system_time& absolute_time);

    void schedule_at_periodicaly(timeout& handle,
        system_time& absolute_time,
        duration_type period);
    void reschedule_at_periodicaly(timeout& handle,
        system_time& absolute_time,
        duration_type period);

    void cancel(timeout& handle);
}

Example:

static const timeout_type t1_type;

struct my_ctx,
typedef timeout_ctx<my_ctx, t1_type> t1;

struct my_ctx {
  my_ctx() :
  t1_(this) {}

  t1 t1_;

  void on_t1();
  static void on_t1(timer* p) {
    static_cast<t1*>(p)->context()->on_t1();
  }
  void funct() {
       the_timer_scheduler::schedule_at(t1_, now()+seconds(10));
      // ...
      the_timer_scheduler::cancel(t1_);
 }
};

timeout_type t1_type(ctx::on_t1);

The implementation of the timeouts scheduler could be based on "Redesigning
the BSD Callout and Timer Facilities" by Adam M. Costello, George Varghese
http://citeseerx.ist.psu.edu/viewdoc/download;jsessionid=B5202FC949FF3EDB0E789F68F509950C?doi=10.1.1.54.6466&rep=rep1&type=pdf

Vicente


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk