Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 27 | namespace art { |
| 28 | namespace gc { |
| 29 | |
| 30 | class 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; |
| 49 | }; |
| 50 | |
| 51 | // Used to process GC tasks (heap trim, heap transitions, concurrent GC). |
| 52 | class TaskProcessor { |
| 53 | public: |
| 54 | TaskProcessor(); |
| 55 | virtual ~TaskProcessor(); |
| 56 | void AddTask(Thread* self, HeapTask* task) LOCKS_EXCLUDED(lock_); |
| 57 | HeapTask* GetTask(Thread* self) LOCKS_EXCLUDED(lock_); |
| 58 | void Start(Thread* self) LOCKS_EXCLUDED(lock_); |
| 59 | // Stop tells the RunAllTasks to finish up the remaining tasks as soon as |
| 60 | // possible then return. |
| 61 | void Stop(Thread* self) LOCKS_EXCLUDED(lock_); |
| 62 | void RunAllTasks(Thread* self) LOCKS_EXCLUDED(lock_); |
| 63 | bool IsRunning() const LOCKS_EXCLUDED(lock_); |
| 64 | void UpdateTargetRunTime(Thread* self, HeapTask* target_time, uint64_t new_target_time) |
| 65 | LOCKS_EXCLUDED(lock_); |
| 66 | |
| 67 | private: |
| 68 | class CompareByTargetRunTime { |
| 69 | public: |
| 70 | bool operator()(const HeapTask* a, const HeapTask* b) const { |
| 71 | return a->GetTargetRunTime() < b->GetTargetRunTime(); |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | mutable Mutex* lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 76 | bool is_running_ GUARDED_BY(lock_); |
| 77 | std::unique_ptr<ConditionVariable> cond_ GUARDED_BY(lock_); |
| 78 | std::multiset<HeapTask*, CompareByTargetRunTime> tasks_ GUARDED_BY(lock_); |
| 79 | }; |
| 80 | |
| 81 | } // namespace gc |
| 82 | } // namespace art |
| 83 | |
| 84 | #endif // ART_RUNTIME_GC_TASK_PROCESSOR_H_ |