]> rtime.felk.cvut.cz Git - frescor/forb.git/blobdiff - src/executor.c
forb: Add forb_executor_synchronize()
[frescor/forb.git] / src / executor.c
index 8c9229a3f78dc4473aefb8b98fb8cb30864dd980..b24db98b55c00ba1e130a5d70acf2f4b23c285b7 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);
 
-int forb_executor_key = -1;
+static pthread_key_t forb_executor_key = -1;
+
+int forb_executor_prepare()
+{
+       return pthread_key_create(&forb_executor_key, NULL);
+}
 
 /** 
  * Initializes executor.
@@ -79,6 +86,11 @@ 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);
+
+       executor->finish = false;
+       fosa_cond_init(&executor->finished);
        return 0;
 }
 
@@ -87,6 +99,21 @@ void forb_executor_destroy(forb_executor_t *executor)
        /* TODO: */
 }
 
+/** 
+ * Waits for the executor to finish processing requests.
+ * 
+ * @param executor 
+ */
+void forb_executor_synchronize(forb_executor_t *executor)
+{
+       fosa_mutex_lock(&executor->mutex);
+       executor->finish = true;
+       fosa_cond_signal(&executor->new_request_in_empty_list);
+       fosa_cond_wait(&executor->finished, &executor->mutex);
+       fosa_mutex_unlock(&executor->mutex);
+
+}
+
 /** 
  * Setup the object @a obj so that requests to it are executed within
  * the thread of the @a executor.
@@ -134,21 +161,13 @@ void forb_executor_unregister_object(forb_executor_t *executor, forb_object obj)
 int forb_executor_run(forb_executor_t *executor)
 {
        int ret;
-       
-       // initializing thread specific data (FIXME: maybe should be somewhere else)
-       if (forb_executor_key == -1) {
-               if (ret = fosa_key_create(forb_executor_key))
-                       goto ret;
-       }
+
        // setting pointer to executor as thread specific data
-       if (ret = fosa_thread_set_specific_data(forb_executor_key, 
-                                       fosa_thread_self(), (void *) executor))         
+       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);
@@ -158,6 +177,14 @@ int forb_executor_run(forb_executor_t *executor)
 
                        fosa_mutex_lock(&executor->mutex);
                }
+               if (executor->finish) {
+                       fosa_cond_broadcast(&executor->finished);
+                       executor->finish = false;
+               }
+
+                       
+               fosa_cond_wait(&executor->new_request_in_empty_list,
+                              &executor->mutex);
        }
        fosa_mutex_unlock(&executor->mutex);
 ret:   
@@ -166,6 +193,8 @@ 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.
  * 
@@ -180,6 +209,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);
 
@@ -193,14 +225,10 @@ error:
 /**
  * Determines the executor we are currently in.
  *
- * @param executor Current executor.
- *
- * @return Zero in case of success.
+ * @return Pointer to the current executor or NULL if not called
+ * within executor.
  */
-int forb_get_current_executor(forb_executor_t *executor)
+forb_executor_t *forb_get_current_executor(void)
 {
-       int ret;
-       ret = fosa_thread_get_specific_data(forb_executor_key, 
-                                           fosa_thread_self(), &executor);
-       return ret;
+       return pthread_getspecific(forb_executor_key);
 }