1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 1993-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 /******************************* DisplayPort********************************\
25 *                                                                           *
26 * Module: dp_timer.h                                                        *
27 *    Local timer interface                                                  *
28 *                                                                           *
29 \***************************************************************************/
30 #ifndef INCLUDED_DP_TIMER_H
31 #define INCLUDED_DP_TIMER_H
32 
33 #include "dp_list.h"
34 
35 namespace DisplayPort
36 {
37     //
38     //  RawTimer
39     //      This API is expected to be implemented by the
40     //      library client.
41     //
42     class RawTimer : virtual public Object
43     {
44     public:
45         struct Callback : virtual public Object
46         {
47             virtual void expired() = 0;
48         };
49         virtual void queueCallback(Callback * callback, int milliseconds) = 0;
50         virtual NvU64 getTimeUs() = 0;
51         virtual void sleep(int milliseconds) = 0;
52     };
53 
54 
55     //
56     //  Timer
57     //
58     class Timer : public RawTimer::Callback
59     {
60     public:
61         struct TimerCallback
62         {
63             virtual void expired(const  void * context) = 0;
64         };
65 
66     private:
67         RawTimer * raw;
68         List       pending;
69         struct PendingCallback : ListElement
70         {
71             TimerCallback *    target;
72             const void *       context;
73             NvU64              timestamp; // in usec
74             bool               executeInSleep;
75 
76         };
77 
78         virtual void expired();
79         unsigned fire(bool fromSleep);
80 
81         void _pump(unsigned milliseconds, bool fromSleep);
82     public:
Timer(RawTimer * raw)83         Timer(RawTimer * raw) : raw(raw) {}
~Timer()84         virtual ~Timer() {}
85 
86         //
87         //  Queue a timer callback.
88         //      Unless the dont-execute-in-sleep flag is
89         //
90         void queueCallback(Timer::TimerCallback * target, const  void * context, unsigned milliseconds, bool executeInSleep = true);
91         NvU64 getTimeUs();
92         void sleep(unsigned milliseconds);
93         void cancelCallbacks(Timer::TimerCallback * to);
94 
95         void cancelCallback(Timer::TimerCallback * to, const void * context);
96         void queueCallbackInOrder(Timer::TimerCallback * target, const  void * context, unsigned milliseconds, bool executeInSleep);
97         void cancelCallbacksWithoutContext(const  void * context);
98         void cancelAllCallbacks();
99         bool checkCallbacksOfSameContext(const void * context);
100     };
101 }
102 
103 #endif //INCLUDED_DP_TIMER_H
104