blob: e40fa063191b41fc77630a4dc3a7cd99be252c04 [file] [log] [blame]
Mathieu Chartiera5eae692014-12-17 17:56:03 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_RUNTIME_GC_TASK_PROCESSOR_H_
18#define ART_RUNTIME_GC_TASK_PROCESSOR_H_
19
20#include <memory>
21#include <set>
22
23#include "base/mutex.h"
24#include "globals.h"
25#include "thread_pool.h"
26
27namespace art {
28namespace gc {
29
30class HeapTask : public SelfDeletingTask {
31 public:
32 explicit HeapTask(uint64_t target_run_time) : target_run_time_(target_run_time) {
33 }
34 uint64_t GetTargetRunTime() const {
35 return target_run_time_;
36 }
37
38 private:
39 // Update the updated_target_run_time_, the task processor will re-insert the task when it is
40 // popped and update the target_run_time_.
41 void SetTargetRunTime(uint64_t new_target_run_time) {
42 target_run_time_ = new_target_run_time;
43 }
44
45 // Time in ns at which we want the task to run.
46 uint64_t target_run_time_;
47
48 friend class TaskProcessor;
Mathieu Chartier3130cdf2015-05-03 15:20:23 -070049 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapTask);
Mathieu Chartiera5eae692014-12-17 17:56:03 -080050};
51
52// Used to process GC tasks (heap trim, heap transitions, concurrent GC).
53class TaskProcessor {
54 public:
55 TaskProcessor();
56 virtual ~TaskProcessor();
Mathieu Chartier90443472015-07-16 20:32:27 -070057 void AddTask(Thread* self, HeapTask* task) REQUIRES(!*lock_);
58 HeapTask* GetTask(Thread* self) REQUIRES(!*lock_);
59 void Start(Thread* self) REQUIRES(!*lock_);
Mathieu Chartiera5eae692014-12-17 17:56:03 -080060 // Stop tells the RunAllTasks to finish up the remaining tasks as soon as
61 // possible then return.
Mathieu Chartier90443472015-07-16 20:32:27 -070062 void Stop(Thread* self) REQUIRES(!*lock_);
63 void RunAllTasks(Thread* self) REQUIRES(!*lock_);
64 bool IsRunning() const REQUIRES(!*lock_);
Mathieu Chartiera5eae692014-12-17 17:56:03 -080065 void UpdateTargetRunTime(Thread* self, HeapTask* target_time, uint64_t new_target_time)
Mathieu Chartier90443472015-07-16 20:32:27 -070066 REQUIRES(!*lock_);
67 Thread* GetRunningThread() const REQUIRES(!*lock_);
Mathieu Chartiera5eae692014-12-17 17:56:03 -080068
69 private:
70 class CompareByTargetRunTime {
71 public:
72 bool operator()(const HeapTask* a, const HeapTask* b) const {
73 return a->GetTargetRunTime() < b->GetTargetRunTime();
74 }
75 };
76
77 mutable Mutex* lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
78 bool is_running_ GUARDED_BY(lock_);
79 std::unique_ptr<ConditionVariable> cond_ GUARDED_BY(lock_);
80 std::multiset<HeapTask*, CompareByTargetRunTime> tasks_ GUARDED_BY(lock_);
Hiroshi Yamauchia1c9f012015-04-02 10:18:12 -070081 Thread* running_thread_ GUARDED_BY(lock_);
Mathieu Chartier3130cdf2015-05-03 15:20:23 -070082
83 DISALLOW_COPY_AND_ASSIGN(TaskProcessor);
Mathieu Chartiera5eae692014-12-17 17:56:03 -080084};
85
86} // namespace gc
87} // namespace art
88
89#endif // ART_RUNTIME_GC_TASK_PROCESSOR_H_