github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/3rd_party/fflib/include/ff/functionflow/runtime/rtcmn.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_RTCMN_H_
    25  #define FF_RUNTIME_RTCMN_H_
    26  #include "ff/functionflow/common/common.h"
    27  namespace ff {
    28  extern bool g_initialized_flag;
    29  inline bool is_initialized() { return g_initialized_flag; }
    30  namespace rt {
    31  
    32  extern size_t s_hardware_concurrency;
    33  extern std::atomic<size_t> s_current_concurrency;
    34  
    35  inline size_t hardware_concurrency() { return s_hardware_concurrency; }
    36  
    37  extern size_t max_concurrency;
    38  
    39  inline bool set_concurrency(size_t c = 0) {
    40    if (s_current_concurrency == 0) {
    41      if (c == 0) {
    42        s_current_concurrency = max_concurrency;
    43        return true;
    44      }
    45      s_current_concurrency = c;
    46      return true;
    47    } else {
    48      return false;
    49    }
    50  }
    51  inline size_t concurrency() {
    52    if (s_current_concurrency)
    53      return s_current_concurrency;
    54    else
    55      return s_hardware_concurrency;
    56  }
    57  
    58  extern thread_local thrd_id_t s_id;
    59  inline thrd_id_t get_thrd_id() { return s_id; }
    60  
    61  bool set_local_thrd_id(thrd_id_t i);
    62  
    63  // Give other tasks opportunities to run!
    64  void yield();
    65  }  // end namespace rt
    66  
    67  }  // end namespace ff
    68  #endif