resources.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #pragma once
  2. //
  3. // Copyright (c) 2019-2022 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
  4. //
  5. // Distributed under the MIT Software License
  6. //
  7. #include "plugin_base.h"
  8. #include <vector>
  9. #if defined(_MSC_VER)
  10. #pragma warning(push)
  11. #pragma warning(disable : 4251)
  12. #endif
  13. namespace rotor::plugin {
  14. using resource_id_t = std::size_t;
  15. /** \struct resources_plugin_t
  16. *
  17. * \brief "lock" for external resources
  18. *
  19. * The main purpose of the plugin is to let actor know, that some
  20. * external resources are acquired, and the actor should will be
  21. * suspended until they will be released.
  22. *
  23. * The suspension will happen during init and shutdown phases, e.g.:
  24. * - actor can wait, until connection will be estableshed
  25. * - actor can wait, until it receives handshake from remote system
  26. * - etc...
  27. *
  28. * The used `resource_id` can be anything, which has meaning for
  29. * an actor. Internally `resource_id` is just an index in vector,
  30. * so don't create it too big.
  31. *
  32. */
  33. struct ROTOR_API resources_plugin_t : public plugin_base_t {
  34. using plugin_base_t::plugin_base_t;
  35. /** The plugin unique identity to allow further static_cast'ing*/
  36. static const void *class_identity;
  37. const void *identity() const noexcept override;
  38. void activate(actor_base_t *actor) noexcept override;
  39. bool handle_init(message::init_request_t *message) noexcept override;
  40. bool handle_shutdown(message::shutdown_request_t *message) noexcept override;
  41. /** \brief increments the resource counter (aka locks)
  42. *
  43. * It will block init/shutdown until the resource be released
  44. *
  45. */
  46. virtual void acquire(resource_id_t = 0) noexcept;
  47. /** \brief decrements the resource counter (aka unlocks)
  48. *
  49. * If there is no any resource free, the init/shutdown
  50. * procedure (if it was started) will be continued
  51. *
  52. */
  53. virtual bool release(resource_id_t = 0) noexcept;
  54. /** \brief returns counter value for `resource_id`
  55. *
  56. * if the resource was freed, zero is returned
  57. *
  58. */
  59. virtual std::uint32_t has(resource_id_t = 0) noexcept;
  60. /** \brief returns true if there is any resource is locked */
  61. virtual bool has_any() noexcept;
  62. /** \brief generic non-public fields accessor */
  63. template <typename T> auto &access() noexcept;
  64. private:
  65. using Resources = std::vector<std::uint32_t>;
  66. Resources resources;
  67. bool configured = false;
  68. };
  69. } // namespace rotor::plugin
  70. #if defined(_MSC_VER)
  71. #pragma warning(pop)
  72. #endif