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