1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_VCL_TASK_HXX
21 #define INCLUDED_VCL_TASK_HXX
22 
23 #include <vcl/dllapi.h>
24 
25 struct ImplSchedulerData;
26 
27 enum class TaskPriority
28 {
29     HIGHEST,       ///< These events should run very fast!
30     DEFAULT,       ///< Default priority used, e.g. the default timer priority
31     // Input from the OS event queue is processed before HIGH_IDLE tasks.
32     HIGH_IDLE,     ///< Important idle events to be run before processing drawing events
33     RESIZE,        ///< Resize runs before repaint, so we won't paint twice
34     REPAINT,       ///< All repaint events should go in here
35     POST_PAINT,    ///< Everything running directly after painting
36     DEFAULT_IDLE,  ///< Default idle priority
37     LOWEST         ///< Low, very idle cleanup tasks
38 };
39 
40 #define PRIO_COUNT (static_cast<int>(TaskPriority::LOWEST) + 1)
41 
42 class VCL_DLLPUBLIC Task
43 {
44     friend class Scheduler;
45     friend struct ImplSchedulerData;
46 
47     ImplSchedulerData *mpSchedulerData; ///< Pointer to the element in scheduler list
48     const char        *mpDebugName;     ///< Useful for debugging
49     TaskPriority       mePriority;      ///< Task priority
50     bool               mbActive;        ///< Currently in the scheduler
51     bool               mbStatic;        ///< Is a static object
52 
53 protected:
54     static void StartTimer( sal_uInt64 nMS );
55 
GetSchedulerData() const56     const ImplSchedulerData* GetSchedulerData() const { return mpSchedulerData; }
57 
58     virtual void SetDeletionFlags();
59 
60     /**
61      * How long (in MS) until the Task is ready to be dispatched?
62      *
63      * Simply return Scheduler::ImmediateTimeoutMs if you're ready, like an
64      * Idle. If you have to return Scheduler::InfiniteTimeoutMs, you probably
65      * need another mechanism to wake up the Scheduler or rely on other
66      * Tasks to be scheduled, or simply use a polling Timer.
67      *
68      * @param nTimeNow the current time
69      * @return the sleep time of the Task to become ready
70      */
71     virtual sal_uInt64 UpdateMinPeriod( sal_uInt64 nTimeNow ) const = 0;
72 
73 public:
74     Task( const char *pDebugName );
75     Task( const Task& rTask );
76     virtual ~Task() COVERITY_NOEXCEPT_FALSE;
77     Task& operator=( const Task& rTask );
78 
79     void            SetPriority(TaskPriority ePriority);
GetPriority() const80     TaskPriority    GetPriority() const { return mePriority; }
81 
SetDebugName(const char * pDebugName)82     void            SetDebugName( const char *pDebugName ) { mpDebugName = pDebugName; }
GetDebugName() const83     const char     *GetDebugName() const { return mpDebugName; }
84 
85     // Call handler
86     virtual void    Invoke() = 0;
87 
88     virtual void    Start();
89     void            Stop();
90 
IsActive() const91     bool            IsActive() const { return mbActive; }
92 
93     /**
94      * This function must be called for static tasks, so the Task destructor
95      * ignores the scheduler mutex, as it may not be available anymore.
96      * The cleanup is still correct, as it has already happened in
97      * DeInitScheduler call well before the static destructor calls.
98      */
SetStatic()99     void            SetStatic() { mbStatic = true; }
IsStatic() const100     bool            IsStatic() const { return mbStatic; }
101 };
102 
103 #endif // INCLUDED_VCL_TASK_HXX
104 
105 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
106