blob: 82bb0a224952e0cbd691cf781b095bf61762be50 [file] [log] [blame]
Josh Gao95068bb2019-07-08 15:23:17 -07001/*
2 * Copyright 2006, Brian Swetland <swetland@frotz.net>
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 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080016
Yabin Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG FDEVENT
JP Abgrall408fa572011-03-16 15:57:42 -070018
Dan Albert33134262015-03-19 15:21:08 -070019#include "sysdeps.h"
Dan Albert33134262015-03-19 15:21:08 -070020
Josh Gaoded557f2018-06-18 14:55:27 -070021#include <inttypes.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022
Elliott Hughes4f713192015-12-04 22:00:26 -080023#include <android-base/stringprintf.h>
Yabin Cuia1080162015-09-04 16:19:56 -070024
Josh Gao95068bb2019-07-08 15:23:17 -070025#include "fdevent.h"
26#include "fdevent_poll.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027
Josh Gao95068bb2019-07-08 15:23:17 -070028std::string dump_fde(const fdevent* fde) {
Yabin Cuia1080162015-09-04 16:19:56 -070029 std::string state;
30 if (fde->state & FDE_ACTIVE) {
31 state += "A";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032 }
Yabin Cuia1080162015-09-04 16:19:56 -070033 if (fde->state & FDE_PENDING) {
34 state += "P";
35 }
36 if (fde->state & FDE_CREATED) {
37 state += "C";
38 }
39 if (fde->state & FDE_READ) {
40 state += "R";
41 }
42 if (fde->state & FDE_WRITE) {
43 state += "W";
44 }
45 if (fde->state & FDE_ERROR) {
46 state += "E";
47 }
Josh Gaoded557f2018-06-18 14:55:27 -070048 return android::base::StringPrintf("(fdevent %" PRIu64 ": fd %d %s)", fde->id, fde->fd.get(),
49 state.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050}
51
Josh Gaoc2cf1212019-06-28 16:34:36 -070052static fdevent_context* g_ambient_fdevent_context = new fdevent_context_poll();
53
54static fdevent_context* fdevent_get_ambient() {
55 return g_ambient_fdevent_context;
56}
57
Josh Gaoc2cf1212019-06-28 16:34:36 -070058fdevent* fdevent_create(int fd, fd_func func, void* arg) {
59 unique_fd ufd(fd);
60 return fdevent_get_ambient()->Create(std::move(ufd), func, arg);
61}
62
63fdevent* fdevent_create(int fd, fd_func2 func, void* arg) {
64 unique_fd ufd(fd);
65 return fdevent_get_ambient()->Create(std::move(ufd), func, arg);
66}
67
68unique_fd fdevent_release(fdevent* fde) {
69 return fdevent_get_ambient()->Destroy(fde);
70}
71
72void fdevent_destroy(fdevent* fde) {
73 fdevent_get_ambient()->Destroy(fde);
74}
75
76void fdevent_set(fdevent* fde, unsigned events) {
77 fdevent_get_ambient()->Set(fde, events);
78}
79
80void fdevent_add(fdevent* fde, unsigned events) {
81 fdevent_get_ambient()->Add(fde, events);
82}
83
84void fdevent_del(fdevent* fde, unsigned events) {
85 fdevent_get_ambient()->Del(fde, events);
86}
87
88void fdevent_set_timeout(fdevent* fde, std::optional<std::chrono::milliseconds> timeout) {
89 fdevent_get_ambient()->SetTimeout(fde, timeout);
90}
91
92void fdevent_run_on_main_thread(std::function<void()> fn) {
93 fdevent_get_ambient()->Run(std::move(fn));
94}
95
96void fdevent_loop() {
97 fdevent_get_ambient()->Loop();
98}
99
100void check_main_thread() {
101 fdevent_get_ambient()->CheckMainThread();
102}
103
Josh Gao022d4472016-02-10 14:49:00 -0800104void fdevent_terminate_loop() {
Josh Gaoc2cf1212019-06-28 16:34:36 -0700105 fdevent_get_ambient()->TerminateLoop();
Josh Gao022d4472016-02-10 14:49:00 -0800106}
107
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700108size_t fdevent_installed_count() {
Josh Gaoc2cf1212019-06-28 16:34:36 -0700109 return fdevent_get_ambient()->InstalledCount();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700110}
111
112void fdevent_reset() {
Josh Gaoc2cf1212019-06-28 16:34:36 -0700113 return fdevent_get_ambient()->Reset();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700114}