tag_allocator.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // -*- C++ -*-
  2. // Copyright (C) 2007 Douglas Gregor <doug.gregor@gmail.com>
  3. // Use, modification and distribution is subject to the Boost Software
  4. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_GRAPH_DISTRIBUTED_TAG_ALLOCATOR_HPP
  7. #define BOOST_GRAPH_DISTRIBUTED_TAG_ALLOCATOR_HPP
  8. #ifndef BOOST_GRAPH_USE_MPI
  9. #error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included"
  10. #endif
  11. #include <vector>
  12. namespace boost { namespace graph { namespace distributed { namespace detail {
  13. /**
  14. * \brief The tag allocator allows clients to request unique tags that
  15. * can be used for one-time communications.
  16. *
  17. * The tag allocator hands out tag values from a predefined maximum
  18. * (given in the constructor) moving downward. Tags are provided one
  19. * at a time via a @c token. When the @c token goes out of scope, the
  20. * tag is returned and may be reallocated. These tags should be used,
  21. * for example, for one-time communication of values.
  22. */
  23. class tag_allocator {
  24. public:
  25. class token;
  26. friend class token;
  27. /**
  28. * Construct a new tag allocator that provides unique tags starting
  29. * with the value @p top_tag and moving lower, as necessary.
  30. */
  31. explicit tag_allocator(int top_tag) : bottom(top_tag) { }
  32. /**
  33. * Retrieve a new tag. The token itself holds onto the tag, which
  34. * will be released when the token is destroyed.
  35. */
  36. token get_tag();
  37. private:
  38. int bottom;
  39. std::vector<int> freed;
  40. };
  41. /**
  42. * A token used to represent an allocated tag.
  43. */
  44. class tag_allocator::token {
  45. public:
  46. /// Transfer ownership of the tag from @p other.
  47. token(const token& other);
  48. /// De-allocate the tag, if this token still owns it.
  49. ~token();
  50. /// Retrieve the tag allocated for this task.
  51. operator int() const { return tag_; }
  52. private:
  53. /// Create a token with a specific tag from the given tag_allocator
  54. token(tag_allocator* allocator, int tag)
  55. : allocator(allocator), tag_(tag) { }
  56. /// Undefined: tokens are not copy-assignable
  57. token& operator=(const token&);
  58. /// The allocator from which this tag was allocated.
  59. tag_allocator* allocator;
  60. /// The stored tag flag. If -1, this token does not own the tag.
  61. mutable int tag_;
  62. friend class tag_allocator;
  63. };
  64. } } } } // end namespace boost::graph::distributed::detail
  65. #endif // BOOST_GRAPH_DISTRIBUTED_TAG_ALLOCATOR_HPP