• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
HeapTask(uint64_t target_run_time)32   explicit HeapTask(uint64_t target_run_time) : target_run_time_(target_run_time) {
33   }
GetTargetRunTime()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_.
SetTargetRunTime(uint64_t new_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   DISALLOW_IMPLICIT_CONSTRUCTORS(HeapTask);
50 };
51 
52 // Used to process GC tasks (heap trim, heap transitions, concurrent GC).
53 class TaskProcessor {
54  public:
55   TaskProcessor();
56   virtual ~TaskProcessor();
57   void AddTask(Thread* self, HeapTask* task) REQUIRES(!*lock_);
58   HeapTask* GetTask(Thread* self) REQUIRES(!*lock_);
59   void Start(Thread* self) REQUIRES(!*lock_);
60   // Stop tells the RunAllTasks to finish up the remaining tasks as soon as
61   // possible then return.
62   void Stop(Thread* self) REQUIRES(!*lock_);
63   void RunAllTasks(Thread* self) REQUIRES(!*lock_);
64   bool IsRunning() const REQUIRES(!*lock_);
65   void UpdateTargetRunTime(Thread* self, HeapTask* target_time, uint64_t new_target_time)
66       REQUIRES(!*lock_);
67   Thread* GetRunningThread() const REQUIRES(!*lock_);
68 
69  private:
70   class CompareByTargetRunTime {
71    public:
operator()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_);
81   Thread* running_thread_ GUARDED_BY(lock_);
82 
83   DISALLOW_COPY_AND_ASSIGN(TaskProcessor);
84 };
85 
86 }  // namespace gc
87 }  // namespace art
88 
89 #endif  // ART_RUNTIME_GC_TASK_PROCESSOR_H_
90