adb: add fdevent_release.

Implement a version of fdevent_destroy that returns the file descriptor
previously owned by the fdevent, instead of closing it.

Bug: http://b/74616284
Test: treehugger
Change-Id: I6a14ac96e6b8c801bc71bfcc9094851c158510ae
This commit is contained in:
Josh Gao 2018-09-20 17:38:55 -07:00
parent ed17650438
commit 2f6c2fa935
2 changed files with 18 additions and 7 deletions

View file

@ -147,24 +147,34 @@ fdevent* fdevent_create(int fd, fd_func func, void* arg) {
return fde;
}
void fdevent_destroy(fdevent* fde) {
unique_fd fdevent_release(fdevent* fde) {
check_main_thread();
if (fde == nullptr) return;
if (!fde) {
return {};
}
if (!(fde->state & FDE_CREATED)) {
LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde);
}
unique_fd result = std::move(fde->fd);
if (fde->state & FDE_ACTIVE) {
g_poll_node_map.erase(fde->fd.get());
g_poll_node_map.erase(result.get());
if (fde->state & FDE_PENDING) {
g_pending_list.remove(fde);
}
fde->fd.reset();
fde->state = 0;
fde->events = 0;
}
delete fde;
return result;
}
void fdevent_destroy(fdevent* fde) {
// Release, and then let unique_fd's destructor cleanup.
fdevent_release(fde);
}
static void fdevent_update(fdevent* fde, unsigned events) {

View file

@ -50,11 +50,12 @@ struct fdevent {
*/
fdevent *fdevent_create(int fd, fd_func func, void *arg);
/* Uninitialize and deallocate an fdevent object that was
** created by fdevent_create()
*/
// Deallocate an fdevent object that was created by fdevent_create.
void fdevent_destroy(fdevent *fde);
// fdevent_destroy, except releasing the file descriptor previously owned by the fdevent.
unique_fd fdevent_release(fdevent* fde);
/* Change which events should cause notifications
*/
void fdevent_set(fdevent *fde, unsigned events);