Home | History | Annotate | Download | only in base
      1 /*
      2  * Copyright (C) 2017 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 INCLUDE_PERFETTO_BASE_ANDROID_TASK_RUNNER_H_
     18 #define INCLUDE_PERFETTO_BASE_ANDROID_TASK_RUNNER_H_
     19 
     20 #include "perfetto/base/scoped_file.h"
     21 #include "perfetto/base/task_runner.h"
     22 #include "perfetto/base/thread_checker.h"
     23 #include "perfetto/base/time.h"
     24 
     25 #include <poll.h>
     26 #include <chrono>
     27 #include <map>
     28 #include <mutex>
     29 #include <queue>
     30 
     31 #include <android/looper.h>
     32 
     33 namespace perfetto {
     34 namespace base {
     35 
     36 // Runs a task runner on a thread owned by an Android Looper (ALooper).
     37 class AndroidTaskRunner : public TaskRunner {
     38  public:
     39   AndroidTaskRunner();
     40   ~AndroidTaskRunner() override;
     41 
     42   // The following methods are only used in cases where the caller wants to take
     43   // ownership of the current thread (e.g., tests and standalone tools).
     44   // Normally the Android Framework runs the event loop on the thread. Run() can
     45   // only be called from the main thread but Quit() can be called from any
     46   // thread.
     47   void Run();
     48   void Quit();
     49 
     50   // Checks whether there are any pending immediate tasks to run. Note that
     51   // delayed tasks don't count even if they are due to run. Can only be called
     52   // from the main thread.
     53   bool IsIdleForTesting();
     54 
     55   // TaskRunner implementation:
     56   void PostTask(std::function<void()>) override;
     57   void PostDelayedTask(std::function<void()>, uint32_t delay_ms) override;
     58   void AddFileDescriptorWatch(int fd, std::function<void()>) override;
     59   void RemoveFileDescriptorWatch(int fd) override;
     60 
     61  private:
     62   bool OnFileDescriptorEvent(int signalled_fd, int events);
     63   void RunImmediateTask();
     64   void RunDelayedTask();
     65 
     66   void GetNextDelayedTaskRunTimeLocked(struct itimerspec* runtime);
     67 
     68   void ScheduleImmediateWakeUp();
     69   void ScheduleDelayedWakeUp(TimeMillis time);
     70 
     71   ALooper* const looper_;
     72   ScopedFile immediate_event_;
     73   ScopedFile delayed_timer_;
     74 
     75   ThreadChecker thread_checker_;
     76 
     77   // --- Begin lock-protected members.
     78   std::mutex lock_;
     79   // Note: std::deque allocates blocks of 4k in some implementations. Consider
     80   // another data structure if we end up having many task runner instances.
     81   std::deque<std::function<void()>> immediate_tasks_;
     82   std::multimap<TimeMillis, std::function<void()>> delayed_tasks_;
     83   std::map<int, std::function<void()>> watch_tasks_;
     84   bool quit_ = false;
     85   // --- End lock-protected members.
     86 };
     87 
     88 }  // namespace base
     89 }  // namespace perfetto
     90 
     91 #endif  // INCLUDE_PERFETTO_BASE_ANDROID_TASK_RUNNER_H_
     92