annotate agent/src/os/win32/IOBuf.hpp @ 3917:eca1193ca245

4965777: GC changes to support use of discovered field for pending references Summary: If and when the reference handler thread is able to use the discovered field to link reference objects in its pending list, so will GC. In that case, GC will scan through this field once a reference object has been placed on the pending list, but not scan that field before that stage, as the field is used by the concurrent GC thread to link discovered objects. When ReferenceHandleR thread does not use the discovered field for the purpose of linking the elements in the pending list, as would be the case in older JDKs, the JVM will fall back to the old behaviour of using the next field for that purpose. Reviewed-by: jcoomes, mchung, stefank
author ysr
date Wed, 07 Sep 2011 13:55:42 -0700
parents c18cbe5936b8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
2 * Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
21 * questions.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 #ifndef _IO_BUF_
a61af66fc99e Initial load
duke
parents:
diff changeset
26 #define _IO_BUF_
a61af66fc99e Initial load
duke
parents:
diff changeset
27
a61af66fc99e Initial load
duke
parents:
diff changeset
28 // This file is currently used for os/solaris/agent/ too. At some point in time
a61af66fc99e Initial load
duke
parents:
diff changeset
29 // the source will be reorganized to avoid these ifdefs.
a61af66fc99e Initial load
duke
parents:
diff changeset
30 // Note that this class can read/write from a file as well as a socket. This
a61af66fc99e Initial load
duke
parents:
diff changeset
31 // file capability is only implemented on win32.
a61af66fc99e Initial load
duke
parents:
diff changeset
32
a61af66fc99e Initial load
duke
parents:
diff changeset
33 #ifdef WIN32
a61af66fc99e Initial load
duke
parents:
diff changeset
34 #include <winsock2.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
35 #else
a61af66fc99e Initial load
duke
parents:
diff changeset
36 #include <sys/types.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
37 #include <sys/socket.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
38 // These are from win32 winsock2.h
a61af66fc99e Initial load
duke
parents:
diff changeset
39 typedef unsigned int SOCKET;
a61af66fc99e Initial load
duke
parents:
diff changeset
40 typedef void * HANDLE;
a61af66fc99e Initial load
duke
parents:
diff changeset
41 typedef unsigned long DWORD;
a61af66fc99e Initial load
duke
parents:
diff changeset
42 #define INVALID_SOCKET (SOCKET)(~0)
a61af66fc99e Initial load
duke
parents:
diff changeset
43 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
44
a61af66fc99e Initial load
duke
parents:
diff changeset
45 #include <vector>
a61af66fc99e Initial load
duke
parents:
diff changeset
46 #include "Buffer.hpp"
a61af66fc99e Initial load
duke
parents:
diff changeset
47
a61af66fc99e Initial load
duke
parents:
diff changeset
48 /** Manages an input/output buffer pair for a socket or file handle. */
a61af66fc99e Initial load
duke
parents:
diff changeset
49 class IOBuf {
a61af66fc99e Initial load
duke
parents:
diff changeset
50 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
51 IOBuf(int inBufLen, int outBufLen);
a61af66fc99e Initial load
duke
parents:
diff changeset
52 ~IOBuf();
a61af66fc99e Initial load
duke
parents:
diff changeset
53
a61af66fc99e Initial load
duke
parents:
diff changeset
54 enum ReadLineResult {
a61af66fc99e Initial load
duke
parents:
diff changeset
55 RL_GOT_DATA,
a61af66fc99e Initial load
duke
parents:
diff changeset
56 RL_NO_DATA,
a61af66fc99e Initial load
duke
parents:
diff changeset
57 RL_ERROR
a61af66fc99e Initial load
duke
parents:
diff changeset
58 };
a61af66fc99e Initial load
duke
parents:
diff changeset
59
a61af66fc99e Initial load
duke
parents:
diff changeset
60 /** Change the socket with which this buffer is associated */
a61af66fc99e Initial load
duke
parents:
diff changeset
61 void setSocket(SOCKET sock);
a61af66fc99e Initial load
duke
parents:
diff changeset
62
a61af66fc99e Initial load
duke
parents:
diff changeset
63 // Reading/writing files is only supported on windows.
a61af66fc99e Initial load
duke
parents:
diff changeset
64 #ifdef WIN32
a61af66fc99e Initial load
duke
parents:
diff changeset
65 /** Change the output file handle with which this buffer is
a61af66fc99e Initial load
duke
parents:
diff changeset
66 associated. Currently IOBufs can not be used to read from a file
a61af66fc99e Initial load
duke
parents:
diff changeset
67 handle. */
a61af66fc99e Initial load
duke
parents:
diff changeset
68 void setOutputFileHandle(HANDLE handle);
a61af66fc99e Initial load
duke
parents:
diff changeset
69 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
70
a61af66fc99e Initial load
duke
parents:
diff changeset
71 /** Reset the input and output buffers, without flushing the output
a61af66fc99e Initial load
duke
parents:
diff changeset
72 data to the socket */
a61af66fc99e Initial load
duke
parents:
diff changeset
73 void reset();
a61af66fc99e Initial load
duke
parents:
diff changeset
74
a61af66fc99e Initial load
duke
parents:
diff changeset
75 /** Try to read a line of data from the given socket without
a61af66fc99e Initial load
duke
parents:
diff changeset
76 blocking. If was able to read a complete line of data, returns a
a61af66fc99e Initial load
duke
parents:
diff changeset
77 character pointer to the beginning of the (null-terminated)
a61af66fc99e Initial load
duke
parents:
diff changeset
78 string. If not, returns NULL, but maintains enough state that
a61af66fc99e Initial load
duke
parents:
diff changeset
79 subsequent calls to tryReadLine() will not ignore the data
a61af66fc99e Initial load
duke
parents:
diff changeset
80 already read. NOTE: this skips end-of-line characters (typically
a61af66fc99e Initial load
duke
parents:
diff changeset
81 CR/LF) as defined by "isEOL()". When switching back and forth
a61af66fc99e Initial load
duke
parents:
diff changeset
82 between binary and text modes, to be sure no data is lost, pad
a61af66fc99e Initial load
duke
parents:
diff changeset
83 the beginning and end of the binary transmission with bytes
a61af66fc99e Initial load
duke
parents:
diff changeset
84 which can not be confused with these characters. */
a61af66fc99e Initial load
duke
parents:
diff changeset
85 ReadLineResult tryReadLine();
a61af66fc99e Initial load
duke
parents:
diff changeset
86
a61af66fc99e Initial load
duke
parents:
diff changeset
87 /** Read a line of data from the given socket, blocking until a
a61af66fc99e Initial load
duke
parents:
diff changeset
88 line, including EOL, appears. Return the line, or NULL if
a61af66fc99e Initial load
duke
parents:
diff changeset
89 something goes wrong. */
a61af66fc99e Initial load
duke
parents:
diff changeset
90 char *readLine();
a61af66fc99e Initial load
duke
parents:
diff changeset
91
a61af66fc99e Initial load
duke
parents:
diff changeset
92 /** Get the pointer to the beginning of the (null-terminated) line.
a61af66fc99e Initial load
duke
parents:
diff changeset
93 This should only be called if tryReadLine() has returned
a61af66fc99e Initial load
duke
parents:
diff changeset
94 RL_GOT_DATA. This sets the "parsing cursor" to the beginning of
a61af66fc99e Initial load
duke
parents:
diff changeset
95 the line. */
a61af66fc99e Initial load
duke
parents:
diff changeset
96 char* getLine();
a61af66fc99e Initial load
duke
parents:
diff changeset
97
a61af66fc99e Initial load
duke
parents:
diff changeset
98 // NOTE: any further data-acquisition routines must ALWAYS call
a61af66fc99e Initial load
duke
parents:
diff changeset
99 // fixupData() at the beginning!
a61af66fc99e Initial load
duke
parents:
diff changeset
100
a61af66fc99e Initial load
duke
parents:
diff changeset
101 //----------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
102 // Output routines
a61af66fc99e Initial load
duke
parents:
diff changeset
103 //
a61af66fc99e Initial load
duke
parents:
diff changeset
104
a61af66fc99e Initial load
duke
parents:
diff changeset
105 /** Flush the output buffer to the socket. Returns true if
a61af66fc99e Initial load
duke
parents:
diff changeset
106 succeeded, false if write error occurred. */
a61af66fc99e Initial load
duke
parents:
diff changeset
107 bool flush();
a61af66fc99e Initial load
duke
parents:
diff changeset
108
a61af66fc99e Initial load
duke
parents:
diff changeset
109 /** Write the given string to the output buffer. May flush if output
a61af66fc99e Initial load
duke
parents:
diff changeset
110 buffer becomes too full to store the data. Not guaranteed to
a61af66fc99e Initial load
duke
parents:
diff changeset
111 work if string is longer than the size of the output buffer.
a61af66fc99e Initial load
duke
parents:
diff changeset
112 Does not include the null terminator of the string. Returns true
a61af66fc99e Initial load
duke
parents:
diff changeset
113 if succeeded, false if write error occurred. */
a61af66fc99e Initial load
duke
parents:
diff changeset
114 bool writeString(const char* str);
a61af66fc99e Initial load
duke
parents:
diff changeset
115
a61af66fc99e Initial load
duke
parents:
diff changeset
116 /** Write the given int to the output buffer. May flush if output
a61af66fc99e Initial load
duke
parents:
diff changeset
117 buffer becomes too full to store the data. Returns true if
a61af66fc99e Initial load
duke
parents:
diff changeset
118 succeeded, false if write error occurred. */
a61af66fc99e Initial load
duke
parents:
diff changeset
119 bool writeInt(int val);
a61af66fc99e Initial load
duke
parents:
diff changeset
120
a61af66fc99e Initial load
duke
parents:
diff changeset
121 /** Write the given unsigned int to the output buffer. May flush if
a61af66fc99e Initial load
duke
parents:
diff changeset
122 output buffer becomes too full to store the data. Returns true
a61af66fc99e Initial load
duke
parents:
diff changeset
123 if succeeded, false if write error occurred. */
a61af66fc99e Initial load
duke
parents:
diff changeset
124 bool writeUnsignedInt(unsigned int val);
a61af66fc99e Initial load
duke
parents:
diff changeset
125
a61af66fc99e Initial load
duke
parents:
diff changeset
126 /** Write the given boolean to the output buffer. May flush if
a61af66fc99e Initial load
duke
parents:
diff changeset
127 output buffer becomes too full to store the data. Returns true
a61af66fc99e Initial load
duke
parents:
diff changeset
128 if succeeded, false if write error occurred. */
a61af66fc99e Initial load
duke
parents:
diff changeset
129 bool writeBoolAsInt(bool val);
a61af66fc99e Initial load
duke
parents:
diff changeset
130
a61af66fc99e Initial load
duke
parents:
diff changeset
131 /** Write the given address to the output buffer. May flush if
a61af66fc99e Initial load
duke
parents:
diff changeset
132 output buffer becomes too full to store the data. Returns true
a61af66fc99e Initial load
duke
parents:
diff changeset
133 if succeeded, false if write error occurred. */
a61af66fc99e Initial load
duke
parents:
diff changeset
134 bool writeAddress(void* val);
a61af66fc99e Initial load
duke
parents:
diff changeset
135
a61af66fc99e Initial load
duke
parents:
diff changeset
136 /** Writes a space to the output buffer. May flush if output buffer
a61af66fc99e Initial load
duke
parents:
diff changeset
137 becomes too full to store the data. Returns true if succeeded,
a61af66fc99e Initial load
duke
parents:
diff changeset
138 false if write error occurred. */
a61af66fc99e Initial load
duke
parents:
diff changeset
139 bool writeSpace();
a61af66fc99e Initial load
duke
parents:
diff changeset
140
a61af66fc99e Initial load
duke
parents:
diff changeset
141 /** Writes an end-of-line sequence to the output buffer. May flush
a61af66fc99e Initial load
duke
parents:
diff changeset
142 if output buffer becomes too full to store the data. Returns
a61af66fc99e Initial load
duke
parents:
diff changeset
143 true if succeeded, false if write error occurred. */
a61af66fc99e Initial load
duke
parents:
diff changeset
144 bool writeEOL();
a61af66fc99e Initial load
duke
parents:
diff changeset
145
a61af66fc99e Initial load
duke
parents:
diff changeset
146 /** Writes a binary character to the output buffer. */
a61af66fc99e Initial load
duke
parents:
diff changeset
147 bool writeBinChar(char c);
a61af66fc99e Initial load
duke
parents:
diff changeset
148
a61af66fc99e Initial load
duke
parents:
diff changeset
149 /** Writes a binary unsigned short in network (big-endian) byte
a61af66fc99e Initial load
duke
parents:
diff changeset
150 order to the output buffer. */
a61af66fc99e Initial load
duke
parents:
diff changeset
151 bool writeBinUnsignedShort(unsigned short i);
a61af66fc99e Initial load
duke
parents:
diff changeset
152
a61af66fc99e Initial load
duke
parents:
diff changeset
153 /** Writes a binary unsigned int in network (big-endian) byte order
a61af66fc99e Initial load
duke
parents:
diff changeset
154 to the output buffer. */
a61af66fc99e Initial load
duke
parents:
diff changeset
155 bool writeBinUnsignedInt(unsigned int i);
a61af66fc99e Initial load
duke
parents:
diff changeset
156
a61af66fc99e Initial load
duke
parents:
diff changeset
157 /** Writes a binary buffer to the output buffer. */
a61af66fc99e Initial load
duke
parents:
diff changeset
158 bool writeBinBuf(char* buf, int size);
a61af66fc99e Initial load
duke
parents:
diff changeset
159
a61af66fc99e Initial load
duke
parents:
diff changeset
160 #ifdef WIN32
a61af66fc99e Initial load
duke
parents:
diff changeset
161 enum FillState {
a61af66fc99e Initial load
duke
parents:
diff changeset
162 DONE = 1,
a61af66fc99e Initial load
duke
parents:
diff changeset
163 MORE_DATA_PENDING = 2,
a61af66fc99e Initial load
duke
parents:
diff changeset
164 FAILED = 3
a61af66fc99e Initial load
duke
parents:
diff changeset
165 };
a61af66fc99e Initial load
duke
parents:
diff changeset
166
a61af66fc99e Initial load
duke
parents:
diff changeset
167 /** Very specialized routine; fill the output buffer from the given
a61af66fc99e Initial load
duke
parents:
diff changeset
168 file handle. Caller is responsible for ensuring that there is
a61af66fc99e Initial load
duke
parents:
diff changeset
169 data to be read on the file handle. */
a61af66fc99e Initial load
duke
parents:
diff changeset
170 FillState fillFromFileHandle(HANDLE fh, DWORD* numRead);
a61af66fc99e Initial load
duke
parents:
diff changeset
171 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
172
a61af66fc99e Initial load
duke
parents:
diff changeset
173 /** Binary utility routine (for poke) */
a61af66fc99e Initial load
duke
parents:
diff changeset
174 static bool isBinEscapeChar(char c);
a61af66fc99e Initial load
duke
parents:
diff changeset
175
a61af66fc99e Initial load
duke
parents:
diff changeset
176 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
177 IOBuf(const IOBuf&);
a61af66fc99e Initial load
duke
parents:
diff changeset
178 IOBuf& operator=(const IOBuf&);
a61af66fc99e Initial load
duke
parents:
diff changeset
179
a61af66fc99e Initial load
duke
parents:
diff changeset
180 // Returns -1 if non-blocking and no data available
a61af66fc99e Initial load
duke
parents:
diff changeset
181 int readChar(bool block);
a61af66fc99e Initial load
duke
parents:
diff changeset
182 // Line-oriented reading
a61af66fc99e Initial load
duke
parents:
diff changeset
183 std::vector<char> curLine;
a61af66fc99e Initial load
duke
parents:
diff changeset
184 bool gotDataLastTime;
a61af66fc99e Initial load
duke
parents:
diff changeset
185
a61af66fc99e Initial load
duke
parents:
diff changeset
186 ReadLineResult doReadLine(bool);
a61af66fc99e Initial load
duke
parents:
diff changeset
187
a61af66fc99e Initial load
duke
parents:
diff changeset
188 bool flushImpl(bool moreDataToCome);
a61af66fc99e Initial load
duke
parents:
diff changeset
189
a61af66fc99e Initial load
duke
parents:
diff changeset
190 SOCKET fd;
a61af66fc99e Initial load
duke
parents:
diff changeset
191 HANDLE outHandle;
a61af66fc99e Initial load
duke
parents:
diff changeset
192 bool usingSocket;
a61af66fc99e Initial load
duke
parents:
diff changeset
193
a61af66fc99e Initial load
duke
parents:
diff changeset
194 // Buffers
a61af66fc99e Initial load
duke
parents:
diff changeset
195 Buffer* inBuf;
a61af66fc99e Initial load
duke
parents:
diff changeset
196 Buffer* outBuf;
a61af66fc99e Initial load
duke
parents:
diff changeset
197
a61af66fc99e Initial load
duke
parents:
diff changeset
198 // Simple finite-state machine to handle binary data
a61af66fc99e Initial load
duke
parents:
diff changeset
199 enum State {
a61af66fc99e Initial load
duke
parents:
diff changeset
200 TEXT_STATE,
a61af66fc99e Initial load
duke
parents:
diff changeset
201 BIN_STATE,
a61af66fc99e Initial load
duke
parents:
diff changeset
202 EOL_STATE
a61af66fc99e Initial load
duke
parents:
diff changeset
203 };
a61af66fc99e Initial load
duke
parents:
diff changeset
204 enum Action {
a61af66fc99e Initial load
duke
parents:
diff changeset
205 NO_ACTION,
a61af66fc99e Initial load
duke
parents:
diff changeset
206 GOT_LINE, // TEXT_STATE -> EOL_STATE transition
a61af66fc99e Initial load
duke
parents:
diff changeset
207 SKIP_EOL_CHAR // EOL_STATE -> EOL_STATE transition
a61af66fc99e Initial load
duke
parents:
diff changeset
208 };
a61af66fc99e Initial load
duke
parents:
diff changeset
209
a61af66fc99e Initial load
duke
parents:
diff changeset
210 State state;
a61af66fc99e Initial load
duke
parents:
diff changeset
211 Action processChar(char c);
a61af66fc99e Initial load
duke
parents:
diff changeset
212
a61af66fc99e Initial load
duke
parents:
diff changeset
213 // Handling incoming binary buffers (poke command)
a61af66fc99e Initial load
duke
parents:
diff changeset
214 int binPos; // Number of binary characters read so far;
a61af66fc99e Initial load
duke
parents:
diff changeset
215 // total number to read is binLength + 4
a61af66fc99e Initial load
duke
parents:
diff changeset
216 int binLength; // Number of binary characters in message;
a61af66fc99e Initial load
duke
parents:
diff changeset
217 // not valid until binPos >= 4
a61af66fc99e Initial load
duke
parents:
diff changeset
218
a61af66fc99e Initial load
duke
parents:
diff changeset
219 bool isEOL(char c);
a61af66fc99e Initial load
duke
parents:
diff changeset
220 };
a61af66fc99e Initial load
duke
parents:
diff changeset
221
a61af66fc99e Initial load
duke
parents:
diff changeset
222 #endif // #defined _IO_BUF_