ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | #ifndef _THRIFT_TCONCURRENTCLIENTSYNCINFO_H_ |
| 20 | #define _THRIFT_TCONCURRENTCLIENTSYNCINFO_H_ 1 |
| 21 | |
| 22 | #include <thrift/protocol/TProtocol.h> |
| 23 | #include <thrift/concurrency/Mutex.h> |
| 24 | #include <thrift/concurrency/Monitor.h> |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 25 | #include <memory> |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 26 | #include <vector> |
| 27 | #include <string> |
| 28 | #include <map> |
| 29 | |
Konrad Grochowski | 7f4be5f | 2015-11-05 20:23:11 +0100 | [diff] [blame] | 30 | namespace apache { |
| 31 | namespace thrift { |
| 32 | namespace async { |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 33 | |
| 34 | class TConcurrentClientSyncInfo; |
| 35 | |
Konrad Grochowski | 7f4be5f | 2015-11-05 20:23:11 +0100 | [diff] [blame] | 36 | class TConcurrentSendSentry { |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 37 | public: |
Konrad Grochowski | 7f4be5f | 2015-11-05 20:23:11 +0100 | [diff] [blame] | 38 | explicit TConcurrentSendSentry(TConcurrentClientSyncInfo* sync); |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 39 | ~TConcurrentSendSentry(); |
| 40 | |
| 41 | void commit(); |
Konrad Grochowski | 7f4be5f | 2015-11-05 20:23:11 +0100 | [diff] [blame] | 42 | |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 43 | private: |
Konrad Grochowski | 7f4be5f | 2015-11-05 20:23:11 +0100 | [diff] [blame] | 44 | TConcurrentClientSyncInfo& sync_; |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 45 | bool committed_; |
| 46 | }; |
| 47 | |
Konrad Grochowski | 7f4be5f | 2015-11-05 20:23:11 +0100 | [diff] [blame] | 48 | class TConcurrentRecvSentry { |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 49 | public: |
Konrad Grochowski | 7f4be5f | 2015-11-05 20:23:11 +0100 | [diff] [blame] | 50 | TConcurrentRecvSentry(TConcurrentClientSyncInfo* sync, int32_t seqid); |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 51 | ~TConcurrentRecvSentry(); |
| 52 | |
| 53 | void commit(); |
Konrad Grochowski | 7f4be5f | 2015-11-05 20:23:11 +0100 | [diff] [blame] | 54 | |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 55 | private: |
Konrad Grochowski | 7f4be5f | 2015-11-05 20:23:11 +0100 | [diff] [blame] | 56 | TConcurrentClientSyncInfo& sync_; |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 57 | int32_t seqid_; |
| 58 | bool committed_; |
| 59 | }; |
| 60 | |
Konrad Grochowski | 7f4be5f | 2015-11-05 20:23:11 +0100 | [diff] [blame] | 61 | class TConcurrentClientSyncInfo { |
| 62 | private: // typedefs |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 63 | typedef std::shared_ptr< ::apache::thrift::concurrency::Monitor> MonitorPtr; |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 64 | typedef std::map<int32_t, MonitorPtr> MonitorMap; |
Konrad Grochowski | 7f4be5f | 2015-11-05 20:23:11 +0100 | [diff] [blame] | 65 | |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 66 | public: |
| 67 | TConcurrentClientSyncInfo(); |
| 68 | |
| 69 | int32_t generateSeqId(); |
| 70 | |
Konrad Grochowski | 7f4be5f | 2015-11-05 20:23:11 +0100 | [diff] [blame] | 71 | bool getPending(std::string& fname, |
| 72 | ::apache::thrift::protocol::TMessageType& mtype, |
| 73 | int32_t& rseqid); /* requires readMutex_ */ |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 74 | |
Konrad Grochowski | 7f4be5f | 2015-11-05 20:23:11 +0100 | [diff] [blame] | 75 | void updatePending(const std::string& fname, |
| 76 | ::apache::thrift::protocol::TMessageType mtype, |
| 77 | int32_t rseqid); /* requires readMutex_ */ |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 78 | |
| 79 | void waitForWork(int32_t seqid); /* requires readMutex_ */ |
| 80 | |
Konrad Grochowski | 7f4be5f | 2015-11-05 20:23:11 +0100 | [diff] [blame] | 81 | ::apache::thrift::concurrency::Mutex& getReadMutex() { return readMutex_; } |
| 82 | ::apache::thrift::concurrency::Mutex& getWriteMutex() { return writeMutex_; } |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 83 | |
Konrad Grochowski | 7f4be5f | 2015-11-05 20:23:11 +0100 | [diff] [blame] | 84 | private: // constants |
| 85 | enum { MONITOR_CACHE_SIZE = 10 }; |
| 86 | |
| 87 | private: // functions |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 88 | MonitorPtr newMonitor_( |
Konrad Grochowski | 7f4be5f | 2015-11-05 20:23:11 +0100 | [diff] [blame] | 89 | const ::apache::thrift::concurrency::Guard& seqidGuard); /* requires seqidMutex_ */ |
| 90 | void deleteMonitor_(const ::apache::thrift::concurrency::Guard& seqidGuard, MonitorPtr& m); |
| 91 | /*noexcept*/ /* requires seqidMutex_ */ |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 92 | void wakeupAnyone_( |
Konrad Grochowski | 7f4be5f | 2015-11-05 20:23:11 +0100 | [diff] [blame] | 93 | const ::apache::thrift::concurrency::Guard& seqidGuard); /* requires seqidMutex_ */ |
| 94 | void markBad_(const ::apache::thrift::concurrency::Guard& seqidGuard); /* requires seqidMutex_ */ |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 95 | void throwBadSeqId_(); |
| 96 | void throwDeadConnection_(); |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 97 | |
Konrad Grochowski | 7f4be5f | 2015-11-05 20:23:11 +0100 | [diff] [blame] | 98 | private: // data members |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 99 | volatile bool stop_; |
| 100 | |
| 101 | ::apache::thrift::concurrency::Mutex seqidMutex_; |
| 102 | // begin seqidMutex_ protected members |
| 103 | int32_t nextseqid_; |
| 104 | MonitorMap seqidToMonitorMap_; |
| 105 | std::vector<MonitorPtr> freeMonitors_; |
| 106 | // end seqidMutex_ protected members |
| 107 | |
| 108 | ::apache::thrift::concurrency::Mutex writeMutex_; |
| 109 | |
| 110 | ::apache::thrift::concurrency::Mutex readMutex_; |
| 111 | // begin readMutex_ protected members |
| 112 | bool recvPending_; |
| 113 | bool wakeupSomeone_; |
| 114 | int32_t seqidPending_; |
| 115 | std::string fnamePending_; |
| 116 | ::apache::thrift::protocol::TMessageType mtypePending_; |
| 117 | // end readMutex_ protected members |
| 118 | |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 119 | friend class TConcurrentSendSentry; |
| 120 | friend class TConcurrentRecvSentry; |
| 121 | }; |
Konrad Grochowski | 7f4be5f | 2015-11-05 20:23:11 +0100 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | } // apache::thrift::async |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 125 | |
| 126 | #endif // _THRIFT_TCONCURRENTCLIENTSYNCINFO_H_ |