Skip to content

File thread_entry_task.hpp

File List > cubrid > src > thread > thread_entry_task.hpp

Go to the documentation of this file

/*
 * Copyright 2008 Search Solution Corporation
 * Copyright 2016 CUBRID Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

/*
 * thread_entry_task.hpp
 */

#ifndef _THREAD_ENTRY_TASK_HPP_
#define _THREAD_ENTRY_TASK_HPP_

#if !defined (SERVER_MODE) && !defined (SA_MODE)
#error Wrong module
#endif // not SERVER_MODE and not SA_MODE

#include "thread_entry.hpp"
#include "thread_task.hpp"
#include "transaction_global.hpp"

#include <cassert>

namespace cubthread
{

  // cubthread::entry_task
  //
  //  description:
  //    entry_task class is a cubthread::task specialization using entry as Context; see thread_task.hpp
  //    class is friend of cubthread::manager and cannot be used directly with cubthread::daemon or
  //      cubthread::worker_pool
  //
  //  how to use:
  //     1. extend entry_task and override virtual functions
  //        override execute (entry &) function to define execution
  //        [optional] override create_context, retire_context and retire functions (see thread_task.hpp)
  //
  //     2. provide custom task to manager and either:
  //     2.1. create a daemon to loop and execute task repeatedly
  //     2.2. pass it to a worker pool to be executed once
  //        see thread_manager.hpp, thread_daemon.hpp and thread_worker_pool.hpp for more details
  //
  using entry_task = task<entry>;
  using entry_callable_task = callable_task<entry>;

  // cubthread::entry_manager
  //
  //  description:
  //    entry_manager is the base class for managing thread execution contexts (cubthread::entry).
  //    complex tasks may require bulky context that can take a long time to construct/destruct.
  //    entry_manager is designed to pool context and hand them quickly on demand.
  //    entry_manager acts as a base for specialized context managers that may need to do additional
  //    operations on entry contexts during create, retire and recycle context operations.
  //
  //  how to use:
  //    1. derive from entry_manager and override on_create, on_retire and on_recycle functions.
  //       create worker pools using derived entry_manager.
  //
  //    2. execute multiple tasks using same context:
  //          custom_entry_manager entry_mgr;
  //          entry& context_ref = entry_mgr.create_context ();
  //          entry_task* task_p = NULL;
  //
  //          for (task_p = get_task (); task_p != NULL; task_p = get_task ())
  //            {
  //              task_p->execute (context_ref);
  //              task_p->retire (); // this will delete task_p
  //              // entry_mgr.recycle_context ();    // optional operation before reusing context
  //            }
  //
  //          entry_mgr.retire_context (context_ref);
  //
  //    [optional]
  //    3. if task execution can take a very long time and you need to force stop it, you can use stop_execution:
  //        3.1. implement entry_manager::stop_execution; should notify context to stop.
  //        3.2. you have to check stop notifications in entry_task::execute
  //        3.3. call entry_mgr.stop_execution (context_ref).
  //
  class entry_manager
  {
    public:
      using context_type = entry;

      entry_manager (void) = default;
      virtual ~entry_manager () = default;

      virtual context_type &create_context (void);
      virtual void retire_context (context_type &context);
      virtual void recycle_context (context_type &context);
      virtual void stop_execution (context_type &context);

    protected:

      virtual void on_create (context_type &)    // manipulate entry after claim and before any execution
      {
    // does nothing by default
      }
      virtual void on_retire (context_type &)    // manipulate entry before retire; should mirror on_create
      {
    // does nothing by default
      }
      virtual void on_recycle (context_type &)   // manipulate entry between execution cycles
      {
    // does nothing by default
      }
  };

  // cubthread::daemon_entry_manager
  //
  //  description:
  //    daemon_entry_manager is derived from entry_manager and adds extra logic specific for daemon threads
  //    initialization and destruction. for more details see entry_manager description.
  //
  //  how to use:
  //    create a daemon_entry_manager derived from entry_manager and override on_daemon_create and on on_daemon_retire
  //    functions if daemon require custom logic on initialization or destruction.
  //    create daemon threads using daemon_entry_manager class
  //
  class daemon_entry_manager : public entry_manager
  {
    public:
      daemon_entry_manager () = default;
      ~daemon_entry_manager () = default;

    protected:
      virtual void on_daemon_create (entry &)
      {
    // does nothing by default
      }

      void on_create (entry &) final;

      virtual void on_daemon_retire (entry &)
      {
    // does nothing by default
      }

      void on_retire (entry &) final;

      void on_recycle (entry &) final
      {
    // daemon threads are not recycled
      }
  };

  // system_worker_entry_manager
  //
  // description:
  //    - generic entry manager implementation can be used to provide custom
  //      thread type and transaction index
  //    - useful in scenarios where there is no actual transaction and perf logging
  //      still needs a non-null transaction index
  //
  class system_worker_entry_manager : public entry_manager
  {
    public:
      system_worker_entry_manager (thread_type a_thread_type);
      system_worker_entry_manager (const system_worker_entry_manager &) = delete;
      system_worker_entry_manager (system_worker_entry_manager &&) = delete;

      system_worker_entry_manager &operator = (const system_worker_entry_manager &) = delete;
      system_worker_entry_manager &operator = (system_worker_entry_manager &&) = delete;

    public:
      void on_create (entry &context) override;
      void on_recycle (entry &context) override;

    private:
      const thread_type m_thread_type;
  };
} // namespace cubthread

#endif // _THREAD_ENTRY_TASK_HPP_