Ignore:
Timestamp:
01/07/10 08:10:43 (3 years ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
cc2efd6
Parents:
031acbf
Message:

improved the scheduled module and re-added the settings command line toy (so now you can migrate settings again)
Schedule module is 80% finished but notification channel subsystem is still missing.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • modules/Scheduler/simple_scheduler.hpp

    r031acbf r753ea6d  
    88#include <boost/thread.hpp> 
    99#include <boost/date_time/local_time/local_time.hpp> 
     10#include <boost/optional.hpp> 
    1011 
    1112namespace scheduler { 
    1213 
     14  class task_not_found { 
     15    int id_; 
     16  public: 
     17    task_not_found(int id) : id_(id) {} 
     18    int get_id() {return id_; } 
     19  }; 
    1320  class target { 
    1421  public: 
     
    1825    std::wstring tag; 
    1926    boost::posix_time::time_duration duration; 
     27    std::wstring  channel; 
     28    //std::wstring duration; 
    2029 
    21     target() {} 
    22     target(const target &other) : id(other.id), command(other.command), duration(other.duration) {} 
     30 
     31    void set_duration(boost::posix_time::time_duration duration_) { 
     32      duration = duration_; 
     33      // TODO! 
     34    } 
     35 
     36    target() : duration(boost::posix_time::minutes(0)) 
     37    {} 
     38     
     39    target(const target &other) : id(other.id), command(other.command), duration(other.duration) {} 
    2340    target& operator=(target const& other) { 
    2441      command = other.command; 
     
    2744      return *this; 
    2845    } 
    29     static target empty() { 
    30       return target(); 
    31     } 
     46    ~target() {} 
    3247  }; 
    3348  struct schedule_instance { 
     
    3954  }; 
    4055 
     56  template<typename T> 
     57  class safe_schedule_queue { 
     58  public: 
     59    typedef boost::optional<T> value_type; 
     60  private: 
     61    typedef std::priority_queue<T> schedule_queue_type; 
     62    schedule_queue_type queue_; 
     63    boost::shared_mutex mutex_; 
     64  public: 
     65    bool empty(unsigned int timeout = 5) { 
     66      boost::shared_lock<boost::shared_mutex> lock(mutex_, boost::get_system_time() + boost::posix_time::seconds(5)); 
     67      if (!lock.owns_lock())  
     68        return false; 
     69      return queue_.empty(); 
     70    } 
     71 
     72    boost::optional<T> top(unsigned int timeout = 5) { 
     73      boost::shared_lock<boost::shared_mutex> lock(mutex_, boost::get_system_time() + boost::posix_time::seconds(5)); 
     74      if (!lock || queue_.empty()) 
     75        return boost::optional<T>(); 
     76      return boost::optional<T>(queue_.top()); 
     77    } 
     78 
     79    boost::optional<T> pop(unsigned int timeout = 5) { 
     80      boost::unique_lock<boost::shared_mutex> lock(mutex_, boost::get_system_time() + boost::posix_time::seconds(timeout)); 
     81      if (!lock || queue_.empty()) 
     82        return boost::optional<T>(); 
     83      boost::optional<T>  ret = queue_.top(); 
     84      queue_.pop(); 
     85      return ret; 
     86    } 
     87 
     88    bool push(T instance, unsigned int timeout = 5) { 
     89      boost::unique_lock<boost::shared_mutex> lock(mutex_, boost::get_system_time() + boost::posix_time::seconds(timeout)); 
     90      if (!lock) { 
     91        return false; 
     92      } 
     93      queue_.push(instance); 
     94      return true; 
     95    } 
     96  }; 
     97 
    4198  class simple_scheduler { 
    4299  private: 
    43100    typedef std::map<int,target> target_list_type; 
    44     typedef std::priority_queue<schedule_instance> schedule_queue_type; 
     101    typedef safe_schedule_queue<schedule_instance> schedule_queue_type; 
    45102    target_list_type targets_; 
    46103    unsigned int target_id_; 
    47104    schedule_queue_type queue_; 
     105    unsigned int thread_count_; 
    48106 
    49107 
    50108    // thread variables 
    51109    volatile bool stop_requested_; 
    52     boost::shared_ptr<boost::thread> thread_; 
     110    volatile bool running_; 
     111    boost::thread_group threads_; 
     112    //boost::shared_ptr<boost::thread> thread_; 
    53113    boost::mutex mutex_; 
    54114 
    55115  public: 
    56116 
    57     simple_scheduler() : target_id_(0) {} 
     117    simple_scheduler() : target_id_(0), stop_requested_(false), running_(false), thread_count_(10) {} 
    58118    ~simple_scheduler() {} 
    59119 
     
    61121    int add_task(target item); 
    62122    void remove_task(int id); 
    63     target get_task(int id); 
     123    boost::optional<target> get_task(int id); 
    64124     
    65125    void start(); 
    66126    void stop(); 
     127 
     128    void set_threads(int threads) { 
     129      thread_count_ = threads; 
     130      start_thread(); 
     131    } 
    67132 
    68133 
     
    72137    void reschedule(target item); 
    73138    void reschedule(target item, boost::posix_time::ptime now); 
     139    void reschedule_wnext(target item, boost::posix_time::ptime next); 
    74140    void execute(target item); 
    75141    void start_thread(); 
    76142 
    77     boost::posix_time::ptime now() { 
     143    inline boost::posix_time::ptime now() { 
    78144      return boost::get_system_time(); 
    79145    } 
Note: See TracChangeset for help on using the changeset viewer.