blob: 2afc35cc26ac460c2a486dc60e27782b7a38f0d4 [file] [log] [blame]
Przemyslaw Szczepaniake03afaa2015-12-16 14:15:51 +00001/*
2 * Copyright (C) 2010 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#ifndef ASYNCHRONOUS_CLOSE_MONITOR_H_included
18#define ASYNCHRONOUS_CLOSE_MONITOR_H_included
19
Przemyslaw Szczepaniake03afaa2015-12-16 14:15:51 +000020#include <pthread.h>
21
22/**
23 * AsynchronousCloseMonitor helps implement Java's asynchronous close semantics.
24 *
25 * AsynchronousCloseMonitor::init must be called before anything else.
26 *
27 * Every blocking I/O operation must be surrounded by an AsynchronousCloseMonitor
28 * instance. For example:
29 *
30 * {
31 * AsynchronousCloseMonitor monitor(fd);
32 * byteCount = ::read(fd, buf, sizeof(buf));
33 * }
34 *
35 * To interrupt all threads currently blocked on file descriptor 'fd', call signalBlockedThreads:
36 *
37 * AsynchronousCloseMonitor::signalBlockedThreads(fd);
38 *
39 * To test to see if the interruption was due to the signalBlockedThreads call:
40 *
41 * monitor.wasSignaled();
42 */
43class AsynchronousCloseMonitor {
44public:
Chih-Hung Hsiehb493dac2016-06-30 14:50:40 -070045 explicit AsynchronousCloseMonitor(int fd);
Przemyslaw Szczepaniake03afaa2015-12-16 14:15:51 +000046 ~AsynchronousCloseMonitor();
47 bool wasSignaled() const;
48
49 static void init();
50
51 static void signalBlockedThreads(int fd);
52
53private:
54 AsynchronousCloseMonitor* mPrev;
55 AsynchronousCloseMonitor* mNext;
56 pthread_t mThread;
57 int mFd;
58 bool mSignaled;
59
60 // Disallow copy and assignment.
61 AsynchronousCloseMonitor(const AsynchronousCloseMonitor&);
62 void operator=(const AsynchronousCloseMonitor&);
63};
64
65#endif // ASYNCHRONOUS_CLOSE_MONITOR_H_included