Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 175 lines (138 sloc) 5.645 kB
bae9856 @yathin adding license information to sources
yathin authored
1 /*
2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3
4 Redistribution and use of this software in source and binary forms, with or
5 without modification, are permitted provided that the following conditions are
6 met:
7
8 * Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10
11 * Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation and/or
13 other materials provided with the distribution.
14
15 * Neither the name of Yahoo! Inc. nor the names of its contributors may be used
16 to endorse or promote products derived from this software without specific prior
17 written permission of Yahoo! Inc.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
2bbc399 @MarkDelany Many portability fixes and autoconf support
MarkDelany authored
31 #include "config.h"
32
f42d421 @yathin first commit. pluton framework
yathin authored
33 #include <sys/types.h>
34
35 #include <assert.h>
36 #include <unistd.h>
37 #include <signal.h>
38
39 #include "util.h"
40
41 #include "clientRequestImpl.h"
42 #include "perCallerClient.h"
43
44 using namespace pluton;
45
46 //////////////////////////////////////////////////////////////////////
47 // The perCallerClient class holds unique per-caller data but most of
48 // the functionality is in the singleton clientImpl.
49 //////////////////////////////////////////////////////////////////////
50
51 pluton::perCallerClient::perCallerClient(const char* yourName, unsigned int timeoutMilliSeconds)
52 : _oneAtATimePerCaller(false), _threadedApplication(false),
53 _timeoutMilliSeconds(timeoutMilliSeconds), _todoCount(0), _readingCount(0),
54 _originalHandler(SIG_DFL), _myThreadImpl(0),
55 _fdsSize(20), _fds(new struct pollfd[_fdsSize]),
56 _fdsToRequests(new pluton::clientRequestImpl*[_fdsSize]),
57 _completedQueue("completed")
58 {
59 util::IA ia;
60 _clientName = yourName;
61 _clientName.append(":");
62 _clientName.append(util::ltoa(ia, static_cast<int>(getpid())));
63 }
64
65 void
66 pluton::perCallerClient::blockSIGPIPE()
67 {
68 _originalHandler = signal(SIGPIPE, SIG_IGN);
69 }
70
71 void
72 pluton::perCallerClient::restoreSIGPIPE()
73 {
74 signal(SIGPIPE, _originalHandler);
75 }
76
77
78 void
79 pluton::perCallerClient::setThreadedApplication(bool tf, pluton::thread_t newID)
80 {
81 _threadedApplication = tf;
82 _owningThreadID = newID;
83 }
84
85
86 void
87 pluton::perCallerClient::addTodoCount()
88 {
89 if (_todoCount++ == 0) { // Timeout clock resets on transition from zero to one requests
90 _clock.stop();
91 }
92 }
93
94 //////////////////////////////////////////////////////////////////////
95 // Serialization protection methods. Neither the perCallerClient nor
96 // the clientImpl can be shared between threads. While this is well
97 // documented (and should be a default assumption of any object by all
98 // programmers), we "trust but verify".
99 //////////////////////////////////////////////////////////////////////
100
101 void
102 pluton::perCallerClient::assertMutexFreeThenLock()
103 {
104 assert(_oneAtATimePerCaller == false);
105
106 _oneAtATimePerCaller = true;
107 }
108
109 //////////////////////////////////////////////////////////////////////
110 // Manage the mapping between fd and clientRequestImpl so that poll
111 // events can be easily associated with the original request.
112 //////////////////////////////////////////////////////////////////////
113
114 void
115 pluton::perCallerClient::setRequestIndex(unsigned int ix, pluton::clientRequestImpl* R)
116 {
117 assert(ix < _fdsSize); // Internal bug
118 _fdsToRequests[ix] = R;
119 }
120
121 pluton::clientRequestImpl*
122 pluton::perCallerClient::getRequestByIndex(unsigned int ix)
123 {
124 if (ix >= _fdsSize) return 0; // callback giving us bogus fds?
125 return _fdsToRequests[ix];
126 }
127
128
129 //////////////////////////////////////////////////////////////////////
130 // Make sure the poll() structure and fd-to-Request map are big enough
131 // for all the possible fds. Reallocation does not retain original
132 // data so this call should only be made prior to populating the
133 // pollfd struct and clientRequestImpl pointer array.
134 //////////////////////////////////////////////////////////////////////
135
136 struct pollfd*
137 pluton::perCallerClient::sizePollArray(unsigned int entriesNeeded)
138 {
139 if (entriesNeeded > _fdsSize) {
140 delete [] _fds;
141 delete [] _fdsToRequests;
142
143 _fdsSize = entriesNeeded + entriesNeeded/2 + 1;
144 _fds = new struct pollfd[_fdsSize];
145 _fdsToRequests = new pluton::clientRequestImpl*[_fdsSize];
146 }
147
148 return _fds;
149 }
150
151
152 //////////////////////////////////////////////////////////////////////
153 // Queue management methods that are longer than one-liners.
154 //////////////////////////////////////////////////////////////////////
155
156 void
157 pluton::perCallerClient::eraseCompletedQueue(pluton::clientRequestImpl::state newState)
158 {
159 while (!_completedQueue.empty()) {
160 pluton::clientRequestImpl* R = _completedQueue.getFirst();
161 R->setState("eraseQueue", newState);
162 _completedQueue.deleteRequest(R);
163 }
164 }
165
166
167 pluton::clientRequestImpl*
168 pluton::perCallerClient::popCompletedRequest()
169 {
170 pluton::clientRequestImpl* R = _completedQueue.getFirst();
171 if (R) _completedQueue.deleteRequest(R);
172
173 return R;
174 }
Something went wrong with that request. Please try again.