changeset 135:be55d80a4e8d

DIO-18: https://bugs.openjdk.java.net/browse/DIO-18 Summary: DIO 1.0.1 version of the fix for spurious wakeup-s driven race conditions Reviewed-by:snazarki Contributed-by:bkvartsk
author onazarkina
date Fri, 18 Mar 2016 16:51:03 +0300
parents 75060fb01074
children f7b7bfe5485e
files src/share/linux/native/com/oracle/dio/uart/serial.c
diffstat 1 files changed, 48 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/linux/native/com/oracle/dio/uart/serial.c	Tue Jul 14 18:20:07 2015 +0300
+++ b/src/share/linux/native/com/oracle/dio/uart/serial.c	Fri Mar 18 16:51:03 2016 +0300
@@ -371,54 +371,59 @@
     serial_handle p = (serial_handle)handle;
     javacall_result result = JAVACALL_FAIL;
 
-    /*
-     * write_thread write result stored in out_buffer_offset
-     * if synch operations
-     */
-    *bytesWritten = p->out_total_written;
-    /*
-     * all incoming java buffer is written
-     */
-    if(size == p->out_total_written){
-        p->write_complete_cb(p, 0, JAVACALL_OK);
-        cleanup_write_buffer(p);
-        return JAVACALL_OK;
-    }
-
-    p->out_buffer_size = size;
-    p->out_buffer = javacall_malloc(size);
-    if (NULL == p->out_buffer) {
-        JAVACALL_REPORT_ERROR(JC_DIO,
-                "[UART] malloc error while jc_serial_write_common");
-        return JAVACALL_OUT_OF_MEMORY;
-    }
-    memcpy(p->out_buffer, buffer, size);
-
-    if(0 == p->write_thread){
+    
+    if(0 == p->write_thread) {
         // initialize write mutex
-        if (pthread_mutex_init(&(p->write_lock), NULL) == 0 ) {
-            if (pthread_cond_init(&(p->out_buffer_ready), NULL) == 0 ) {
-              if(JAVACALL_OK == serial_create_thread(handle, write_thread, &p->write_thread)) {
-                result = JAVACALL_WOULD_BLOCK;
-              }else{
+        if (pthread_mutex_init(&(p->write_lock), NULL) == 0 && pthread_cond_init(&(p->out_buffer_ready), NULL) == 0 ) {
+            if((result = serial_create_thread(handle, write_thread, &p->write_thread)) != JAVACALL_OK) {
                 JAVACALL_REPORT_ERROR(JC_DIO, "[UART] cannot create writing thread");
-              }
-            }else{
-                JAVACALL_REPORT_ERROR(JC_DIO, "[UART]  condition variable initialization error");
             }
-        }else{
-            JAVACALL_REPORT_ERROR(JC_DIO, "[UART] mutex initialization error");
+        } else {
+            JAVACALL_REPORT_ERROR(JC_DIO, "[UART] mutex/write_lock initialization error");
+            // to do: deinit initialized mutex/lock
+            result = JAVACALL_FAIL;
         }
 
         if (result == JAVACALL_FAIL){
             cleanup_write_buffer(p);
+            return result;
         }
-    }else{
+    }
+
+    do {
         pthread_mutex_lock(&p->write_lock);
+        /*
+         * write_thread write result stored in out_buffer_offset
+         * if synch operations
+         */
+        *bytesWritten = p->out_total_written;
+        /*
+         * all incoming java buffer is written
+         */
+        if(size == p->out_total_written){
+            p->write_complete_cb(p, 0, JAVACALL_OK);
+            cleanup_write_buffer(p);
+            result = JAVACALL_OK;
+            break;
+        }
+        
+        p->out_buffer_size = size;
+        p->out_buffer = javacall_malloc(size);
+        if (NULL == p->out_buffer) {
+            JAVACALL_REPORT_ERROR(JC_DIO,
+                    "[UART] malloc error while jc_serial_write_common");
+            result =  JAVACALL_OUT_OF_MEMORY;
+            break;
+        }
+        memcpy(p->out_buffer, buffer, size);
+        
+        
         pthread_cond_signal(&p->out_buffer_ready);
-        pthread_mutex_unlock(&p->write_lock);
         result = JAVACALL_WOULD_BLOCK;
-    }
+    } while (0);
+
+    pthread_mutex_unlock(&p->write_lock);
+    
     return result;
 }
 
@@ -727,6 +732,10 @@
     pthread_mutex_lock(&p->write_lock);
 
     do{
+        while (p->out_buffer == NULL) {
+            pthread_cond_wait(&p->out_buffer_ready, &p->write_lock);
+        }
+
         current_write_operation = 0;
         current_buffer_offset = 0;
 
@@ -777,8 +786,8 @@
         //send signal or event in DA
         p->write_complete_cb(p, current_buffer_offset, error==JAVACALL_TRUE?JAVACALL_FAIL:JAVACALL_OK);
         cleanup_write_buffer(p);
-        pthread_cond_wait(&p->out_buffer_ready, &p->write_lock);
-    }while(1);
+
+    } while(1);
 
     pthread_mutex_unlock(&p->write_lock);