github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/3rd_party/fflib/include/ff/functionflow/runtime/runtime.h (about)

     1  /***********************************************
     2    The MIT License (MIT)
     3  
     4    Copyright (c) 2012 Athrun Arthur <athrunarthur@gmail.com>
     5  
     6    Permission is hereby granted, free of charge, to any person obtaining a copy
     7    of this software and associated documentation files (the "Software"), to deal
     8    in the Software without restriction, including without limitation the rights
     9    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    10    copies of the Software, and to permit persons to whom the Software is
    11    furnished to do so, subject to the following conditions:
    12  
    13    The above copyright notice and this permission notice shall be included in
    14    all copies or substantial portions of the Software.
    15  
    16    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    18    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    19    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    20    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    21    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    22    THE SOFTWARE.
    23   *************************************************/
    24  #ifndef FF_RUNTIME_RUNTIME_H_
    25  #define FF_RUNTIME_RUNTIME_H_
    26  #include "ff/functionflow/common/common.h"
    27  #include "ff/functionflow/runtime/rtcmn.h"
    28  #include "ff/functionflow/runtime/task_queue.h"
    29  #include "ff/functionflow/runtime/threadpool.h"
    30  #include "ff/functionflow/utilities/simo_queue.h"
    31  
    32  namespace ff {
    33  
    34  void initialize(size_t concurrency = 0);
    35  
    36  namespace rt {
    37  
    38  class threadpool;
    39  class runtime;
    40  typedef runtime *runtime_ptr;
    41  
    42  class runtime {
    43   protected:
    44    runtime();
    45    runtime(const runtime &) = delete;
    46  
    47   public:
    48    virtual ~runtime();
    49    static runtime_ptr instance();
    50  
    51    void schedule(task_base_ptr p);
    52    bool take_one_task(task_base_ptr &p);
    53  
    54    bool steal_one_task(task_base_ptr &p);
    55    void run_task(task_base_ptr &p);
    56  
    57    bool is_idle();
    58  
    59    thrd_id_t get_idle();
    60    std::tuple<uint64_t, uint64_t> current_task_counter();
    61  
    62    void abort_all_tasks_and_quit();
    63  
    64  protected:
    65    void thread_run();
    66    void init_for_no_ff_thread();
    67    static void init();
    68  
    69   protected:
    70    std::unique_ptr<threadpool> m_pTP;
    71    std::vector<std::unique_ptr<work_stealing_queue> > m_oQueues;
    72    typedef simo_queue<task_base_ptr, 8> simo_queue_t;
    73    std::vector<std::unique_ptr<simo_queue_t> > m_oWQueues;
    74    //    thread_local static work_stealing_queue *
    75    //    m_pLQueue;
    76    std::atomic<bool> m_bAllThreadsQuit;
    77    static runtime_ptr s_pInstance;
    78    static std::once_flag s_oOnce;
    79    std::atomic_int m_sleep_counter;
    80    std::mutex m_wakeup_mutex;
    81    std::condition_variable m_wakeup;
    82    std::mutex m_queue_mutex;
    83    std::mutex m_join_mutex;
    84    std::atomic_int m_scheduler_thread_running_count;
    85  };  // end class runtime
    86  
    87  class runtime_deletor {
    88   public:
    89    runtime_deletor(runtime *pRT) : m_pRT(pRT){};
    90    ~runtime_deletor() { delete m_pRT; };
    91    static std::shared_ptr<runtime_deletor> s_pInstance;
    92  
    93   protected:
    94    runtime *m_pRT;
    95  };
    96  
    97  //! Get the number of exe_over_tasks and scheduled_tasks
    98  inline std::tuple<uint64_t, uint64_t> current_task_counter() {
    99    static runtime_ptr r = runtime::instance();
   100    return r->current_task_counter();
   101  }
   102  
   103  void schedule(task_base_ptr p);
   104  
   105  template <class Func>
   106  void yield_and_ret_until(Func &&f) {
   107    int cur_id = get_thrd_id();
   108    runtime_ptr r = runtime::instance();
   109    bool b = f();
   110    task_base_ptr pTask;
   111  
   112    while (!b) {
   113      if (r->take_one_task(pTask)) {
   114        r->run_task(pTask);
   115      } else {
   116        yield();
   117      }
   118      b = f();
   119    }
   120  }
   121  }  // end namespace rt
   122  
   123  void abort_all_tasks_and_quit();
   124  
   125  }  // end namespace ff
   126  #endif