This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.
Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
---|---|---|
Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |
Other format: | [Raw text] |
Hi People, This patch attempts to implement Thread.interrupt() on MinGW. Two disclaimers: this does not support interrupting blocking I/O operations. Ironically, I had started to implement this because I wanted interruptible I/O, but these posts from Bryce convinced me to walk away from this: - http://gcc.gnu.org/ml/java/2003-09/msg00066.html and particularly, this: - http://gcc.gnu.org/ml/java/2000-q1/msg00419.html Secondly, I haven't addressed _Jv_select() yet. I'll do that when Michael is further along with the NIO stuff. I've attached a testcase. Your review and feedback is appreciated. Thanks in advance. -- Mohan http://www.thisiscool.com/ http://www.animalsong.org/ ChangeLog 2003-09-15 Mohan Embar <gnustuff@thisiscool.com> * win32-threads.cc: (ensure_interrupt_event_initialized) New function for lazy initialization of an auto-reset event. (_Jv_CondWait) Added thread interrupt support. (_Jv_ThreadInitData) Added initialization of interrupt support members. (_Jv_ThreadDestroyData) Added cleanup of interrupt support members. (_Jv_ThreadStart) Removed unused code. (_Jv_GetInterruptEvent) New method for returning interrupt event to an external caller. (_Jv_ThreadInterrupt) Implemented. * include/win32-threads.h: (_Jv_Thread_t) Added a Win32 auto-reset event for interrupt support as well as a mutex which regulates access to this. (_Jv_GetInterruptEvent) Declared new method for returning interrupt event to an external caller. * java/lang/natWin32Process.cc: (cleanup) Close handle to spawned process. (waitFor) Added interrupt support. Index: win32-threads.cc =================================================================== RCS file: /cvsroot/gcc/gcc/libjava/win32-threads.cc,v retrieving revision 1.9 diff -u -2 -r1.9 win32-threads.cc --- win32-threads.cc 28 Jan 2003 22:23:36 -0000 1.9 +++ win32-threads.cc 15 Sep 2003 06:45:01 -0000 @@ -82,4 +82,14 @@ } +inline void +ensure_interrupt_event_initialized(HANDLE& rhEvent) +{ + if (!rhEvent) + { + rhEvent = CreateEvent (NULL, 0, 0, NULL); + if (!rhEvent) JvFail("CreateEvent() failed"); + } +} + // Reimplementation of the general algorithm described at // http://www.cs.wustl.edu/~schmidt/win32-cv-1.html (isomorphic to @@ -92,4 +102,19 @@ return _JV_NOT_OWNER; + _Jv_Thread_t *current = _Jv_ThreadCurrentData (); + java::lang::Thread *current_obj = _Jv_ThreadCurrent (); + + // Now that we hold the interrupt mutex, check if this thread has been + // interrupted already. + EnterCriticalSection (¤t->interrupt_mutex); + ensure_interrupt_event_initialized (current->interrupt_event); + jboolean interrupted = current_obj->interrupt_flag; + LeaveCriticalSection (¤t->interrupt_mutex); + + if (interrupted) + { + return _JV_INTERRUPTED; + } + EnterCriticalSection (&cv->count_mutex); ensure_condvar_initialized (cv); @@ -104,5 +129,29 @@ _Jv_MutexUnlock (mu); - DWORD rval = WaitForMultipleObjects (2, &(cv->ev[0]), 0, time); + // Set up our array of three events: + // - the auto-reset event (for notify()) + // - the manual-reset event (for notifyAll()) + // - the interrupt event (for interrupt()) + // We wait for any one of these to be signaled. + HANDLE arh[3]; + arh[0] = cv->ev[0]; + arh[1] = cv->ev[1]; + arh[2] = current->interrupt_event; + DWORD rval = WaitForMultipleObjects (3, arh, 0, time); + + EnterCriticalSection (¤t->interrupt_mutex); + + // If we were unblocked by the third event (our thread's interrupt + // event), set the thread's interrupt flag. I think this sanity + // check guards against someone resetting our interrupt flag + // in the time between when interrupt_mutex is released in + // _Jv_ThreadInterrupt and the interval of time between the + // WaitForMultipleObjects call we just made and our acquisition + // of interrupt_mutex. + if (rval == (WAIT_OBJECT_0 + 2)) + current_obj->interrupt_flag = true; + + interrupted = current_obj->interrupt_flag; + LeaveCriticalSection (¤t->interrupt_mutex); EnterCriticalSection(&cv->count_mutex); @@ -117,6 +166,6 @@ _Jv_MutexLock (mu); - - return 0; + + return interrupted ? _JV_INTERRUPTED : 0; } @@ -198,4 +247,6 @@ data->flags = 0; data->thread_obj = obj; + data->interrupt_event = 0; + InitializeCriticalSection (&data->interrupt_mutex); return data; @@ -205,4 +256,7 @@ _Jv_ThreadDestroyData (_Jv_Thread_t *data) { + DeleteCriticalSection (&data->interrupt_mutex); + if (data->interrupt_event) + CloseHandle(data->interrupt_event); _Jv_Free(data); } @@ -309,9 +363,6 @@ data->flags |= FLAG_DAEMON; - HANDLE h = GC_CreateThread(NULL, 0, really_start, info, 0, &id); + GC_CreateThread(NULL, 0, really_start, info, 0, &id); _Jv_ThreadSetPriority(data, thread->getPriority()); - - //if (!h) - //JvThrow (); } @@ -327,8 +378,26 @@ } +// +// Interrupt support +// + +HANDLE +_Jv_GetInterruptEvent (void) +{ + _Jv_Thread_t *current = _Jv_ThreadCurrentData (); + EnterCriticalSection (¤t->interrupt_mutex); + ensure_interrupt_event_initialized (current->interrupt_event); + HANDLE hEvent = current->interrupt_event; + LeaveCriticalSection (¤t->interrupt_mutex); + return hEvent; +} + void _Jv_ThreadInterrupt (_Jv_Thread_t *data) { - MessageBox(NULL, "Unimplemented", "win32-threads.cc:_Jv_ThreadInterrupt", MB_OK); - // FIXME: + EnterCriticalSection (&data->interrupt_mutex); + ensure_interrupt_event_initialized (data->interrupt_event); + data->thread_obj->interrupt_flag = true; + SetEvent (data->interrupt_event); + LeaveCriticalSection (&data->interrupt_mutex); } Index: include/win32-threads.h =================================================================== RCS file: /cvsroot/gcc/gcc/libjava/include/win32-threads.h,v retrieving revision 1.7 diff -u -2 -r1.7 win32-threads.h --- include/win32-threads.h 28 Jan 2003 22:23:36 -0000 1.7 +++ include/win32-threads.h 15 Sep 2003 06:45:04 -0000 @@ -51,4 +51,12 @@ int flags; // Flags are defined in implementation. HANDLE handle; // Actual handle to the thread + + // Protects access to the thread's interrupt_flag and + // interrupt_event variables within this module. + CRITICAL_SECTION interrupt_mutex; + + // A Win32 auto-reset event for thread interruption + HANDLE interrupt_event; + java::lang::Thread *thread_obj; } _Jv_Thread_t; @@ -150,4 +158,22 @@ void _Jv_ThreadWait (void); void _Jv_ThreadInterrupt (_Jv_Thread_t *data); + +// +// Thread interruption support +// + +// Gets the auto-reset event for the current thread which is +// signalled by _Jv_ThreadInterrupt. The caller can wait on this +// event in addition to other waitable objects. +// +// NOTE: After waiting on this event with WaitForMultipleObjects, +// you should ALWAYS use the return value of WaitForMultipleObjects +// to test whether this event was signalled and whether thread +// interruption has occurred. You should do this instead of checking +// the thread's interrupted_flag, because someone could have reset +// this flag in the interval of time between the return of +// WaitForMultipleObjects and the time you query interrupted_flag. +// See java/lang/natWin32Process.cc (waitFor) for an example. +HANDLE _Jv_GetInterruptEvent (void); // Remove defines from <windows.h> that conflict with various things in libgcj code Index: java/lang/natWin32Process.cc =================================================================== RCS file: /cvsroot/gcc/gcc/libjava/java/lang/natWin32Process.cc,v retrieving revision 1.5 diff -u -2 -r1.5 natWin32Process.cc --- java/lang/natWin32Process.cc 29 Aug 2003 04:21:00 -0000 1.5 +++ java/lang/natWin32Process.cc 15 Sep 2003 06:45:06 -0000 @@ -47,4 +47,9 @@ errorStream = NULL; } + if (procHandle) + { + CloseHandle((HANDLE) procHandle); + procHandle = (jint) INVALID_HANDLE_VALUE; + } } @@ -93,6 +98,25 @@ DWORD exitStatus = 0UL; - // FIXME: The wait should be interruptible. - WaitForSingleObject ((HANDLE) procHandle, INFINITE); + // Set up our waitable objects array + // - 0: the handle to the process we just launched + // - 1: our thread's interrupt event + HANDLE arh[2]; + arh[0] = (HANDLE) procHandle; + arh[1] = _Jv_GetInterruptEvent (); + DWORD rval = WaitForMultipleObjects (2, arh, 0, INFINITE); + + // Use the returned value from WaitForMultipleObjects + // instead of our thread's interrupt_flag to test for + // thread interruption. See the comment for _Jv_GetInterruptEvent(). + bool bInterrupted = rval == (WAIT_OBJECT_0 + 1); + + if (bInterrupted) + { + // Querying this forces a reset our thread's interrupt flag. + Thread::interrupted(); + + cleanup (); + throw new InterruptedException (); + } GetExitCodeProcess ((HANDLE) procHandle, &exitStatus);
Attachment:
InterruptTest.tar.bz2
Description: application/bzip2
Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
---|---|---|
Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |