[openrtm-commit:00662] r2307 - trunk/OpenRTM-aist/src/lib/rtm

openrtm @ openrtm.org openrtm @ openrtm.org
2012年 2月 6日 (月) 06:29:18 JST


Author: n-ando
Date: 2012-02-06 06:29:15 +0900 (Mon, 06 Feb 2012)
New Revision: 2307

Modified:
   trunk/OpenRTM-aist/src/lib/rtm/ExecutionContextBase.cpp
   trunk/OpenRTM-aist/src/lib/rtm/ExecutionContextBase.h
   trunk/OpenRTM-aist/src/lib/rtm/ExtTrigExecutionContext.cpp
   trunk/OpenRTM-aist/src/lib/rtm/ExtTrigExecutionContext.h
   trunk/OpenRTM-aist/src/lib/rtm/Makefile.am
   trunk/OpenRTM-aist/src/lib/rtm/OpenHRPExecutionContext.cpp
   trunk/OpenRTM-aist/src/lib/rtm/OpenHRPExecutionContext.h
   trunk/OpenRTM-aist/src/lib/rtm/PeriodicExecutionContext.cpp
   trunk/OpenRTM-aist/src/lib/rtm/PeriodicExecutionContext.h
Log:
[incompat,impl/header,func] New ECs based on new template method patterned ECBase have been implemented. Sync/async activation/deactivation/reset problems has been solved by new EC options. refs #2321 #2315


Modified: trunk/OpenRTM-aist/src/lib/rtm/ExecutionContextBase.cpp
===================================================================
--- trunk/OpenRTM-aist/src/lib/rtm/ExecutionContextBase.cpp	2012-02-05 21:21:42 UTC (rev 2306)
+++ trunk/OpenRTM-aist/src/lib/rtm/ExecutionContextBase.cpp	2012-02-05 21:29:15 UTC (rev 2307)
@@ -5,8 +5,7 @@
  * @date $Date$
  * @author Noriaki Ando <n-ando at aist.go.jp>
  *
- * Copyright (C) 2011
- *     Task-intelligence Research Group,
+ * Copyright (C) 2011-2012
  *     Intelligent Systems Research Institute,
  *     National Institute of
  *         Advanced Industrial Science and Technology (AIST), Japan
@@ -16,10 +15,20 @@
  *
  */
 
+#include <rtm/NVUtil.h>
+#include <rtm/RTObjectStateMachine.h>
 #include <rtm/ExecutionContextBase.h>
 
 namespace RTC
 {
+  ExecutionContextBase::ExecutionContextBase(const char* name)
+    : rtclog("ec_base"),
+      m_activationTimeout(0.5),
+      m_deactivationTimeout(0.5),
+      m_resetTimeout(0.5),
+      m_syncActivation(true), m_syncDeactivation(true), m_syncReset(true)
+  {
+  }
   /*!
    * @if jp
    * @brief 仮想デストラクタ
@@ -30,7 +39,7 @@
   ExecutionContextBase::~ExecutionContextBase(void)
   {
   }
-
+  
   /*!
    * @if jp
    * @brief ExecutionContextの処理を進める
@@ -40,30 +49,844 @@
    */
   void ExecutionContextBase::init(coil::Properties& props)
   {
+    RTC_TRACE(("init()"));
+    RTC_DEBUG_STR((props));
+    
+    // getting rate
+    setExecutionRate(props);
+    
+    // getting sync/async mode flag
+    bool transitionMode;
+    if (setTransitionMode(props, "sync_transition", transitionMode))
+      {
+        m_syncActivation   = transitionMode;
+        m_syncDeactivation = transitionMode;
+        m_syncReset        = transitionMode;
+      }
+    setTransitionMode(props, "sync_activation", m_syncActivation);
+    setTransitionMode(props, "sync_deactivation", m_syncDeactivation);
+    setTransitionMode(props, "sync_reset", m_syncReset);
+    
+    // getting transition timeout
+    coil::TimeValue timeout;
+    if (setTimeout(props, "transition_timeout", timeout))
+      {
+        m_activationTimeout   = timeout;
+        m_deactivationTimeout = timeout;
+        m_resetTimeout        = timeout;
+      }
+    setTimeout(props, "activation_timeout",   m_activationTimeout);
+    setTimeout(props, "deactivation_timeout", m_deactivationTimeout);
+    setTimeout(props, "reset_timeout",        m_resetTimeout);
+
+    RTC_DEBUG(("ExecutionContext's configurations:"));
+    RTC_DEBUG(("Exec rate   : %f [Hz]", getRate()));
+    RTC_DEBUG(("Activation  : Sync = %s, Timeout = %f",
+               m_syncActivation ? "YES" : "NO",
+               (double)m_activationTimeout));
+    RTC_DEBUG(("Deactivation: Sync = %s, Timeout = %f",
+               m_syncDeactivation ? "YES" : "NO",
+               (double)m_deactivationTimeout));
+    RTC_DEBUG(("Reset       : Sync = %s, Timeout = %f",
+               m_syncReset ? "YES" : "NO",
+               (double)m_resetTimeout));
+    // Setting given Properties to EC's profile::properties
+    setProperties(props);
   }
+  
+  /*!
+   * @if jp
+   * @brief コンポーネントをバインドする。
+   *
+   * コンポーネントをバインドする。
+   *
+   * @else
+   * @brief Bind the component.
+   *
+   * Bind the component.
+   *
+   * @endif
+   */
+  RTC::ReturnCode_t ExecutionContextBase::
+  bindComponent(RTC::RTObject_impl* rtc)
+  {
+    return m_worker.bindComponent(rtc);
+  }
+  
+  //============================================================
+  // Functions to be delegated by EC's CORBA operations
+  /*!
+   * @if jp
+   * @brief ExecutionContext 実行状態確認関数
+   * @else
+   * @brief Check for ExecutionContext running state
+   * @endif
+   */
+  CORBA::Boolean ExecutionContextBase::isRunning()
+  {
+    RTC_TRACE(("isRunning()"));
+    return m_worker.isRunning();
+  }
+  
+  /*!
+   * @if jp
+   * @brief ExecutionContext の実行を開始
+   * @else
+   * @brief Start the ExecutionContext
+   * @endif
+   */
+  RTC::ReturnCode_t ExecutionContextBase::start()
+  {
+    RTC_TRACE(("start()"));
+    RTC::ReturnCode_t ret = onStarting(); // Template
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("onStarting() failed. Starting EC aborted."));
+        return ret;
+      }
+    ret = m_worker.start(); // Actual start()
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("Invoking on_startup() for each RTC failed."));
+        return ret;
+      }
+    ret = onStarted(); // Template
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("Invoking on_startup() for each RTC failed."));
+        m_worker.stop();
+        RTC_ERROR(("on_shutdown() was invoked, because of on_startup"));
+        return ret;
+      }
+    return ret;
+  }
+  
+  /*!
+   * @if jp
+   * @brief ExecutionContext の実行を停止
+   * @else
+   * @brief Stopping the ExecutionContext
+   * @endif
+   */
+  RTC::ReturnCode_t ExecutionContextBase::stop()
+  {
+    RTC_TRACE(("start()"));
+    RTC::ReturnCode_t ret = onStopping(); // Template
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("onStopping() failed. Stopping EC aborted."));
+        return ret;
+      }
+    ret = m_worker.stop(); // Actual stop()
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("Invoking on_shutdown() for each RTC failed."));
+        return ret;
+      }
+    ret = onStopped(); // Template
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("Invoking on_shutdown() for each RTC failed."));
+        return ret;
+      }
+    return ret;
+  }
+  
+  /*!
+   * @if jp
+   * @brief ExecutionContext の実行周期(Hz)を取得する
+   * @else
+   * @brief Get execution rate(Hz) of ExecutionContext
+   * @return Execution cycle(Unit:Hz)
+   * @endif
+   */
+  double ExecutionContextBase::getRate(void) const
+  {
+    double rate = m_profile.getRate(); // Actual getRate()
+    return onGetRate(rate); // Template
+  }
+  
+  coil::TimeValue ExecutionContextBase::getPeriod(void) const
+  {
+    coil::TimeValue period = m_profile.getPeriod();
+    return period;
+  }
+  
+  /*!
+   * @if jp
+   * @brief ExecutionContext の実行周期(Hz)を設定する
+   * @else
+   * @brief Set execution rate(Hz) of ExecutionContext
+   * @endif
+   */
+  RTC::ReturnCode_t ExecutionContextBase::setRate(double rate)
+  {
+    RTC_TRACE(("setRate(%f)", rate));
+    RTC::ReturnCode_t ret = m_profile.setRate(onSettingRate(rate));
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("Setting execution rate failed. %f", rate));
+        return ret;
+      }
+    ret = onSetRate(rate);
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("onSetRate(%f) failed.", rate));
+        return ret;
+      }
+    RTC_INFO(("setRate(%f) done", rate));
+    return ret;
+  }
+  
+  /*!
+   * @if jp
+   * @brief RTコンポーネントを追加する
+   * @else
+   * @brief Add an RT-component
+   * @endif
+   */
+  RTC::ReturnCode_t ExecutionContextBase::
+  addComponent(RTC::LightweightRTObject_ptr comp)
+  {
+    RTC_TRACE(("addComponent()"));
+    RTC::ReturnCode_t ret = onAddingComponent(comp); // Template
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("Error: onAddingComponent(). RTC is not attached."));
+        return ret;
+      }
+    ret = m_worker.addComponent(comp); // Actual addComponent()
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("Error: ECWorker addComponent() faild."));
+        return ret;
+      }
+    ret = m_profile.addComponent(comp); // Actual addComponent()
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("Error: ECProfile addComponent() faild."));
+        return ret;
+      }
+    ret = onAddedComponent(comp); // Template
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("Error: onAddedComponent() faild."));
+        RTC_INFO(("Removing attached RTC."));
+        m_worker.removeComponent(comp);
+        m_profile.removeComponent(comp);
+        return ret;
+      }
+    RTC_INFO(("Component has been added to this EC."));
+    return RTC::RTC_OK;
+  }
+  
+  /*!
+   * @if jp
+   * @brief RTコンポーネントを参加者リストから削除する
+   * @else
+   * @brief Remove the RT-Component from participant list
+   * @endif
+   */
+  RTC::ReturnCode_t ExecutionContextBase::
+  removeComponent(RTC::LightweightRTObject_ptr comp)
+  {
+    RTC_TRACE(("removeComponent()"));
+    RTC::ReturnCode_t ret = onRemovingComponent(comp); // Template
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("Error: onRemovingComponent(). "
+                   "RTC will not not attached."));
+        return ret;
+      }
+    ret = m_worker.removeComponent(comp); // Actual removeComponent()
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("Error: ECWorker removeComponent() faild."));
+        return ret;
+      }
+    ret = m_profile.removeComponent(comp); // Actual removeComponent()
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("Error: ECProfile removeComponent() faild."));
+        return ret;
+      }
+    ret = onRemovedComponent(comp); // Template
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("Error: onRemovedComponent() faild."));
+        RTC_INFO(("Removing attached RTC."));
+        m_worker.removeComponent(comp);
+        m_profile.removeComponent(comp);
+        return ret;
+      }
+    RTC_INFO(("Component has been removeed to this EC."));
+    return RTC::RTC_OK;
+  }
+  
+  /*!
+   * @if jp
+   * @brief RTコンポーネントをアクティブ化する
+   * @else
+   * @brief Activate an RT-component
+   * @endif
+   */
+  RTC::ReturnCode_t ExecutionContextBase::
+  activateComponent(RTC::LightweightRTObject_ptr comp)
+  {
+    RTC_TRACE(("activateComponent()"));
+    RTC::ReturnCode_t ret = onActivating(comp); // Template
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("onActivating() failed."));
+        return ret;
+      }
+    RTC_impl::RTObjectStateMachine* rtobj;
+    ret = m_worker.activateComponent(comp, rtobj); // Actual activateComponent()
+    if (ret != RTC::RTC_OK) { return ret; }
+    if (!m_syncActivation) // Asynchronous activation mode
+      {
+        ret = onActivated(rtobj, -1);
+        if (ret != RTC::RTC_OK)
+          {
+            RTC_ERROR(("onActivated() failed."));
+          }
+        return ret;
+      }
+    //------------------------------------------------------------
+    // Synchronized activation mode
+    RTC_DEBUG(("Synchronous activation mode. "
+               "Waiting for the RTC to be ACTIVE state. "));
+    return waitForActivated(rtobj);
+  }
 
+  RTC::ReturnCode_t ExecutionContextBase::
+  waitForActivated(RTC_impl::RTObjectStateMachine* rtobj)
+  {
+    long int count(0);
+    RTC::ReturnCode_t ret = onWaitingActivated(rtobj, count);
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("onWaitingActivated failed."));
+        return ret;
+      }
+    long int cycle =
+      (long int)((double)m_activationTimeout / (double)getPeriod());
+    RTC_DEBUG(("Timeout is %f [s] (%f [s] in %d times)",
+               (double)m_activationTimeout, getRate(), cycle));
+    // Wating INACTIVE -> ACTIVE
+    coil::TimeValue starttime(coil::gettimeofday());
+    while (rtobj->isCurrentState(RTC::INACTIVE_STATE))
+      {
+        ret = onWaitingActivated(rtobj, count); // Template method
+        if (ret != RTC::RTC_OK)
+          {
+            RTC_ERROR(("onWaitingActivated failed."));
+            return ret;
+          }
+        coil::sleep(getPeriod());
+        coil::TimeValue delta(coil::gettimeofday() - starttime);
+        RTC_DEBUG(("Waiting to be ACTIVE state. %f [s] slept (%d/%d)",
+                   (double)delta, count, cycle));
+        ++count;
+        if (delta > m_activationTimeout || count > cycle)
+          {
+            RTC_WARN(("The component is not responding."));
+            break;
+          }
+      }
+    // Now State must be ACTIVE or ERROR
+    if (rtobj->isCurrentState(RTC::INACTIVE_STATE))
+      {
+        RTC_ERROR(("Unknown error: Invalid state transition."));
+        return RTC::RTC_ERROR;
+      }
+    RTC_DEBUG(("Current state is %s", getStateString(rtobj->getState())));
+    ret = onActivated(rtobj, count); // Template method
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("onActivated() failed."));
+      }
+    RTC_DEBUG(("onActivated() done."))
+    return ret;
+  }
 
-    /*!
-     * @if jp
-     * @brief コンポーネントをバインドする。
-     * @else
-     * @brief Bind the component.
-     * @endif
-     */
-  //  TC::ReturnCode_t bindComponent(RTObject_impl* rtc) = 0;
+  /*!
+   * @if jp
+   * @brief RTコンポーネントを非アクティブ化する
+   * @else
+   * @brief Deactivate an RT-component
+   * @endif
+   */
+  RTC::ReturnCode_t ExecutionContextBase::
+  deactivateComponent(RTC::LightweightRTObject_ptr comp)
+  {
+    RTC_TRACE(("deactivateComponent()"));
+    RTC::ReturnCode_t ret = onDeactivating(comp); // Template
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("onDeactivatingComponent() failed."));
+        return ret;
+      }
+    // Deactivate all the RTCs
+    RTC_impl::RTObjectStateMachine* rtobj;
+    ret = m_worker.deactivateComponent(comp, rtobj);
+    if (ret != RTC::RTC_OK) { return ret; }
+    if (!m_syncDeactivation)
+      {
+        ret = onDeactivated(rtobj, -1);
+        if (ret != RTC::RTC_OK)
+          {
+            RTC_ERROR(("onDeactivated() failed."));
+          }
+        return ret;
+      }
+    //------------------------------------------------------------
+    // Waiting for synchronized deactivation
+    RTC_DEBUG(("Synchronous deactivation mode. "
+               "Waiting for the RTC to be INACTIVE state. "));
+    return waitForDeactivated(rtobj);
+  }
 
-    /*!
-     * @if jp
-     * @brief オブジェクトのリファレンスを取得する。
-     *
-     * オブジェクトのリファレンスを取得する。
-     *
-     * @else
-     * @brief Get the reference of the object. 
-     *
-     * Get the reference of the object.
-     *
-     * @endif
-     */
-  //    virtual RTC::ExecutionContextService_ptr getObjRef() = 0;
+  RTC::ReturnCode_t ExecutionContextBase::
+  waitForDeactivated(RTC_impl::RTObjectStateMachine* rtobj)
+  {
+    long int count(0);
+    RTC::ReturnCode_t ret = onWaitingDeactivated(rtobj, count);
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("onWaitingDeactivated failed."));
+        return ret;
+      }
+    long int cycle =
+      (long int)((double)m_deactivationTimeout / (double)getPeriod());
+    RTC_DEBUG(("Timeout is %f [s] (%f [s] in %d times)",
+               (double)m_deactivationTimeout, getRate(), cycle));
+    // Wating ACTIVE -> INACTIVE
+    coil::TimeValue starttime(coil::gettimeofday());
+    while (rtobj->isCurrentState(RTC::ACTIVE_STATE))
+      {
+        ret = onWaitingDeactivated(rtobj, count); // Template method
+        if (ret != RTC::RTC_OK)
+          {
+            RTC_ERROR(("onWaitingDeactivated failed."));
+            return ret;
+          }
+        coil::sleep(getPeriod());
+        coil::TimeValue delta(coil::gettimeofday() - starttime);
+        RTC_DEBUG(("Waiting to be INACTIVE state. Sleeping %f [s] (%d/%d)",
+                   (double)delta, count, cycle));
+        ++count;
+        if (delta > m_deactivationTimeout || count > cycle)
+          {
+            RTC_ERROR(("The component is not responding."));
+            break;
+          }
+      }
+    // Now State must be INACTIVE or ERROR
+    if (rtobj->isCurrentState(RTC::ACTIVE_STATE))
+      {
+        RTC_ERROR(("Unknown error: Invalid state transition."));
+        return RTC::RTC_ERROR;
+      }
+    RTC_DEBUG(("Current state is %s", getStateString(rtobj->getState())));
+    ret = onDeactivated(rtobj, count);
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("onDeactivated() failed."));
+      }
+    RTC_DEBUG(("onDeactivated() done."))
+    return ret;
+  }
+  
+  /*!
+   * @if jp
+   * @brief RTコンポーネントをリセットする
+   * @else
+   * @brief Reset the RT-component
+   * @endif
+   */
+  RTC::ReturnCode_t ExecutionContextBase::
+  resetComponent(RTC::LightweightRTObject_ptr comp)
+  {
+    RTC_TRACE(("resetComponent()"));
+    RTC::ReturnCode_t ret = onResetting(comp); // Template
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("onResetting() failed."));
+        return ret;
+      }
+    RTC_impl::RTObjectStateMachine* rtobj;
+    ret = m_worker.resetComponent(comp, rtobj); // Actual resetComponent()
+    if (ret != RTC::RTC_OK) { return ret; }
+    if (!m_syncReset)
+      {
+        ret = onReset(rtobj, -1);
+        if (ret != RTC::RTC_OK)
+          {
+            RTC_ERROR(("onReset() failed."));
+          }
+        return ret;
+      }
+    //------------------------------------------------------------
+    // Waiting for synchronized reset
+    RTC_DEBUG(("Synchronous reset mode. "
+               "Waiting for the RTC to be INACTIVE state. "));
+    return waitForReset(rtobj);
+  }
+  
+  RTC::ReturnCode_t ExecutionContextBase::
+  waitForReset(RTC_impl::RTObjectStateMachine* rtobj)
+  {
+    long int count(0);
+    RTC::ReturnCode_t ret = onWaitingReset(rtobj, count);
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("onWaitingReset() failed."));
+        return ret;
+      }
+    long int cycle =
+      (long int)((double)m_resetTimeout / (double)getPeriod());
+    RTC_DEBUG(("Timeout is %f [s] (%f [s] in %d times)",
+               (double)m_resetTimeout, getRate(), cycle));
+    // Wating ERROR -> INACTIVE
+    coil::TimeValue starttime(coil::gettimeofday());
+    while (rtobj->isCurrentState(RTC::ERROR_STATE))
+      {
+        ret = onWaitingReset(rtobj, count); // Template
+        if (ret != RTC::RTC_OK)
+          {
+            RTC_ERROR(("onWaitingReset failed."));
+            return ret;
+          }
+        coil::sleep(getPeriod());
+        coil::TimeValue delta(coil::gettimeofday() - starttime);
+        RTC_DEBUG(("Waiting to be INACTIVE state. Sleeping %f [s] (%d/%d)",
+                   (double)delta, count, cycle));
+        ++count;
+        if (delta > m_resetTimeout || count > cycle)
+          {
+            RTC_ERROR(("The component is not responding."));
+            break;
+          }
+      }
+    // Now State must be INACTIVE
+    if (!rtobj->isCurrentState(RTC::INACTIVE_STATE))
+      {
+        RTC_ERROR(("Unknown error: Invalid state transition."));
+        return RTC::RTC_ERROR;
+      }
+    RTC_DEBUG(("Current state is %s", getStateString(rtobj->getState())));
+    ret = onReset(rtobj, count); // Template method
+    if (ret != RTC::RTC_OK)
+      {
+        RTC_ERROR(("onResetd() failed."));
+      }
+    RTC_DEBUG(("onReset() done."))
+    return ret;
+  }
+
+  /*!
+   * @if jp
+   * @brief RTコンポーネントの状態を取得する
+   *
+   * 指定したRTコンポーネントの状態(LifeCycleState)を取得する。指定し
+   * たRTコンポーネントが参加者リストに含まれない場合は、
+   * UNKNOWN_STATE が返される。
+   *
+   * @param comp 状態取得対象RTコンポーネント
+   *
+   * @return 現在の状態(LifeCycleState)
+   *
+   * @else
+   *
+   * @brief Get RT-component's state
+   *
+   * This operation shall report the LifeCycleState of the given
+   * participant RTC.  UNKNOWN_STATE will be returned, if the given
+   * RT-Component is not inclued in the participant list.
+   *
+   * @param comp The target RT-Component to get the state
+   *
+   * @return The current state of the target RT-Component(LifeCycleState)
+   *
+   * @endif
+   */
+  RTC::LifeCycleState ExecutionContextBase::
+  getComponentState(RTC::LightweightRTObject_ptr comp)
+  {
+    RTC::LifeCycleState state = m_worker.getComponentState(comp);
+    RTC_TRACE(("getComponentState() = %s", getStateString(state)));
+    if (state == RTC::CREATED_STATE)
+      {
+        RTC_ERROR(("CREATED state: not initialized "
+                   "RTC or unknwon RTC specified."));
+      }
+    return onGetComponentState(state);
+  }
+
+  const char* ExecutionContextBase::getStateString(RTC::LifeCycleState state)
+  {
+    return m_worker.getStateString(state);
+  }
+  /*!
+   * @if jp
+   * @brief ExecutionKind を取得する
+   *
+   * 本 ExecutionContext の ExecutionKind を取得する
+   *
+   * @return ExecutionKind
+   *
+   * @else
+   *
+   * @brief Get the ExecutionKind
+   *
+   * This operation shall report the execution kind of the execution
+   * context.
+   *
+   * @return ExecutionKind
+   *
+   * @endif
+   */
+  RTC::ExecutionKind ExecutionContextBase::getKind(void) const
+  {
+    RTC::ExecutionKind kind = m_profile.getKind();
+    RTC_TRACE(("getKind() = %s", getKindString(kind)));
+    kind = onGetKind(kind);
+    RTC_DEBUG(("onGetKind() returns %s", getKindString(kind)));
+    return kind;
+  }
+  
+  /*!
+   * @if jp
+   * @brief Profileを取得する
+   *
+   * RTC::ExecutionContextProfile を取得する。取得した
+   * ExecutionContextProfile の所有権は呼び出し側にある。取得されたオ
+   * ブジェクトが不要になった場合、呼び出し側が開放する責任を負う。
+   *
+   * @return RTC::ExecutionContextProfile
+   *
+   * @else
+   * @brief Getting Profile
+   *
+   * This function gets RTC::ExecutionContextProfile.  The ownership
+   * of the obtained ExecutionContextProfile is given to caller. The
+   * caller should release obtained object when it is unneccessary
+   * anymore.
+   *
+   * @return RTC::ExecutionContextProfile
+   *
+   * @endif
+   */
+  RTC::ExecutionContextProfile* ExecutionContextBase::getProfile(void)
+  {
+    RTC_TRACE(("getProfile()"));
+    RTC::ExecutionContextProfile* prof = m_profile.getProfile();
+    RTC_DEBUG(("kind: %s", getKindString(prof->kind)));
+    RTC_DEBUG(("rate: %f", prof->rate));
+    RTC_DEBUG(("properties:"));
+    coil::Properties props;
+    NVUtil::copyToProperties(props, prof->properties);
+    RTC_DEBUG_STR((props));
+    return onGetProfile(prof);
+  }
+  
+  //============================================================
+  // Delegated functions to ExecutionContextProfile
+  //============================================================
+  /*!
+   * @if jp
+   * @brief CORBA オブジェクト参照の取得
+   * @else
+   * @brief Get the reference to the CORBA object
+   * @endif
+   */
+  void ExecutionContextBase::setObjRef(RTC::ExecutionContextService_ptr ec_ptr)
+  {
+    m_worker.setECRef(ec_ptr);
+    m_profile.setObjRef(ec_ptr);
+  }
+  /*!
+   * @if jp
+   * @brief CORBA オブジェクト参照の取得
+   * @else
+   * @brief Getting object reference
+   * @endif
+   */
+  RTC::ExecutionContextService_ptr ExecutionContextBase::getObjRef(void) const
+  {
+    return m_profile.getObjRef();
+  }
+  
+  /*!
+   * @if jp
+   * @brief ExecutionKind を文字列化する
+   * @else
+   * @brief Converting ExecutionKind enum to string 
+   * @endif
+   */
+  const char* ExecutionContextBase::getKindString(RTC::ExecutionKind kind) const
+  {
+    return m_profile.getKindString(kind);
+  }
+  
+  /*!
+   * @if jp
+   * @brief ExecutionKind を設定する
+   * @else
+   * @brief Set the ExecutionKind
+   * @endif
+   */
+  RTC::ReturnCode_t ExecutionContextBase::setKind(RTC::ExecutionKind kind)
+  {
+    return m_profile.setKind(kind);
+  }
+  
+  /*!
+   * @if jp
+   * @brief Ownerコンポーネントをセットする。
+   * @else
+   * @brief Setting owner component of the execution context
+   * @endif
+   */
+  RTC::ReturnCode_t ExecutionContextBase::
+  setOwner(RTC::LightweightRTObject_ptr comp)
+  {
+    return m_profile.setOwner(comp);
+  }
+  
+  /*!
+   * @if jp
+   * @brief Ownerコンポーネントの参照を取得する
+   * @else
+   * @brief Getting a reference of the owner component
+   * @endif
+   */
+  const RTC::RTObject_ptr ExecutionContextBase::getOwner() const
+  {
+    return m_profile.getOwner();
+  }
+  
+  /*!
+   * @if jp
+   * @brief RTコンポーネントの参加者リストを取得する
+   * @else
+   * @brief Getting participant RTC list
+   * @endif
+   */
+  const RTC::RTCList& ExecutionContextBase::getComponentList() const
+  {
+    return m_profile.getComponentList();
+  }
+  
+  /*!
+   * @if jp
+   * @brief Propertiesをセットする
+   * @else
+   * @brief Setting Properties
+   * @endif
+   */
+  void ExecutionContextBase::setProperties(coil::Properties& props)
+  {
+    m_profile.setProperties(props);
+  }
+  
+  /*!
+   * @if jp
+   * @brief Propertiesを取得する
+   * @else
+   * @brief Setting Properties
+   * @endif
+   */
+  const coil::Properties ExecutionContextBase::getProperties() const
+  {
+    return m_profile.getProperties();
+  }
+  
+  /*!
+   * @if jp
+   * @brief Profileを取得する
+   * @else
+   * @brief Getting Profile
+   * @endif
+   */
+  const RTC::ExecutionContextProfile& ExecutionContextBase::
+  getProfile(void) const
+  {
+    return m_profile.getProfile();
+  }
+  // end of delegated functions to ExecutionContextProfile
+  //============================================================
+  
+  
+  //============================================================
+  // private functions
+  /*!
+   * @if jp
+   * @brief Propertiesから実行コンテキストをセットする
+   * @else
+   * @brief Setting execution rate from given properties.
+   * @endif
+   */
+  bool ExecutionContextBase::setExecutionRate(coil::Properties& props)
+  {
+    if (props.findNode("rate") != NULL)
+      {
+        double rate;
+        if (coil::stringTo(rate, props["rate"].c_str()))
+          {
+            setRate(rate);
+            return true;
+          }
+      }
+    return false;
+  }
+  
+  /*!
+   * @if jp
+   * @brief Propertiesから状態遷移モードをセットする
+   * @else
+   * @brief Setting state transition mode from given properties.
+   * @endif
+   */
+  bool ExecutionContextBase::
+  setTransitionMode(coil::Properties& props, const char* key, bool& flag)
+  {
+    RTC_TRACE(("setTransitionMode(%s)", key));
+    if (props.findNode(key) != NULL)
+      {
+        flag = coil::toBool(props[key], "YES", "NO", "YES");
+        RTC_DEBUG(("Transition Mode: %s = %s",
+                   key, flag ? "YES" : "NO"));
+        return true;
+      }
+    RTC_DEBUG(("Configuration %s not found.", key));
+    return false;
+  }
+  
+  /*!
+   * @if jp
+   * @brief Propertiesから状態遷移Timeoutをセットする
+   * @else
+   * @brief Setting state transition timeout from given properties.
+   * @endif
+   */
+  bool ExecutionContextBase::
+  setTimeout(coil::Properties& props, const char* key,
+             coil::TimeValue& timevalue)
+  {
+    RTC_TRACE(("setTimeout(%s)", key));
+    if (props.findNode(key) != NULL)
+      {
+        double timeout;
+        if (coil::stringTo(timeout, props[key].c_str()))
+          {
+            timevalue = timeout;
+            RTC_DEBUG(("Timeout (%s): %f [s]", key, timeout));
+            return true;
+          }
+      }
+    RTC_DEBUG(("Configuration %s not found.", key));
+    return false;
+  }
 };  // namespace RTC

