1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_VCL_TASK_HXX
21 #define INCLUDED_VCL_TASK_HXX
23 #include <vcl/dllapi.h>
25 struct ImplSchedulerData
;
27 enum class TaskPriority
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
40 #define PRIO_COUNT (static_cast<int>(TaskPriority::LOWEST) + 1)
42 class VCL_DLLPUBLIC Task
44 friend class Scheduler
;
45 friend struct ImplSchedulerData
;
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
54 static void StartTimer( sal_uInt64 nMS
);
56 const ImplSchedulerData
* GetSchedulerData() const { return mpSchedulerData
; }
58 virtual void SetDeletionFlags();
61 * How long (in MS) until the Task is ready to be dispatched?
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.
68 * @param nTimeNow the current time
69 * @return the sleep time of the Task to become ready
71 virtual sal_uInt64
UpdateMinPeriod( sal_uInt64 nTimeNow
) const = 0;
74 Task( const char *pDebugName
);
75 Task( const Task
& rTask
);
76 virtual ~Task() COVERITY_NOEXCEPT_FALSE
;
77 Task
& operator=( const Task
& rTask
);
79 void SetPriority(TaskPriority ePriority
);
80 TaskPriority
GetPriority() const { return mePriority
; }
82 const char *GetDebugName() const { return mpDebugName
; }
85 virtual void Invoke() = 0;
88 * Schedules the task for execution
90 * If the timer is already active, it's reset!
91 * Check with Task::IsActive() to prevent reset.
93 * If you unset bStartTimer, the Task must call Task::StartTimer(...) to be correctly scheduled!
94 * Otherwise it might just be picked up when the Scheduler runs the next time.
96 * @param bStartTimer if false, don't schedule the Task by calling Task::StartTimer(0).
98 virtual void Start(bool bStartTimer
= true);
101 bool IsActive() const { return mbActive
; }
104 * This function must be called for static tasks, so the Task destructor
105 * ignores the scheduler mutex, as it may not be available anymore.
106 * The cleanup is still correct, as it has already happened in
107 * DeInitScheduler call well before the static destructor calls.
109 void SetStatic() { mbStatic
= true; }
110 bool IsStatic() const { return mbStatic
; }
113 #endif // INCLUDED_VCL_TASK_HXX
115 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */