]> rtime.felk.cvut.cz Git - frescor/forb.git/blobdiff - src/executor.c
forb: Fix deadlock in executor
[frescor/forb.git] / src / executor.c
index db1cf6b7420cf5e8a1a840f726f7495049e0ca01..08e00680f448689f782d1b8b68cd167a3484cc96 100644 (file)
@@ -1,7 +1,81 @@
+/**************************************************************************/
+/* ---------------------------------------------------------------------- */
+/* Copyright (C) 2006 - 2008 FRESCOR consortium partners:                */
+/*                                                                       */
+/*   Universidad de Cantabria,              SPAIN                        */
+/*   University of York,                    UK                           */
+/*   Scuola Superiore Sant'Anna,            ITALY                        */
+/*   Kaiserslautern University,             GERMANY                      */
+/*   Univ. Politécnica  Valencia,           SPAIN                       */
+/*   Czech Technical University in Prague,  CZECH REPUBLIC               */
+/*   ENEA                                   SWEDEN                       */
+/*   Thales Communication S.A.              FRANCE                       */
+/*   Visual Tools S.A.                      SPAIN                        */
+/*   Rapita Systems Ltd                     UK                           */
+/*   Evidence                               ITALY                        */
+/*                                                                       */
+/*   See http://www.frescor.org for a link to partners' websites         */
+/*                                                                       */
+/*          FRESCOR project (FP6/2005/IST/5-034026) is funded            */
+/*       in part by the European Union Sixth Framework Programme         */
+/*       The European Union is not liable of any use that may be         */
+/*       made of this code.                                              */
+/*                                                                       */
+/*                                                                       */
+/*  This file is part of FORB (Frescor Object Request Broker)            */
+/*                                                                       */
+/* FORB is free software; you can redistribute it and/or modify it       */
+/* under terms of the GNU General Public License as published by the     */
+/* Free Software Foundation; either version 2, or (at your option) any   */
+/* later version.  FORB is distributed in the hope that it will be       */
+/* useful, but WITHOUT ANY WARRANTY; without even the implied warranty   */
+/* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   */
+/* General Public License for more details. You should have received a   */
+/* copy of the GNU General Public License along with FORB; see file      */
+/* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
+/* Cambridge, MA 02139, USA.                                             */
+/*                                                                       */
+/* As a special exception, including FORB header files in a file,        */
+/* instantiating FORB generics or templates, or linking other files      */
+/* with FORB objects to produce an executable application, does not      */
+/* by itself cause the resulting executable application to be covered    */
+/* by the GNU General Public License. This exception does not            */
+/* however invalidate any other reasons why the executable file might be  */
+/* covered by the GNU Public License.                                    */
+/**************************************************************************/
+
+/**
+ * @file   executor.c
+ * @author Michal Sojka <sojkam1@fel.cvut.cz>
+ * @date   Sun Oct 12 16:18:04 2008
+ * 
+ * @brief  Implementation of executors.
+ */
+
+
 #include "executor.h"
 #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.
+ * 
+ * @param executor 
+ * 
+ * @return Zero on success, FOSA error code on error.
+ */
 int forb_executor_init(forb_executor_t *executor)
 {
        int ret;
@@ -12,8 +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);
        return 0;
 }
+
 void forb_executor_destroy(forb_executor_t *executor)
 {
        /* TODO: */
@@ -38,18 +115,41 @@ int forb_executor_register_object(forb_executor_t *executor, forb_object obj)
 }
 
 /** 
- * Executor's main loop which processes requests.
+ * Unregisteres the object @a obj from @a executor.
+ * 
+ * @param executor
+ * @param obj 
+ */
+void forb_executor_unregister_object(forb_executor_t *executor, forb_object obj)
+{
+       if (obj) {
+               obj->executor = NULL;
+       }
+}
+
+/** 
+ * Executor's main loop which executes object implementation methods
+ * upon request.
+ *
+ * The requests are represented by ::forb_exec_req_t and are enqueued
+ * to the executor's request queue by receiver threads of individual
+ * ports (forb_iop_receiver_thread()).
  * 
  * @param executor 
  * 
- * @return Will be defined later.
+ * @return Zero on normal termination, non-zero error codes will be
+ * defined later.
  */
 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);
@@ -59,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.
  * 
@@ -80,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);
 
@@ -88,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);
+}