Modified: trunk/OpenRTM-aist/src/lib/rtm/ExecutionContextBase.h
===================================================================
--- trunk/OpenRTM-aist/src/lib/rtm/ExecutionContextBase.h	2012-02-05 21:21:42 UTC (rev 2306)
+++ trunk/OpenRTM-aist/src/lib/rtm/ExecutionContextBase.h	2012-02-05 21:29:15 UTC (rev 2307)
@@ -5,8 +5,7 @@
  * @date $Date: 2008-01-14 07:48:55 $
  * @author Noriaki Ando <n-ando at aist.go.jp>
  *
- * Copyright (C) 2007
- *     Task-intelligence Research Group,
+ * Copyright (C) 2007, 2011-2012
  *     Intelligent Systems Research Institute,
  *     National Institute of
  *         Advanced Industrial Science and Technology (AIST), Japan
@@ -25,11 +24,14 @@
 #include <rtm/idl/OpenRTMSkel.h>
 #include <rtm/Factory.h>
 #include <rtm/ExecutionContextProfile.h>
+#include <rtm/ExecutionContextWorker.h>
 
 #ifdef WIN32
 #pragma warning( disable : 4290 )
 #endif
 
+#define DEFAULT_EXECUTION_RATE 1000
+
 namespace coil
 {
   class Properties;
@@ -42,6 +44,307 @@
    * @class ExecutionContextBase
    * @brief ExecutionContext用基底クラス
    *
+   * ECの実装クラスでは、この基底クラスを継承し、かつECのCORBAオペレー
+   * ションを実装しなければならない。さらに、実際にロジックを駆動するた
+   * め、幾つかの約束に則りExecutionContextBaseの関数を呼び出す必要があ
+   * る。ECのCORBAオペレーションは以下のものがあり、それぞれ
+   * ExecutionContextBaseのメンバ関数に対応している。
+   *
+   * - is_running(): ExecutionContextBase::isRunning()
+   * - start(): ExecutionContextBase::start()
+   * - stop(): ExecutionContextBase::stop()
+   *
+   * - get_rate(): ExecutionContextBase::gatRate()
+   * - set_rate(): ExecutioinContextBase::setRate()
+   *
+   * - add_component(): ExecutionContextBase::addComponent()
+   * - remove_component(): ExecutionContextBase::removeComponent()
+   *
+   * - activate_component(): ExecutionContextBase::activateComponent()
+   * - deactivate_component(): ExecutionContextBase::deactivateComponent()
+   * - reset_component(): ExecutionContextBase::resetComponent()
+   *
+   * - get_component_state(): ExecutionContextBase::getComponentState()
+   * - get_kind(): ExecutionContextBase::getKind()
+   * - get_profile(): ExecutionContextBase::getProfile()
+   *
+   * @par 実行状態に関係する関数と実装方法
+   * - is_running(): ExecutionContextBase::isRunning()
+   * - start(): ExecutionContextBase::start()
+   * - stop(): ExecutionContextBase::stop()
+   *
+   * 実行状態に関係する関数は、is_running(), start(), stop() の3つがあ
+   * る。ExecutionContextBaseでは単純に running/stopped のフラグを持っ
+   * ており、start/stopでフラグのON/OFF切り替え、is_running()で状態読み
+   * 出しを行っている。通常、ECの実装クラスでは、protected な仮想メン
+   * バ関数 onStarting(), onStarted(), onStopping(), onStopped() 関数を
+   * 実装したうえで、CORBAオペレーションを以下のように実装する必要がある。
+   *
+   * is_running() のCORBAオペレーションでは、単純に
+   * ExecutionContextBase の isRunning() を呼び出すだけである。この関数
+   * に関連する protected 仮想関数はonIsRunning() が用意されているが、
+   * 通常特に実装する必要はない。あえて、現在の running/stopped 状態を
+   * 書き換えたい場合にこの関数を利用することができるが推奨はされない。
+   *
+   * <pre>
+   * public:
+   *  CORBA::Boolean is_runing()
+   *  {
+   *    return ExecutionContextBase::isRunning();
+   *  }
+   * protected:
+   *  CORBA::Boolean onIsRunning(CORBA::Boolean running)
+   *  {
+   *    return running;
+   *  }
+   * </pre>
+   *
+   * start(), stop() CORBAオペレーションでは、通常
+   * ExecutionContextBase の start(), stop() 関数を呼び出すよう実装する。
+   * この関数に関連する protected 仮想関数は、start() および stop() に
+   * ついてそれぞれ2つづつの onStarting(), onStarted(), および
+   * onStopping(), onStopped() 関数がある。ECの実装クラスにおいては、そ
+   * れぞれ以下のように実装する。
+   *
+   * <pre>
+   *  RTC::ReturnCode_t start()
+   *  {
+   *    return ExecutionContextBase::start();
+   *  }
+   *  RTC::ReturnCode_t stop()
+   *  {
+   *    return ExecutionContextBase::stop();
+   *  }
+   * protected:
+   *  RTC::ReturnCode_t onStarting()
+   *  {
+   *    RTC::ReturnCode_t ret = // スレッドを開始する処理など
+   *    return ret;
+   *  }
+   *  RTC::ReturnCode_t onStarted()
+   *  {
+   *    RTC::ReturnCode_t ret = // スレッドを開始する処理など
+   *    return ret;
+   *  }
+   *  RTC::ReturnCode_t onStopping()
+   *  {
+   *    // スレッドを停止する処理など
+   *    return retcode;
+   *  }
+   *  RTC::ReturnCode_t onStopped()
+   *  {
+   *    // スレッドを停止する処理など
+   *    return retcode;
+   *  }
+   * </pre>
+   *
+   * @par 実行周期に関する関数と実装方法
+   * - get_rate(): ExecutionContextBase::gatRate()
+   * - set_rate(): ExecutioinContextBase::setRate()
+   *
+   * 実行周期に関する関数は set_rate(), get_rate() の2種類がある。実装
+   * する実行コンテキストがもし set_rate() により指定される周期を利用する
+   * 場合、テンプレート関数 onSetRate() をオーバーライドし実装する。
+   * onSetRate() は引数に double 型の周期を取り、この値は正当な値である
+   * ことが保証されている。onSetRate() がRTC::RTC_OK 以外の値を返した場
+   * 合、ECのProfileの周期は設定される以前の値を保持することが保証され
+   * る。
+   *
+   * set_rate() 同様 get_rate() 呼び出し時にonGetRate() が呼び出される
+   * が、これは通常オーバーライド剃る必要はない。ただし、get_rate() が
+   * 返す値を変更したい場合、onGetRate() をオーバーライドすることでその
+   * 値を書き換えることができる。ただし、これは推奨されない。
+   *
+   * <pre>
+   * public:
+   *  RTC::ReturnCode_t set_rate(double rate)
+   *  {
+   *    return setRate(rate);
+   *  }
+   *  double get_rate(void) const
+   *  {
+   *    return getRate();
+   *  }
+   * protected:
+   *  virtual RTC::ReturnCode_t onSetRate(double rate)
+   *  {
+   *    RTC::ReturnCode_t ret = // 周期を設定する何らかの処理
+   *    if (ret != RTC::RTC_OK)
+   *      {
+   *        RTC_ERROR(("Error message"));
+   *      }
+   *    return ret;
+   *  }
+   *  virtual double onGetRate(rate)
+   *  {
+   *    // get_rate() が返す値を加工したい場合
+   *    // 通常はこの関数を実装する必要はない。
+   *    return rate;
+   *  }
+   * </pre>
+   *
+   * @par コンポーネントの追加と削除に関する関数
+   * - add_component(): ExecutionContextBase::addComponent()
+   * - remove_component(): ExecutionContextBase::removeComponent()
+   *
+   * コンポーネントの追加と削除に関する関数は、add_component(),
+   * remove_component() の二種類がある。実行コンテキストの実装クラスに
+   * おいては、ExecutionContextBase のそれぞれ addComponent(),
+   * removeComponent() を呼び出す形で実装を行う。これらの関数に関連する
+   * protected 仮想関数は onAddingComponent(), onAddedComponent(),
+   * onRemovingComponent(), onRemovedComponent() の4種類ある。ただし、
+   * これらの仮想関数は通常オーバーライドする必要はなく、使用は推奨され
+   * ない。
+   *
+   * <pre>
+   * public:
+   *  RTC::ReturnCode_t add_component(RTC::LightweightRTObject_ptr comp)
+   *  {
+   *    return ExecutionContextBase::addComponent(comp);
+   *  }
+   *  RTC::ReturnCode_t remove_component(RTC::LightweightRTObject_ptr comp)
+   *  {
+   *    return ExecutionContextBase::removeComponent(comp);
+   *  }
+   * protected:
+   *  virtual RTC::ReturnCode_t
+   *  onAddingComponent(RTC::LightweightRTObject rtobj)
+   *  {
+   *     // コンポーネント追加時に実行したい処理を記述
+   *     // RTC::RTC_OK 以外を返した場合、コンポーネントの追加は行われない。
+   *     return RTC::RTC_OK;
+   *  }
+   *  virtual RTC::ReturnCode_t
+   *  onAddedComponent(RTC::LightweightRTObject rtobj)
+   *  {
+   *     // コンポーネント追加時に実行したい処理を記述
+   *     // RTC::RTC_OK 以外を返した場合、removeComponent() が呼び出され、
+   *     // 追加されたコンポーネントが削除される。
+   *     return RTC::RTC_OK;
+   *  }
+   *  virtual RTC::ReturnCode_t
+   *  onRemovingComponent(RTC::LightweightRTObject rtobj)
+   *  {
+   *     // コンポーネント削除時に実行したい処理を記述
+   *     // RTC::RTC_OK 以外を返した場合、コンポーネントの削除は行われない。
+   *     return RTC::RTC_OK;
+   *  }
+   *  virtual RTC::ReturnCode_t
+   *  onRemovedComponent(RTC::LightweightRTObject rtobj)
+   *  {
+   *     // コンポーネント追加時に実行したい処理を記述
+   *     // RTC::RTC_OK 以外を返した場合、addComponent() が呼び出され、
+   *     // 削除されたコンポーネントが再び追加される。
+   *     return RTC::RTC_OK;
+   *  }
+   * </pre>
+   *
+   * @par コンポーネントのアクティブ化等に関する関数
+   * - activate_component(): ExecutionContextBase::activateComponent()
+   * - deactivate_component(): ExecutionContextBase::deactivateComponent()
+   * - reset_component(): ExecutionContextBase::resetComponent()
+   *
+   * コンポーネントのアクティブ化等に関する関数は、
+   * activate_component(), deactivate_component(), reset_component() の
+   * 三種類がある。実行コンテキストの実装クラスにおいては、
+   * ExecutionContextBase のそれぞれ activateComponent(),
+   * deactivateComponent(), resetComponent() を呼び出す形で実装を行う。
+   * これらの関数に関連する protected 仮想関数は
+   * onActivatingComponent(), onAtivatingComponent(),
+   * onActivatedComponent(), onDeactivatingComponent(),
+   * onDeactivatedComponent(), onResettingComponent(),
+   * onResetComponent() の6種類ある。ただし、これらの仮想関数は通常オー
+   * バーライドする必要はなく、使用は推奨されない。
+   *
+   * <pre>
+   * public:
+   *  RTC::ReturnCode_t add_component(RTC::LightweightRTObject_ptr comp)
+   *  {
+   *    return ExecutionContextBase::addComponent(comp);
+   *  }
+   *  RTC::ReturnCode_t remove_component(RTC::LightweightRTObject_ptr comp)
+   *  {
+   *    return ExecutionContextBase::removeComponent(comp);
+   *  }
+   * protected:
+   *  virtual RTC::ReturnCode_t
+   *  onAddingComponent(RTC::LightweightRTObject rtobj)
+   *  {
+   *    // コンポーネント追加時に実行したい処理を記述
+   *    // RTC::RTC_OK 以外を返した場合、コンポーネントの追加は行われない。
+   *    return RTC::RTC_OK;
+   *  }
+   *  virtual RTC::ReturnCode_t
+   *  onAddedComponent(RTC::LightweightRTObject rtobj)
+   *  {
+   *    // コンポーネント追加時に実行したい処理を記述
+   *    // RTC::RTC_OK 以外を返した場合、removeComponent() が呼び出され、
+   *    // 追加されたコンポーネントが削除される。
+   *    return RTC::RTC_OK;
+   *  }
+   *  virtual RTC::ReturnCode_t
+   *  onRemovingComponent(RTC::LightweightRTObject rtobj)
+   *  {
+   *    // コンポーネント削除時に実行したい処理を記述
+   *    // RTC::RTC_OK 以外を返した場合、コンポーネントの削除は行われない。
+   *    return RTC::RTC_OK;
+   *  }
+   *  virtual RTC::ReturnCode_t
+   *  onRemovedComponent(RTC::LightweightRTObject rtobj)
+   *  {
+   *    // コンポーネント追加時に実行したい処理を記述
+   *    // RTC::RTC_OK 以外を返した場合、addComponent() が呼び出され、
+   *    // 削除されたコンポーネントが再び追加される。
+   *    return RTC::RTC_OK;
+   *  }
+   * </pre>
+   *
+   * @par 実行コンテキストの情報取得に関する関数
+   * - get_component_state(): ExecutionContextBase::getComponentState()
+   * - get_kind(): ExecutionContextBase::getKind()
+   * - get_profile(): ExecutionContextBase::getProfile()
+   *
+   * 実行コンテキストの情報取得に関する関数は、get_component_state(),
+   * get_kind(), get_profile() の3種類がある。実行コンテキストの実装ク
+   * ラスにおいては、ExecutionContextBase のそれぞれ
+   * getComponentState(), getKind(), getProfile() を呼び出す形で実装を
+   * 行う。これらの関数に関連する protected 仮想関数は
+   * onGetComponentState(), onGetKind(), onGetProfile() の3種類ある。こ
+   * れらの仮想関数は通常オーバーライドする必要はなく、使用は推奨されな
+   * い。ただし、返す情報を変更したい場合は、これらの関数を適切に実装す
+   * ることで、呼び出し側に返す値を上書きすることができる。
+   *
+   * <pre>
+   * public:
+   *  LifeCycleState get_component_state(RTC::LightweightRTObject_ptr comp)
+   *  {
+   *    return getComponentState(comp);
+   *  }
+   *  ExecutionKind PeriodicExecutionContext::get_kind()
+   *  {
+   *    return getKind();
+   *  }
+   *  ExecutionContextProfile* get_profile()
+   *  {
+   *    return getProfile();
+   *  }
+   *
+   * protected:
+   *  virtual LifeCycleState onGetComponentState(LifeCycleState state)
+   *  { // 返すstateを書き換えたい場合はこの関数を実装する
+   *    return state;
+   *  }
+   *  virtual ExecutionKind onGetKind(ExecutionKind kind)
+   *  { // 返すkindを書き換えたい場合はこの関数を実装する
+   *    return kind;
+   *  }
+   *  virtual ExecutionContextProfile*
+   *  onGetProfile(ExecutionContextProfile*& profile)
+   *  { // 返すprofileを書き換えたい場合はこの関数を実装する
+   *    return profile;
+   *  }
+   * </pre>
+   *
    * ExecutionContextの基底クラス。
    *
    * @since 0.4.0
@@ -72,6 +375,21 @@
      *
      * @endif
      */
+    ExecutionContextBase(const char* name);
+
+    /*!
+     * @if jp
+     * @brief 仮想デストラクタ
+     *
+     * 仮想デストラクタ
+     *
+     * @else
+     * @brief Virtual Destructor
+     *
+     * Virtual Destructor
+     *
+     * @endif
+     */
     virtual ~ExecutionContextBase(void);
 
     /*!
@@ -102,61 +420,118 @@
      *
      * @endif
      */
-    virtual RTC::ReturnCode_t bindComponent(RTObject_impl* rtc) = 0;
+    virtual RTC::ReturnCode_t bindComponent(RTC::RTObject_impl* rtc);
 
     //============================================================
-    // Delegated functions to ExecutionContextProfile
+    // implementation functions for EC's CORBA operation
     //============================================================
     /*!
      * @if jp
-     * @brief CORBA オブジェクト参照の取得
+     * @brief ExecutionContext 実行状態確認関数
      *
-     * 本オブジェクトの ExecutioncontextService としての CORBA オブジェ
-     * クト参照を取得する。
+     * この操作は ExecutionContext が Runnning 状態の場合に true を返す。
+     * 返り値は、start() 関数が呼ばれたあとはRunning状態となり
+     * true を、stop() 関数が呼ばれたあとはStopped状態となりfalseを返す。
      *
-     * @return CORBA オブジェクト参照
+     * @return 動作状態 (Running状態:true、Stopped状態: false)
      *
      * @else
-     * @brief Get the reference to the CORBA object
      *
-     * Get the reference to the CORBA object as
-     * ExecutioncontextService of this object.
+     * @brief Check for ExecutionContext running state
      *
-     * @return The reference to CORBA object
+     * This operation shall return true if the context is in the
+     * Running state.  After calling the start() of this class, this
+     * function returns true, after calling to stop function of it,
+     * this function returns false.
      *
+     * @return Working status (Running:true、Stopped:false)
+     *
      * @endif
      */
-    void setObjRef(RTC::ExecutionContextService_ptr ec_ptr)
-    {
-      m_profile.setObjRef(ec_ptr);
-    }
+    CORBA::Boolean isRunning();
 
     /*!
      * @if jp
-     * @brief CORBA オブジェクト参照の取得
+     * @brief ExecutionContext の実行を開始
      *
-     * 本オブジェクトの ExecutioncontextService としての CORBA オブジェ
-     * クト参照を取得する。
+     * ExecutionContext の実行状態を Runnning とするためのリクエストを
+     * 発行する。ExecutionContext の状態が遷移すると
+     * ComponentAction::on_startup が呼び出される。参加しているRTコンポー
+     * ネントが、初期化されるまで ExecutionContext を開始することはでき
+     * ない。ExecutionContext は複数回開始/停止を繰り返すことができる。
      *
-     * @return CORBA オブジェクト参照
+     * @return ReturnCode_t 型のリターンコード
      *
      * @else
-     * @brief Get the reference to the CORBA object
      *
-     * Get the reference to the CORBA object as
-     * ExecutioncontextService of this object.
+     * @brief Start the ExecutionContext
      *
-     * @return The reference to CORBA object
+     * Request that the context enter the Running state.  Once the
+     * state transition occurs, the ComponentAction::on_startup
+     * operation will be invoked.  An execution context may not be
+     * started until the RT-Components that participate in it have
+     * been initialized.  An execution context may be started and
+     * stopped multiple times.
      *
+     * @return The return code of ReturnCode_t type
+     *
      * @endif
      */
-    RTC::ExecutionContextService_ptr getObjRef(void) const
-    {
-      return m_profile.getObjRef();
-    }
+    RTC::ReturnCode_t start(void);
 
     /*!
      * @if jp
+     * @brief ExecutionContext の実行を停止
+     *
+     * ExecutionContext の状態を Stopped とするためのリクエストを発行す
+     * る。遷移が発生した場合は、ComponentAction::on_shutdown が呼び出
+     * される。参加しているRTコンポーネントが終了する前に
+     * ExecutionContext を停止する必要がある。ExecutionContext は複数回
+     * 開始/停止を繰り返すことができる。
+     *
+     * @return ReturnCode_t 型のリターンコード
+     *
+     * @else
+     *
+     * @brief Stop the ExecutionContext
+     *
+     * Request that the context enter the Stopped state.  Once the
+     * transition occurs, the ComponentAction::on_shutdown operation
+     * will be invoked.  An execution context must be stopped before
+     * the RT components that participate in it are finalized.  An
+     * execution context may be started and stopped multiple times.
+     *
+     * @return The return code of ReturnCode_t type
+     *
+     * @endif
+     */
+    virtual RTC::ReturnCode_t stop(void);
+
+    /*!
+     * @if jp
+     * @brief ExecutionContext の実行周期(Hz)を取得する
+     *
+     * Active 状態にてRTコンポーネントが実行される周期(単位:Hz)を取得す
+     * る。
+     *
+     * @return 処理周期(単位:Hz)
+     *
+     * @else
+     *
+     * @brief Get execution rate(Hz) of ExecutionContext
+     *
+     * This operation shall return the rate (in hertz) at which its
+     * Active participating RTCs are being invoked.
+     *
+     * @return Execution cycle(Unit:Hz)
+     *
+     * @endif
+     */
+    double getRate(void) const;
+    coil::TimeValue getPeriod(void) const;
+ 
+    /*!
+     * @if jp
      * @brief ExecutionContext の実行周期(Hz)を設定する
      *
      * Active 状態にてRTコンポーネントが実行される周期(単位:Hz)を設定す
@@ -188,88 +563,214 @@
      *
      * @endif
      */
-    RTC::ReturnCode_t setRate(double rate)
-    {
-      return m_profile.setRate(rate);;
-    }
+    RTC::ReturnCode_t setRate(double rate);
 
     /*!
      * @if jp
-     * @brief ExecutionContext の実行周期(Hz)を取得する
+     * @brief RTコンポーネントを追加する
      *
-     * Active 状態にてRTコンポーネントが実行される周期(単位:Hz)を取得す
-     * る。
+     * 指定したRTコンポーネントを参加者リストに追加する。追加されたRTコ
+     * ンポーネントは attach_context が呼ばれ、Inactive 状態に遷移する。
+     * 指定されたRTコンポーネントがnullの場合は、BAD_PARAMETER が返され
+     * る。指定されたRTコンポーネントが DataFlowComponent 以外の場合は、
+     * BAD_PARAMETER が返される。
      *
-     * @return 処理周期(単位:Hz)
+     * @param comp 追加対象RTコンポーネント
      *
+     * @return ReturnCode_t 型のリターンコード
+     *
      * @else
      *
-     * @brief Get execution rate(Hz) of ExecutionContext
+     * @brief Add an RT-component
      *
-     * This operation shall return the rate (in hertz) at which its
-     * Active participating RTCs are being invoked.
+     * The operation causes the given RTC to begin participating in
+     * the execution context.  The newly added RTC will receive a call
+     * to LightweightRTComponent::attach_context and then enter the
+     * Inactive state.  BAD_PARAMETER will be invoked, if the given
+     * RT-Component is null or if the given RT-Component is other than
+     * DataFlowComponent.
      *
-     * @return Execution cycle(Unit:Hz)
+     * @param comp The target RT-Component for add
      *
+     * @return The return code of ReturnCode_t type
+     *
      * @endif
      */
-    double getRate(void) const
-    {
-      return m_profile.getRate();
-    }
+    RTC::ReturnCode_t addComponent(RTC::LightweightRTObject_ptr comp);
 
     /*!
      * @if jp
-     * @brief ExecutionKind を文字列化する
+     * @brief RTコンポーネントを参加者リストから削除する
      *
-     * RTC::ExecutionKind で定義されている PERIODIC, EVENT_DRIVEN,
-     * OTHER を文字列化する。
+     * 指定したRTコンポーネントを参加者リストから削除する。削除された
+     * RTコンポーネントは detach_context が呼ばれる。指定されたRTコンポー
+     * ネントが参加者リストに登録されていない場合は、BAD_PARAMETER が返
+     * される。
      *
-     * @param kind ExecutionKind
-     * @return 文字列化されたExecutionKind
+     * @param comp 削除対象RTコンポーネント
      *
+     * @return ReturnCode_t 型のリターンコード
+     *
      * @else
      *
-     * @brief Converting ExecutionKind enum to string 
+     * @brief Remove the RT-Component from participant list
      *
-     * This function converts enumeration (PERIODIC, EVENT_DRIVEN,
-     * OTHER) defined in RTC::ExecutionKind to string.
+     * This operation causes a participant RTC to stop participating in the
+     * execution context.
+     * The removed RTC will receive a call to
+     * LightweightRTComponent::detach_context.
+     * BAD_PARAMETER will be returned, if the given RT-Component is not
+     * participating in the participant list.
      *
-     * @param kind ExecutionKind
-     * @return String of ExecutionKind
+     * @param comp The target RT-Component for delete
      *
+     * @return The return code of ReturnCode_t type
+     *
      * @endif
      */
-    const char* getKindString(RTC::ExecutionKind kind) const
-    {
-      return m_profile.getKindString(kind);
-    }
+    RTC::ReturnCode_t removeComponent(RTC::LightweightRTObject_ptr comp);
 
     /*!
      * @if jp
-     * @brief ExecutionKind を設定する
+     * @brief RTコンポーネントをアクティブ化する
      *
-     * この ExecutionContext の ExecutionKind を設定する
+     * Inactive 状態にあるRTコンポーネントをActive に遷移させ、アクティ
+     * ブ化する。この操作が呼ばれた結果、on_activate が呼び出される。指
+     * 定したRTコンポーネントが参加者リストに含まれない場合は、
+     * BAD_PARAMETER が返される。指定したRTコンポーネントの状態が
+     * Inactive 以外の場合は、PRECONDITION_NOT_MET が返される。
      *
-     * @param kind ExecutionKind
+     * @param comp アクティブ化対象RTコンポーネント
      *
+     * @return ReturnCode_t 型のリターンコード
+     *
      * @else
      *
-     * @brief Set the ExecutionKind
+     * @brief Activate an RT-component
      *
-     * This operation sets the kind of the execution context.
+     * The given participant RTC is Inactive and is therefore not
+     * being invoked according to the execution context’s execution
+     * kind. This operation shall cause the RTC to transition to the
+     * Active state such that it may subsequently be invoked in this
+     * execution context.  The callback on_activate shall be called as
+     * a result of calling this operation. This operation shall not
+     * return until the callback has returned, and shall result in an
+     * error if the callback does.
      *
-     * @param kind ExecutionKind
+     * @param comp The target RT-Component for activation
      *
+     * @return The return code of ReturnCode_t type
+     *
      * @endif
      */
-    RTC::ReturnCode_t setKind(RTC::ExecutionKind kind)
-    {
-      return m_profile.setKind(kind);
-    }
+    RTC::ReturnCode_t activateComponent(RTC::LightweightRTObject_ptr comp);
 
     /*!
      * @if jp
+     * @brief RTコンポーネントを非アクティブ化する
+     *
+     * Inactive 状態にあるRTコンポーネントを非アクティブ化し、Inactive
+     * に遷移させる。この操作が呼ばれた結果、on_deactivate が呼び出され
+     * る。指定したRTコンポーネントが参加者リストに含まれない場合は、
+     * BAD_PARAMETER が返される。指定したRTコンポーネントの状態が
+     * Active 以外の場合は、PRECONDITION_NOT_MET が返される。
+     *
+     *
+     *
+
+
+     * |
+
+
+     
+     * @param comp 非アクティブ化対象RTコンポーネント
+     *
+     * @return ReturnCode_t 型のリターンコード
+     *
+     * @else
+     *
+     * @brief Deactivate an RT-component
+     *
+     * The given RTC is Active in the execution context. Cause it to
+     * transition to the Inactive state such that it will not be
+     * subsequently invoked from the context unless and until it is
+     * activated again.  The callback on_deactivate shall be called as
+     * a result of calling this operation. This operation shall not
+     * return until the callback has returned, and shall result in an
+     * error if the callback does.
+     *
+     * @param comp The target RT-Component for deactivate
+     *
+     * @return The return code of ReturnCode_t type
+     *
+     * @endif
+     */
+    RTC::ReturnCode_t deactivateComponent(RTC::LightweightRTObject_ptr comp);
+
+    /*!
+     * @if jp
+     * @brief RTコンポーネントをリセットする
+     *
+     * Error 状態のRTコンポーネントの復帰を試みる。この操作が呼ばれた結
+     * 果、on_reset が呼び出される。指定したRTコンポーネントが参加者リ
+     * ストに含まれない場合は、BAD_PARAMETER が返される。指定したRTコン
+     * ポーネントの状態が Error 以外の場合は、PRECONDITION_NOT_MET が返
+     * される。
+     *
+     * @param comp リセット対象RTコンポーネント
+     *
+     * @return ReturnCode_t 型のリターンコード
+     *
+     * @else
+     *
+     * @brief Reset the RT-component
+     *
+     * Attempt to recover the RTC when it is in Error.  The
+     * ComponentAction::on_reset callback shall be invoked. This
+     * operation shall not return until the callback has returned, and
+     * shall result in an error if the callback does. If possible, the
+     * RTC developer should implement that callback such that the RTC
+     * may be returned to a valid state.
+     *
+     * @param comp The target RT-Component for reset
+     *
+     * @return The return code of ReturnCode_t type
+     *
+     * @endif
+     */
+    RTC::ReturnCode_t resetComponent(RTC::LightweightRTObject_ptr comp);
+
+    /*!
+     * @if jp
+     * @brief RTコンポーネントの状態を取得する
+     *
+     * 指定したRTコンポーネントの状態(LifeCycleState)を取得する。指定し
+     * たRTコンポーネントが参加者リストに含まれない場合は、
+     * UNKNOWN_STATE が返される。
+     *
+     * @param comp 状態取得対象RTコンポーネント
+     *
+     * @return 現在の状態(LifeCycleState)
+     *
+     * @else
+     *
+     * @brief Get RT-component's state
+     *
+     * This operation shall report the LifeCycleState of the given
+     * participant RTC.  UNKNOWN_STATE will be returned, if the given
+     * RT-Component is not inclued in the participant list.
+     *
+     * @param comp The target RT-Component to get the state
+     *
+     * @return The current state of the target RT-Component(LifeCycleState)
+     *
+     * @endif
+     */
+    RTC::LifeCycleState getComponentState(RTC::LightweightRTObject_ptr comp);
+    const char* getStateString(RTC::LifeCycleState state);
+    
+    /*!
+     * @if jp
      * @brief ExecutionKind を取得する
      *
      * 本 ExecutionContext の ExecutionKind を取得する
@@ -287,124 +788,156 @@
      *
      * @endif
      */
-    RTC::ExecutionKind getKind(void) const
-    {
-      return m_profile.getKind();
-    }
+    RTC::ExecutionKind getKind(void) const;
 
     /*!
      * @if jp
-     * @brief Ownerコンポーネントをセットする。
+     * @brief Profileを取得する
      *
-     * このECのOwnerとなるRTCをセットする。
+     * RTC::ExecutionContextProfile を取得する。取得した
+     * ExecutionContextProfile の所有権は呼び出し側にある。取得されたオ
+     * ブジェクトが不要になった場合、呼び出し側が開放する責任を負う。
      *
-     * @param comp OwnerとなるRTコンポーネント
-     * @return ReturnCode_t 型のリターンコード
+     * @return RTC::ExecutionContextProfile
+     *
      * @else
-     * @brief Setting owner component of the execution context
+     * @brief Getting Profile
      *
-     * This function sets an RT-Component to be owner of the execution context.
+     * This function gets RTC::ExecutionContextProfile.  The ownership
+     * of the obtained ExecutionContextProfile is given to caller. The
+     * caller should release obtained object when it is unneccessary
+     * anymore.
      *
-     * @param comp an owner RT-Component of this execution context
-     * @return The return code of ReturnCode_t type
+     * @return RTC::ExecutionContextProfile
+     *
      * @endif
      */
-    RTC::ReturnCode_t setOwner(RTC::LightweightRTObject_ptr comp)
-    {
-      return m_profile.setOwner(comp);
-    }
+    RTC::ExecutionContextProfile* getProfile(void);
 
+    //============================================================
+    // Delegated functions to ExecutionContextProfile
+    //============================================================
     /*!
      * @if jp
-     * @brief Ownerコンポーネントの参照を取得する
+     * @brief CORBA オブジェクト参照の取得
      *
-     * このECのOwnerであるRTCの参照を取得する。
+     * 本オブジェクトの ExecutioncontextService としての CORBA オブジェ
+     * クト参照を取得する。
      *
-     * @return OwnerRTコンポーネントの参照
+     * @return CORBA オブジェクト参照
+     *
      * @else
-     * @brief Getting a reference of the owner component
+     * @brief Get the reference to the CORBA object
      *
-     * This function returns a reference of the owner RT-Component of
-     * this execution context
+     * Get the reference to the CORBA object as
+     * ExecutioncontextService of this object.
      *
-     * @return a reference of the owner RT-Component
+     * @return The reference to CORBA object
+     *
      * @endif
      */
-    const RTC::RTObject_ptr getOwner() const
-    {
-      return m_profile.getOwner();
-    }
+    void setObjRef(RTC::ExecutionContextService_ptr ec_ptr);
 
     /*!
      * @if jp
-     * @brief RTコンポーネントを追加する
+     * @brief CORBA オブジェクト参照の取得
      *
-     * 指定したRTコンポーネントを参加者リストに追加する。追加されたRTコ
-     * ンポーネントは attach_context が呼ばれ、Inactive 状態に遷移する。
-     * 指定されたRTコンポーネントがnullの場合は、BAD_PARAMETER が返され
-     * る。指定されたRTコンポーネントが DataFlowComponent 以外の場合は、
-     * BAD_PARAMETER が返される。
+     * 本オブジェクトの ExecutioncontextService としての CORBA オブジェ
+     * クト参照を取得する。
      *
-     * @param comp 追加対象RTコンポーネント
+     * @return CORBA オブジェクト参照
      *
-     * @return ReturnCode_t 型のリターンコード
+     * @else
      *
+     * Get the reference to the CORBA object as
+     * ExecutioncontextService of this object.
+     *
+     * @return The reference to CORBA object
+     *
+     * @endif
+     */
+    RTC::ExecutionContextService_ptr getObjRef(void) const;
+
+    /*!
+     * @if jp
+     * @brief ExecutionKind を文字列化する
+     *
+     * RTC::ExecutionKind で定義されている PERIODIC, EVENT_DRIVEN,
+     * OTHER を文字列化する。
+     *
+     * @param kind ExecutionKind
+     * @return 文字列化されたExecutionKind
+     *
      * @else
      *
-     * @brief Add an RT-component
+     * @brief Converting ExecutionKind enum to string 
      *
-     * The operation causes the given RTC to begin participating in
-     * the execution context.  The newly added RTC will receive a call
-     * to LightweightRTComponent::attach_context and then enter the
-     * Inactive state.  BAD_PARAMETER will be invoked, if the given
-     * RT-Component is null or if the given RT-Component is other than
-     * DataFlowComponent.
+     * This function converts enumeration (PERIODIC, EVENT_DRIVEN,
+     * OTHER) defined in RTC::ExecutionKind to string.
      *
-     * @param comp The target RT-Component for add
+     * @param kind ExecutionKind
+     * @return String of ExecutionKind
      *
-     * @return The return code of ReturnCode_t type
-     *
      * @endif
      */
-    RTC::ReturnCode_t addComponent(RTC::LightweightRTObject_ptr comp)
-    {
-      return m_profile.addComponent(comp);
-    }
+    const char* getKindString(RTC::ExecutionKind kind) const;
 
     /*!
      * @if jp
-     * @brief RTコンポーネントを参加者リストから削除する
+     * @brief ExecutionKind を設定する
      *
-     * 指定したRTコンポーネントを参加者リストから削除する。削除された
-     * RTコンポーネントは detach_context が呼ばれる。指定されたRTコンポー
-     * ネントが参加者リストに登録されていない場合は、BAD_PARAMETER が返
-     * される。
+     * この ExecutionContext の ExecutionKind を設定する
      *
-     * @param comp 削除対象RTコンポーネント
+     * @param kind ExecutionKind
      *
-     * @return ReturnCode_t 型のリターンコード
-     *
      * @else
      *
-     * @brief Remove the RT-Component from participant list
+     * @brief Set the ExecutionKind
      *
-     * This operation causes a participant RTC to stop participating in the
-     * execution context.
-     * The removed RTC will receive a call to
-     * LightweightRTComponent::detach_context.
-     * BAD_PARAMETER will be returned, if the given RT-Component is not
-     * participating in the participant list.
+     * This operation sets the kind of the execution context.
      *
-     * @param comp The target RT-Component for delete
+     * @param kind ExecutionKind
      *
+     * @endif
+     */
+    RTC::ReturnCode_t setKind(RTC::ExecutionKind kind);
+
+    /*!
+     * @if jp
+     * @brief Ownerコンポーネントをセットする。
+     *
+     * このECのOwnerとなるRTCをセットする。
+     *
+     * @param comp OwnerとなるRTコンポーネント
+     * @return ReturnCode_t 型のリターンコード
+     * @else
+     * @brief Setting owner component of the execution context
+     *
+     * This function sets an RT-Component to be owner of the execution context.
+     *
+     * @param comp an owner RT-Component of this execution context
      * @return The return code of ReturnCode_t type
+     * @endif
+     */
+    RTC::ReturnCode_t setOwner(RTC::LightweightRTObject_ptr comp);
+
+    /*!
+     * @if jp
+     * @brief Ownerコンポーネントの参照を取得する
      *
+     * このECのOwnerであるRTCの参照を取得する。
+     *
+     * @return OwnerRTコンポーネントの参照
+     * @else
+     * @brief Getting a reference of the owner component
+     *
+     * This function returns a reference of the owner RT-Component of
+     * this execution context
+     *
+     * @return a reference of the owner RT-Component
      * @endif
      */
-    RTC::ReturnCode_t removeComponent(RTC::LightweightRTObject_ptr comp)
-    {
-      return m_profile.removeComponent(comp);
-    }
+    const RTC::RTObject_ptr getOwner() const;
 
     /*!
      * @if jp
@@ -424,10 +957,7 @@
      *
      * @endif
      */
-    const RTC::RTCList& getComponentList() const
-    {
-      return m_profile.getComponentList();
-    }
+    const RTC::RTCList& getComponentList() const;
 
     /*!
      * @if jp
@@ -449,10 +979,7 @@
      *
      * @endif
      */
-    void setProperties(coil::Properties& props)
-    {
-      m_profile.setProperties(props);
-    }
+    void setProperties(coil::Properties& props);
 
     /*!
      * @if jp
@@ -473,66 +1000,198 @@
      *
      * @endif
      */
-    const coil::Properties getProperties() const
-    {
-      return m_profile.getProperties();
-    }
+    const coil::Properties getProperties() const;
 
     /*!
      * @if jp
      * @brief Profileを取得する
      *
-     * RTC::ExecutionContextProfile を取得する。取得した
-     * ExecutionContextProfile の所有権は呼び出し側にある。取得されたオ
-     * ブジェクトが不要になった場合、呼び出し側が開放する責任を負う。
+     * RTC::ExecutionContextProfile を取得する。
      *
      * @return RTC::ExecutionContextProfile
      *
      * @else
      * @brief Getting Profile
      *
-     * This function gets RTC::ExecutionContextProfile.  The ownership
-     * of the obtained ExecutionContextProfile is given to caller. The
-     * caller should release obtained object when it is unneccessary
-     * anymore.
+     * This function gets RTC::ExecutionContextProfile.
      *
      * @return RTC::ExecutionContextProfile
      *
      * @endif
      */
-    RTC::ExecutionContextProfile* getProfile(void)
+    const RTC::ExecutionContextProfile& getProfile(void) const;
+    // end of delegated functions to ExecutionContextProfile
+    //============================================================
+
+    //============================================================
+    // Delegated functions to ExecutionContextWorker
+    //============================================================
+    bool isAllCurrentState(RTC::LifeCycleState state)
     {
-      return m_profile.getProfile();
+      return m_worker.isAllCurrentState(state);
+    };
+    bool isAllNextState(RTC::LifeCycleState state)
+    {
+      return m_worker.isAllNextState(state);
+    };
+    bool isOneOfCurrentState(RTC::LifeCycleState state)
+    {
+      return m_worker.isOneOfCurrentState(state);
+    };
+    bool isOneOfNextState(RTC::LifeCycleState state)
+    {
+      return m_worker.isOneOfNextState(state);
+    };
+    
+    void invokeWorker()       { m_worker.invokeWorker(); }
+    void invokeWorkerPreDo()  { m_worker.invokeWorkerPreDo(); }
+    void invokeWorkerDo()     { m_worker.invokeWorkerDo(); }
+    void invokeWorkerPostDo() { m_worker.invokeWorkerPostDo(); }
+
+
+  protected:
+    // template virtual functions related to start/stop
+    virtual bool onIsRunning(bool running) { return running; }
+    virtual RTC::ReturnCode_t onStarting() { return RTC::RTC_OK; }
+    virtual RTC::ReturnCode_t onStarted() { return RTC::RTC_OK; }
+    virtual RTC::ReturnCode_t onStopping() { return RTC::RTC_OK; }
+    virtual RTC::ReturnCode_t onStopped() { return RTC::RTC_OK; }
+
+    // template virtual functions getting/setting execution rate
+    virtual double onGetRate(double rate) const { return rate; }
+    virtual double onSettingRate(double rate) { return rate; }
+    virtual RTC::ReturnCode_t onSetRate(double rate) { return RTC::RTC_OK; }
+
+    // template virtual functions adding/removing component
+    virtual RTC::ReturnCode_t
+    onAddingComponent(RTC::LightweightRTObject_ptr rtobj)
+    {
+      return RTC::RTC_OK;
     }
+    virtual RTC::ReturnCode_t
+    onAddedComponent(RTC::LightweightRTObject_ptr rtobj)
+    {
+      return RTC::RTC_OK;
+    }
+    virtual RTC::ReturnCode_t
+    onRemovingComponent(RTC::LightweightRTObject_ptr rtobj)
+    {
+      return RTC::RTC_OK;
+    }
+    virtual RTC::ReturnCode_t
+    onRemovedComponent(RTC::LightweightRTObject_ptr rtobj)
+    {
+      return RTC::RTC_OK;
+    }
 
+    // template virtual functions related to activation/deactivation/reset
+    virtual RTC::ReturnCode_t
+    onActivating(RTC::LightweightRTObject_ptr comp)
+    {
+      return RTC::RTC_OK;
+    }
+    virtual RTC::ReturnCode_t
+    onWaitingActivated(RTC_impl::RTObjectStateMachine* comp, long int count)
+    {
+      return RTC::RTC_OK;
+    }
+    virtual RTC::ReturnCode_t
+    onActivated(RTC_impl::RTObjectStateMachine* comp,
+                                     long int count)
+    {
+      return RTC::RTC_OK;
+    }
+    virtual RTC::ReturnCode_t
+    onDeactivating(RTC::LightweightRTObject_ptr comp)
+    {
+      return RTC::RTC_OK;
+    }
+    virtual RTC::ReturnCode_t
+    onWaitingDeactivated(RTC_impl::RTObjectStateMachine* comp, long int count)
+    {
+      return RTC::RTC_OK;
+    }
+    virtual RTC::ReturnCode_t
+    onDeactivated(RTC_impl::RTObjectStateMachine* comp, long int count)
+    {
+      return RTC::RTC_OK;
+    }
+    virtual RTC::ReturnCode_t onResetting(RTC::LightweightRTObject_ptr comp)
+    {
+      return RTC::RTC_OK;
+    }
+    virtual RTC::ReturnCode_t
+    onWaitingReset(RTC_impl::RTObjectStateMachine* comp, long int count)
+    {
+      return RTC::RTC_OK;
+    }
+    virtual RTC::ReturnCode_t
+    onReset(RTC_impl::RTObjectStateMachine* comp, long int count)
+    {
+      return RTC::RTC_OK;
+    }
+
+    virtual RTC::LifeCycleState
+    onGetComponentState(RTC::LifeCycleState state)
+    {
+      return state;
+    }
+    virtual RTC::ExecutionKind
+    onGetKind(RTC::ExecutionKind kind) const
+    {
+      return kind;
+    }
+    virtual RTC::ExecutionContextProfile*
+    onGetProfile(RTC::ExecutionContextProfile*& profile)
+    {
+      return profile;
+    }
+
+  private:
     /*!
      * @if jp
-     * @brief Profileを取得する
-     *
-     * RTC::ExecutionContextProfile を取得する。
-     *
-     * @return RTC::ExecutionContextProfile
-     *
+     * @brief Propertiesから実行コンテキストをセットする
      * @else
-     * @brief Getting Profile
-     *
-     * This function gets RTC::ExecutionContextProfile.
-     *
-     * @return RTC::ExecutionContextProfile
-     *
+     * @brief Setting execution rate from given properties.
      * @endif
      */
-    const RTC::ExecutionContextProfile& getProfile(void) const
-    {
-      return m_profile.getProfile();
-    }
-    // end of delegated functions to ExecutionContextProfile
-    //============================================================
+    bool setExecutionRate(coil::Properties& props);
+    /*!
+     * @if jp
+     * @brief Propertiesから状態遷移モードをセットする
+     * @else
+     * @brief Setting state transition mode from given properties.
+     * @endif
+     */
+    bool setTransitionMode(coil::Properties& props,
+                           const char* key, bool& flag);
+    /*!
+     * @if jp
+     * @brief Propertiesから状態遷移Timeoutをセットする
+     * @else
+     * @brief Setting state transition timeout from given properties.
+     * @endif
+     */
+    bool setTimeout(coil::Properties& props, const char* key,
+                    coil::TimeValue& timevalue);
 
+    RTC::ReturnCode_t waitForActivated(RTC_impl::RTObjectStateMachine* rtobj);
+    RTC::ReturnCode_t waitForDeactivated(RTC_impl::RTObjectStateMachine* rtobj);
+    RTC::ReturnCode_t waitForReset(RTC_impl::RTObjectStateMachine* rtobj);
+    
+  protected:
+    mutable RTC::Logger rtclog;
 
+    RTC_impl::ExecutionContextWorker m_worker;
+    RTC_impl::ExecutionContextProfile m_profile;
 
-  protected:
-    RTC_impl::ExecutionContextProfile m_profile;
+    coil::TimeValue m_activationTimeout;
+    coil::TimeValue m_deactivationTimeout;
+    coil::TimeValue m_resetTimeout;
+
+    bool m_syncActivation;
+    bool m_syncDeactivation;
+    bool m_syncReset;
   };  // class ExecutionContextBase
 
   typedef coil::GlobalFactory<ExecutionContextBase> ExecutionContextFactory;

