• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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_SOCKET_CLOSE_MONITOR_H_included
18 #define ASYNCHRONOUS_SOCKET_CLOSE_MONITOR_H_included
19 
20 #include "ScopedPthreadMutexLock.h"
21 #include <pthread.h>
22 
23 /**
24  * AsynchronousSocketCloseMonitor helps implement Java's asynchronous Socket.close semantics.
25  *
26  * AsynchronousSocketCloseMonitor::init must be called before anything else.
27  *
28  * Every blocking network I/O operation must be surrounded by an AsynchronousSocketCloseMonitor
29  * instance. For example:
30  *
31  *   {
32  *     AsynchronousSocketCloseMonitor monitor(fd);
33  *     byteCount = ::read(fd, buf, sizeof(buf));
34  *   }
35  *
36  * To interrupt all threads currently blocked on file descriptor 'fd', call signalBlockedThreads:
37  *
38  *   AsynchronousSocketCloseMonitor::signalBlockedThreads(fd);
39  */
40 class AsynchronousSocketCloseMonitor {
41 public:
42     AsynchronousSocketCloseMonitor(int fd);
43     ~AsynchronousSocketCloseMonitor();
44 
45     static void init();
46 
47     static void signalBlockedThreads(int fd);
48 
49 private:
50     AsynchronousSocketCloseMonitor* mPrev;
51     AsynchronousSocketCloseMonitor* mNext;
52     pthread_t mThread;
53     int mFd;
54 
55     // Disallow copy and assignment.
56     AsynchronousSocketCloseMonitor(const AsynchronousSocketCloseMonitor&);
57     void operator=(const AsynchronousSocketCloseMonitor&);
58 };
59 
60 #endif  // ASYNCHRONOUS_SOCKET_CLOSE_MONITOR_H_included
61