shm: Remove a shared memory if not using it
authorgiwoong.kim <giwoong.kim@samsung.com>
Thu, 25 Apr 2013 04:52:21 +0000 (13:52 +0900)
committergiwoong.kim <giwoong.kim@samsung.com>
Thu, 25 Apr 2013 05:41:16 +0000 (14:41 +0900)
Remove a shared memory if not using it
anymore.

Change-Id: I199b14cea966f1d340dc815f3b86e2a54848ccfc
Signed-off-by: GiWoong Kim <giwoong.kim@samsung.com>
tizen/src/maru_shm.c
tizen/src/skin/client/native_src/share.c
tizen/src/skin/client/src/org/tizen/emulator/skin/EmulatorSkin.java
tizen/src/skin/client/src/org/tizen/emulator/skin/comm/sock/SocketCommunicator.java
tizen/src/skin/client/src/org/tizen/emulator/skin/util/IOUtil.java

index 961cf623f1e5ed26fe775619ed4e74c3b18f4481..7b33c747fd79ee8e63771d2617e6d95d9198b5f2 100644 (file)
@@ -158,22 +158,38 @@ void maruskin_shm_init(uint64 swt_handle,
 
     /* default screen */
     memset(shared_memory, 0x00, (size_t)shm_size);
-    printf("Memory attached at 0x%X\n", (int)shared_memory);
+    INFO("Memory attached at 0x%X\n", (int)shared_memory);
 }
 
 void maruskin_shm_quit(void)
 {
+    struct shmid_ds shm_info;
+
     INFO("maru shm quit\n");
 
+    if (shmctl(skin_shmid, IPC_STAT, &shm_info) == -1) {
+        ERR("shmctl failed\n");
+        shm_info.shm_nattch = -1;
+    }
+
     if (shmdt(shared_memory) == -1) {
         ERR("shmdt failed\n");
         perror("maru_vga: ");
+        return;
     }
     shared_memory = NULL;
 
-    if (shmctl(skin_shmid, IPC_RMID, 0) == -1) {
-        INFO("segment was already removed\n");
-        perror("maru_vga: ");
+    if (shm_info.shm_nattch == 1) {
+        /* remove */
+        if (shmctl(skin_shmid, IPC_RMID, 0) == -1) {
+            INFO("segment was already removed\n");
+            perror("maru_vga: ");
+        } else {
+            INFO("shared memory was removed\n");
+        }
+    } else if (shm_info.shm_nattch != -1) {
+        INFO("number of current attaches = %d\n",
+            (int)shm_info.shm_nattch);
     }
 }
 
index 059a25d3496c00d2813ed1450ccd0aaf027b1ea0..a8bef4e578bda9b229f5f1512f9d3275cd3e48b1 100644 (file)
@@ -10,7 +10,7 @@
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
- * of the License, or ( at your option ) any later version.
+ * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -63,7 +63,8 @@ JNIEXPORT jint JNICALL Java_org_tizen_emulator_skin_EmulatorShmSkin_shmget
         return 2;
     }
 
-    fprintf(stdout, "Memory attached at 0x%X\n", (int)shared_memory);
+    fprintf(stdout, "share.c: memory attached at 0x%X\n",
+        (int)shared_memory);
     fflush(stdout);
 
     return 0;
@@ -72,6 +73,16 @@ JNIEXPORT jint JNICALL Java_org_tizen_emulator_skin_EmulatorShmSkin_shmget
 JNIEXPORT jint JNICALL Java_org_tizen_emulator_skin_EmulatorShmSkin_shmdt
     (JNIEnv *env, jobject obj)
 {
+    struct shmid_ds shm_info;
+
+    if (shmctl(shmid, IPC_STAT, &shm_info) == -1) {
+        fprintf(stderr, "share.c: shmctl failed\n");
+        fflush(stderr);
+        perror("share.c: ");
+
+        shm_info.shm_nattch = -1;
+    }
+
     fprintf(stdout, "share.c: clean up the shared memory\n");
     fflush(stdout);
 
@@ -84,14 +95,19 @@ JNIEXPORT jint JNICALL Java_org_tizen_emulator_skin_EmulatorShmSkin_shmdt
     }
     shared_memory = NULL;
 