Modified: trunk/OpenRTM-aist/src/lib/rtm/ExtTrigExecutionContext.cpp
===================================================================
--- trunk/OpenRTM-aist/src/lib/rtm/ExtTrigExecutionContext.cpp	2012-02-05 21:21:42 UTC (rev 2306)
+++ trunk/OpenRTM-aist/src/lib/rtm/ExtTrigExecutionContext.cpp	2012-02-05 21:29:15 UTC (rev 2307)
@@ -18,8 +18,11 @@
  */
 
 #include <coil/TimeValue.h>
+#include <coil/Guard.h>
+
 #include <rtm/ExtTrigExecutionContext.h>
 #include <rtm/ECFactory.h>
+#include <rtm/RTObjectStateMachine.h>
 
 namespace RTC
 {
@@ -31,9 +34,21 @@
    * @endif
    */
   ExtTrigExecutionContext::ExtTrigExecutionContext()
-    : PeriodicExecutionContext()
+    : ExecutionContextBase("exttrig_async_ec"),
+      rtclog("exttrig_async_ec"),
+      m_svc(false)
   {
-    rtclog.setName("exttrig_ec");
+    RTC_TRACE(("ExtTrigExecutionContext()"));
+
+    // getting my reference
+    setObjRef(this->_this());
+
+    // profile initialization
+    setKind(RTC::PERIODIC);
+    setRate(DEFAULT_EXECUTION_RATE);
+
+    RTC_DEBUG(("Actual period: %d [sec], %d [usec]",
+               m_profile.getPeriod().sec(), m_profile.getPeriod().usec()));
   }
   
   /*!
@@ -45,26 +60,39 @@
    */
   ExtTrigExecutionContext::~ExtTrigExecutionContext()
   {
+    RTC_TRACE(("~ExtTrigExecutionContext()"));
+    {
+      {
+        Guard guard(m_svcmutex);
+        m_svc = false;
+      }
+      {
+        Guard guard(m_worker.mutex_);
+        m_worker.ticked_ = true;
+        m_worker.cond_.signal();
+      }
+    }
+    wait();
   }
-  
+
+  /*------------------------------------------------------------
+   * Start activity
+   * ACE_Task class method over ride.
+   *------------------------------------------------------------*/
   /*!
    * @if jp
-   * @brief 処理を1ステップ進める
+   * @brief ExecutionContext用アクティビティスレッドを生成する
    * @else
-   * @brief Move forward one step of ExecutionContext
+   * @brief Generate internal activity thread for ExecutionContext
    * @endif
    */
-  void ExtTrigExecutionContext::tick()
-    throw (CORBA::SystemException)
+  int ExtTrigExecutionContext::open(void *args)
   {
-    RTC_TRACE(("tick()"));
-    m_worker._mutex.lock();
-    m_worker._called = true;
-    m_worker._cond.signal();
-    m_worker._mutex.unlock();
-    return;
+    RTC_TRACE(("open()"));
+    activate();
+    return 0;
   }
-  
+
   /*!
    * @if jp
    * @brief 各 Component の処理を呼び出す。
@@ -75,23 +103,346 @@
   int ExtTrigExecutionContext::svc(void)
   {
     RTC_TRACE(("svc()"));
+    unsigned int count(0);
     do
       {
-	m_worker._mutex.lock();
-	while (!m_worker._called && m_running)
-	  {
-	    m_worker._cond.wait();
-	  }
-	if (m_worker._called)
-	  {
-	    m_worker._called = false;
-	    std::for_each(m_comps.begin(), m_comps.end(), invoke_worker());
-	  }
-	m_worker._mutex.unlock();
-      } while (m_running);
-    
+        {
+          Guard gurad(m_worker.mutex_);
+          RTC_DEBUG(("Start of worker invocation. ticked = %s",
+                     m_worker.ticked_ ? "true" : "false"));
+          while (!m_worker.ticked_)
+            {
+              m_worker.cond_.wait(); // wait for tick
+              RTC_DEBUG(("Thread was woken up."));
+            }
+          if (!m_worker.ticked_) { continue; }
+        }
+        coil::TimeValue t0(coil::gettimeofday());
+        ExecutionContextBase::invokeWorkerPreDo();
+        ExecutionContextBase::invokeWorkerDo();
+        ExecutionContextBase::invokeWorkerPostDo();
+        coil::TimeValue t1(coil::gettimeofday());
+        {
+          Guard gurad(m_worker.mutex_);
+          m_worker.ticked_ = false;
+        }
+        coil::TimeValue period(getPeriod());
+        if (1) //count > 1000)
+          {
+            RTC_PARANOID(("Period:    %f [s]", (double)period));
+            RTC_PARANOID(("Execution: %f [s]", (double)(t1 - t0)));
+            RTC_PARANOID(("Sleep:     %f [s]", (double)(period - (t1 - t0))));
+          }
+        coil::TimeValue t2(coil::gettimeofday());
+        if (period > (t1 - t0))
+          {
+            if (1 /*count > 1000*/) { RTC_PARANOID(("sleeping...")); }
+            coil::sleep((coil::TimeValue)(period - (t1 - t0)));
+          }
+        if (1) //count > 1000)
+          {
+            coil::TimeValue t3(coil::gettimeofday());
+            RTC_PARANOID(("Slept:       %f [s]", (double)(t3 - t2)));
+            count = 0;
+          }
+        ++count;
+      } while (threadRunning());
+
     return 0;
   }
