Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
_flow_graph_async_msg_impl.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2005-2019 Intel Corporation
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 
16 
17 
18 
19 */
20 
21 #ifndef __TBB__flow_graph_async_msg_impl_H
22 #define __TBB__flow_graph_async_msg_impl_H
23 
24 #ifndef __TBB_flow_graph_H
25 #error Do not #include this internal file directly; use public TBB headers instead.
26 #endif
27 
28 namespace internal {
29 
30 template <typename T>
32 public:
33  typedef receiver<T> async_storage_client;
34 
35  async_storage() : my_graph(nullptr) {
37  }
38 
40  // Release reference to the graph if async_storage
41  // was destructed before set() call
42  if (my_graph) {
43  my_graph->release_wait();
44  my_graph = nullptr;
45  }
46  }
47 
48  template<typename C>
49  async_storage(C&& data) : my_graph(nullptr), my_data( std::forward<C>(data) ) {
50  using namespace tbb::internal;
51  __TBB_STATIC_ASSERT( (is_same_type<typename strip<C>::type, typename strip<T>::type>::value), "incoming type must be T" );
52 
54  }
55 
56  template<typename C>
57  bool set(C&& data) {
58  using namespace tbb::internal;
59  __TBB_STATIC_ASSERT( (is_same_type<typename strip<C>::type, typename strip<T>::type>::value), "incoming type must be T" );
60 
61  {
63 
65  __TBB_ASSERT(false, "double set() call");
66  return false;
67  }
68 
69  my_data = std::forward<C>(data);
71  }
72 
73  // Thread sync is on my_data_ready flag
74  for (typename subscriber_list_type::iterator it = my_clients.begin(); it != my_clients.end(); ++it) {
75  (*it)->try_put(my_data);
76  }
77 
78  // Data was sent, release reference to the graph
79  if (my_graph) {
80  my_graph->release_wait();
81  my_graph = nullptr;
82  }
83 
84  return true;
85  }
86 
87  task* subscribe(async_storage_client& client, graph& g) {
89  {
91 
92  if (! my_data_ready.load<tbb::relaxed>()) {
93 #if TBB_USE_ASSERT
94  for (typename subscriber_list_type::iterator it = my_clients.begin(); it != my_clients.end(); ++it) {
95  __TBB_ASSERT(*it != &client, "unexpected double subscription");
96  }
97 #endif // TBB_USE_ASSERT
98 
99  // Increase graph lifetime
100  my_graph = &g;
101  my_graph->reserve_wait();
102 
103  // Subscribe
104  my_clients.push_back(&client);
105  return SUCCESSFULLY_ENQUEUED;
106  }
107  }
108 
109  __TBB_ASSERT(my_data_ready.load<tbb::relaxed>(), "data is NOT ready");
110  return client.try_put_task(my_data);
111  }
112 
113 private:
114  graph* my_graph;
118  typedef std::vector<async_storage_client*> subscriber_list_type;
120 };
121 
122 } // namespace internal
123 
124 template <typename T>
125 class async_msg {
126  template< typename > friend class receiver;
127  template< typename, typename > friend struct internal::async_helpers;
128 public:
130 
131  async_msg() : my_storage(std::make_shared< internal::async_storage<T> >()) {}
132 
133  async_msg(const T& t) : my_storage(std::make_shared< internal::async_storage<T> >(t)) {}
134 
135  async_msg(T&& t) : my_storage(std::make_shared< internal::async_storage<T> >( std::move(t) )) {}
136 
137  virtual ~async_msg() {}
138 
139  void set(const T& t) {
140  my_storage->set(t);
141  }
142 
143  void set(T&& t) {
144  my_storage->set( std::move(t) );
145  }
146 
147 protected:
148  // Can be overridden in derived class to inform that
149  // async calculation chain is over
150  virtual void finalize() const {}
151 
152 private:
153  typedef std::shared_ptr< internal::async_storage<T> > async_storage_ptr;
155 };
156 
157 #endif // __TBB__flow_graph_async_msg_impl_H
static tbb::task *const SUCCESSFULLY_ENQUEUED
No ordering.
Definition: atomic.h:51
#define __TBB_STATIC_ASSERT(condition, msg)
Definition: tbb_stddef.h:536
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
task * subscribe(async_storage_client &client, graph &g)
Detects whether two given types are the same.
Acquire.
Definition: atomic.h:47
A lock that occupies a single byte.
Definition: spin_mutex.h:40
value_type load() const
Definition: atomic.h:306
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_id __itt_string_handle __itt_metadata_type size_t void * data
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task * task
Represents acquisition of a mutex.
Definition: spin_mutex.h:54
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long value
std::vector< async_storage_client * > subscriber_list_type
void move(tbb_thread &t1, tbb_thread &t2)
Definition: tbb_thread.h:309
void set(const T &t)
virtual void finalize() const
std::shared_ptr< internal::async_storage< T > > async_storage_ptr
void store(value_type value)
Definition: atomic.h:317
Identifiers declared inside namespace internal should never be used directly by client code.
Definition: atomic.h:55
Release.
Definition: atomic.h:49
async_storage_ptr my_storage

Copyright © 2005-2019 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.