• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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 #define LOG_TAG "LibAAH_RTP"
18 #include <utils/Log.h>
19 
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <poll.h>
23 #include <unistd.h>
24 
25 #include "pipe_event.h"
26 
27 namespace android {
28 
PipeEvent()29 PipeEvent::PipeEvent() {
30     pipe_[0] = -1;
31     pipe_[1] = -1;
32 
33     // Create the pipe.
34     if (pipe(pipe_) >= 0) {
35         // Set non-blocking mode on the read side of the pipe so we can
36         // easily drain it whenever we wakeup.
37         fcntl(pipe_[0], F_SETFL, O_NONBLOCK);
38     } else {
39         ALOGE("Failed to create pipe event %d %d %d",
40               pipe_[0], pipe_[1], errno);
41         pipe_[0] = -1;
42         pipe_[1] = -1;
43     }
44 }
45 
~PipeEvent()46 PipeEvent::~PipeEvent() {
47     if (pipe_[0] >= 0) {
48         close(pipe_[0]);
49     }
50 
51     if (pipe_[1] >= 0) {
52         close(pipe_[1]);
53     }
54 }
55 
clearPendingEvents()56 void PipeEvent::clearPendingEvents() {
57     char drain_buffer[16];
58     while (read(pipe_[0], drain_buffer, sizeof(drain_buffer)) > 0) {
59         // No body.
60     }
61 }
62 
wait(int timeout)63 bool PipeEvent::wait(int timeout) {
64     struct pollfd wait_fd;
65 
66     wait_fd.fd = getWakeupHandle();
67     wait_fd.events = POLLIN;
68     wait_fd.revents = 0;
69 
70     int res = poll(&wait_fd, 1, timeout);
71 
72     if (res < 0) {
73         ALOGE("Wait error in PipeEvent; sleeping to prevent overload!");
74         usleep(1000);
75     }
76 
77     return (res > 0);
78 }
79 
setEvent()80 void PipeEvent::setEvent() {
81     char foo = 'q';
82     write(pipe_[1], &foo, 1);
83 }
84 
85 }  // namespace android
86 
87