+
+  /*!
+   * @if jp
+   * @brief ExecutionContext 用のスレッド実行関数
+   * @else
+   * @brief Thread execution function for ExecutionContext
+   * @endif
+   */
+  int ExtTrigExecutionContext::close(unsigned long flags)
+  {
+    RTC_TRACE(("close()"));
+    // At this point, this component have to be finished.
+    // Current state and Next state should be RTC_EXITING.
+    return 0;
+  }
+
+  //============================================================
+  // ExtTrigExecutionContextService
+  //============================================================
+  /*!
+   * @if jp
+   * @brief 処理を1ステップ進める
+   * @else
+   * @brief Move forward one step of ExecutionContext
+   * @endif
+   */
+  void ExtTrigExecutionContext::tick()
+    throw (CORBA::SystemException)
+  {
+    RTC_TRACE(("tick()"));
+    if (!isRunning())
+      {
+        RTC_DEBUG(("EC is not running. do nothing."))
+        return;
+      }
+    Guard guard(m_worker.mutex_);
+    m_worker.ticked_ = true;
+    m_worker.cond_.signal();
+    RTC_PARANOID(("EC was ticked. Signal was sent to worker thread."));
+    return;
+  }
+
+  //============================================================
+  // ExecutionContextService
+  //============================================================
+  /*!
+   * @if jp
+   * @brief ExecutionContext 実行状態確認関数
+   * @else
+   * @brief Check for ExecutionContext running state
+   * @endif
+   */
+  CORBA::Boolean ExtTrigExecutionContext::is_running()
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::isRunning();
+  }
+
+  /*!
+   * @if jp
+   * @brief ExecutionContext の実行を開始
+   * @else
+   * @brief Start the ExecutionContext
+   * @endif
+   */
+  RTC::ReturnCode_t ExtTrigExecutionContext::start()
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::start();
+  }
+
+  /*!
+   * @if jp
+   * @brief ExecutionContext の実行を停止
+   * @else
+   * @brief Stop the ExecutionContext
+   * @endif
+   */
+  RTC::ReturnCode_t ExtTrigExecutionContext::stop()
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::stop();
+  }
+
+
+
+  /*!
+   * @if jp
+   * @brief ExecutionContext の実行周期(Hz)を取得する
+   * @else
+   * @brief Get execution rate(Hz) of ExecutionContext
+   * @endif
+   */
+  CORBA::Double ExtTrigExecutionContext::get_rate()
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::getRate();
+  }
+
+  /*!
+   * @if jp
+   * @brief ExecutionContext の実行周期(Hz)を設定する
+   * @else
+   * @brief Set execution rate(Hz) of ExecutionContext
+   * @endif
+   */
+  RTC::ReturnCode_t ExtTrigExecutionContext::set_rate(CORBA::Double rate)
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::setRate(rate);
+  }
+
+  /*!
+   * @if jp
+   * @brief RTコンポーネントを追加する
+   * @else
+   * @brief Add an RT-Component
+   * @endif
+   */
+  RTC::ReturnCode_t
+  ExtTrigExecutionContext::add_component(RTC::LightweightRTObject_ptr comp)
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::addComponent(comp);
+  }
+
+  /*!
+   * @if jp
+   * @brief コンポーネントをコンポーネントリストから削除する
+   * @else
+   * @brief Remove the RT-Component from participant list
+   * @endif
+   */
+  RTC::ReturnCode_t ExtTrigExecutionContext::
+  remove_component(RTC::LightweightRTObject_ptr comp)
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::removeComponent(comp);
+  }
+
+  /*!
+   * @if jp
+   * @brief RTコンポーネントをアクティブ化する
+   * @else
+   * @brief Activate an RT-Component
+   * @endif
+   */
+  RTC::ReturnCode_t ExtTrigExecutionContext::
+  activate_component(RTC::LightweightRTObject_ptr comp)
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::activateComponent(comp);
+  }
+
+  /*!
+   * @if jp
+   * @brief RTコンポーネントを非アクティブ化する
+   * @else
+   * @brief Deactivate an RT-Component
+   * @endif
+   */
+  RTC::ReturnCode_t ExtTrigExecutionContext::
+  deactivate_component(RTC::LightweightRTObject_ptr comp)
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::deactivateComponent(comp);
+  }
+
+  /*!
+   * @if jp
+   * @brief RTコンポーネントをリセットする
+   * @else
+   * @brief Reset the RT-Component
+   * @endif
+   */
+  RTC::ReturnCode_t ExtTrigExecutionContext::
+  reset_component(RTC::LightweightRTObject_ptr comp)
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::resetComponent(comp);
+  }
+
+  /*!
+   * @if jp
+   * @brief RTコンポーネントの状態を取得する
+   * @else
+   * @brief Get RT-Component's state
+   * @endif
+   */
+  RTC::LifeCycleState ExtTrigExecutionContext::
+  get_component_state(RTC::LightweightRTObject_ptr comp)
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::getComponentState(comp);
+  }
+
+  /*!
+   * @if jp
+   * @brief ExecutionKind を取得する
+   * @else
+   * @brief Get the ExecutionKind
+   * @endif
+   */
+  RTC::ExecutionKind ExtTrigExecutionContext::get_kind()
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::getKind();
+  }
+
+  //------------------------------------------------------------
+  // ExecutionContextService interfaces
+  //------------------------------------------------------------
+  /*!
+   * @if jp
+   * @brief ExecutionContextProfile を取得する
+   * @else
+   * @brief Get the ExecutionContextProfile
+   * @endif
+   */
+  RTC::ExecutionContextProfile* ExtTrigExecutionContext::get_profile()
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::getProfile();
+  }
+
+  //============================================================
+  // protected functions
+  //============================================================
+  /*!
+   * @brief onStarted() template function
+   */
+  RTC::ReturnCode_t ExtTrigExecutionContext::onStarted()
+  {
+    // change EC thread state
+    Guard gurad(m_svcmutex);
+    if (!m_svc)
+      { // If start() is called first time, start the worker thread.
+        m_svc = true;
+        this->open(0);
+      }
+    return RTC::RTC_OK;
+  }
+
+  /*!
+   * @brief onWaitingActivated() template function
+   */
+  RTC::ReturnCode_t ExtTrigExecutionContext::
+  onWaitingActivated(RTC_impl::RTObjectStateMachine* comp, long int count)
+  {
+    RTC_TRACE(("onWaitingActivated(count = %d)", count));
+    RTC_PARANOID(("curr: %s, next: %s",
+                  getStateString(comp->getStates().curr),
+                  getStateString(comp->getStates().next)));
+    // Now comp's next state must be ACTIVE state
+    // If worker thread is stopped, restart worker thread.
+    Guard guard(m_worker.mutex_);
+    m_worker.ticked_ = true;
+    m_worker.cond_.signal();
+    return RTC::RTC_OK;
+  }
+
+
+  /*!
+   * @brief onWaitingDeactivated() template function
+   */
+  RTC::ReturnCode_t ExtTrigExecutionContext::
+  onWaitingDeactivated(RTC_impl::RTObjectStateMachine* comp, long int count)
+  {
+    RTC_TRACE(("onWaitingDeactivated(count = %d)", count));
+    RTC_PARANOID(("curr: %s, next: %s",
+                  getStateString(comp->getStates().curr),
+                  getStateString(comp->getStates().next)));
+    Guard guard(m_worker.mutex_);
+    m_worker.ticked_ = true;
+    m_worker.cond_.signal();
+    return RTC::RTC_OK;
+  }
+
+  /*!
+   * @brief onWaitingReset() template function
+   */
+  RTC::ReturnCode_t ExtTrigExecutionContext::
+  onWaitingReset(RTC_impl::RTObjectStateMachine* comp, long int count)
+  {
+    RTC_TRACE(("onWaitingReset(count = %d)", count));
+    RTC_PARANOID(("curr: %s, next: %s",
+                  getStateString(comp->getStates().curr),
+                  getStateString(comp->getStates().next)));
+    Guard guard(m_worker.mutex_);
+    m_worker.ticked_ = true;
+    m_worker.cond_.signal();
+    return RTC::RTC_OK;
+  }
 };
 
 

Modified: trunk/OpenRTM-aist/src/lib/rtm/ExtTrigExecutionContext.h
===================================================================
--- trunk/OpenRTM-aist/src/lib/rtm/ExtTrigExecutionContext.h	2012-02-05 21:21:42 UTC (rev 2306)
+++ trunk/OpenRTM-aist/src/lib/rtm/ExtTrigExecutionContext.h	2012-02-05 21:29:15 UTC (rev 2307)
@@ -5,8 +5,7 @@
  * @date $Date: 2008-01-14 07:49:16 $
  * @author Noriaki Ando <n-ando at aist.go.jp>
  *
- * Copyright (C) 2007
- *     Task-intelligence Research Group,
+ * Copyright (C) 2007,2012
  *     Intelligent Systems Research Institute,
  *     National Institute of
  *         Advanced Industrial Science and Technology (AIST), Japan
@@ -25,8 +24,7 @@
 #include <coil/Condition.h>
 #include <coil/Task.h>
 
-#include <rtm/Manager.h>
-#include <rtm/PeriodicExecutionContext.h>
+#include <rtm/ExecutionContextBase.h>
 
 #ifdef WIN32
 #pragma warning( disable : 4290 )
@@ -57,10 +55,14 @@
    * @endif
    */
   class ExtTrigExecutionContext
