Просмотр исходного кода

[LibOS] Move helper threads' status checking after lock acquisition

borysp 6 лет назад
Родитель
Сommit
f71bd2ca84
2 измененных файлов с 19 добавлено и 4 удалено
  1. 11 1
      LibOS/shim/src/ipc/shim_ipc_helper.c
  2. 8 3
      LibOS/shim/src/shim_async.c

+ 11 - 1
LibOS/shim/src/ipc/shim_ipc_helper.c

@@ -849,8 +849,13 @@ static int create_ipc_helper(void) {
  * problematic for the thread itself to release its own resources e.g. stack).
  */
 struct shim_thread* terminate_ipc_helper(void) {
-    if (ipc_helper_state != HELPER_ALIVE)
+    /* First check if thread is alive. */
+    lock(&ipc_helper_lock);
+    if (ipc_helper_state != HELPER_ALIVE) {
+        unlock(&ipc_helper_lock);
         return NULL;
+    }
+    unlock(&ipc_helper_lock);
 
     /* NOTE: Graphene doesn't have an abstraction of a queue of pending signals
      * between communicating processes (instead all communication is done over
@@ -867,6 +872,11 @@ struct shim_thread* terminate_ipc_helper(void) {
     DkThreadDelayExecution(500);
 
     lock(&ipc_helper_lock);
+    if (ipc_helper_state != HELPER_ALIVE) {
+        unlock(&ipc_helper_lock);
+        return NULL;
+    }
+
     struct shim_thread* ret = ipc_helper_thread;
     if (ret)
         get_thread(ret);

+ 8 - 3
LibOS/shim/src/shim_async.c

@@ -114,8 +114,10 @@ int64_t install_async_event(PAL_HANDLE object, uint64_t time,
 
     if (async_helper_state == HELPER_NOTALIVE) {
         int ret = create_async_helper();
-        if (ret < 0)
+        if (ret < 0) {
+            unlock(&async_helper_lock);
             return ret;
+        }
     }
 
     unlock(&async_helper_lock);
@@ -306,10 +308,13 @@ static int create_async_helper(void) {
  * problematic for the thread itself to release its own resources e.g. stack).
  */
 struct shim_thread* terminate_async_helper(void) {
-    if (async_helper_state != HELPER_ALIVE)
+    lock(&async_helper_lock);
+
+    if (async_helper_state != HELPER_ALIVE) {
+        unlock(&async_helper_lock);
         return NULL;
+    }
 
-    lock(&async_helper_lock);
     struct shim_thread* ret = async_helper_thread;
     if (ret)
         get_thread(ret);