]> rtime.felk.cvut.cz Git - frescor/forb.git/blobdiff - src/executor.c
forb: Fix deadlock in executor
[frescor/forb.git] / src / executor.c
index da1a4d70304f6df240c50c8f7b3e69ebe57bfeae..08e00680f448689f782d1b8b68cd167a3484cc96 100644 (file)
 #include "exec_req.h"
 #include "object.h"
 #include <ul_log.h>
+#include <stdio.h>
+#include <pthread.h>
 
 extern UL_LOG_CUST(ulogd_forb_executor);
 
+static pthread_key_t forb_executor_key = -1;
+
+int forb_executor_prepare()
+{
+       return pthread_key_create(&forb_executor_key, NULL);
+}
+
 /** 
  * Initializes executor.
  * 
@@ -77,6 +86,8 @@ int forb_executor_init(forb_executor_t *executor)
        if (ret) return ret;
 
        forb_exec_req_nolock_init_head(executor);
+       
+       forb_syncobj_init(&executor->reply_processed, 0);
        return 0;
 }
 
@@ -131,10 +142,14 @@ void forb_executor_unregister_object(forb_executor_t *executor, forb_object obj)
  */
 int forb_executor_run(forb_executor_t *executor)
 {
+       int ret;
+
+       // setting pointer to executor as thread specific data
+       if ((ret = pthread_setspecific(forb_executor_key, executor)))           
+               goto ret;
+       
        fosa_mutex_lock(&executor->mutex);
        while (1) {
-               fosa_cond_wait(&executor->new_request_in_empty_list,
-                              &executor->mutex);
                while (!forb_exec_req_nolock_is_empty(executor)) {
                        forb_exec_req_t *exec_req;
                        exec_req = forb_exec_req_nolock_cut_first(executor);
@@ -144,13 +159,18 @@ int forb_executor_run(forb_executor_t *executor)
 
                        fosa_mutex_lock(&executor->mutex);
                }
+               fosa_cond_wait(&executor->new_request_in_empty_list,
+                              &executor->mutex);
        }
        fosa_mutex_unlock(&executor->mutex);
-       return 0;
+ret:   
+       return ret;
 }
 
 /** 
  * Convenience function for executing only one object in one executor.
+ * This function calls forb_signal_server_ready() at the appropriate
+ * place.
  * 
  * @param obj The object to execute.
  * 
@@ -165,6 +185,9 @@ int forb_execute_object(forb_object obj)
        if (ret) goto error;
        ret = forb_executor_register_object(&executor, obj);
        if (ret) goto destroy_and_error;
+
+       ret = forb_signal_server_ready(obj->orb);
+       if (ret) goto destroy_and_error;
        
        ret = forb_executor_run(&executor);
 
@@ -173,3 +196,15 @@ destroy_and_error:
 error:
        return ret;
 }
+
+
+/**
+ * Determines the executor we are currently in.
+ *
+ * @return Pointer to the current executor or NULL if not called
+ * within executor.
+ */
+forb_executor_t *forb_get_current_executor(void)
+{
+       return pthread_getspecific(forb_executor_key);
+}