-    : public virtual PeriodicExecutionContext
+    : public virtual POA_OpenRTM::ExtTrigExecutionContextService,
+      public virtual PortableServer::RefCountServantBase,
+      public RTC::ExecutionContextBase,
+      public coil::Task
   {
     typedef coil::Mutex Mutex;
     typedef coil::Condition<Mutex> Condition;
+    typedef coil::Guard<coil::Mutex> Guard;
   public:
     /*!
      * @if jp
@@ -94,20 +96,30 @@
     
     /*!
      * @if jp
-     * @brief 処理を1ステップ進める
+     * @brief ExecutionContext用アクティビティスレッドを生成する
      *
-     * ExecutionContextの処理を1周期分進める。
+     * Executioncontext 用の内部アクティビティスレッドを生成し起動する。
+     * これは coil::Task サービスクラスメソッドのオーバーライド。
      *
+     * @param args 通常は0
+     *
+     * @return 生成処理実行結果
+     *
      * @else
-     * @brief Move forward one step of ExecutionContext
      *
-     * Move forward one step of the ExecutionContext processing.
+     * @brief Generate internal activity thread for ExecutionContext
      *
+     * Generate internal activity thread and run.  This is coil::Task
+     * class method's override.
+     *
+     * @param args Usually give 0
+     *
+     * @return The generation result
+     *
      * @endif
      */
-    virtual void tick()
-      throw (CORBA::SystemException);
-    
+    virtual int open(void *args);
+
     /*!
      * @if jp
      * @brief 各 Component の処理を呼び出す。
@@ -120,23 +132,517 @@
      * @else
      * @brief Invoke each component's operation
      *
-     * Invoke each component's operation which is attached this ExecutionContext.
-     * Stop until the next operation is invoked after all component operations
-     * are invoked.
+     * Invoke each component's operation which is attached this
+     * ExecutionContext.  Stop until the next operation is invoked
+     * after all component operations are invoked.
      *
      * @return Operation result
      *
      * @endif
      */
     virtual int svc(void);
+
+    /*!
+     * @if jp
+     * @brief ExecutionContext 用のスレッド実行関数
+     *
+     * ExecutionContext 用のスレッド終了時に呼ばれる。コンポーネントオ
+     * ブジェクトの非アクティブ化、マネージャへの通知を行う。これは
+     * coil::Task サービスクラスメソッドのオーバーライド。
+     *
+     * @param flags 終了処理フラグ
+     *
+     * @return 終了処理結果
+     *
+     * @else
+     *
+     * @brief Thread execution function for ExecutionContext
+     *
+     * This function is invoked when activity thread for
+     * ExecutionContext exits.  Deactivate the component object and
+     * notify it to manager.  This is coil::Task class method's
+     * override.
+     *
+     * @param flags Flag of the close
+     *
+     * @return The close result
+     *
+     * @endif
+     */
+    virtual int close(unsigned long flags);
+
+    //============================================================
+    // ExtTrigExecutionContextService
+    //============================================================
+    /*!
+     * @if jp
+     * @brief 処理を1ステップ進める
+     *
+     * ExecutionContextの処理を1周期分進める。
+     *
+     * @else
+     * @brief Move forward one step of ExecutionContext
+     *
+     * Move forward one step of the ExecutionContext processing.
+     *
+     * @endif
+     */
+    virtual void tick()
+      throw (CORBA::SystemException);
     
+    //============================================================
+    // ExecutionContextService
+    //============================================================
+    /*!
+     * @if jp
+     * @brief ExecutionContext 実行状態確認関数
+     *
+     * この操作は ExecutionContext が Runnning 状態の場合に true を返す。
+     * Executioncontext が Running の間、当該 Executioncontext に参加し
+     * ている全てのアクティブRTコンポーネントが、ExecutionContext の実
+     * 行種類に応じて実行される。
+     *
+     * @return 状態確認関数(動作中:true、停止中:false)
+     *
+     * @else
+     *
+     * @brief Check for ExecutionContext running state
+     *
+     * This operation shall return true if the context is in the
+     * Running state.  While the context is Running, all Active RTCs
+     * participating in the context shall be executed according to the
+     * context’s execution kind.
+     *
+     * @return Check state function (Running:true、Stopping:false)
+     *
+     * @endif
+     */
+    virtual CORBA::Boolean is_running(void)
+      throw (CORBA::SystemException);
+
+    /*!
+     * @if jp
+     * @brief ExecutionContext の実行を開始
+     *
+     * ExecutionContext の実行状態を Runnning とするためのリクエストを
+     * 発行する。ExecutionContext の状態が遷移すると
+     * ComponentAction::on_startup が呼び出される。参加しているRTコンポー
+     * ネントが、初期化されるまで ExecutionContext を開始することはでき
+     * ない。ExecutionContext は複数回開始/停止を繰り返すことができる。
+     *
+     * @return ReturnCode_t 型のリターンコード
+     *
+     * @else
+     *
+     * @brief Start the ExecutionContext
+     *
+     * Request that the context enter the Running state.  Once the
+     * state transition occurs, the ComponentAction::on_startup
+     * operation will be invoked.  An execution context may not be
+     * started until the RT-Components that participate in it have
+     * been initialized.  An execution context may be started and
+     * stopped multiple times.
+     *
+     * @return The return code of ReturnCode_t type
+     *
+     * @endif
+     */
+    virtual RTC::ReturnCode_t start(void)
+      throw (CORBA::SystemException);
+
+    /*!
+     * @if jp
+     * @brief ExecutionContext の実行を停止
+     *
+     * ExecutionContext の状態を Stopped とするためのリクエストを発行す
+     * る。遷移が発生した場合は、ComponentAction::on_shutdown が呼び出
+     * される。参加しているRTコンポーネントが終了する前に
+     * ExecutionContext を停止する必要がある。ExecutionContext は複数回
+     * 開始/停止を繰り返すことができる。
+     *
+     * @return ReturnCode_t 型のリターンコード
+     *
+     * @else
+     *
+     * @brief Stop the ExecutionContext
+     *
+     * Request that the context enter the Stopped state.  Once the
+     * transition occurs, the ComponentAction::on_shutdown operation
+     * will be invoked.  An execution context must be stopped before
+     * the RT components that participate in it are finalized.  An
+     * execution context may be started and stopped multiple times.
+     *
+     * @return The return code of ReturnCode_t type
+     *
+     * @endif
+     */
+    virtual RTC::ReturnCode_t stop(void)
+      throw (CORBA::SystemException);
+
+    /*!
+     * @if jp
+     * @brief ExecutionContext の実行周期(Hz)を取得する
+     *
+     * Active 状態にてRTコンポーネントが実行される周期(単位:Hz)を取得す
+     * る。
+     *
+     * @return 処理周期(単位:Hz)
+     *
+     * @else
+     *
+     * @brief Get execution rate(Hz) of ExecutionContext
+     *
+     * This operation shall return the rate (in hertz) at which its
+     * Active participating RTCs are being invoked.
+     *
+     * @return Execution cycle(Unit:Hz)
+     *
+     * @endif
+     */
+    virtual CORBA::Double get_rate(void)
+      throw (CORBA::SystemException);
+
+    /*!
+     * @if jp
+     * @brief ExecutionContext の実行周期(Hz)を設定する
+     *
+     * Active 状態にてRTコンポーネントが実行される周期(単位:Hz)を設定す
+     * る。実行周期の変更は、DataFlowComponentAction の
+     * on_rate_changed によって各RTコンポーネントに伝達される。
+     *
+     * @param rate 処理周期(単位:Hz)
+     *
+     * @return ReturnCode_t 型のリターンコード
+     *
+     * @else
+     *
+     * @brief Set execution rate(Hz) of ExecutionContext
+     *
+     * This operation shall set the rate (in hertz) at which this
+     * context’s Active participating RTCs are being called.  If the
+     * execution kind of the context is PERIODIC, a rate change shall
+     * result in the invocation of on_rate_changed on any RTCs
+     * realizing DataFlowComponentAction that are registered with any
+     * RTCs participating in the context.
+     *
+     * @param rate Execution cycle(Unit:Hz)
+     *
+     * @return The return code of ReturnCode_t type
+     *
+     * @endif
+     */
+    virtual RTC::ReturnCode_t  set_rate(CORBA::Double rate)
+      throw (CORBA::SystemException);
+
+    /*!
+     * @if jp
+     * @brief RTコンポーネントをアクティブ化する
+     *
+     * Inactive 状態にあるRTコンポーネントをActive に遷移させ、アクティ
+     * ブ化する。この操作が呼ばれた結果、on_activate が呼び出される。指
+     * 定したRTコンポーネントが参加者リストに含まれない場合は、
+     * BAD_PARAMETER が返される。指定したRTコンポーネントの状態が
+     * Inactive 以外の場合は、PRECONDITION_NOT_MET が返される。
+     *
+     * @param comp アクティブ化対象RTコンポーネント
+     *
+     * @return ReturnCode_t 型のリターンコード
+     *
+     * @else
+     *
+     * @brief Activate an RT-component
+     *
+     * The given participant RTC is Inactive and is therefore not
+     * being invoked according to the execution context’s execution
+     * kind. This operation shall cause the RTC to transition to the
+     * Active state such that it may subsequently be invoked in this
+     * execution context.  The callback on_activate shall be called as
+     * a result of calling this operation. This operation shall not
+     * return until the callback has returned, and shall result in an
+     * error if the callback does.
+     *
+     * @param comp The target RT-Component for activation
+     *
+     * @return The return code of ReturnCode_t type
+     *
+     * @endif
+     */
+    virtual RTC::ReturnCode_t
+    activate_component(RTC::LightweightRTObject_ptr comp)
+      throw (CORBA::SystemException);
+    
+    /*!
+     * @if jp
+     * @brief RTコンポーネントを非アクティブ化する
+     *
+     * Inactive 状態にあるRTコンポーネントを非アクティブ化し、Inactive
+     * に遷移させる。この操作が呼ばれた結果、on_deactivate が呼び出され
+     * る。指定したRTコンポーネントが参加者リストに含まれない場合は、
+     * BAD_PARAMETER が返される。指定したRTコンポーネントの状態が
+     * Active 以外の場合は、PRECONDITION_NOT_MET が返される。
+     *
+     * @param comp 非アクティブ化対象RTコンポーネント
+     *
+     * @return ReturnCode_t 型のリターンコード
+     *
+     * @else
+     *
+     * @brief Deactivate an RT-component
+     *
+     * The given RTC is Active in the execution context. Cause it to
+     * transition to the Inactive state such that it will not be
+     * subsequently invoked from the context unless and until it is
+     * activated again.  The callback on_deactivate shall be called as
+     * a result of calling this operation. This operation shall not
+     * return until the callback has returned, and shall result in an
+     * error if the callback does.
+     *
+     * @param comp The target RT-Component for deactivate
+     *
+     * @return The return code of ReturnCode_t type
+     *
+     * @endif
+     */
+    virtual RTC::ReturnCode_t
+    deactivate_component(RTC::LightweightRTObject_ptr comp)
+      throw (CORBA::SystemException);
+
+    /*!
+     * @if jp
+     * @brief RTコンポーネントをリセットする
+     *
+     * Error 状態のRTコンポーネントの復帰を試みる。この操作が呼ばれた結
+     * 果、on_reset が呼び出される。指定したRTコンポーネントが参加者リ
+     * ストに含まれない場合は、BAD_PARAMETER が返される。指定したRTコン
+     * ポーネントの状態が Error 以外の場合は、PRECONDITION_NOT_MET が返
+     * される。
+     *
+     * @param comp リセット対象RTコンポーネント
+     *
+     * @return ReturnCode_t 型のリターンコード
+     *
+     * @else
+     *
+     * @brief Reset the RT-component
+     *
+     * Attempt to recover the RTC when it is in Error.  The
+     * ComponentAction::on_reset callback shall be invoked. This
+     * operation shall not return until the callback has returned, and
+     * shall result in an error if the callback does. If possible, the
+     * RTC developer should implement that callback such that the RTC
+     * may be returned to a valid state.
+     *
+     * @param comp The target RT-Component for reset
+     *
+     * @return The return code of ReturnCode_t type
+     *
+     * @endif
+     */
+    virtual RTC::ReturnCode_t
+    reset_component(RTC::LightweightRTObject_ptr comp)
+      throw (CORBA::SystemException);
+
+    /*!
+     * @if jp
+     * @brief RTコンポーネントの状態を取得する
+     *
+     * 指定したRTコンポーネントの状態(LifeCycleState)を取得する。指定し
+     * たRTコンポーネントが参加者リストに含まれない場合は、
+     * UNKNOWN_STATE が返される。
+     *
+     * @param comp 状態取得対象RTコンポーネント
+     *
+     * @return 現在の状態(LifeCycleState)
+     *
+     * @else
+     *
+     * @brief Get RT-component's state
+     *
+     * This operation shall report the LifeCycleState of the given
+     * participant RTC.  UNKNOWN_STATE will be returned, if the given
+     * RT-Component is not inclued in the participant list.
+     *
+     * @param comp The target RT-Component to get the state
+     *
+     * @return The current state of the target RT-Component(LifeCycleState)
+     *
+     * @endif
+     */
+    virtual RTC::LifeCycleState
+    get_component_state(RTC::LightweightRTObject_ptr comp)
+      throw (CORBA::SystemException);
+
+    /*!
+     * @if jp
+     * @brief ExecutionKind を取得する
+     *
+     * 本 ExecutionContext の ExecutionKind を取得する
+     *
+     * @return ExecutionKind
+     *
+     * @else
+     *
+     * @brief Get the ExecutionKind
+     *
+     * This operation shall report the execution kind of the execution
+     * context.
+     *
+     * @return ExecutionKind
+     *
+     * @endif
+     */
+    virtual RTC::ExecutionKind get_kind(void)
+      throw (CORBA::SystemException);
+
+    /*!
+     * @if jp
+     * @brief RTコンポーネントを追加する
+     *
+     * 指定したRTコンポーネントを参加者リストに追加する。追加されたRTコ
+     * ンポーネントは attach_context が呼ばれ、Inactive 状態に遷移する。
+     * 指定されたRTコンポーネントがnullの場合は、BAD_PARAMETER が返され
+     * る。指定されたRTコンポーネントが DataFlowComponent 以外の場合は、
+     * BAD_PARAMETER が返される。
+     *
+     * @param comp 追加対象RTコンポーネント
+     *
+     * @return ReturnCode_t 型のリターンコード
+     *
+     * @else
+     *
+     * @brief Add an RT-component
+     *
+     * The operation causes the given RTC to begin participating in
+     * the execution context.  The newly added RTC will receive a call
+     * to LightweightRTComponent::attach_context and then enter the
+     * Inactive state.  BAD_PARAMETER will be invoked, if the given
+     * RT-Component is null or if the given RT-Component is other than
+     * DataFlowComponent.
+     *
+     * @param comp The target RT-Component for add
+     *
+     * @return The return code of ReturnCode_t type
+     *
+     * @endif
+     */
+    virtual RTC::ReturnCode_t add_component(RTC::LightweightRTObject_ptr comp)
+      throw (CORBA::SystemException);
+
+    /*!
+     * @if jp
+     * @brief RTコンポーネントを参加者リストから削除する
+     *
+     * 指定したRTコンポーネントを参加者リストから削除する。削除された
+     * RTコンポーネントは detach_context が呼ばれる。指定されたRTコンポー
+     * ネントが参加者リストに登録されていない場合は、BAD_PARAMETER が返
+     * される。
+     *
+     * @param comp 削除対象RTコンポーネント
+     *
+     * @return ReturnCode_t 型のリターンコード
+     *
+     * @else
+     *
+     * @brief Remove the RT-Component from participant list
+     *
+     * This operation causes a participant RTC to stop participating in the
+     * execution context.
+     * The removed RTC will receive a call to
+     * LightweightRTComponent::detach_context.
+     * BAD_PARAMETER will be returned, if the given RT-Component is not
+     * participating in the participant list.
+     *
+     * @param comp The target RT-Component for delete
+     *
+     * @return The return code of ReturnCode_t type
+     *
+     * @endif
+     */
+    virtual RTC::ReturnCode_t
+    remove_component(RTC::LightweightRTObject_ptr comp)
+      throw (CORBA::SystemException);
+
+    /*!
+     * @if jp
+     * @brief ExecutionContextProfile を取得する
+     *
+     * 本 ExecutionContext のプロファイルを取得する。
+     *
+     * @return ExecutionContextProfile
+     *
+     * @else
+     *
+     * @brief Get the ExecutionContextProfile
+     *
+     * This operation provides a profile “descriptor” for the execution
+     * context.
+     *
+     * @return ExecutionContextProfile
+     *
+     * @endif
+     */
+    virtual RTC::ExecutionContextProfile* get_profile(void)
+      throw (CORBA::SystemException);
+
+  protected:
+    /*!
+     * @brief onStarted() template function
+     */
+    virtual RTC::ReturnCode_t onStarted();
+    /*!
+     * @brief onWaitingActivated() template function
+     */
+    virtual RTC::ReturnCode_t
+    onWaitingActivated(RTC_impl::RTObjectStateMachine* comp, long int count);
+    /*!
+     * @brief onWaitingDeactivated() template function
+     */
+    virtual RTC::ReturnCode_t
+    onWaitingDeactivated(RTC_impl::RTObjectStateMachine* comp, long int count);
+    /*!
+     * @brief onWaitingReset() template function
+     */
+    virtual RTC::ReturnCode_t
+    onWaitingReset(RTC_impl::RTObjectStateMachine* comp, long int count);
+
   private:
+    bool threadRunning()
+    {
+      Guard guard(m_svcmutex);
+      return m_svc;
+    }
+    /*!
+     * @if jp
+     * @brief ロガーストリーム
+     * @else
+     * @brief Logger stream
+     * @endif
+     */
+    RTC::Logger rtclog;
+
+    /*!
+     * @if jp
+     * @brief ExecutionContext のスレッド実行フラグ
+     * @else
+     * @brief The thread running flag of ExecutionContext
+     * @endif
+     */
+    bool m_svc;
+    Mutex m_svcmutex;
+
+    /*!
+     * @if jp
+     * @brief worker 用状態変数クラス
+     * @else
+     * @brief Condition variable class for worker
+     * @endif
+     */
     struct Worker
     {
-      Worker() : _cond(_mutex), _called(false) {};
-      Mutex _mutex;
-      Condition _cond;
-      bool _called;
+      Worker() : cond_(mutex_), ticked_(false) {};
+      Mutex mutex_;
+      Condition cond_;
+      bool ticked_;
     };
     // A condition variable for external triggered worker
     Worker m_worker;

Modified: trunk/OpenRTM-aist/src/lib/rtm/Makefile.am
===================================================================
--- trunk/OpenRTM-aist/src/lib/rtm/Makefile.am	2012-02-05 21:21:42 UTC (rev 2306)
+++ trunk/OpenRTM-aist/src/lib/rtm/Makefile.am	2012-02-05 21:29:15 UTC (rev 2307)
@@ -66,6 +66,8 @@
 	DataFlowComponentBase.cpp    \
 	ExecutionContextBase.cpp     \
 	ExecutionContextProfile.cpp  \
+	RTObjectStateMachine.cpp     \
+	ExecutionContextWorker.cpp   \
 	PeriodicExecutionContext.cpp \
 	ExtTrigExecutionContext.cpp  \
 	OpenHRPExecutionContext.cpp  \

Modified: trunk/OpenRTM-aist/src/lib/rtm/OpenHRPExecutionContext.cpp
===================================================================
--- trunk/OpenRTM-aist/src/lib/rtm/OpenHRPExecutionContext.cpp	2012-02-05 21:21:42 UTC (rev 2306)
+++ trunk/OpenRTM-aist/src/lib/rtm/OpenHRPExecutionContext.cpp	2012-02-05 21:29:15 UTC (rev 2307)
@@ -1,6 +1,22 @@
 // -*- C++ -*-
+/*!
+ * @file OpenHRPExecutionContext.cpp
+ * @brief OpenHRPExecutionContext class
+ * @date $Date$
+ * @author Noriaki Ando <n-ando at aist.go.jp>
+ *
+ * Copyright (C) 2006-2008,2012
+ *     Noriaki Ando
+ *     Intelligent Systems Research Institute,
+ *     National Institute of
+ *         Advanced Industrial Science and Technology (AIST), Japan
+ *     All rights reserved.
+ *
+ * $Id$
+ *
+ */
 
-#include "OpenHRPExecutionContext.h"
+#include <rtm/OpenHRPExecutionContext.h>
 #include <rtm/ECFactory.h>
 
 namespace RTC
@@ -13,8 +29,21 @@
    * @endif
    */
   OpenHRPExecutionContext::OpenHRPExecutionContext()
-    : PeriodicExecutionContext()
+    :  ExecutionContextBase("exttrig_sync_ec"),
+       rtclog("exttrig_sync_ec"), m_count(0)
   {
+    RTC_TRACE(("OpenHRPExecutionContext()"));
+
+    // getting my reference
+    setObjRef(this->_this());
+
+    // profile initialization
+    setKind(RTC::PERIODIC);
+    setRate(DEFAULT_EXECUTION_RATE);
+
+    RTC_DEBUG(("Actual period: %d [sec], %d [usec]",
+               m_profile.getPeriod().sec(), m_profile.getPeriod().usec()));
+
   }
 
   /*!
@@ -26,33 +55,240 @@
    */
   OpenHRPExecutionContext::~OpenHRPExecutionContext()
   {
+    RTC_TRACE(("~OpenHRPExecutionContext()"));
+    Guard guard(m_tickmutex);
   }
 
+  //============================================================
+  // OpenHRPExecutionContextService
+  //============================================================
   /*!
    * @if jp
-   * @brief ExecutionContextの処理を進める
+   * @brief 処理を1ステップ進める
    * @else
-   * @brief Proceed with tick of ExecutionContext
+   * @brief Move forward one step of ExecutionContext
    * @endif
    */
   void OpenHRPExecutionContext::tick()
     throw (CORBA::SystemException)
   {
-    std::for_each(m_comps.begin(), m_comps.end(), invoke_worker());
+    RTC_TRACE(("tick()"));
+    if (!isRunning()) { return; }
+    Guard guard(m_tickmutex);
+
+    ExecutionContextBase::invokeWorkerPreDo(); // update state
+    coil::TimeValue t0(coil::gettimeofday());
+    ExecutionContextBase::invokeWorkerDo();
+    coil::TimeValue t1(coil::gettimeofday());
+    ExecutionContextBase::invokeWorkerPostDo();
+    coil::TimeValue t2(coil::gettimeofday());
+
+    coil::TimeValue period(getPeriod());
+    if (m_count > 1000)
+      {
+        RTC_PARANOID(("Period:      %f [s]", (double)period));
+        RTC_PARANOID(("Exec-Do:     %f [s]", (double)(t1 - t0)));
+        RTC_PARANOID(("Exec-PostDo: %f [s]", (double)(t2 - t1)));
+        RTC_PARANOID(("Sleep:       %f [s]", (double)(period - (t2 - t0))));
+      }
+    coil::TimeValue t3(coil::gettimeofday());
+    if (period > (t2 - t0))
+      {
+        if (m_count > 1000) { RTC_PARANOID(("sleeping...")); }
+        coil::sleep((coil::TimeValue)(period - (t2 - t0)));
+      }
+    if (m_count > 1000)
+      {
+        coil::TimeValue t4(coil::gettimeofday());
+        RTC_PARANOID(("Slept:       %f [s]", (double)(t4 - t3)));
+        m_count = 0;
+      }
+    ++m_count;
     return;
   }
 
+  //============================================================
+  // ExecutionContextService
+  //============================================================
   /*!
    * @if jp
-   * @brief ExecutionContext のスレッド実行フラグ
+   * @brief ExecutionContext 実行状態確認関数
    * @else
-   * @brief The thread running flag of ExecutionContext
+   * @brief Check for ExecutionContext running state
    * @endif
    */
-  int OpenHRPExecutionContext::svc(void)
+  CORBA::Boolean OpenHRPExecutionContext::is_running()
+    throw (CORBA::SystemException)
   {
-    return 0;
+    return ExecutionContextBase::isRunning();
   }
+
+  /*!
+   * @if jp
+   * @brief ExecutionContext の実行を開始
+   * @else
+   * @brief Start the ExecutionContext
+   * @endif
+   */
+  RTC::ReturnCode_t OpenHRPExecutionContext::start()
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::start();
+  }
+
+  /*!
+   * @if jp
+   * @brief ExecutionContext の実行を停止
+   * @else
+   * @brief Stop the ExecutionContext
+   * @endif
+   */
+  RTC::ReturnCode_t OpenHRPExecutionContext::stop()
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::stop();
+  }
+
+
+
+  /*!
+   * @if jp
+   * @brief ExecutionContext の実行周期(Hz)を取得する
+   * @else
+   * @brief Get execution rate(Hz) of ExecutionContext
+   * @endif
+   */
+  CORBA::Double OpenHRPExecutionContext::get_rate()
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::getRate();
+  }
+
+  /*!
+   * @if jp
+   * @brief ExecutionContext の実行周期(Hz)を設定する
+   * @else
+   * @brief Set execution rate(Hz) of ExecutionContext
+   * @endif
+   */
+  RTC::ReturnCode_t OpenHRPExecutionContext::set_rate(CORBA::Double rate)
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::setRate(rate);
+  }
+
+  /*!
+   * @if jp
+   * @brief RTコンポーネントを追加する
+   * @else
+   * @brief Add an RT-Component
+   * @endif
+   */
+  RTC::ReturnCode_t
+  OpenHRPExecutionContext::add_component(RTC::LightweightRTObject_ptr comp)
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::addComponent(comp);
+  }
+
+  /*!
+   * @if jp
+   * @brief コンポーネントをコンポーネントリストから削除する
+   * @else
+   * @brief Remove the RT-Component from participant list
+   * @endif
+   */
+  RTC::ReturnCode_t OpenHRPExecutionContext::
+  remove_component(RTC::LightweightRTObject_ptr comp)
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::removeComponent(comp);
+  }
+
+  /*!
+   * @if jp
+   * @brief RTコンポーネントをアクティブ化する
+   * @else
+   * @brief Activate an RT-Component
+   * @endif
+   */
+  RTC::ReturnCode_t OpenHRPExecutionContext::
+  activate_component(RTC::LightweightRTObject_ptr comp)
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::activateComponent(comp);
+  }
+
+  /*!
+   * @if jp
+   * @brief RTコンポーネントを非アクティブ化する
+   * @else
+   * @brief Deactivate an RT-Component
+   * @endif
+   */
+  RTC::ReturnCode_t OpenHRPExecutionContext::
+  deactivate_component(RTC::LightweightRTObject_ptr comp)
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::deactivateComponent(comp);
+  }
+
+  /*!
+   * @if jp
+   * @brief RTコンポーネントをリセットする
+   * @else
+   * @brief Reset the RT-Component
+   * @endif
+   */
+  RTC::ReturnCode_t OpenHRPExecutionContext::
+  reset_component(RTC::LightweightRTObject_ptr comp)
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::resetComponent(comp);
+  }
+
+  /*!
+   * @if jp
+   * @brief RTコンポーネントの状態を取得する
+   * @else
+   * @brief Get RT-Component's state
+   * @endif
+   */
+  RTC::LifeCycleState OpenHRPExecutionContext::
+  get_component_state(RTC::LightweightRTObject_ptr comp)
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::getComponentState(comp);
+  }
+
+  /*!
+   * @if jp
+   * @brief ExecutionKind を取得する
+   * @else
+   * @brief Get the ExecutionKind
+   * @endif
+   */
+  RTC::ExecutionKind OpenHRPExecutionContext::get_kind()
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::getKind();
+  }
+
+  //------------------------------------------------------------
+  // ExecutionContextService interfaces
+  //------------------------------------------------------------
+  /*!
+   * @if jp
+   * @brief ExecutionContextProfile を取得する
+   * @else
+   * @brief Get the ExecutionContextProfile
+   * @endif
+   */
+  RTC::ExecutionContextProfile* OpenHRPExecutionContext::get_profile()
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::getProfile();
+  }
 };
 
 

Modified: trunk/OpenRTM-aist/src/lib/rtm/OpenHRPExecutionContext.h
===================================================================
--- trunk/OpenRTM-aist/src/lib/rtm/OpenHRPExecutionContext.h	2012-02-05 21:21:42 UTC (rev 2306)
+++ trunk/OpenRTM-aist/src/lib/rtm/OpenHRPExecutionContext.h	2012-02-05 21:29:15 UTC (rev 2307)
@@ -5,9 +5,8 @@
  * @date  $Date: 2008-01-14 07:49:59 $
  * @author Noriaki Ando <n-ando at aist.go.jp>
  *
- * Copyright (C) 2006-2008
+ * Copyright (C) 2006-2008,2012
  *     Noriaki Ando
- *     Task-intelligence Research Group,
  *     Intelligent Systems Research Institute,
  *     National Institute of
  *         Advanced Industrial Science and Technology (AIST), Japan
@@ -20,13 +19,11 @@
 #ifndef RTC_OPENHRPEXECUTIONCONTEXT_H
 #define RTC_OPENHRPEXECUTIONCONTEXT_H
 
+#include <coil/Mutex.h>
+#include <coil/Guard.h>
 #include <rtm/RTC.h>
+#include <rtm/ExecutionContextBase.h>
 
-#include <coil/Task.h>
-
-#include <rtm/Manager.h>
-#include <rtm/PeriodicExecutionContext.h>
-
 #ifdef WIN32
 #pragma warning( disable : 4290 )
 #endif
@@ -52,8 +49,12 @@
    * @endif
    */
   class OpenHRPExecutionContext
-    : public virtual PeriodicExecutionContext
+    : public virtual POA_OpenRTM::ExtTrigExecutionContextService,
+      public virtual PortableServer::RefCountServantBase,
+      public RTC::ExecutionContextBase
   {
+    typedef coil::Mutex Mutex;
+    typedef coil::Guard<coil::Mutex> Guard;
   public:
     /*!
      * @if jp
@@ -73,32 +74,438 @@
      */
     virtual ~OpenHRPExecutionContext(void);
 
+
+    //============================================================
+    // ExtTrigExecutionContextService
+    //============================================================
     /*!
      * @if jp
-     * @brief ExecutionContextの処理を進める
+     * @brief 処理を1ステップ進める
      *
-     * ExecutionContextの処理を1周期分進める。
+     * ExecutionContextの処理を1周期分進める。
      *
      * @else
-     * @brief Proceed with tick of ExecutionContext
+     * @brief Move forward one step of ExecutionContext
      *
-     * Proceed with tick of ExecutionContext for one period.
+     * Move forward one step of the ExecutionContext processing.
      *
      * @endif
      */
-    virtual void tick(void)
+    virtual void tick()
       throw (CORBA::SystemException);
+    
+    //============================================================
+    // ExecutionContextService
+    //============================================================
+    /*!
+     * @if jp
+     * @brief ExecutionContext 実行状態確認関数
+     *
+     * この操作は ExecutionContext が Runnning 状態の場合に true を返す。
+     * Executioncontext が Running の間、当該 Executioncontext に参加し
+     * ている全てのアクティブRTコンポーネントが、ExecutionContext の実
+     * 行種類に応じて実行される。
+     *
+     * @return 状態確認関数(動作中:true、停止中:false)
+     *
+     * @else
+     *
+     * @brief Check for ExecutionContext running state
+     *
+     * This operation shall return true if the context is in the
+     * Running state.  While the context is Running, all Active RTCs
+     * participating in the context shall be executed according to the
+     * context’s execution kind.
+     *
+     * @return Check state function (Running:true、Stopping:false)
+     *
+     * @endif
+     */
+    virtual CORBA::Boolean is_running(void)
+      throw (CORBA::SystemException);
 
     /*!
      * @if jp
-     * @brief ExecutionContext のスレッド実行フラグ
+     * @brief ExecutionContext の実行を開始
+     *
+     * ExecutionContext の実行状態を Runnning とするためのリクエストを
+     * 発行する。ExecutionContext の状態が遷移すると
+     * ComponentAction::on_startup が呼び出される。参加しているRTコンポー
+     * ネントが、初期化されるまで ExecutionContext を開始することはでき
+     * ない。ExecutionContext は複数回開始/停止を繰り返すことができる。
+     *
+     * @return ReturnCode_t 型のリターンコード
+     *
      * @else
-     * @brief The thread running flag of ExecutionContext
+     *
+     * @brief Start the ExecutionContext
+     *
+     * Request that the context enter the Running state.  Once the
+     * state transition occurs, the ComponentAction::on_startup
+     * operation will be invoked.  An execution context may not be
+     * started until the RT-Components that participate in it have
+     * been initialized.  An execution context may be started and
+     * stopped multiple times.
+     *
+     * @return The return code of ReturnCode_t type
+     *
      * @endif
      */
-    virtual int svc(void);
+    virtual RTC::ReturnCode_t start(void)
+      throw (CORBA::SystemException);
 
+    /*!
+     * @if jp
+     * @brief ExecutionContext の実行を停止
+     *
+     * ExecutionContext の状態を Stopped とするためのリクエストを発行す
+     * る。遷移が発生した場合は、ComponentAction::on_shutdown が呼び出
+     * される。参加しているRTコンポーネントが終了する前に
+     * ExecutionContext を停止する必要がある。ExecutionContext は複数回
+     * 開始/停止を繰り返すことができる。
+     *
+     * @return ReturnCode_t 型のリターンコード
+     *
+     * @else
+     *
+     * @brief Stop the ExecutionContext
+     *
+     * Request that the context enter the Stopped state.  Once the
+     * transition occurs, the ComponentAction::on_shutdown operation
+     * will be invoked.  An execution context must be stopped before
+     * the RT components that participate in it are finalized.  An
+     * execution context may be started and stopped multiple times.
+     *
+     * @return The return code of ReturnCode_t type
+     *
+     * @endif
+     */
+    virtual RTC::ReturnCode_t stop(void)
+      throw (CORBA::SystemException);
+
+    /*!
+     * @if jp
+     * @brief ExecutionContext の実行周期(Hz)を取得する
+     *
+     * Active 状態にてRTコンポーネントが実行される周期(単位:Hz)を取得す
+     * る。
+     *
+     * @return 処理周期(単位:Hz)
+     *
+     * @else
+     *
+     * @brief Get execution rate(Hz) of ExecutionContext
+     *
+     * This operation shall return the rate (in hertz) at which its
+     * Active participating RTCs are being invoked.
+     *
+     * @return Execution cycle(Unit:Hz)
+     *
+     * @endif
+     */
+    virtual CORBA::Double get_rate(void)
+      throw (CORBA::SystemException);
+
+    /*!
+     * @if jp
+     * @brief ExecutionContext の実行周期(Hz)を設定する
+     *
+     * Active 状態にてRTコンポーネントが実行される周期(単位:Hz)を設定す
+     * る。実行周期の変更は、DataFlowComponentAction の
+     * on_rate_changed によって各RTコンポーネントに伝達される。
+     *
+     * @param rate 処理周期(単位:Hz)
+     *
+     * @return ReturnCode_t 型のリターンコード
+     *
+     * @else
+     *
+     * @brief Set execution rate(Hz) of ExecutionContext
+     *
+     * This operation shall set the rate (in hertz) at which this
+     * context’s Active participating RTCs are being called.  If the
+     * execution kind of the context is PERIODIC, a rate change shall
+     * result in the invocation of on_rate_changed on any RTCs
+     * realizing DataFlowComponentAction that are registered with any
+     * RTCs participating in the context.
+     *
+     * @param rate Execution cycle(Unit:Hz)
+     *
+     * @return The return code of ReturnCode_t type
+     *
+     * @endif
+     */
+    virtual RTC::ReturnCode_t  set_rate(CORBA::Double rate)
+      throw (CORBA::SystemException);
+
+    /*!
+     * @if jp
+     * @brief RTコンポーネントをアクティブ化する
+     *
+     * Inactive 状態にあるRTコンポーネントをActive に遷移させ、アクティ
+     * ブ化する。この操作が呼ばれた結果、on_activate が呼び出される。指
+     * 定したRTコンポーネントが参加者リストに含まれない場合は、
+     * BAD_PARAMETER が返される。指定したRTコンポーネントの状態が
+     * Inactive 以外の場合は、PRECONDITION_NOT_MET が返される。
+     *
+     * @param comp アクティブ化対象RTコンポーネント
+     *
+     * @return ReturnCode_t 型のリターンコード
+     *
+     * @else
+     *
+     * @brief Activate an RT-component
+     *
+     * The given participant RTC is Inactive and is therefore not
+     * being invoked according to the execution context’s execution
+     * kind. This operation shall cause the RTC to transition to the
+     * Active state such that it may subsequently be invoked in this
+     * execution context.  The callback on_activate shall be called as
+     * a result of calling this operation. This operation shall not
+     * return until the callback has returned, and shall result in an
+     * error if the callback does.
+     *
+     * @param comp The target RT-Component for activation
+     *
+     * @return The return code of ReturnCode_t type
+     *
+     * @endif
+     */
+    virtual RTC::ReturnCode_t
+    activate_component(RTC::LightweightRTObject_ptr comp)
+      throw (CORBA::SystemException);
+    
+    /*!
+     * @if jp
+     * @brief RTコンポーネントを非アクティブ化する
+     *
+     * Inactive 状態にあるRTコンポーネントを非アクティブ化し、Inactive
+     * に遷移させる。この操作が呼ばれた結果、on_deactivate が呼び出され
+     * る。指定したRTコンポーネントが参加者リストに含まれない場合は、
+     * BAD_PARAMETER が返される。指定したRTコンポーネントの状態が
+     * Active 以外の場合は、PRECONDITION_NOT_MET が返される。
+     *
+     * @param comp 非アクティブ化対象RTコンポーネント
+     *
+     * @return ReturnCode_t 型のリターンコード
+     *
+     * @else
+     *
+     * @brief Deactivate an RT-component
+     *
+     * The given RTC is Active in the execution context. Cause it to
+     * transition to the Inactive state such that it will not be
+     * subsequently invoked from the context unless and until it is
+     * activated again.  The callback on_deactivate shall be called as
+     * a result of calling this operation. This operation shall not
+     * return until the callback has returned, and shall result in an
+     * error if the callback does.
+     *
+     * @param comp The target RT-Component for deactivate
+     *
+     * @return The return code of ReturnCode_t type
+     *
+     * @endif
+     */
+    virtual RTC::ReturnCode_t
+    deactivate_component(RTC::LightweightRTObject_ptr comp)
+      throw (CORBA::SystemException);
+
+    /*!
+     * @if jp
+     * @brief RTコンポーネントをリセットする
+     *
+     * Error 状態のRTコンポーネントの復帰を試みる。この操作が呼ばれた結
+     * 果、on_reset が呼び出される。指定したRTコンポーネントが参加者リ
+     * ストに含まれない場合は、BAD_PARAMETER が返される。指定したRTコン
+     * ポーネントの状態が Error 以外の場合は、PRECONDITION_NOT_MET が返
+     * される。
+     *
+     * @param comp リセット対象RTコンポーネント
+     *
+     * @return ReturnCode_t 型のリターンコード
+     *
+     * @else
+     *
+     * @brief Reset the RT-component
+     *
+     * Attempt to recover the RTC when it is in Error.  The
+     * ComponentAction::on_reset callback shall be invoked. This
+     * operation shall not return until the callback has returned, and
+     * shall result in an error if the callback does. If possible, the
+     * RTC developer should implement that callback such that the RTC
+     * may be returned to a valid state.
+     *
+     * @param comp The target RT-Component for reset
+     *
+     * @return The return code of ReturnCode_t type
+     *
+     * @endif
+     */
+    virtual RTC::ReturnCode_t
+    reset_component(RTC::LightweightRTObject_ptr comp)
+      throw (CORBA::SystemException);
+
+    /*!
+     * @if jp
+     * @brief RTコンポーネントの状態を取得する
+     *
+     * 指定したRTコンポーネントの状態(LifeCycleState)を取得する。指定し
+     * たRTコンポーネントが参加者リストに含まれない場合は、
+     * UNKNOWN_STATE が返される。
+     *
+     * @param comp 状態取得対象RTコンポーネント
+     *
+     * @return 現在の状態(LifeCycleState)
+     *
+     * @else
+     *
+     * @brief Get RT-component's state
+     *
+     * This operation shall report the LifeCycleState of the given
+     * participant RTC.  UNKNOWN_STATE will be returned, if the given
+     * RT-Component is not inclued in the participant list.
+     *
+     * @param comp The target RT-Component to get the state
+     *
+     * @return The current state of the target RT-Component(LifeCycleState)
+     *
+     * @endif
+     */
+    virtual RTC::LifeCycleState
+    get_component_state(RTC::LightweightRTObject_ptr comp)
+      throw (CORBA::SystemException);
+
+    /*!
+     * @if jp
+     * @brief ExecutionKind を取得する
+     *
+     * 本 ExecutionContext の ExecutionKind を取得する
+     *
+     * @return ExecutionKind
+     *
+     * @else
+     *
+     * @brief Get the ExecutionKind
+     *
+     * This operation shall report the execution kind of the execution
+     * context.
+     *
+     * @return ExecutionKind
+     *
+     * @endif
+     */
+    virtual RTC::ExecutionKind get_kind(void)
+      throw (CORBA::SystemException);
+
+    /*!
+     * @if jp
+     * @brief RTコンポーネントを追加する
+     *
+     * 指定したRTコンポーネントを参加者リストに追加する。追加されたRTコ
+     * ンポーネントは attach_context が呼ばれ、Inactive 状態に遷移する。
+     * 指定されたRTコンポーネントがnullの場合は、BAD_PARAMETER が返され
+     * る。指定されたRTコンポーネントが DataFlowComponent 以外の場合は、
+     * BAD_PARAMETER が返される。
+     *
+     * @param comp 追加対象RTコンポーネント
+     *
+     * @return ReturnCode_t 型のリターンコード
+     *
+     * @else
+     *
+     * @brief Add an RT-component
+     *
+     * The operation causes the given RTC to begin participating in
+     * the execution context.  The newly added RTC will receive a call
+     * to LightweightRTComponent::attach_context and then enter the
+     * Inactive state.  BAD_PARAMETER will be invoked, if the given
+     * RT-Component is null or if the given RT-Component is other than
+     * DataFlowComponent.
+     *
+     * @param comp The target RT-Component for add
+     *
+     * @return The return code of ReturnCode_t type
+     *
+     * @endif
+     */
+    virtual RTC::ReturnCode_t add_component(RTC::LightweightRTObject_ptr comp)
+      throw (CORBA::SystemException);
+
+    /*!
+     * @if jp
+     * @brief RTコンポーネントを参加者リストから削除する
+     *
+     * 指定したRTコンポーネントを参加者リストから削除する。削除された
+     * RTコンポーネントは detach_context が呼ばれる。指定されたRTコンポー
+     * ネントが参加者リストに登録されていない場合は、BAD_PARAMETER が返
+     * される。
+     *
+     * @param comp 削除対象RTコンポーネント
+     *
+     * @return ReturnCode_t 型のリターンコード
+     *
+     * @else
+     *
+     * @brief Remove the RT-Component from participant list
+     *
+     * This operation causes a participant RTC to stop participating in the
+     * execution context.
+     * The removed RTC will receive a call to
+     * LightweightRTComponent::detach_context.
+     * BAD_PARAMETER will be returned, if the given RT-Component is not
+     * participating in the participant list.
+     *
+     * @param comp The target RT-Component for delete
+     *
+     * @return The return code of ReturnCode_t type
+     *
+     * @endif
+     */
+    virtual RTC::ReturnCode_t
+    remove_component(RTC::LightweightRTObject_ptr comp)
+      throw (CORBA::SystemException);
+
+    /*!
+     * @if jp
+     * @brief ExecutionContextProfile を取得する
+     *
+     * 本 ExecutionContext のプロファイルを取得する。
+     *
+     * @return ExecutionContextProfile
+     *
+     * @else
+     *
+     * @brief Get the ExecutionContextProfile
+     *
+     * This operation provides a profile “descriptor” for the execution
+     * context.
+     *
+     * @return ExecutionContextProfile
+     *
+     * @endif
+     */
+    virtual RTC::ExecutionContextProfile* get_profile(void)
+      throw (CORBA::SystemException);
+
   private:
+    /*!
+     * @if jp
+     * @brief ロガーストリーム
+     * @else
+     * @brief Logger stream
+     * @endif
+     */
+    RTC::Logger rtclog;
+
+    /*!
+     * @brief A counter for log message in worker
+     */
+    unsigned int m_count;
+    /*!
+     * @brief Mutex to gurad tick() reenter.
+     */
+    coil::Mutex m_tickmutex;
   };  // class OpenHRPExecutionContext
 };  // namespace RTC
 

