Ocean
Scheduler.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_BASE_SCHEDULER_H
9 #define META_OCEAN_BASE_SCHEDULER_H
10 
11 #include "ocean/base/Base.h"
12 #include "ocean/base/Callback.h"
13 #include "ocean/base/Singleton.h"
14 #include "ocean/base/Thread.h"
15 
16 #include <map>
17 
18 namespace Ocean
19 {
20 
21 /**
22  * This class represents a round robin scheduler.
23  * The scheduler is based on one single thread and executes all registered functions in a round robin manner.<br>
24  * The registered functions are called with high frequency (e.g., each 10-50 ms).<br>
25  * A registered function should return immediately so that a high call frequency for all registered function can be ensured.
26  * @see ThreadPool, TaskQueue.
27  * @ingroup base
28  */
29 class OCEAN_BASE_EXPORT Scheduler :
30  public Singleton<Scheduler>,
31  protected Thread
32 {
33  friend class Singleton<Scheduler>;
34 
35  public:
36 
37  /**
38  * Definition of a callback function.
39  */
41 
42  private:
43 
44  /**
45  * Definition of a callback pair holding a callback function and an active flag.
46  */
48 
49  public:
50 
51  /**
52  * Registers a scheduler function.
53  * Each registered function must be unregistered by the caller if the scheduler event is not needed anymore.
54  * @param callback Callback function to register
55  * @see unregisterFunction().
56  */
57  void registerFunction(const Callback& callback);
58 
59  /**
60  * Unregisters a scheduler function.
61  * This function may be called from a registered scheduler function.
62  * @param callback Callback function to unregister
63  * @see registerFunction().
64  */
65  void unregisterFunction(const Callback& callback);
66 
67  protected:
68 
69  /**
70  * Creates a new scheduler.
71  */
73 
74  /**
75  * Destructs a scheduler.
76  */
77  virtual ~Scheduler();
78 
79  /**
80  * Derived thread run function.
81  */
82  virtual void threadRun();
83 
84  protected:
85 
86  /// Scheduler callbacks.
88 };
89 
90 }
91 
92 #endif // META_OCEAN_BASE_SCHEDULER_H
This class represents a round robin scheduler.
Definition: Scheduler.h:32
void unregisterFunction(const Callback &callback)
Unregisters a scheduler function.
Scheduler()
Creates a new scheduler.
virtual void threadRun()
Derived thread run function.
Callbacks callbacks
Scheduler callbacks.
Definition: Scheduler.h:87
virtual ~Scheduler()
Destructs a scheduler.
ConcurrentCallbacks< Callback > Callbacks
Definition of a callback pair holding a callback function and an active flag.
Definition: Scheduler.h:47
Ocean::Callback< void > Callback
Definition of a callback function.
Definition: Scheduler.h:40
void registerFunction(const Callback &callback)
Registers a scheduler function.
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
This class implements a thread.
Definition: Thread.h:115
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15