changeset 132:c004adba7229

8076493: DIO event needs to be processed inside 1 thread Summary: Make all event processing functions run in the same thread Reviewed-by: snazarki Contributed-by: alexey.karaksin@oracle.com
author amironov
date Mon, 06 Apr 2015 17:46:18 +0300
parents 0dea0345520e
children a6f18afacb4c
files src/se/classes/com/oracle/dio/impl/EventQueue.java src/se/classes/com/oracle/dio/uart/impl/UARTEventHandler.java src/share/classes/com/oracle/dio/uart/impl/UARTImpl.java
diffstat 3 files changed, 15 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/src/se/classes/com/oracle/dio/impl/EventQueue.java	Fri Apr 03 10:00:48 2015 +0300
+++ b/src/se/classes/com/oracle/dio/impl/EventQueue.java	Mon Apr 06 17:46:18 2015 +0300
@@ -43,6 +43,10 @@
     private Thread                  nativeEventThread;
     private Thread                  eventDispatchThread;
 
+    public Thread getEventDispatchThread(){
+        return eventDispatchThread;
+    }
+
     private static class QueueInstanceHolder {
         private static EventQueue sharedEventQueue = new EventQueue(SHARED_QUEUE_BUFFER_SIZE);
     }
@@ -108,24 +112,23 @@
     private class EventDispatchThread implements Runnable {
         @Override
         public void run() {
+            Event evt = null;
             while (true) {
                 synchronized (queue) {
                     try {
                         if (queue.isEmpty()) {
                             queue.wait();
                         }
-
-                        while (!queue.isEmpty()) {
-                            Event evt = queue.poll();
-                            if (evt != null) {
-                                dispatch(evt);
-                            }
-                        }
+                        evt = queue.poll();
                     } catch (InterruptedException ex) {
                         // do something
                     }
+                }//synchronized queue
+
+                if (evt != null) {
+                    dispatch(evt);
                 }
-            }
+            }//while true
         }
     }
 
@@ -167,13 +170,8 @@
         synchronized (listeners) {
             for (int i = 0; i < listeners.size(); i += 2) {
                 if (listeners.get(i).equals(evt.getClass())) {
-                    final EventHandler h = (EventHandler)listeners.get(i+1);
-                    new Thread() {
-                        @Override
-                        public void run() {
-                            h.handleEvent(evt);
-                        }
-                    }.start();
+                    EventHandler h = (EventHandler)listeners.get(i+1);
+                    h.handleEvent(evt);
                 }
             }
         }
--- a/src/se/classes/com/oracle/dio/uart/impl/UARTEventHandler.java	Fri Apr 03 10:00:48 2015 +0300
+++ b/src/se/classes/com/oracle/dio/uart/impl/UARTEventHandler.java	Mon Apr 06 17:46:18 2015 +0300
@@ -158,9 +158,6 @@
                                                 Class<UARTEvent> eventClass);
 
     public boolean isDispatchThread() {
-        /*
-         * because of nature of dispatch for se where an event processed in separate thread
-         */
-        return false;
+        return queue.getEventDispatchThread().equals(Thread.currentThread());
     }
 }
--- a/src/share/classes/com/oracle/dio/uart/impl/UARTImpl.java	Fri Apr 03 10:00:48 2015 +0300
+++ b/src/share/classes/com/oracle/dio/uart/impl/UARTImpl.java	Mon Apr 06 17:46:18 2015 +0300
@@ -188,7 +188,7 @@
         AccessController.checkPermission(new UARTPermission(getSecurityName(), DevicePermission.POWER_MANAGE));
     }
 
-    protected synchronized void processEvent(int event, int bytesProcessed){
+    protected void processEvent(int event, int bytesProcessed){
         UARTEventListener listener = eventListeners.get(event);
         if (listener != null){
             try{