Modified: trunk/OpenRTM-aist/src/lib/rtm/PeriodicExecutionContext.cpp
===================================================================
--- trunk/OpenRTM-aist/src/lib/rtm/PeriodicExecutionContext.cpp	2012-02-05 21:21:42 UTC (rev 2306)
+++ trunk/OpenRTM-aist/src/lib/rtm/PeriodicExecutionContext.cpp	2012-02-05 21:29:15 UTC (rev 2307)
@@ -5,9 +5,8 @@
  * @date $Date: 2008-01-14 07:53:01 $
  * @author Noriaki Ando <n-ando at aist.go.jp>
  *
- * Copyright (C) 2006-2008
+ * Copyright (C) 2006-2008,2012
  *     Noriaki Ando
- *     Task-intelligence Research Group,
  *     Intelligent Systems Research Institute,
  *     National Institute of
  *         Advanced Industrial Science and Technology (AIST), Japan
@@ -17,15 +16,17 @@
  *
  */
 
+#include <algorithm>
+#include <iostream>
+
 #include <coil/Time.h>
 #include <coil/TimeValue.h>
+
 #include <rtm/PeriodicExecutionContext.h>
-#include <rtm/RTObject.h>
-#include <algorithm>
-#include <iostream>
+#include <rtm/RTObjectStateMachine.h>
 
 #define DEEFAULT_PERIOD 0.000001
-namespace RTC
+namespace RTC_exp
 {
   /*!
    * @if jp
@@ -36,18 +37,18 @@
    */
   PeriodicExecutionContext::
   PeriodicExecutionContext()
-    : rtclog("periodic_ec"), m_running(false), m_svc(true), m_nowait(false)
+    : ExecutionContextBase("periodic_ec"),
+      rtclog("periodic_ec"),
+      m_svc(false), m_nowait(false)
   {
     RTC_TRACE(("PeriodicExecutionContext()"));
 
-
     // getting my reference
-    m_profile.setObjRef(this->_this());
+    setObjRef(this->_this());
 
     // profile initialization
-    m_profile.setKind(PERIODIC);
-    m_profile.setRate(1.0 / (double)DEEFAULT_PERIOD);
-    m_profile.setOwner(RTC::RTObject::_nil());
+    setKind(RTC::PERIODIC);
+    setRate(1.0 / (double)DEEFAULT_PERIOD);
 
     RTC_DEBUG(("Actual period: %d [sec], %d [usec]",
                m_profile.getPeriod().sec(), m_profile.getPeriod().usec()));
@@ -55,35 +56,6 @@
 
   /*!
    * @if jp
-   * @brief コンストラクタ
-   * @else
-   * @brief Construnctor
-   * @endif
-   */
-  PeriodicExecutionContext::
-  PeriodicExecutionContext(OpenRTM::DataFlowComponent_ptr owner,
-                           double rate)
-    : rtclog("periodic_ec"), m_running(false), m_svc(true), m_nowait(true)
-  {
-    RTC_TRACE(("PeriodicExecutionContext(owner, rate = %f)", rate));
-
-    if (rate == 0) { rate = 1.0 / DEEFAULT_PERIOD; }
-    m_profile.setPeriod(coil::TimeValue(1.0 / rate));
-    if (m_period < 0.000001) { m_nowait = true; }
-    RTC_DEBUG(("Actual period: %d [sec], %d [usec]",
-               m_profile.getPeriod().sec(), m_profile.getPeriod().usec()));
-
-    // getting my reference
-    m_profile.setObjRef(this->_this());
-
-    // profile initialization
-    m_profile.setKind(PERIODIC);
-    m_profile.setRate(1.0 / (double)DEEFAULT_PERIOD);
-    m_profile.setOwner(RTC::RTObject::_nil());
-  }
-  
-  /*!
-   * @if jp
    * @brief デストラクタ
    * @else
    * @brief Destructor
@@ -92,15 +64,18 @@
   PeriodicExecutionContext::~PeriodicExecutionContext()
   {
     RTC_TRACE(("~PeriodicExecutionContext()"));
-    m_worker.mutex_.lock();
-    m_worker.running_ = true;
-    m_worker.cond_.signal();
-    m_worker.mutex_.unlock();
-    m_svc = false;
+    {
+      Guard guard(m_svcmutex);
+      m_svc = false;
+    }
+    {
+      Guard guard(m_workerthread.mutex_);
+      m_workerthread.running_ = true;
+      m_workerthread.cond_.signal();
+    }
     wait();
-
   }
-  
+
   /*------------------------------------------------------------
    * Start activity
    * ACE_Task class method over ride.
@@ -118,7 +93,7 @@
     activate();
     return 0;
   }
-  
+
   /*------------------------------------------------------------
    * Run by a daemon thread to handle deferred processing
    * ACE_Task class method over ride.
@@ -136,44 +111,47 @@
     int count(0);
     do
       {
-        m_worker.mutex_.lock();
-        while (!m_worker.running_)
-          {
-            m_worker.cond_.wait();
-          }
+        ExecutionContextBase::invokeWorkerPreDo();
+        // Thread will stopped when all RTCs are INACTIVE.
+        // Therefore WorkerPreDo(updating state) have to be invoked
+        // before stopping thread.
+        {
+          Guard guard(m_workerthread.mutex_);
+          while (!m_workerthread.running_)
+            {
+              m_workerthread.cond_.wait();
+            }
+        }
         coil::TimeValue t0(coil::gettimeofday());
-        if (m_worker.running_)
-          {
-            std::for_each(m_comps.begin(), m_comps.end(), invoke_worker_pre());
-            std::for_each(m_comps.begin(), m_comps.end(), invoke_worker_do());
-            std::for_each(m_comps.begin(), m_comps.end(), invoke_worker_post());
-          }
-        m_worker.mutex_.unlock();
+        ExecutionContextBase::invokeWorkerDo();
+        ExecutionContextBase::invokeWorkerPostDo();
         coil::TimeValue t1(coil::gettimeofday());
-        if (count > 1000)
+
+        coil::TimeValue period(getPeriod());
+        if (1 /*count > 1000*/)
           {
-            RTC_PARANOID(("Period:    %f [s]", (double)m_period));
+            RTC_PARANOID(("Period:    %f [s]", (double)period));
             RTC_PARANOID(("Execution: %f [s]", (double)(t1 - t0)));
-            RTC_PARANOID(("Sleep:     %f [s]", (double)(m_period - (t1 - t0))));
+            RTC_PARANOID(("Sleep:     %f [s]", (double)(period - (t1 - t0))));
           }
         coil::TimeValue t2(coil::gettimeofday());
-        if (!m_nowait && m_period > (t1 - t0))
+        if (!m_nowait && period > (t1 - t0))
           {
             if (count > 1000) { RTC_PARANOID(("sleeping...")); }
-            coil::sleep((coil::TimeValue)(m_period - (t1 - t0)));
+            coil::sleep((coil::TimeValue)(period - (t1 - t0)));
           }
-        if (count > 1000)
+        if (1 /* count > 1000*/ )
           {
             coil::TimeValue t3(coil::gettimeofday());
             RTC_PARANOID(("Slept:     %f [s]", (double)(t3 - t2)));
             count = 0;
           }
         ++count;
-      } while (m_svc);
-
+      } while (threadRunning());
+    RTC_DEBUG(("Thread terminated."));
     return 0;
   }
-  
+
   /*!
    * @if jp
    * @brief ExecutionContext 用のスレッド実行関数
@@ -184,16 +162,14 @@
   int PeriodicExecutionContext::close(unsigned long flags)
   {
     RTC_TRACE(("close()"));
-    
     // At this point, this component have to be finished.
     // Current state and Next state should be RTC_EXITING.
-    //    delete this;
     return 0;
   }
-  
-  
+
+
   //============================================================
-  // ExecutionContext
+  // ExecutionContext CORBA operations
   //============================================================
   /*!
    * @if jp
@@ -205,10 +181,9 @@
   CORBA::Boolean PeriodicExecutionContext::is_running()
     throw (CORBA::SystemException)
   {
-    RTC_TRACE(("is_running()"));
-    return m_running;
+    return ExecutionContextBase::isRunning();
   }
-  
+
   /*!
    * @if jp
    * @brief ExecutionContext の実行を開始
@@ -216,29 +191,12 @@
    * @brief Start the ExecutionContext
    * @endif
    */
-  ReturnCode_t PeriodicExecutionContext::start()
+  RTC::ReturnCode_t PeriodicExecutionContext::start()
     throw (CORBA::SystemException)
   {
-    RTC_TRACE(("start()"));
-    if (m_running) return RTC::PRECONDITION_NOT_MET;
-    
-    // invoke ComponentAction::on_startup for each comps.
-    std::for_each(m_comps.begin(), m_comps.end(), invoke_on_startup());
-    
-    // change EC thread state
-    m_running = true;
-    {
-      m_worker.mutex_.lock();
-      m_worker.running_ = true;
-      m_worker.cond_.signal();
-      m_worker.mutex_.unlock();
-    }
-    
-    this->open(0);
-    
-    return RTC::RTC_OK;
+    return ExecutionContextBase::start();
   }
-  
+
   /*!
    * @if jp
    * @brief ExecutionContext の実行を停止
@@ -246,27 +204,14 @@
    * @brief Stop the ExecutionContext
    * @endif
    */
-  ReturnCode_t PeriodicExecutionContext::stop()
+  RTC::ReturnCode_t PeriodicExecutionContext::stop()
     throw (CORBA::SystemException)
   {
-    RTC_TRACE(("stop()"));
-    if (!m_running)
-      {
-        return RTC::PRECONDITION_NOT_MET;
-      }
-    // stop thread
-    m_running = false;
-    {
-      m_worker.mutex_.lock();
-      m_worker.running_ = false;
-      m_worker.mutex_.unlock();
-    }
-    // invoke on_shutdown for each comps.
-    std::for_each(m_comps.begin(), m_comps.end(), invoke_on_shutdown());
-
-    return RTC::RTC_OK;
+    return ExecutionContextBase::stop();
   }
 
+
+
   /*!
    * @if jp
    * @brief ExecutionContext の実行周期(Hz)を取得する
@@ -277,8 +222,7 @@
   CORBA::Double PeriodicExecutionContext::get_rate()
     throw (CORBA::SystemException)
   {
-    RTC_TRACE(("get_rate()"));
-    return m_profile.getRate();
+    return ExecutionContextBase::getRate();
   }
 
   /*!
@@ -288,64 +232,52 @@
    * @brief Set execution rate(Hz) of ExecutionContext
    * @endif
    */
-  ReturnCode_t PeriodicExecutionContext::set_rate(CORBA::Double rate)
+  RTC::ReturnCode_t PeriodicExecutionContext::set_rate(CORBA::Double rate)
     throw (CORBA::SystemException)
   {
-    RTC_TRACE(("set_rate(%f)", rate));
-    if (rate > 0.0)
-      {
-        m_profile.setRate(rate);
-        if ((double)m_profile.getRate() == 0.0) { m_nowait = true; }
-        std::for_each(m_comps.begin(), m_comps.end(), invoke_on_rate_changed());
-        RTC_DEBUG(("Actual period: %d [sec], %d [usec]",
-                   m_profile.getPeriod().sec(), m_profile.getPeriod().usec()));
-        return RTC::RTC_OK;
-      }
-    return RTC::BAD_PARAMETER;
+    return ExecutionContextBase::setRate(rate);
   }
 
   /*!
    * @if jp
-   * @brief RTコンポーネントをアクティブ化する
+   * @brief RTコンポーネントを追加する
    * @else
-   * @brief Activate an RT-Component
+   * @brief Add an RT-Component
    * @endif
    */
-  ReturnCode_t
-  PeriodicExecutionContext::activate_component(LightweightRTObject_ptr comp)
+  RTC::ReturnCode_t
+  PeriodicExecutionContext::add_component(RTC::LightweightRTObject_ptr comp)
     throw (CORBA::SystemException)
   {
-    RTC_TRACE(("activate_component()"));
-// Why RtORB does not allow STL's find_if and iterator?
-#ifndef ORB_IS_RTORB
-    CompItr it;
-    it = std::find_if(m_comps.begin(), m_comps.end(),
-                      find_comp(comp));
+    return ExecutionContextBase::addComponent(comp);
+  }
 
-    if (it == m_comps.end())
-      return RTC::BAD_PARAMETER;
+  /*!
+   * @if jp
+   * @brief コンポーネントをコンポーネントリストから削除する
+   * @else
+   * @brief Remove the RT-Component from participant list
+   * @endif
+   */
+  RTC::ReturnCode_t PeriodicExecutionContext::
+  remove_component(RTC::LightweightRTObject_ptr comp)
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::removeComponent(comp);
+  }
 
-    if (!(it->_sm.m_sm.isIn(INACTIVE_STATE)))
-        return RTC::PRECONDITION_NOT_MET;
-
-    it->_sm.m_sm.goTo(ACTIVE_STATE);
-    return RTC::RTC_OK;
-#else // ORB_IS_RTORB
-    for (int i(0); i < (int)m_comps.size() ; ++i)
-      {
-        if(m_comps.at(i)._ref->_is_equivalent(comp))
-          {
-
-            if (!(m_comps.at(i)._sm.m_sm.isIn(INACTIVE_STATE)))
-              {
-                return RTC::PRECONDITION_NOT_MET;
-              }
-            m_comps.at(i)._sm.m_sm.goTo(ACTIVE_STATE);
-            return RTC::RTC_OK;
-          }
-      }
-    return RTC::BAD_PARAMETER;
-#endif // ORB_IS_RTORB
+  /*!
+   * @if jp
+   * @brief RTコンポーネントをアクティブ化する
+   * @else
+   * @brief Activate an RT-Component
+   * @endif
+   */
+  RTC::ReturnCode_t PeriodicExecutionContext::
+  activate_component(RTC::LightweightRTObject_ptr comp)
+    throw (CORBA::SystemException)
+  {
+    return ExecutionContextBase::activateComponent(comp);
   }
 
   /*!
@@ -355,82 +287,11 @@
    * @brief Deactivate an RT-Component
    * @endif
    */
-  ReturnCode_t
-  PeriodicExecutionContext::deactivate_component(LightweightRTObject_ptr comp)
+  RTC::ReturnCode_t PeriodicExecutionContext::
+  deactivate_component(RTC::LightweightRTObject_ptr comp)
     throw (CORBA::SystemException)
   {
-    RTC_TRACE(("deactivate_component()"));
-// Why RtORB does not allow STL's find_if and iterator?
-#ifndef ORB_IS_RTORB
-    CompItr it;
-    it = std::find_if(m_comps.begin(), m_comps.end(),
-                      find_comp(comp));
-    if (it == m_comps.end()) { return RTC::BAD_PARAMETER; }
-    if (!(it->_sm.m_sm.isIn(ACTIVE_STATE)))
-      {
-        return RTC::PRECONDITION_NOT_MET;
-      }
-
-    it->_sm.m_sm.goTo(INACTIVE_STATE);
-    int count(0);
-    const double usec_per_sec(1.0e6);
-    double sleeptime(10.0 * usec_per_sec / get_rate());
-    RTC_PARANOID(("Sleep time is %f [us]", sleeptime));
-    while (it->_sm.m_sm.isIn(ACTIVE_STATE))
-      {
-        RTC_TRACE(("Waiting to be the INACTIVE state %d %f",
-                   count, (double)coil::gettimeofday()));
-        coil::usleep(sleeptime);
-        if (count > 1000)
-          {
-            RTC_ERROR(("The component is not responding."));
-            break;
-          }
-        ++count;
-      }
-    if (it->_sm.m_sm.isIn(INACTIVE_STATE))
-      {
-        RTC_TRACE(("The component has been properly deactivated."));
-        return RTC::RTC_OK;
-      }
-    RTC_ERROR(("The component could not be deactivated."));
-    return RTC::RTC_ERROR;
-#else // ORB_IS_RTORB
-    for (int i(0); i < (int)m_comps.size(); ++i)
-      {
-        if(m_comps.at(i)._ref->_is_equivalent(comp))
-          {
-            if (!(m_comps.at(i)._sm.m_sm.isIn(ACTIVE_STATE)))
-              {
-                return RTC::PRECONDITION_NOT_MET;
-              }
-            m_comps.at(i)._sm.m_sm.goTo(INACTIVE_STATE);
-            int count(0);
-            const double usec_per_sec(1.0e6);
-            double sleeptime(usec_per_sec / get_rate());
-            RTC_PARANOID(("Sleep time is %f [us]", sleeptime));
-            while (m_comps.at(i)._sm.m_sm.isIn(ACTIVE_STATE))
-              {
-                RTC_TRACE(("Waiting to be the INACTIVE state"));
-                coil::usleep(sleeptime);
-                if (count > 1000)
-                  {
-                    RTC_ERROR(("The component is not responding."));
-                    break;
-                  }
-                ++count;
-              }
-            if (m_comps.at(i)._sm.m_sm.isIn(INACTIVE_STATE))
-              {
-                RTC_TRACE(("The component has been properly deactivated."));
-                return RTC::RTC_OK;
-              }
-            RTC_ERROR(("The component could not be deactivated."));
-            return RTC::RTC_ERROR;
-          }
-      }
-    return RTC::BAD_PARAMETER;
-#endif // ORB_IS_RTORB
+    return ExecutionContextBase::deactivateComponent(comp);
   }
 
   /*!
@@ -440,24 +301,11 @@
    * @brief Reset the RT-Component
    * @endif
    */
-  ReturnCode_t
-  PeriodicExecutionContext::reset_component(LightweightRTObject_ptr comp)
+  RTC::ReturnCode_t PeriodicExecutionContext::
+  reset_component(RTC::LightweightRTObject_ptr comp)
     throw (CORBA::SystemException)
   {
-    RTC_TRACE(("reset_component()"));
-    CompItr it;
-    it = std::find_if(m_comps.begin(), m_comps.end(),
-                      find_comp(comp));
-    if (it == m_comps.end())
-      {
-        return RTC::BAD_PARAMETER;
-      }
-    if (!(it->_sm.m_sm.isIn(ERROR_STATE)))
-      {
-        return RTC::PRECONDITION_NOT_MET;
-      }
-    it->_sm.m_sm.goTo(INACTIVE_STATE);
-    return RTC::RTC_OK;
+    return ExecutionContextBase::resetComponent(comp);
   }
 
   /*!
@@ -467,31 +315,12 @@
    * @brief Get RT-Component's state
    * @endif
    */
