Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-17 08:35:01

0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one
0003  * or more contributor license agreements. See the NOTICE file
0004  * distributed with this work for additional information
0005  * regarding copyright ownership. The ASF licenses this file
0006  * to you under the Apache License, Version 2.0 (the
0007  * "License"); you may not use this file except in compliance
0008  * with the License. You may obtain a copy of the License at
0009  *
0010  *   http://www.apache.org/licenses/LICENSE-2.0
0011  *
0012  * Unless required by applicable law or agreed to in writing,
0013  * software distributed under the License is distributed on an
0014  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0015  * KIND, either express or implied. See the License for the
0016  * specific language governing permissions and limitations
0017  * under the License.
0018  */
0019 
0020 #ifndef _THRIFT_CONCURRENCY_TIMERMANAGER_H_
0021 #define _THRIFT_CONCURRENCY_TIMERMANAGER_H_ 1
0022 
0023 #include <thrift/concurrency/Monitor.h>
0024 #include <thrift/concurrency/ThreadFactory.h>
0025 
0026 #include <memory>
0027 #include <map>
0028 
0029 namespace apache {
0030 namespace thrift {
0031 namespace concurrency {
0032 
0033 /**
0034  * Timer Manager
0035  *
0036  * This class dispatches timer tasks when they fall due.
0037  *
0038  * @version $Id:$
0039  */
0040 class TimerManager {
0041 
0042 public:
0043   class Task;
0044   typedef std::weak_ptr<Task> Timer;
0045 
0046   TimerManager();
0047 
0048   virtual ~TimerManager();
0049 
0050   virtual std::shared_ptr<const ThreadFactory> threadFactory() const;
0051 
0052   virtual void threadFactory(std::shared_ptr<const ThreadFactory> value);
0053 
0054   /**
0055    * Starts the timer manager service
0056    *
0057    * @throws IllegalArgumentException Missing thread factory attribute
0058    */
0059   virtual void start();
0060 
0061   /**
0062    * Stops the timer manager service
0063    */
0064   virtual void stop();
0065 
0066   virtual size_t taskCount() const;
0067 
0068   /**
0069    * Adds a task to be executed at some time in the future by a worker thread.
0070    *
0071    * @param task The task to execute
0072    * @param timeout Time in milliseconds to delay before executing task
0073    * @return Handle of the timer, which can be used to remove the timer.
0074    */
0075   virtual Timer add(std::shared_ptr<Runnable> task, const std::chrono::milliseconds &timeout);
0076   Timer add(std::shared_ptr<Runnable> task, uint64_t timeout) { return add(task,std::chrono::milliseconds(timeout)); }
0077 
0078   /**
0079    * Adds a task to be executed at some time in the future by a worker thread.
0080    *
0081    * @param task The task to execute
0082    * @param abstime Absolute time in the future to execute task.
0083    * @return Handle of the timer, which can be used to remove the timer.
0084    */
0085   virtual Timer add(std::shared_ptr<Runnable> task, const std::chrono::time_point<std::chrono::steady_clock>& abstime);
0086 
0087   /**
0088    * Removes a pending task
0089    *
0090    * @param task The task to remove. All timers which execute this task will
0091    * be removed.
0092    * @throws NoSuchTaskException Specified task doesn't exist. It was either
0093    *                             processed already or this call was made for a
0094    *                             task that was never added to this timer
0095    *
0096    * @throws UncancellableTaskException Specified task is already being
0097    *                                    executed or has completed execution.
0098    */
0099   virtual void remove(std::shared_ptr<Runnable> task);
0100 
0101   /**
0102    * Removes a single pending task
0103    *
0104    * @param timer The timer to remove. The timer is returned when calling the
0105    * add() method.
0106    * @throws NoSuchTaskException Specified task doesn't exist. It was either
0107    *                             processed already or this call was made for a
0108    *                             task that was never added to this timer
0109    *
0110    * @throws UncancellableTaskException Specified task is already being
0111    *                                    executed or has completed execution.
0112    */
0113   virtual void remove(Timer timer);
0114 
0115   enum STATE { UNINITIALIZED, STARTING, STARTED, STOPPING, STOPPED };
0116 
0117   virtual STATE state() const;
0118 
0119 private:
0120   std::shared_ptr<const ThreadFactory> threadFactory_;
0121   friend class Task;
0122   std::multimap<std::chrono::time_point<std::chrono::steady_clock>, std::shared_ptr<Task> > taskMap_;
0123   size_t taskCount_;
0124   Monitor monitor_;
0125   STATE state_;
0126   class Dispatcher;
0127   friend class Dispatcher;
0128   std::shared_ptr<Dispatcher> dispatcher_;
0129   std::shared_ptr<Thread> dispatcherThread_;
0130   using task_iterator = decltype(taskMap_)::iterator;
0131   typedef std::pair<task_iterator, task_iterator> task_range;
0132 };
0133 }
0134 }
0135 } // apache::thrift::concurrency
0136 
0137 #endif // #ifndef _THRIFT_CONCURRENCY_TIMERMANAGER_H_