resources.h 2.3 KB

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