-  LifeCycleState
-  PeriodicExecutionContext::get_component_state(LightweightRTObject_ptr comp)
+  RTC::LifeCycleState PeriodicExecutionContext::
+  get_component_state(RTC::LightweightRTObject_ptr comp)
     throw (CORBA::SystemException)
   {
-    RTC_TRACE(("get_component_state()"));
-#ifndef ORB_IS_RTORB
-    CompItr it;
-    it = std::find_if(m_comps.begin(), m_comps.end(), find_comp(comp));
-
-    if (it == m_comps.end())
-      {
-        return RTC::CREATED_STATE;
-      }
-
-    return it->_sm.m_sm.getState();
-#else // ORB_IS_RTORB
-    for (int i(0); i < (int)m_comps.size(); ++i)
-      {
-        if(m_comps.at(i)._ref->_is_equivalent(comp))
-          {
-            return m_comps.at(i)._sm.m_sm.getState();
-          }
-      }
-    return RTC::CREATED_STATE;
-#endif // ORB_IS_RTORB
+    RTC::LifeCycleState ret = ExecutionContextBase::getComponentState(comp);
+    return ret;
   }
 
   /*!
@@ -501,126 +330,206 @@
    * @brief Get the ExecutionKind
    * @endif
    */
-  ExecutionKind PeriodicExecutionContext::get_kind()
+  RTC::ExecutionKind PeriodicExecutionContext::get_kind()
     throw (CORBA::SystemException)
   {
-    RTC_TRACE(("get_kind() = %s",
-               m_profile.getKindString()));
-    return getKind();
+    return ExecutionContextBase::getKind();
   }
 
+  //------------------------------------------------------------
+  // ExecutionContextService interfaces
+  //------------------------------------------------------------
   /*!
    * @if jp
-   * @brief RTコンポーネントを追加する
+   * @brief ExecutionContextProfile を取得する
    * @else
-   * @brief Add an RT-Component
+   * @brief Get the ExecutionContextProfile
    * @endif
    */
-  ReturnCode_t
-  PeriodicExecutionContext::add_component(LightweightRTObject_ptr comp)
+  RTC::ExecutionContextProfile* PeriodicExecutionContext::get_profile()
     throw (CORBA::SystemException)
   {
-    RTC_TRACE(("add_component()"));
-    if (CORBA::is_nil(comp)) return RTC::BAD_PARAMETER;
+    return ExecutionContextBase::getProfile();
+  }
 
-    try
+
+  //============================================================
+  // protected functions
+  //============================================================
+  /*!
+   * @brief onStarted() template function
+   */
+  RTC::ReturnCode_t PeriodicExecutionContext::onStarted()
+  {
+    // change EC thread state
+    {
+      Guard guard(m_svcmutex);
+      if (!m_svc)
+        {
+          m_svc = true;
+          this->open(0);
+        }
+    }
+    if (isAllNextState(RTC::INACTIVE_STATE))
       {
-        OpenRTM::DataFlowComponent_var dfp;
-        dfp = OpenRTM::DataFlowComponent::_narrow(comp);
-        RTC::RTObject_var rtc;
-        rtc = RTC::RTObject::_narrow(comp);
-        //Check the pointer.
-        if(CORBA::is_nil(dfp) || CORBA::is_nil(rtc))
-          {
-            return RTC::BAD_PARAMETER;
-          }
-        ExecutionContextService_var ec = m_profile.getObjRef();
-        ExecutionContextHandle_t id;
-        id = dfp->attach_context(ec);
-        m_comps.push_back(Comp(comp, dfp, id));
-        m_profile.addComponent(rtc._retn());
-        return RTC::RTC_OK;
+        Guard guard(m_workerthread.mutex_);
+        m_workerthread.running_ = false;
       }
-    catch (CORBA::Exception& e)
+    else
       {
-        (void)(e);
-        return RTC::BAD_PARAMETER;
+        Guard guard(m_workerthread.mutex_);
+        m_workerthread.running_ = true;
+        m_workerthread.cond_.signal();
       }
     return RTC::RTC_OK;
   }
 
-  RTC::ReturnCode_t PeriodicExecutionContext::bindComponent(RTObject_impl* rtc)
+  /*!
+   * @brief onStopping() template function
+   */
+  RTC::ReturnCode_t PeriodicExecutionContext::onStopping()
   {
-    RTC_TRACE(("bindComponent()"));
-    if (rtc == NULL) return RTC::BAD_PARAMETER;
+    // stop thread
+    Guard guard(m_workerthread.mutex_);
+    m_workerthread.running_ = false;
+    return RTC::RTC_OK;
+  }
 
-    LightweightRTObject_var comp = RTC::RTObject::_duplicate(rtc->getObjRef());
-    OpenRTM::DataFlowComponent_var dfp;
-    dfp = OpenRTM::DataFlowComponent::_narrow(comp);
-    ExecutionContextService_var ec = m_profile.getObjRef();
-    ExecutionContextHandle_t id = rtc->bindContext(ec);
-    if (id < 0 || id > ECOTHER_OFFSET) 
+  /*!
+   * @brief onWaitingActivated() template function
+   */
+  RTC::ReturnCode_t PeriodicExecutionContext::
+  onWaitingActivated(RTC_impl::RTObjectStateMachine* comp, long int count)
+  {
+    RTC_TRACE(("onWaitingActivated(count = %d)", count));
+    RTC_PARANOID(("curr: %s, next: %s",
+                  getStateString(comp->getStates().curr),
+                  getStateString(comp->getStates().next)));
+    // Now comp's next state must be ACTIVE state
+    // If worker thread is stopped, restart worker thread.
+    Guard guard(m_workerthread.mutex_);
+    if (m_workerthread.running_ == false)
       {
-        // id should be owned context id < ECOTHER_OFFSET
-        RTC_ERROR(("bindContext returns invalid id: %d", id));
-        return RTC::RTC_ERROR;
+        m_workerthread.running_ = true;
+        m_workerthread.cond_.signal();
       }
-    RTC_DEBUG(("bindContext returns id = %d", id));
+    return RTC::RTC_OK;
+  }
 
-    // rtc is owner of this EC
-    m_comps.push_back(Comp(comp,dfp,id));
-    m_profile.setOwner(RTC::RTObject::_duplicate(dfp));
+  /*!
+   * @brief onActivated() template function
+   */
+  RTC::ReturnCode_t PeriodicExecutionContext::
+  onActivated(RTC_impl::RTObjectStateMachine* comp, long int count)
+  {
+    RTC_TRACE(("onActivated(count = %d)", count));
+    RTC_PARANOID(("curr: %s, next: %s",
+                  getStateString(comp->getStates().curr),
+                  getStateString(comp->getStates().next)));
+    // count = -1; Asynch mode. Since onWaitingActivated is not
+    // called, onActivated() have to send restart singnal to worker
+    // thread.
+    // count > 0: Synch mode.
+
+    // Now comp's next state must be ACTIVE state
+    // If worker thread is stopped, restart worker thread.
+    Guard guard(m_workerthread.mutex_);
+    if (m_workerthread.running_ == false)
+      {
+        m_workerthread.running_ = true;
+        m_workerthread.cond_.signal();
+      }
     return RTC::RTC_OK;
   }
 
   /*!
-   * @if jp
-   * @brief コンポーネントをコンポーネントリストから削除する
-   * @else
-   * @brief Remove the RT-Component from participant list
-   * @endif
+   * @brief onWaitingDeactivated() template function
    */
-  ReturnCode_t
-  PeriodicExecutionContext::remove_component(LightweightRTObject_ptr comp)
-    throw (CORBA::SystemException)
+  RTC::ReturnCode_t PeriodicExecutionContext::
+  onWaitingDeactivated(RTC_impl::RTObjectStateMachine* comp, long int count)
   {
-    RTC_TRACE(("remove_component()"));
-    CompItr it;
-    it = std::find_if(m_comps.begin(), m_comps.end(),
-                      find_comp(comp));
-    if (it == m_comps.end())
+    RTC_TRACE(("onWaitingDeactivated(count = %d)", count));
+    RTC_PARANOID(("curr: %s, next: %s",
+                  getStateString(comp->getStates().curr),
+                  getStateString(comp->getStates().next)));
+    if (isAllNextState(RTC::INACTIVE_STATE))
       {
-        RTC_TRACE(("remove_component(): no RTC found in this context."));
-        return RTC::BAD_PARAMETER;
+        Guard guard(m_workerthread.mutex_);
+        if (m_workerthread.running_ == true)
+          {
+            m_workerthread.running_ = false;
+            RTC_TRACE(("All RTCs are INACTIVE. Stopping worker thread."));
+          }
       }
+    return RTC::RTC_OK;
+  }
 
-    Comp& c(*it);
-    c._ref->detach_context(c._sm.ec_id);
-    c._ref = RTC::LightweightRTObject::_nil();
-    m_comps.erase(it);
-    RTC_TRACE(("remove_component(): an RTC removed from this context."));
+  /*!
+   * @brief onDeactivated() template function
+   */
+  RTC::ReturnCode_t PeriodicExecutionContext::
+  onDeactivated(RTC_impl::RTObjectStateMachine* comp, long int count)
+  {
+    RTC_TRACE(("onDeactivated(count = %d)", count));
+    RTC_PARANOID(("curr: %s, next: %s",
+                  getStateString(comp->getStates().curr),
+                  getStateString(comp->getStates().next)));
+    if (isAllNextState(RTC::INACTIVE_STATE))
+      {
+        Guard guard(m_workerthread.mutex_);
+        if (m_workerthread.running_ == true)
+          {
+            m_workerthread.running_ = false;
+            RTC_TRACE(("All RTCs are INACTIVE. Stopping worker thread."));
+          }
+      }
+    return RTC::RTC_OK;
+  }
 
-    m_profile.removeComponent(comp);
-
+  /*!
+   * @brief onWaitingReset() template function
+   */
+  RTC::ReturnCode_t PeriodicExecutionContext::
+  onWaitingReset(RTC_impl::RTObjectStateMachine* comp, long int count)
+  {
+    RTC_TRACE(("onWaitingReset(count = %d)", count));
+    RTC_PARANOID(("curr: %s, next: %s",
+                  getStateString(comp->getStates().curr),
+                  getStateString(comp->getStates().next)));
+    if (isAllNextState(RTC::INACTIVE_STATE))
+      {
+        Guard guard(m_workerthread.mutex_);
+        if (m_workerthread.running_ == true)
+          {
+            m_workerthread.running_ = false;
+            RTC_TRACE(("All RTCs are INACTIVE. Stopping worker thread."));
+          }
+      }
     return RTC::RTC_OK;
   }
-  
-  //============================================================
-  // ExecutionContextService interfaces
-  //============================================================
+
   /*!
-   * @if jp
-   * @brief ExecutionContextProfile を取得する
-   * @else
-   * @brief Get the ExecutionContextProfile
-   * @endif
+   * @brief onReset() template function
    */
-  ExecutionContextProfile* PeriodicExecutionContext::get_profile()
-    throw (CORBA::SystemException)
+  RTC::ReturnCode_t PeriodicExecutionContext::
+  onReset(RTC_impl::RTObjectStateMachine* comp, long int count)
   {
-    RTC_TRACE(("get_profile()"));
-    return m_profile.getProfile();
+    RTC_TRACE(("onReset(count = %d)", count));
+    RTC_PARANOID(("curr: %s, next: %s",
+                  getStateString(comp->getStates().curr),
+                  getStateString(comp->getStates().next)));
+    if (isAllNextState(RTC::INACTIVE_STATE))
+      {
+        Guard guard(m_workerthread.mutex_);
+        if (m_workerthread.running_ == true)
+          {
+            m_workerthread.running_ = false;
+            RTC_TRACE(("All RTCs are INACTIVE. Stopping worker thread."));
+          }
+      }
+    return RTC::RTC_OK;
   }
+
 }; // namespace RTC  
 
 extern "C"
@@ -632,14 +541,18 @@
    * @brief Initialization function to register to ECFactory
    * @endif
    */
-  void PeriodicExecutionContextInit(RTC::Manager* manager)
+
+ void PeriodicExecutionContextInit(RTC::Manager* manager)
   {
     RTC::ExecutionContextFactory::
       instance().addFactory("PeriodicExecutionContext",
                             ::coil::Creator< ::RTC::ExecutionContextBase,
-                            ::RTC::PeriodicExecutionContext>,
+                            ::RTC_exp::PeriodicExecutionContext>,
                             ::coil::Destructor< ::RTC::ExecutionContextBase,
-                            ::RTC::PeriodicExecutionContext>);
+                            ::RTC_exp::PeriodicExecutionContext>);
+
+    coil::vstring ecs;
+    ecs = RTC::ExecutionContextFactory::instance().getIdentifiers();
   }
 };
 

Modified: trunk/OpenRTM-aist/src/lib/rtm/PeriodicExecutionContext.h
===================================================================
--- trunk/OpenRTM-aist/src/lib/rtm/PeriodicExecutionContext.h	2012-02-05 21:21:42 UTC (rev 2306)
+++ trunk/OpenRTM-aist/src/lib/rtm/PeriodicExecutionContext.h	2012-02-05 21:29:15 UTC (rev 2307)
@@ -5,9 +5,8 @@
  * @date $Date: 2008-01-14 07:53:05 $
  * @author Noriaki Ando <n-ando at aist.go.jp>
  *
- * Copyright (C) 2006-2008
+ * Copyright (C) 2006-2008,2012
  *     Noriaki Ando
- *     Task-intelligence Research Group,
  *     Intelligent Systems Research Institute,
  *     National Institute of
  *         Advanced Industrial Science and Technology (AIST), Japan
@@ -17,20 +16,16 @@
  *
  */
 
-#ifndef RTC_PERIODICEXECUTIONCONTEXT_H
-#define RTC_PERIODICEXECUTIONCONTEXT_H
+#ifndef RTC_PERIODICEXECUTIONCONTEXT2_H
+#define RTC_PERIODICEXECUTIONCONTEXT2_H
 
+#include <vector>
+#include <iostream>
+
 #include <coil/Task.h>
 #include <coil/Mutex.h>
 #include <coil/Condition.h>
-#include <vector>
-#include <iostream>
 
-#include <rtm/RTC.h>
-#include <rtm/idl/RTCSkel.h>
-#include <rtm/idl/OpenRTMSkel.h>
-#include <rtm/Manager.h>
-#include <rtm/StateMachine.h>
 #include <rtm/ExecutionContextBase.h>
 
 #define NUM_OF_LIFECYCLESTATE 4
@@ -39,7 +34,7 @@
 #pragma warning( disable : 4290 )
 #endif
 
