Commit 5cd4d463 by Mohan Embar Committed by Mohan Embar

win32.cc (WSAEventWrapper): Implemented default constructor and init() methods.

	* win32.cc (WSAEventWrapper): Implemented default
	constructor and init() methods.
	(_Jv_select): Removed.
	* gnu/java/nio/natSelectorImplWin32.cc
	(helper_put_filedescriptors): Removed.
	(helper_get_filedescriptors): Removed.
	(implSelect): Implemented in terms of WSAEventWrapper
	and WSAWaitForMultipleEvents instead of _Jv_select().
	Added support for thread interruption.
	* include/win32.h (WSAEventWrapper): Minor formatting
	changes; added default constructor declaration, init(),
	getFD() and getEventHandle() methods.
	(_Jv_select): Removed.

From-SVN: r74715
parent 027e655b
2003-12-16 Mohan Embar <gnustuff@thisiscool.com> 2003-12-16 Mohan Embar <gnustuff@thisiscool.com>
* win32.cc (WSAEventWrapper): Implemented default
constructor and init() methods.
(_Jv_select): Removed.
* gnu/java/nio/natSelectorImplWin32.cc
(helper_put_filedescriptors): Removed.
(helper_get_filedescriptors): Removed.
(implSelect): Implemented in terms of WSAEventWrapper
and WSAWaitForMultipleEvents instead of _Jv_select().
Added support for thread interruption.
* include/win32.h (WSAEventWrapper): Minor formatting
changes; added default constructor declaration, init(),
getFD() and getEventHandle() methods.
(_Jv_select): Removed.
2003-12-16 Mohan Embar <gnustuff@thisiscool.com>
* gnu/java/net/natPlainDatagramSocketImplPosix.cc * gnu/java/net/natPlainDatagramSocketImplPosix.cc
(peekData): Throw SocketTimeoutException instead of (peekData): Throw SocketTimeoutException instead of
InterruptedIOException on timeout. InterruptedIOException on timeout.
......
...@@ -11,81 +11,83 @@ details. */ ...@@ -11,81 +11,83 @@ details. */
#include <config.h> #include <config.h>
#include <platform.h> #include <platform.h>
#include <errno.h>
#include <string.h>
#include <gnu/java/nio/SelectorImpl.h> #include <gnu/java/nio/SelectorImpl.h>
#include <java/io/IOException.h> #include <java/lang/Thread.h>
void jint
helper_put_filedescriptors (jintArray fdArray, fd_set& fds, int& max_fd) gnu::java::nio::SelectorImpl::implSelect (jintArray read, jintArray write,
jintArray except, jlong timeout)
{ {
jint* tmpFDArray = elements (fdArray); // FIXME: The API for implSelect is biased towards POSIX implementations.
jint* pReadFD = elements (read);
int nNbReadFDs = JvGetArrayLength (read);
for (int index = 0; index < JvGetArrayLength (fdArray); index++) jint* pWriteFD = elements (write);
{ int nNbWriteFDs = JvGetArrayLength (write);
FD_SET (tmpFDArray [index], &fds);
if (tmpFDArray [index] > max_fd) int nNbEvents = nNbReadFDs + nNbWriteFDs;
max_fd = tmpFDArray [index];
}
}
void // Create and initialize our event wrapper array
helper_get_filedescriptors (jintArray& fdArray, fd_set fds)
{
jint* tmpFDArray = elements (fdArray);
for (int index = 0; index < JvGetArrayLength (fdArray); index++) // FIXME: We're creating fresh WSAEVENTs for each call.
if (!FD_ISSET (tmpFDArray [index], &fds)) // This is inefficient. It would probably be better to cache these
tmpFDArray [index] = 0; // in the Win32 socket implementation class.
} WSAEventWrapper aArray[nNbEvents];
jint int nCurIndex = 0;
gnu::java::nio::SelectorImpl::implSelect (jintArray read, jintArray write, for (int i=0; i < nNbReadFDs; ++i)
jintArray except, jlong timeout) aArray[nCurIndex++].init(pReadFD[i], FD_ACCEPT | FD_READ);
{
jint result;
int max_fd = 0;
fd_set read_fds;
fd_set write_fds;
fd_set except_fds;
struct timeval real_time_data;
struct timeval *time_data = NULL;
real_time_data.tv_sec = 0;
real_time_data.tv_usec = timeout;
// If not legal timeout value is given, use NULL.
// This means an infinite timeout.
if (timeout >= 0)
{
time_data = &real_time_data;
}
// Reset all fd_set structures for (int i=0; i < nNbWriteFDs; ++i)
FD_ZERO (&read_fds); aArray[nCurIndex++].init(pWriteFD[i], FD_WRITE);
FD_ZERO (&write_fds);
FD_ZERO (&except_fds);
// Fill the fd_set data structures for the _Jv_select() call. // Build our array of WSAEVENTs to wait on. Also throw in our thread's
helper_put_filedescriptors (read, read_fds, max_fd); // interrupt event in order to detect thread interruption.
helper_put_filedescriptors (write, write_fds, max_fd); HANDLE arh[nNbEvents + 1];
helper_put_filedescriptors (except, except_fds, max_fd); for (int i=0; i < nNbEvents; ++i)
arh[i] = aArray[i].getEventHandle();
arh[nNbEvents] = _Jv_Win32GetInterruptEvent ();
// Actually do the select // A timeout value of 0 needs to be treated as infinite.
result = _Jv_select (max_fd + 1, &read_fds, &write_fds, &except_fds, time_data); if (timeout <= 0)
timeout = WSA_INFINITE;
if (result < 0) // Do the select.
{ DWORD dwRet = WSAWaitForMultipleEvents (nNbEvents+1, arh, 0, timeout, false);
char* strerr = strerror (errno);
throw new ::java::io::IOException (JvNewStringUTF (strerr));
}
// Set the file descriptors according to the values returned from select(). if (dwRet == WSA_WAIT_FAILED)
helper_get_filedescriptors (read, read_fds); _Jv_ThrowIOException ();
helper_get_filedescriptors (write, write_fds);
helper_get_filedescriptors (except, except_fds);
return result; // Before we do anything else, clear output file descriptor arrays.
memset(pReadFD, 0, sizeof(jint) * nNbReadFDs);
memset(pWriteFD, 0, sizeof(jint) * nNbWriteFDs);
memset(elements (except), 0, sizeof(jint) * JvGetArrayLength (except));
if (dwRet == DWORD(WSA_WAIT_EVENT_0 + nNbEvents))
{
// We were interrupted. Set the current thread's interrupt
// status and get out of here, with nothing selected..
::java::lang::Thread::currentThread ()->interrupt ();
return 0;
}
else if (dwRet < DWORD(WSA_WAIT_EVENT_0 + nNbEvents))
{
int nSelectedEventIndex = dwRet - WSA_WAIT_EVENT_0;
// Record the selected file descriptor.
// FIXME: This implementation only allows one file descriptor
// to be selected at a time. Remedy this by looping on
// WSAWaitForMultipleEvents 'til nothing more is selected.
jint fd = aArray[nSelectedEventIndex].getFD();
if (nSelectedEventIndex < nNbReadFDs)
pReadFD[0] = fd;
else
pWriteFD[0] = fd;
return 1;
}
else
// None of the event objects was signalled, so nothing was
// selected.
return 0;
} }
...@@ -100,9 +100,20 @@ extern jstring _Jv_Win32NewString (LPCTSTR pcsz); ...@@ -100,9 +100,20 @@ extern jstring _Jv_Win32NewString (LPCTSTR pcsz);
class WSAEventWrapper class WSAEventWrapper
{ {
public: public:
// Default constructor. Call init() after this.
WSAEventWrapper();
WSAEventWrapper(int fd, DWORD dwSelFlags); WSAEventWrapper(int fd, DWORD dwSelFlags);
~WSAEventWrapper(); ~WSAEventWrapper();
// Used for two-step initialization after calling
// default constructor.
void init(int fd, DWORD dwSelFlags);
int getFD()
{
return m_fd;
}
WSAEVENT getEventHandle() WSAEVENT getEventHandle()
{ {
return m_hEvent; return m_hEvent;
...@@ -141,7 +152,6 @@ _Jv_ThrowSocketException (); ...@@ -141,7 +152,6 @@ _Jv_ThrowSocketException ();
extern void _Jv_platform_initialize (void); extern void _Jv_platform_initialize (void);
extern void _Jv_platform_initProperties (java::util::Properties*); extern void _Jv_platform_initProperties (java::util::Properties*);
extern jlong _Jv_platform_gettimeofday (); extern jlong _Jv_platform_gettimeofday ();
extern int _Jv_select (int n, fd_set *, fd_set *, fd_set *, struct timeval *);
extern int _Jv_pipe (int filedes[2]); extern int _Jv_pipe (int filedes[2]);
extern void extern void
......
...@@ -143,11 +143,24 @@ _Jv_Win32TempString::~_Jv_Win32TempString() ...@@ -143,11 +143,24 @@ _Jv_Win32TempString::~_Jv_Win32TempString()
} }
// class WSAEventWrapper // class WSAEventWrapper
WSAEventWrapper::WSAEventWrapper ():
m_hEvent(0),
m_fd(0),
m_dwSelFlags(0)
{}
WSAEventWrapper::WSAEventWrapper (int fd, DWORD dwSelFlags): WSAEventWrapper::WSAEventWrapper (int fd, DWORD dwSelFlags):
m_hEvent(0), m_hEvent(0),
m_fd(fd), m_fd(0),
m_dwSelFlags(dwSelFlags) m_dwSelFlags(0)
{ {
init(fd, dwSelFlags);
}
void WSAEventWrapper::init(int fd, DWORD dwSelFlags)
{
m_fd = fd;
m_dwSelFlags = dwSelFlags;
m_hEvent = WSACreateEvent (); m_hEvent = WSACreateEvent ();
if (dwSelFlags) if (dwSelFlags)
WSAEventSelect(fd, m_hEvent, dwSelFlags); WSAEventSelect(fd, m_hEvent, dwSelFlags);
...@@ -446,19 +459,6 @@ backtrace (void **__array, int __size) ...@@ -446,19 +459,6 @@ backtrace (void **__array, int __size)
} }
int int
_Jv_select (int n, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout)
{
int r = ::select (n, readfds, writefds, exceptfds, timeout);
if (r == SOCKET_ERROR)
{
DWORD dwErrorCode = WSAGetLastError ();
throw new java::io::IOException (_Jv_WinStrError (dwErrorCode));
}
return r;
}
int
_Jv_pipe (int filedes[2]) _Jv_pipe (int filedes[2])
{ {
return _pipe (filedes, 4096, _O_BINARY); return _pipe (filedes, 4096, _O_BINARY);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment