init.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright (c) 2006-2018 Maxim Khizhinsky
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef CDSLIB_INIT_H
  6. #define CDSLIB_INIT_H
  7. #include <cds/details/defs.h>
  8. #include <cds/os/topology.h>
  9. #include <cds/threading/model.h>
  10. namespace cds {
  11. //@cond
  12. namespace details {
  13. bool CDS_EXPORT_API init_first_call();
  14. bool CDS_EXPORT_API fini_last_call();
  15. void CDS_EXPORT_API check_hpstat_enabled( bool enabled );
  16. } // namespace details
  17. //@endcond
  18. /// Initialize CDS library
  19. /**
  20. The function initializes \p CDS library framework.
  21. Before usage of \p CDS library features your application must initialize it
  22. by calling \p %Initialize() function, see \ref cds_how_to_use "how to use the library".
  23. You can call \p Initialize several times, only first call is significant others will be ignored.
  24. To terminate the \p CDS library correctly, each call to \p %Initialize() must be balanced
  25. by a corresponding \p Terminate() call.
  26. Note, that this function does not initialize garbage collectors. To use GC you need you should call
  27. GC-specific constructor function to initialize internal structures of GC.
  28. See \p cds::gc for details.
  29. */
  30. static inline void Initialize(
  31. unsigned int nFeatureFlags = 0 ///< for future use, must be zero.
  32. )
  33. {
  34. CDS_UNUSED( nFeatureFlags );
  35. details::check_hpstat_enabled(
  36. #ifdef CDS_ENABLE_HPSTAT
  37. true
  38. #else
  39. false
  40. #endif
  41. );
  42. if ( cds::details::init_first_call())
  43. {
  44. cds::OS::topology::init();
  45. cds::threading::ThreadData::s_nProcCount = cds::OS::topology::processor_count();
  46. if ( cds::threading::ThreadData::s_nProcCount == 0 )
  47. cds::threading::ThreadData::s_nProcCount = 1;
  48. cds::threading::Manager::init();
  49. }
  50. }
  51. /// Terminate CDS library
  52. /**
  53. This function terminates \p CDS library.
  54. After \p %Terminate() calling many features of the library are unavailable.
  55. This call should be the last call of \p CDS library in your application,
  56. see \ref cds_how_to_use "how to use the library".
  57. */
  58. static inline void Terminate()
  59. {
  60. if ( cds::details::fini_last_call()) {
  61. cds::threading::Manager::fini();
  62. cds::OS::topology::fini();
  63. }
  64. }
  65. } // namespace cds
  66. #endif // CDSLIB_INIT_H