Primiano Tucci | ce72002 | 2017-10-30 12:50:06 +0000 | [diff] [blame] | 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 | |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 17 | #ifndef PERFETTO_BASE_TASK_RUNNER_H_ |
| 18 | #define PERFETTO_BASE_TASK_RUNNER_H_ |
Primiano Tucci | ce72002 | 2017-10-30 12:50:06 +0000 | [diff] [blame] | 19 | |
| 20 | #include <functional> |
| 21 | |
| 22 | namespace perfetto { |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 23 | namespace base { |
Primiano Tucci | ce72002 | 2017-10-30 12:50:06 +0000 | [diff] [blame] | 24 | |
| 25 | // A generic interface to allow the library clients to interleave the execution |
| 26 | // of the tracing internals in their runtime environment. |
| 27 | // The expectation is that all tasks, which are queued either via PostTask() or |
| 28 | // AddFileDescriptorWatch(), are executed on the same sequence (either on the |
| 29 | // same thread, or on a thread pool that gives sequencing guarantees). |
Sami Kyostila | 2c6c2f5 | 2017-11-21 16:08:16 +0000 | [diff] [blame^] | 30 | // |
| 31 | // Tasks are never executed synchronously inside PostTask and there is a full |
| 32 | // memory barrier between tasks. |
| 33 | // |
| 34 | // All methods of this interface can be called from any thread. |
Primiano Tucci | ce72002 | 2017-10-30 12:50:06 +0000 | [diff] [blame] | 35 | class TaskRunner { |
| 36 | public: |
| 37 | virtual ~TaskRunner() = default; |
| 38 | |
Sami Kyostila | 2c6c2f5 | 2017-11-21 16:08:16 +0000 | [diff] [blame^] | 39 | // Schedule a task for immediate execution. Immediate tasks are always |
| 40 | // executed in the order they are posted. |
Primiano Tucci | ce72002 | 2017-10-30 12:50:06 +0000 | [diff] [blame] | 41 | virtual void PostTask(std::function<void()>) = 0; |
Sami Kyostila | 2c6c2f5 | 2017-11-21 16:08:16 +0000 | [diff] [blame^] | 42 | |
| 43 | // Schedule a task for execution after |delay_ms|. Note that there is no |
| 44 | // strict ordering guarantee between immediate and delayed tasks. |
| 45 | virtual void PostDelayedTask(std::function<void()>, int delay_ms) = 0; |
| 46 | |
| 47 | // Schedule a task to run when |fd| becomes readable. The same |fd| can only |
| 48 | // be monitored by one function. Note that this function only needs to be |
| 49 | // implemented on platforms where the built-in ipc framework is used. |
| 50 | // TODO(skyostil): Refactor this out of the shared interface. |
Primiano Tucci | ce72002 | 2017-10-30 12:50:06 +0000 | [diff] [blame] | 51 | virtual void AddFileDescriptorWatch(int fd, std::function<void()>) = 0; |
Sami Kyostila | 2c6c2f5 | 2017-11-21 16:08:16 +0000 | [diff] [blame^] | 52 | |
| 53 | // Remove a previously scheduled watch for |fd|. If this is run on the target |
| 54 | // thread of this TaskRunner, guarantees that the task registered to this fd |
| 55 | // will not be executed after this function call. |
Primiano Tucci | ce72002 | 2017-10-30 12:50:06 +0000 | [diff] [blame] | 56 | virtual void RemoveFileDescriptorWatch(int fd) = 0; |
| 57 | }; |
| 58 | |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 59 | } // namespace base |
Primiano Tucci | ce72002 | 2017-10-30 12:50:06 +0000 | [diff] [blame] | 60 | } // namespace perfetto |
| 61 | |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 62 | #endif // PERFETTO_BASE_TASK_RUNNER_H_ |