Subversion Repositories SmartDukaan

Rev

Rev 30 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements. See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership. The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License. You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied. See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
 
20
#ifndef _THRIFT_CONCURRENCY_TIMERMANAGER_H_
21
#define _THRIFT_CONCURRENCY_TIMERMANAGER_H_ 1
22
 
23
#include "Exception.h"
24
#include "Monitor.h"
25
#include "Thread.h"
26
 
27
#include <boost/shared_ptr.hpp>
28
#include <map>
29
#include <time.h>
30
 
31
namespace apache { namespace thrift { namespace concurrency {
32
 
33
/**
34
 * Timer Manager
35
 *
36
 * This class dispatches timer tasks when they fall due.
37
 *
38
 * @version $Id:$
39
 */
40
class TimerManager {
41
 
42
 public:
43
 
44
  TimerManager();
45
 
46
  virtual ~TimerManager();
47
 
48
  virtual boost::shared_ptr<const ThreadFactory> threadFactory() const;
49
 
50
  virtual void threadFactory(boost::shared_ptr<const ThreadFactory> value);
51
 
52
  /**
53
   * Starts the timer manager service
54
   *
55
   * @throws IllegalArgumentException Missing thread factory attribute
56
   */
57
  virtual void start();
58
 
59
  /**
60
   * Stops the timer manager service
61
   */
62
  virtual void stop();
63
 
64
  virtual size_t taskCount() const ;
65
 
66
  /**
67
   * Adds a task to be executed at some time in the future by a worker thread.
68
   *
69
   * @param task The task to execute
70
   * @param timeout Time in milliseconds to delay before executing task
71
   */
72
  virtual void add(boost::shared_ptr<Runnable> task, int64_t timeout);
73
 
74
  /**
75
   * Adds a task to be executed at some time in the future by a worker thread.
76
   *
77
   * @param task The task to execute
78
   * @param timeout Absolute time in the future to execute task.
79
   */
80
  virtual void add(boost::shared_ptr<Runnable> task, const struct timespec& timeout);
81
 
82
  /**
83
   * Removes a pending task
84
   *
85
   * @throws NoSuchTaskException Specified task doesn't exist. It was either
86
   *                             processed already or this call was made for a
87
   *                             task that was never added to this timer
88
   *
89
   * @throws UncancellableTaskException Specified task is already being
90
   *                                    executed or has completed execution.
91
   */
92
  virtual void remove(boost::shared_ptr<Runnable> task);
93
 
94
  enum STATE {
95
    UNINITIALIZED,
96
    STARTING,
97
    STARTED,
98
    STOPPING,
99
    STOPPED
100
  };
101
 
102
  virtual const STATE state() const;
103
 
104
 private:
105
  boost::shared_ptr<const ThreadFactory> threadFactory_;
106
  class Task;
107
  friend class Task;
108
  std::multimap<int64_t, boost::shared_ptr<Task> > taskMap_;
109
  size_t taskCount_;
110
  Monitor monitor_;
111
  STATE state_;
112
  class Dispatcher;
113
  friend class Dispatcher;
114
  boost::shared_ptr<Dispatcher> dispatcher_;
115
  boost::shared_ptr<Thread> dispatcherThread_;
116
  typedef std::multimap<int64_t, boost::shared_ptr<TimerManager::Task> >::iterator task_iterator;
117
  typedef std::pair<task_iterator, task_iterator> task_range;
118
};
119
 
120
}}} // apache::thrift::concurrency
121
 
122
#endif // #ifndef _THRIFT_CONCURRENCY_TIMERMANAGER_H_