-namespace RTC
+namespace RTC_exp
 {
   /*!
    * @if jp
@@ -62,16 +57,13 @@
    * @endif
    */
   class PeriodicExecutionContext
-    : public virtual POA_OpenRTM::ExtTrigExecutionContextService,
+    : public virtual POA_RTC::ExecutionContextService,
       public virtual PortableServer::RefCountServantBase,
-      public virtual ExecutionContextBase,
+      public RTC::ExecutionContextBase,
       public coil::Task
   {
     typedef coil::Guard<coil::Mutex> Guard;
   public:
-    virtual void tick()
-      throw (CORBA::SystemException)
-    {};
     /*!
      * @if jp
      * @brief デフォルトコンストラクタ
@@ -92,33 +84,9 @@
      * @endif
      */
     PeriodicExecutionContext();
-    
+
     /*!
      * @if jp
-     * @brief コンストラクタ
-     *
-     * コンストラクタ
-     * 設定された値をプロファイルに設定する。
-     *
-     * @param owner 当該 Executioncontext の owner
-     * @param rate 動作周期(Hz)(デフォルト値:1000)
-     *
-     * @else
-     * @brief Constructor
-     *
-     * Constructor
-     * Set the configuration value to profile.
-     *
-     * @param owner The owner of this Executioncontext
-     * @param rate Execution cycle(Hz)(The default value:1000)
-     *
-     * @endif
-     */
-    PeriodicExecutionContext(OpenRTM::DataFlowComponent_ptr owner,
-			     double rate = 1000.0);
-    
-    /*!
-     * @if jp
      * @brief デストラクタ
      *
      * デストラクタ
@@ -157,7 +125,7 @@
      * @endif
      */
     virtual int open(void *args);
-    
+
     /*!
      * @if jp
      * @brief ExecutionContext 用のスレッド実行関数
@@ -178,7 +146,7 @@
      * @endif
      */
     virtual int svc(void);
-    
+
     /*!
      * @if jp
      * @brief ExecutionContext 用のスレッド実行関数
@@ -205,9 +173,9 @@
      * @return The close result
      *
      * @endif
-     */     
+     */
     virtual int close(unsigned long flags);
-    
+
     //============================================================
     // ExecutionContext
     //============================================================
@@ -237,7 +205,7 @@
      */
     virtual CORBA::Boolean is_running(void)
       throw (CORBA::SystemException);
-    
+
     /*!
      * @if jp
      * @brief ExecutionContext の実行を開始
@@ -265,9 +233,9 @@
      *
      * @endif
      */
-    virtual ReturnCode_t start(void)
+    virtual RTC::ReturnCode_t start(void)
       throw (CORBA::SystemException);
-    
+
     /*!
      * @if jp
      * @brief ExecutionContext の実行を停止
@@ -294,9 +262,9 @@
      *
      * @endif
      */
-    virtual ReturnCode_t stop(void)
+    virtual RTC::ReturnCode_t stop(void)
       throw (CORBA::SystemException);
-    
+
     /*!
      * @if jp
      * @brief ExecutionContext の実行周期(Hz)を取得する
@@ -319,7 +287,7 @@
      */
     virtual CORBA::Double get_rate(void)
       throw (CORBA::SystemException);
-    
+
     /*!
      * @if jp
      * @brief ExecutionContext の実行周期(Hz)を設定する
@@ -349,9 +317,9 @@
      *
      * @endif
      */
-    virtual ReturnCode_t  set_rate(CORBA::Double rate)
+    virtual RTC::ReturnCode_t  set_rate(CORBA::Double rate)
       throw (CORBA::SystemException);
-    
+
     /*!
      * @if jp
      * @brief RTコンポーネントをアクティブ化する
@@ -385,7 +353,8 @@
      *
      * @endif
      */
-    virtual ReturnCode_t activate_component(LightweightRTObject_ptr comp)
+    virtual RTC::ReturnCode_t
+    activate_component(RTC::LightweightRTObject_ptr comp)
       throw (CORBA::SystemException);
     
     /*!
@@ -420,9 +389,10 @@
      *
      * @endif
      */
-    virtual ReturnCode_t deactivate_component(LightweightRTObject_ptr comp)
+    virtual RTC::ReturnCode_t
+    deactivate_component(RTC::LightweightRTObject_ptr comp)
       throw (CORBA::SystemException);
-    
+
     /*!
      * @if jp
      * @brief RTコンポーネントをリセットする
@@ -454,9 +424,10 @@
      *
      * @endif
      */
-    virtual ReturnCode_t reset_component(LightweightRTObject_ptr comp)
+    virtual RTC::ReturnCode_t
+    reset_component(RTC::LightweightRTObject_ptr comp)
       throw (CORBA::SystemException);
-    
+
     /*!
      * @if jp
      * @brief RTコンポーネントの状態を取得する
@@ -483,9 +454,10 @@
      *
      * @endif
      */
-    virtual LifeCycleState get_component_state(LightweightRTObject_ptr comp)
+    virtual RTC::LifeCycleState
+    get_component_state(RTC::LightweightRTObject_ptr comp)
       throw (CORBA::SystemException);
-    
+
     /*!
      * @if jp
      * @brief ExecutionKind を取得する
@@ -505,9 +477,9 @@
      *
      * @endif
      */
-    virtual ExecutionKind get_kind(void)
+    virtual RTC::ExecutionKind get_kind(void)
       throw (CORBA::SystemException);
-    
+
     /*!
      * @if jp
      * @brief RTコンポーネントを追加する
@@ -539,30 +511,11 @@
      *
      * @endif
      */
-    virtual ReturnCode_t add_component(LightweightRTObject_ptr comp)
+    virtual RTC::ReturnCode_t add_component(RTC::LightweightRTObject_ptr comp)
       throw (CORBA::SystemException);
 
     /*!
      * @if jp
-     * @brief コンポーネントをバインドする。
-     *
-     * コンポーネントをバインドする。
-     *
-     * @param rtc RTコンポーネント
-     * @return ReturnCode_t 型のリターンコード
-     * @else
-     * @brief Bind the component.
-     *
-     * Bind the component.
-     *
-     * @param rtc RT-Component's instances
-     * @return The return code of ReturnCode_t type
-     * @endif
-     */
-    virtual RTC::ReturnCode_t bindComponent(RTObject_impl* rtc);
-    
-    /*!
-     * @if jp
      * @brief RTコンポーネントを参加者リストから削除する
      *
      * 指定したRTコンポーネントを参加者リストから削除する。削除された
@@ -582,7 +535,7 @@
      * execution context.
      * The removed RTC will receive a call to
      * LightweightRTComponent::detach_context.
-     * BAD_PARAMETER will be returned, if the given RT-Component is not 
+     * BAD_PARAMETER will be returned, if the given RT-Component is not
      * participating in the participant list.
      *
      * @param comp The target RT-Component for delete
@@ -591,9 +544,10 @@
      *
      * @endif
      */
-    virtual ReturnCode_t remove_component(LightweightRTObject_ptr comp)
+    virtual RTC::ReturnCode_t
+    remove_component(RTC::LightweightRTObject_ptr comp)
       throw (CORBA::SystemException);
-    
+
     /*!
      * @if jp
      * @brief ExecutionContextProfile を取得する
@@ -606,915 +560,81 @@
      *
      * @brief Get the ExecutionContextProfile
      *
-     * This operation provides a profile “descriptor” for the execution 
+     * This operation provides a profile “descriptor” for the execution
      * context.
      *
      * @return ExecutionContextProfile
      *
      * @endif
      */
-    virtual ExecutionContextProfile* get_profile(void)
+    virtual RTC::ExecutionContextProfile* get_profile(void)
       throw (CORBA::SystemException);
-    
+
   protected:
-    //============================================================
-    // DFPBase
-    //============================================================
-    typedef LifeCycleState ExecContextState;
-    /*
-      enum ExecContextState
-      {
-        INACTIVE_STATE,
-        ACTIVE_STATE,
-        ERROR_STATE,
-      };
-    */
-    typedef RTC_Utils::StateHolder<ExecContextState> ECStates;
-    
     /*!
-     * @if jp
-     * @class DFPBase
-     * @brief DFPBase クラス
-     *
-     * 参加者リストに登録された DataFlowParticipant を管理するための抽象クラス。
-     *
-     * @since 0.4.0
-     *
-     * @else
-     * @class DFPBase
-     * @brief DFPBase class
-     *
-     * The abstract class to manage DataFlowParticipant registered in 
-     * tha participant list.
-     *
-     * @since 0.4.0
-     *
-     * @endif
+     * @brief onStarted() template function
      */
-    class DFPBase
-    {
-    public:
-      
-      /*!
-       * @if jp
-       * @brief コンストラクタ
-       *
-       * コンストラクタ
-       *
-       * @param id 所属する ExecutionContext のID
-       *
-       * @else
-       * @brief Constructor
-       *
-       * Constructor
-       *
-       * @param id ID of participating ExecutionContext
-       *
-       * @endif
-       */
-      DFPBase(RTC::ExecutionContextHandle_t id)
-	: ec_id(id), m_sm(NUM_OF_LIFECYCLESTATE)
-      {
-	m_sm.setListener(this);
-	m_sm.setEntryAction (ACTIVE_STATE, &DFPBase::on_activated);
-	m_sm.setDoAction    (ACTIVE_STATE, &DFPBase::on_execute);
-	m_sm.setPostDoAction(ACTIVE_STATE, &DFPBase::on_state_update);
-	m_sm.setExitAction  (ACTIVE_STATE, &DFPBase::on_deactivated);
-	m_sm.setEntryAction (ERROR_STATE,  &DFPBase::on_aborting);
-	m_sm.setDoAction    (ERROR_STATE,  &DFPBase::on_error);
-	m_sm.setExitAction  (ERROR_STATE,  &DFPBase::on_reset);
-	
-	ECStates st;
-	st.prev = INACTIVE_STATE;
-	st.curr = INACTIVE_STATE;
-	st.next = INACTIVE_STATE;
-	m_sm.setStartState(st);
-	m_sm.goTo(INACTIVE_STATE);
-      }	
-      
-      /*!
-       * @if jp
-       * @brief デストラクタ
-       *
-       * デストラクタ
-       *
-       * @else
-       * @brief Destructor
-       *
-       * Destructor
-       *
-       * @endif
-       */
-      virtual ~DFPBase(void){}
-      
-      /*!
-       * @if jp
-       * @brief ExecutionContext 実行開始時に呼ばれる純粋仮想関数
-       *
-       * 参加している ExecutionContext が実行を開始する時(Running状態へ遷移時)
-       * に呼ばれる純粋仮想関数。
-       *
-       * @else
-       *
-       * @brief Pure virtual function to be invoked when ExecutionContext starts
-       *
-       * Pure virtual function to be invoked when given execution context, in
-       * which the RTC is participating, has transited from Stopped to Running.
-       *
-       * @endif
-       */
-      virtual void on_startup(void) = 0;
-      
-      /*!
-       * @if jp
-       * @brief ExecutionContext 停止時に呼ばれる純粋仮想関数
-       *
-       * 参加している ExecutionContext が実行を停止する時(Stopped状態へ遷移時)
-       * に呼ばれる純粋仮想関数。
-       *
-       * @else
-       *
-       * @brief Pure virtual function to be invoked when ExecutionContext stops
-       *
-       * Pure virtual function to be invoked when given execution context, in
-       * which the RTC is participating, has transited from Running to Stopped.
-       *
-       * @endif
-       */
-      virtual void on_shutdown(void) = 0;
-      
-      /*!
-       * @if jp
-       * @brief RTコンポーネントがアクティブ化された時に呼ばれる純粋仮想関数
-       *
-       * 管理対象のRTコンポーネントがアクティブ化された時
-       * (Active状態へ遷移時)に呼ばれる純粋仮想関数。
-       *
-       * @param st 対象RTコンポーネントの現在の状態
-       *
-       * @else
-       *
-       * @brief Pure virtual function to be invoked when RT-Component is 
-       *        activated
-       *
-       * Pure virtual function to be invoked when the RTC has been activated
-       * in the given execution context.
-       *
-       * @param st The current state of the target RT-Component
-       *
-       * @endif
-       */
-      virtual void on_activated(const ECStates& st) = 0;
-      
-      /*!
-       * @if jp
-       * @brief RTコンポーネントが非アクティブ化された時に呼ばれる純粋仮想関数
-       *
-       * 管理対象のRTコンポーネントが非アクティブ化された時
-       * (Deactive状態へ遷移時)に呼ばれる純粋仮想関数。
-       *
-       * @param st 対象RTコンポーネントの現在の状態
-       *
-       * @else
-       *
-       * @brief Pure virtual function to be invoked when RT-Component is 
-       *        deactivated
-       *
-       * Pure virtual function to be invoked when the RTC has been deactivated
-       * in the given execution context.
-       *
-       * @param st The current state of the target RT-Component
-       *
-       * @endif
-       */
-      virtual void on_deactivated(const ECStates& st) = 0;
-      
-      /*!
-       * @if jp
-       * @brief RTコンポーネントでエラーが発生した時に呼ばれる純粋仮想関数
-       *
-       * 管理対象のRTコンポーネントにエラーが発生した時(Error状態へ遷移時)
-       * に呼ばれる純粋仮想関数。
-       *
-       * @param st 対象RTコンポーネントの現在の状態
-       *
-       * @else
-       *
-       * @brief Pure virtual function to be invoked when RT-Component occurs 
-       *        error
-       *
-       * Pure virtual function to be invoked when the RTC is transiting from
-       * the Active state to the Error state in some execution context.
-       *
-       * @param st The current state of the target RT-Component
-       *
-       * @endif
-       */
-      virtual void on_aborting(const ECStates& st) = 0;
-      
-      /*!
-       * @if jp
-       * @brief RTコンポーネントがエラー状態の時に呼ばれる純粋仮想関数
-       *
-       * 管理対象のRTコンポーネントがエラー状態にいる間、on_execute と
-       * on_state_update に替わって定期的に呼び出される純粋仮想関数。
-       *
-       * @param st 対象RTコンポーネントの現在の状態
-       *
-       * @else
-       *
-       * @brief Pure virtual function to be invoked while RT-Component
-       * is in the error state
-       *
-       * If the RTC is in the Error state relative to some execution context
-       * when it would otherwise be invoked from that context.
-       * This operation shall be invoked in sorted order at the rate of the
-       * context instead of DataFlowComponentAction::on_execute and 
-       * on_state_update.The RTC is transitioning from the Active state to 
-       * the Error state in some execution context.
-       *
-       * @param st The current state of the target RT-Component
-       *
-       * @endif
-       */
-      virtual void on_error(const ECStates& st) = 0;
-      
-      /*!
-       * @if jp
-       * @brief RTコンポーネントをリセットする時に呼ばれる純粋仮想関数
-       *
-       * 管理対象のRTコンポーネントをリセットする際に呼ばれる純粋仮想関数。
-       * この関数が正常に終了すると,RTCは Inactive 状態に復帰する。
-       * この関数が正常に終了しなかった場合は, Error 状態に留まる。
-       *
-       * @param st 対象RTコンポーネントの現在の状態
-       *
-       * @else
-       *
-       * @brief Pure virtual function to be invoked when RT-Component resets.
-       *
-       * The RTC is in the Error state. An attempt is being made to recover it 
-       * such that it can return to the Inactive state.
-       * If the RTC was successfully recovered and can safely return to 
-       * the Inactive state, this method shall complete with ReturnCode_t::OK.
-       * Any other result shall indicate that the RTC should remain in the 
-       * Error state.
-       *
-       * @param st The current state of the target RT-Component
-       *
-       * @endif
-       */
-      virtual void on_reset(const ECStates& st) = 0;
-      
-      /*!
-       * @if jp
-       * @brief RTコンポーネント実行時に定期的に呼ばれる純粋仮想関数
-       *
-       * 管理対象のRTコンポーネントが Active 状態であるとともに、
-       * ExecutionContext が Running 状態の場合に、設定された動作周期で定期的に
-       * 呼び出される純粋仮想関数。
-       * Two-Pass Execution の最初の実行で呼ばれる。
-       *
-       * @param st 対象RTコンポーネントの現在の状態
-       *
-       * @else
-       *
-       * @brief Pure virtual function to be periodically invoked while 
-       *        RT-Component is running
-       *
-       * This operation will be invoked periodically at the rate of the given
-       * execution context as long as the following conditions hold:
-       *  - The RTC is Active.
-       *  - The given execution context is Running.
-       * This callback occurs during the first execution pass.
-       *
-       * @param st The current state of the target RT-Component
-       *
-       * @endif
-       */
-      virtual void on_execute(const ECStates& st) = 0;
-      
-      /*!
-       * @if jp
-       * @brief RTコンポーネント実行時に定期的に呼ばれる純粋仮想関数
-       *
-       * 管理対象のRTコンポーネントが Active 状態であるとともに、
-       * ExecutionContext が Running 状態の場合に、設定された動作周期で定期的に
-       * 呼び出される純粋仮想関数。
-       * Two-Pass Execution の2番目の実行で呼ばれる。
-       *
-       * @param st 対象RTコンポーネントの現在の状態
-       *
-       * @else
-       *
-       * @brief Pure virtual function to be periodically invoked while 
-       *        RT-Component is running
-       *
-       * This operation will be invoked periodically at the rate of the given
-       * execution context as long as the following conditions hold:
-       *  - The RTC is Active.
-       *  - The given execution context is Running.
-       * This callback occurs during the second execution pass.
-       *
-       * @param st The current state of the target RT-Component
-       *
-       * @endif
-       */
-      virtual void on_state_update(const ECStates& st) = 0;
-      
-      /*!
-       * @if jp
-       * @brief ExecutionContext の実行周期変更時に呼ばれる純粋仮想関数
-       *
-       * 参加している ExecutionContext の実行周期が変更となった場合に、
-       * この変更を伝達するために呼び出される純粋仮想関数。
-       *
-       * @else
-       *
-       * @brief Pure virtual function to be invoked when when the execution 
-       *        cycles of ExecutionContext is changed.
-       *
-       * This operation is a notification that the rate of the indicated
-       * execution context has changed.
-       *
-       * @endif
-       */
-      virtual void on_rate_changed(void) = 0;
-      
-      /*!
-       * @if jp
-       * @brief 状態遷移を実行するワーカーを取得する
-       *
-       * 管理対象RTコンポーネントの状態遷移を実行するワーカーを取得する。
-       *
-       * @return ワーカー
-       *
-       * @else
-       * @brief Get the worker to execute the state transition
-       *
-       * Get the worker that executes the state transition of the target
-       * component to manage.
-       *
-       * @return The worker
-       *
-       * @endif
-       */
-      virtual void worker(void) {return m_sm.worker();}
-      virtual void worker_pre(void) {return m_sm.worker_pre();}
-      virtual void worker_do(void) {return m_sm.worker_do();}
-      virtual void worker_post(void) {return m_sm.worker_post();}
-      
-      /*!
-       * @if jp
-       * @brief 現在の状態を取得する
-       *
-       * 管理対象RTコンポーネントの現在の状態を取得する。
-       *
-       * @return 現在状態
-       *
-       * @else
-       * @brief Get the current state of the target component
-       *
-       * Get the current state of the target component to manage
-       *
-       * @return The current state of the target RT-Component
-       *
-       * @endif
-       */
-      virtual ExecContextState get_state(void){ return m_sm.getState();}
-      
-      /*!
-       * @if jp
-       * @brief 参加している ExecutionContext の ID
-       * @else
-       * @brief ID of participating ExecutionContext
-       * @endif
-       */
-      ExecutionContextHandle_t ec_id;
-      
-      /*!
-       * @if jp
-       * @brief 管理対象RTコンポーネントのステートマシン
-       * @else
-       * @brief The state machine of the target RT-Component to manage
-       * @endif
-       */
-      RTC_Utils::StateMachine<ExecContextState, DFPBase> m_sm;
-    };
-    
-    //============================================================
-    // DFP
-    //============================================================
+    virtual RTC::ReturnCode_t onStarted();
     /*!
-     * @if jp
-     * @class DFP
-     * @brief DFP クラス
-     *
-     * 参加者リストに登録された DataFlowParticipant の関数を起動するための
-     * テンプレートクラス。
-     *
-     * @param Object 管理対象コンポーネントの型
-     *
-     * @since 0.4.0
-     *
-     * @else
-     * @class DFP
-     * @brief DFP class
-     *
-     * Template class to invoke DataFlowParticipant registered
-     * in the participant list.
-     *
-     * @param Object Type of the target component to manage
-     *
-     * @since 0.4.0
-     *
-     * @endif
+     * @brief onStopping() template function
      */
-    template <class Object>
-    class DFP
-      : public DFPBase
-    {
-    public:
-      /*!
-       * @if jp
-       * @brief デフォルトコンストラクタ
-       *
-       * デフォルトコンストラクタ
-       *
-       * @param obj 管理対象コンポーネント
-       * @param id 所属する ExecutionContext のID
-       *
-       * @else
-       * @brief Default constructor
-       *
-       * Default constructor
-       *
-       * @param obj The target component to manage
-       * @param id ID of participating ExecutionContext
-       *
-       * @endif
-       */
-      DFP(Object obj, ExecutionContextHandle_t id)
-	: DFPBase(id), m_obj(obj), m_active(true)
-      {
-      }
-      
-      /*!
-       * @if jp
-       * @brief ExecutionContext 実行開始時に呼ばれる関数
-       *
-       * 参加している ExecutionContext が実行を開始する時(Running状態へ遷移時)
-       * に、管理対象コンポーネントの on_startup を呼びだす。
-       *
-       * @else
-       * @brief Function to be invoked when ExecutionContext starts
-       *
-       * When the given ExecutionContext transits from Stopped to Running,
-       * on_startup of the participation component will be invoked.
-       *
-       * @endif
-       */
-      void on_startup(void)
-      {
-	m_obj->on_startup(ec_id);
-      }
-      
-      /*!
-       * @if jp
-       * @brief ExecutionContext 停止時に呼ばれる関数
-       *
-       * 参加している ExecutionContext が実行を停止する時(Stopped状態へ遷移時)
-       * に、管理対象コンポーネントの on_shutdown を呼びだす。
-       *
-       * @else
-       * @brief Function to be invoked when ExecutionContext stops
-       *
-       * When the given ExecutionContext transits from Running to Stopped,
-       * on_shutdown of the participation component will be invoked.
-       *
-       * @endif
-       */
-      void on_shutdown(void)
-      {
-	m_obj->on_shutdown(ec_id);
-      }
-      
-      /*!
-       * @if jp
-       * @brief RTコンポーネントがアクティブ化された時に呼ばれる関数
-       *
-       * 管理対象のRTコンポーネントがアクティブ化された時(Active状態へ遷移時)
-       * に、管理対象コンポーネントの on_activated を呼びだす。
-       * 管理対象コンポーネントのアクティブ化が失敗した場合には、ステートマシン
-       * を Error 状態に遷移させる。
-       *
-       * @param st 対象RTコンポーネントの現在の状態
-       *
-       * @else
-       * @brief Function to be invoked when RT-Component was activated
-       *
-       * When the given ExecutionContext transits to the Active state,
-       * on_activated of the participation component will be invoked.
-       * If it fails, the state machine transits to the Errot state.
-       *
-       * @param st The current state of the target RT-Component
-       *
-       * @endif
-       */
-      void on_activated(const ECStates& st)
-      {
-	if (m_obj->on_activated(ec_id) != RTC::RTC_OK)
-	  {
-	    m_sm.goTo(ERROR_STATE);
-	    return;
-	  }
-	return;
-      }
-      
-      /*!
-       * @if jp
-       * @brief RTコンポーネントが非アクティブ化された時に呼ばれる関数
-       *
-       * 管理対象のRTコンポーネントが非アクティブ化された時
-       * (Deactive状態へ遷移時)に、管理対象コンポーネントの on_deactivated を
-       * 呼びだす。
-       *
-       * @param st 対象RTコンポーネントの現在の状態
-       *
-       * @else
-       * @brief Function to be invoked when RT-Component was deactivated
-       *
-       * When the given ExecutionContext transits the Deactivate state,
-       * on_deactivated of the participation component will be invoked.
-       *
-       * @param st The current state of the target RT-Component
-       *
-       * @endif
-       */
-      void on_deactivated(const ECStates& st)
-      {
-	m_obj->on_deactivated(ec_id);
-      }
-      
-      /*!
-       * @if jp
-       * @brief RTコンポーネントでエラーが発生した時に呼ばれる関数
-       *
-       * 管理対象のRTコンポーネントにエラーが発生した時(Error状態へ遷移時)
-       * に管理対象コンポーネントの on_aborting を呼びだす。
-       *
-       * @param st 対象RTコンポーネントの現在の状態
-       *
-       * @else
-       * @brief Function to be invoked when RT-Component occured error
-       *
-       * When the given ExecutionContext transits the Error state,
-       * on_aborting of the participation component will be invoked.
-       *
-       * @param st The current state of the target RT-Component
-       *
-       * @endif
-       */
-      void on_aborting(const ECStates& st)
-      {
-	m_obj->on_aborting(ec_id);
-      }
-      
-      /*!
-       * @if jp
-       * @brief RTコンポーネントがエラー状態の時に呼ばれる関数
-       *
-       * 管理対象のRTコンポーネントがエラー状態にいる間、
-       * 管理対象コンポーネントの on_aborting を定期的に呼びだす。
-       *
-       * @param st 対象RTコンポーネントの現在の状態
-       *
-       * @else
-       * @brief Function to be invoked while RT-Component is in the error state
-       *
-       * While the given RT-Component is in the Error state,
-       * its on_aborting will be periodically invoked.
-       *
-       * @param st The current state of the target RT-Component
-       *
-       * @endif
-       */
-      void on_error(const ECStates& st)
-      {
-	m_obj->on_error(ec_id);
-      }
-      
-      /*!
-       * @if jp
-       * @brief RTコンポーネントをリセットする時に呼ばれる関数
-       *
-       * 管理対象のRTコンポーネントをリセットする際に、管理対象コンポーネント
-       * の on_reset を呼びだす。
-       *
-       * @param st 対象RTコンポーネントの現在の状態
-       *
-       * @else
-       * @brief Function to be invoked when RT-Component is reset.
-       *
-       * When the target RT-Component is reset,
-       * invoke on_reset of the target component to manage.
-       *
-       * @param st The current state of the target RT-Component
-       *
-       * @endif
-       */
-      void on_reset(const ECStates& st)
-      {
-	if (m_obj->on_reset(ec_id) != RTC::RTC_OK)
-	  {
-	    m_sm.goTo(ERROR_STATE);
-	    return;
-	  }
-	return;
-      }
-      
-      /*!
-       * @if jp
-       * @brief RTコンポーネント実行時に定期的に呼ばれる関数
-       *
-       * 管理対象のRTコンポーネントが Active 状態であるとともに、
-       * ExecutionContext が Running 状態の場合に、設定された動作周期で
-       * 定期的に管理対象コンポーネントの on_execute を呼びだす。関数の
-       * 実行に失敗した場合(返値が RTC_OK 以外)、管理対象コンポーネント
-       * の状態を Error 状態に遷移させる。
-       *
-       * @param st 対象RTコンポーネントの現在の状態
-       *
-       * @else
-       * @brief Periodic exection function while running RT-Component
-       *
-       * If the given RT-Component is in the Active state and
-       * ExecutionContext is in the Running state, on_execute of the
-       * given component will be invoked periodically at the specified
-       * execution cycle.  If it fails (the return value is other than
-       * RTC_OK), its state transits to the Errot state.
-       *
-       * @param st The current state of the target RT-Component
-       *
-       * @endif
-       */
-      void on_execute(const ECStates& st)
-      {
-	if (m_obj->on_execute(ec_id) != RTC::RTC_OK)
-	  {
-	    m_sm.goTo(ERROR_STATE);
-	    return;
-	  }  
-	return;
-      }
-      
-      /*!
-       * @if jp
-       * @brief RTコンポーネント実行時に定期的に呼ばれる関数
-       *
-       * 管理対象のRTコンポーネントが Active 状態であるとともに、
-       * ExecutionContext が Running 状態の場合に、設定された動作周期で
-       * 定期的に管理対象コンポーネントの on_state_update を呼びだす。
-       * 関数の実行に失敗した場合(返値が RTC_OK 以外)、管理対象コンポー
-       * ネントの状態を Error 状態に遷移させる。
-       *
-       * @param st 対象RTコンポーネントの現在の状態
-       *
-       * @else
-       * @brief Function to be invoked periodically while RT-Component executes
-       *
-       * When the target RT-Component to manage is in the Active state
-       * and ExecutionContext is the Running, invoke on_state_update
-       * of the target component to manage periodically in specified
-       * execution cycle.  If it fails (the return value is other than
-       * RTC_OK), its state transits to the Errot state.
-       *
-       * @param st The current state of the target RT-Component
-       *
-       * @endif
-       */
-      void on_state_update(const ECStates& st)
-      {
-	if (m_obj->on_state_update(ec_id) != RTC::RTC_OK)
-	  {
-	    m_sm.goTo(ERROR_STATE);
-	    return;
-	  }
-	return;
-      }
-      
-      /*!
-       * @if jp
-       * @brief ExecutionContext の実行周期変更時に呼ばれる関数
-       *
-       * 参加している ExecutionContext の実行周期が変更となった場合に、
-       * 管理対象コンポーネントの on_rate_changed を呼びだす。
-       *
-       * @else
-       * @brief Function to be invoked when the execution cycles of
-       *        ExecutionContext is changed
-       *
-       * When the execution cycle of the participating
-       * ExecutionContext is changed, invoke on_rate_changed of the
-       * target component will be invoked.
-       *
-       * @endif
-       */
-      void on_rate_changed(void)
-      {
-	m_obj->on_rate_changed(ec_id);
-      }
-      
-      /*!
-       * @if jp
-       * @brief 管理対象コンポーネント
-       * @else
-       * @brief The target component to manage
-       * @endif
-       */
-      Object m_obj;
-      
-      /*!
-       * @if jp
-       * @brief 管理対象コンポーネントの動作状態フラグ
-       * @else
-       * @brief State flag of the target component to manage
-       * @endif
-       */
-      bool m_active;
-    };
-    
+    virtual RTC::ReturnCode_t onStopping();
     /*!
-     * @if jp
-     * @brief コンポーネント管理用構造体
-     * @else
-     * @brief The structure for the component management
-     * @endif
+     * @brief onWaitingActivated() template function
      */
-    struct Comp
-    {
-      Comp(LightweightRTObject_ptr ref, OpenRTM::DataFlowComponent_ptr dfp,
-	   ExecutionContextHandle_t id)
-	: _ref(LightweightRTObject::_duplicate(ref)),
-	  _sm(OpenRTM::DataFlowComponent::_duplicate(dfp), id)
-      {
-      }
-      ~Comp(void)
-      {
-      }
-      Comp(const Comp& comp)
-	: _ref(comp._ref), _sm(comp._sm.m_obj, comp._sm.ec_id)
-      {
-      }
-      Comp& operator=(const Comp& comp)
-      {
-	_ref = comp._ref;
-	_sm.m_obj = comp._sm.m_obj;
-	_sm.ec_id = comp._sm.ec_id;
-	return *this;
-      }
-      LightweightRTObject_var _ref;
-      DFP<OpenRTM::DataFlowComponent_var> _sm;
-    };
-
+    virtual RTC::ReturnCode_t
+    onWaitingActivated(RTC_impl::RTObjectStateMachine* comp, long int count);
     /*!
-     * @if jp
-     * @brief コンポーネント検索用ファンクタ
-     * @else
-     * @brief Functor to find the component
-     * @endif
+     * @brief onActivated() template function
      */
-    struct find_comp
-    {
-      LightweightRTObject_var m_comp;
-      find_comp(LightweightRTObject_ptr comp)
-        : m_comp(LightweightRTObject::_duplicate(comp)) {}
-      bool operator()(Comp& comp)
-      {
-	return comp._ref->_is_equivalent(m_comp);
-      }
-    };
-    
+    virtual RTC::ReturnCode_t
+    onActivated(RTC_impl::RTObjectStateMachine* comp, long int count);
     /*!
-     * @if jp
-     * @brief on_startup 起動用ファンクタ
-     * @else
-     * @brief Functor to invoke on_startup
-     * @endif
+     * @brief onWaitingDeactivated() template function
      */
-    struct invoke_on_startup
-    {
-      void operator()(Comp& comp)
-      {
-	comp._sm.on_startup();
-      }
-    };
-    
+    virtual RTC::ReturnCode_t
+    onWaitingDeactivated(RTC_impl::RTObjectStateMachine* comp, long int count);
     /*!
-     * @if jp
-     * @brief on_shutdown 起動用ファンクタ
-     * @else
-     * @brief Functor to invoke on_shutdown
-     * @endif
+     * @brief onDeactivated() template function
      */
-    struct invoke_on_shutdown
-    {
-      void operator()(Comp& comp)
-      {
-	comp._sm.on_shutdown();
-      }
-    };
-    
+    virtual RTC::ReturnCode_t 
+    onDeactivated(RTC_impl::RTObjectStateMachine* comp, long int count);
     /*!
-     * @if jp
-     * @brief on_rate_changed 起動用ファンクタ
-     * @else
-     * @brief Functor to invoke on_rate_changed
-     * @endif
+     * @brief onWaitingReset() template function
      */
-    struct invoke_on_rate_changed
-    {
-      void operator()(Comp& comp)
-      {
-	comp._sm.on_rate_changed();
-      }
-    };
-    
+    virtual RTC::ReturnCode_t
+    onWaitingReset(RTC_impl::RTObjectStateMachine* comp, long int count);
     /*!
-     * @if jp
-     * @brief ワーカー実行用ファンクタ
-     * @else
-     * @brief Functor to invoke worker
-     * @endif
+     * @brief onReset() template function
      */
-    struct invoke_worker
+    virtual RTC::ReturnCode_t 
+    onReset(RTC_impl::RTObjectStateMachine* comp, long int count);
+
+    bool threadRunning()
     {
-      void operator()(Comp& comp)
-      {
-        comp._sm.worker();
-      }
-    };
-    struct invoke_worker_pre
-    {
-      void operator()(Comp& comp)
-      {
-        comp._sm.worker_pre();
-      }
-    };
-    struct invoke_worker_do
-    {
-      void operator()(Comp& comp)
-      {
-        comp._sm.worker_do();
-      }
-    };
-    struct invoke_worker_post
-    {
-      void operator()(Comp& comp)
-      {
-        comp._sm.worker_post();
-      }
-    };
-    
+      Guard guard(m_svcmutex);
+      return m_svc;
+    }
+  protected:
     /*!
      * @if jp
-     * @brief コンポーネントの参加者リスト
-     * @else
-     * @brief List of the participating component
-     * @endif
-     */
-    std::vector<Comp> m_comps;
-    typedef std::vector<Comp>::iterator CompItr;
-    
-    /*!
-     * @if jp
      * @brief ロガーストリーム
      * @else
      * @brief Logger stream
      * @endif
      */
-    Logger rtclog;
+    RTC::Logger rtclog;
 
     /*!
      * @if jp
-     * @brief ExecutionContext の実行状態
-     * true: running, false: stopped
-     * @else
-     * @brief The running state of ExecutionContext
-     * true: running, false: stopped
-     * @endif
-     */
-    bool m_running;
-
-    /*!
-     * @if jp
      * @brief ExecutionContext のスレッド実行フラグ
      * @else
      * @brief The thread running flag of ExecutionContext
      * @endif
      */
     bool m_svc;
-
+    coil::Mutex m_svcmutex;
+    
     /*!
      * @if jp
      * @brief worker 用状態変数クラス
@@ -1522,9 +642,9 @@
      * @brief Condition variable class for worker
      * @endif
      */
-    struct Worker
+    struct WorkerThreadCtrl
     {
-      Worker() : cond_(mutex_), running_(false) {};
+      WorkerThreadCtrl() : cond_(mutex_), running_(false) {};
       coil::Mutex mutex_;
       coil::Condition<coil::Mutex> cond_;
       bool running_;
@@ -1537,19 +657,10 @@
      * @brief A condition variable for external triggered worker
      * @endif
      */
-    Worker m_worker;
+    WorkerThreadCtrl m_workerthread;
 
     /*!
      * @if jp
-     * @brief ExecutionContext の実行周期
-     * @else
-     * @brief Execution cycle of ExecutionContext
-     * @endif
-     */
-    coil::TimeValue m_period;
-
-    /*!
-     * @if jp
      * @brief ExecutionContext 即時実行(wait無し実行)フラグ
      * @else
      * @brief Flag of ExecutionContext to run immediately
@@ -1558,17 +669,6 @@
      */
     bool m_nowait;
 
-    class find_participant
-    {
-      RTObject_var m_comp;
-    public:      
-      find_participant(RTObject_ptr comp)
-        : m_comp(RTObject::_duplicate(comp)) {}
-      bool operator()(RTObject_ptr comp)
-      {
-        return m_comp->_is_equivalent(comp);
-      }
-    };
   }; // class PeriodicExecutionContext
 }; // namespace RTC
 



openrtm-commit メーリングリストの案内