-    /*
-    if (shmctl(shmid, IPC_RMID, 0) == -1) {
-        fprintf(stdout, "share.c: segment was already removed\n");
+    if (shm_info.shm_nattch == 1) {
+        /* remove */
+        if (shmctl(shmid, IPC_RMID, 0) == -1) {
+            fprintf(stdout, "share.c: segment was already removed\n");
+            fflush(stdout);
+            perror("share.c: ");
+            return 2;
+        }
+    } else if (shm_info.shm_nattch != -1) {
+        fprintf(stdout, "share.c: number of current attaches = %d\n",
+            (int)shm_info.shm_nattch);
         fflush(stdout);
-        perror("share.c: ");
-        return 2;
     }
-    */
 
     return 0;
 }
index 063b77cf6ce0f39deaf9558d25f96b5f0192d4b7..04902276c6b4af916281aae2d95391c63ad5261a 100644 (file)
@@ -1700,6 +1700,11 @@ public class EmulatorSkin {
 
                                if (answer == SWT.OK) {
                                        logger.info("force close!!!");
+
+                                       if (communicator != null) {
+                                               communicator.terminate();
+                                       }
+
                                        System.exit(-1);
                                }
                        }
index 757f7e25731730f27d970ff47035ece48e9e83aa..07bb4d4ed5ebacb1a4c1fc965124aa06e3df85df 100644 (file)
@@ -207,16 +207,16 @@ public class SocketCommunicator implements ICommunicator {
 
                                        }
 
+                                       if (isTerminated) {
+                                               list.clear();
+                                               break;
+                                       }
+
                                        for ( SkinSendData data : list ) {
                                                sendToQEMUInternal( data );
                                        }
 
                                        list.clear();
-
-                                       if ( isTerminated ) {
-                                               break;
-                                       }
-
                                }
 
                        }
@@ -406,7 +406,6 @@ public class SocketCommunicator implements ICommunicator {
                                        break;
                                }
                                }
-
                        } catch (IOException e) {
                                logger.log(Level.SEVERE, e.getMessage(), e);
                                break;
@@ -414,6 +413,7 @@ public class SocketCommunicator implements ICommunicator {
 
                }
 
+               logger.info("communicatorThread is stopped");
        }
 
        private void receiveData(
@@ -674,9 +674,15 @@ public class SocketCommunicator implements ICommunicator {
 
        @Override
        public void terminate() {
-               isTerminated = true;
+               if (isTerminated == true) {
+                       logger.info("has been terminated");
+                       return;
+               }
+
                logger.info("terminated");
 
+               isTerminated = true;
+
                if (null != sendQueue) {
                        synchronized (sendQueue) {
                                sendQueue.notifyAll();
@@ -685,6 +691,7 @@ public class SocketCommunicator implements ICommunicator {
 
                if (null != heartbeatTimer) {
                        heartbeatTimer.cancel();
+                       heartbeatTimer = null;
                }
 
                IOUtil.closeSocket(socket);
index bfcf6f2ff376d5165945681c04588c74bc21f5ff..43f4077e9ae10abe901756ca916f5b2176167603 100644 (file)
@@ -1,7 +1,7 @@
 /**
  * 
  *
- * Copyright (C) 2011 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
+ * Copyright (C) 2011 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
  *
  * Contact:
  * GiWoong Kim <giwoong.kim@samsung.com>
@@ -40,22 +40,24 @@ import java.net.Socket;
  *
  */
 public class IOUtil {
+       private IOUtil() {
+               /* do nothing */
+       }
 
-       private IOUtil(){}
-
-       public static void closeSocket( Socket socket ) {
+       public static void closeSocket(Socket socket) {
                try {
-                       if ( null != socket ) {
+                       if (null != socket) {
                                socket.close();
+                               socket = null;
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }
 
-       public static void close( Closeable closeable ) {
+       public static void close(Closeable closeable) {
                try {
-                       if ( null != closeable ) {
+                       if (null != closeable) {
                                closeable.close();
                        }
                } catch (IOException e) {
@@ -63,18 +65,15 @@ public class IOUtil {
                }
        }
 
-       public static byte[] getBytes( InputStream is ) throws IOException {
-               
+       public static byte[] getBytes(InputStream is) throws IOException {
                ByteArrayOutputStream bao = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                
                int read = 0;
-               while( 0 < ( read = is.read( buffer ) ) ) {
-                       bao.write( buffer, 0, read );
+               while(0 < (read = is.read(buffer))) {
+                       bao.write(buffer, 0, read);
                }
-               
+
                return bao.toByteArray();
-               
        }
-       
 }