blob: 1575a8cfdad26452f12a5875dad85bbb88c52701 [file] [log] [blame]
Primiano Tuccice720022017-10-30 12:50:06 +00001/*
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 Tuccid7d1be02017-10-30 17:41:34 +000017#ifndef PERFETTO_BASE_TASK_RUNNER_H_
18#define PERFETTO_BASE_TASK_RUNNER_H_
Primiano Tuccice720022017-10-30 12:50:06 +000019
20#include <functional>
21
22namespace perfetto {
Primiano Tuccid7d1be02017-10-30 17:41:34 +000023namespace base {
Primiano Tuccice720022017-10-30 12:50:06 +000024
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 Kyostila2c6c2f52017-11-21 16:08:16 +000030//
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 Tuccice720022017-10-30 12:50:06 +000035class TaskRunner {
36 public:
37 virtual ~TaskRunner() = default;
38
Sami Kyostila2c6c2f52017-11-21 16:08:16 +000039 // Schedule a task for immediate execution. Immediate tasks are always
Sami Kyostila7ae34482017-11-28 13:17:07 +000040 // executed in the order they are posted. Can be called from any thread.
Primiano Tuccice720022017-10-30 12:50:06 +000041 virtual void PostTask(std::function<void()>) = 0;
Sami Kyostila2c6c2f52017-11-21 16:08:16 +000042
43 // Schedule a task for execution after |delay_ms|. Note that there is no
Sami Kyostila7ae34482017-11-28 13:17:07 +000044 // strict ordering guarantee between immediate and delayed tasks. Can be
45 // called from any thread.
Sami Kyostila2c6c2f52017-11-21 16:08:16 +000046 virtual void PostDelayedTask(std::function<void()>, int delay_ms) = 0;
47
48 // Schedule a task to run when |fd| becomes readable. The same |fd| can only
49 // be monitored by one function. Note that this function only needs to be
Sami Kyostila7ae34482017-11-28 13:17:07 +000050 // implemented on platforms where the built-in ipc framework is used. Can be
51 // called from any thread.
Sami Kyostila2c6c2f52017-11-21 16:08:16 +000052 // TODO(skyostil): Refactor this out of the shared interface.
Primiano Tuccice720022017-10-30 12:50:06 +000053 virtual void AddFileDescriptorWatch(int fd, std::function<void()>) = 0;
Sami Kyostila2c6c2f52017-11-21 16:08:16 +000054
55 // Remove a previously scheduled watch for |fd|. If this is run on the target
56 // thread of this TaskRunner, guarantees that the task registered to this fd
Sami Kyostila7ae34482017-11-28 13:17:07 +000057 // will not be executed after this function call. Can be called from any
58 // thread.
Primiano Tuccice720022017-10-30 12:50:06 +000059 virtual void RemoveFileDescriptorWatch(int fd) = 0;
60};
61
Primiano Tuccid7d1be02017-10-30 17:41:34 +000062} // namespace base
Primiano Tuccice720022017-10-30 12:50:06 +000063} // namespace perfetto
64
Primiano Tuccid7d1be02017-10-30 17:41:34 +000065#endif // PERFETTO_BASE_TASK_RUNNER_H_