Changeset 753ea6d in nscp for modules/Scheduler/simple_scheduler.hpp
- Timestamp:
- 01/07/10 08:10:43 (3 years ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- cc2efd6
- Parents:
- 031acbf
- File:
-
- 1 edited
-
modules/Scheduler/simple_scheduler.hpp (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
modules/Scheduler/simple_scheduler.hpp
r031acbf r753ea6d 8 8 #include <boost/thread.hpp> 9 9 #include <boost/date_time/local_time/local_time.hpp> 10 #include <boost/optional.hpp> 10 11 11 12 namespace scheduler { 12 13 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 }; 13 20 class target { 14 21 public: … … 18 25 std::wstring tag; 19 26 boost::posix_time::time_duration duration; 27 std::wstring channel; 28 //std::wstring duration; 20 29 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) {} 23 40 target& operator=(target const& other) { 24 41 command = other.command; … … 27 44 return *this; 28 45 } 29 static target empty() { 30 return target(); 31 } 46 ~target() {} 32 47 }; 33 48 struct schedule_instance { … … 39 54 }; 40 55 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 41 98 class simple_scheduler { 42 99 private: 43 100 typedef std::map<int,target> target_list_type; 44 typedef s td::priority_queue<schedule_instance> schedule_queue_type;101 typedef safe_schedule_queue<schedule_instance> schedule_queue_type; 45 102 target_list_type targets_; 46 103 unsigned int target_id_; 47 104 schedule_queue_type queue_; 105 unsigned int thread_count_; 48 106 49 107 50 108 // thread variables 51 109 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_; 53 113 boost::mutex mutex_; 54 114 55 115 public: 56 116 57 simple_scheduler() : target_id_(0) {}117 simple_scheduler() : target_id_(0), stop_requested_(false), running_(false), thread_count_(10) {} 58 118 ~simple_scheduler() {} 59 119 … … 61 121 int add_task(target item); 62 122 void remove_task(int id); 63 targetget_task(int id);123 boost::optional<target> get_task(int id); 64 124 65 125 void start(); 66 126 void stop(); 127 128 void set_threads(int threads) { 129 thread_count_ = threads; 130 start_thread(); 131 } 67 132 68 133 … … 72 137 void reschedule(target item); 73 138 void reschedule(target item, boost::posix_time::ptime now); 139 void reschedule_wnext(target item, boost::posix_time::ptime next); 74 140 void execute(target item); 75 141 void start_thread(); 76 142 77 boost::posix_time::ptime now() {143 inline boost::posix_time::ptime now() { 78 144 return boost::get_system_time(); 79 145 }
Note: See TracChangeset
for help on using the changeset viewer.








