Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
mailbox.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 #ifndef _TBB_mailbox_H
18 #define _TBB_mailbox_H
19 
20 #include "tbb/tbb_stddef.h"
22 
23 #include "scheduler_common.h"
24 #include "tbb/atomic.h"
25 
26 namespace tbb {
27 namespace internal {
28 
29 struct task_proxy : public task {
30  static const intptr_t pool_bit = 1<<0;
31  static const intptr_t mailbox_bit = 1<<1;
32  static const intptr_t location_mask = pool_bit | mailbox_bit;
33  /* All but two low-order bits represent a (task*).
34  Two low-order bits mean:
35  1 = proxy is/was/will be in task pool
36  2 = proxy is/was/will be in mailbox */
37  intptr_t task_and_tag;
38 
41 
44 
46  static bool is_shared ( intptr_t tat ) {
47  return (tat & location_mask) == location_mask;
48  }
49 
51  static task* task_ptr ( intptr_t tat ) {
52  return (task*)(tat & ~location_mask);
53  }
54 
56  template<intptr_t from_bit>
57  inline task* extract_task () {
58  __TBB_ASSERT( prefix().extra_state == es_task_proxy, "Normal task misinterpreted as a proxy?" );
59  intptr_t tat = __TBB_load_with_acquire(task_and_tag);
60  __TBB_ASSERT( tat == from_bit || (is_shared(tat) && task_ptr(tat)),
61  "Proxy's tag cannot specify both locations if the proxy "
62  "was retrieved from one of its original locations" );
63  if ( tat != from_bit ) {
64  const intptr_t cleaner_bit = location_mask & ~from_bit;
65  // Attempt to transition the proxy to the "empty" state with
66  // cleaner_bit specifying entity responsible for its eventual freeing.
67  // Explicit cast to void* is to work around a seeming ICC 11.1 bug.
68  if ( as_atomic(task_and_tag).compare_and_swap(cleaner_bit, tat) == tat ) {
69  // Successfully grabbed the task, and left new owner with the job of freeing the proxy
70  return task_ptr(tat);
71  }
72  }
73  // Proxied task has already been claimed from another proxy location.
74  __TBB_ASSERT( task_and_tag == from_bit, "Empty proxy cannot contain non-zero task pointer" );
75  return NULL;
76  }
77 }; // struct task_proxy
78 
81 protected:
83 
85  proxy_ptr my_first;
86 
88  proxy_ptr* __TBB_atomic my_last;
89 
91  bool my_is_idle;
92 };
93 
95 
96 class mail_outbox : padded<unpadded_mail_outbox> {
97 
99  task_proxy* curr = __TBB_load_relaxed( my_first );
100  if ( !curr )
101  return NULL;
102  task_proxy **prev_ptr = &my_first;
103 #if __TBB_TASK_ISOLATION
104  if ( isolation != no_isolation ) {
105  while ( curr->prefix().isolation != isolation ) {
106  prev_ptr = &curr->next_in_mailbox;
107  curr = curr->next_in_mailbox;
108  if ( !curr )
109  return NULL;
110  }
111  }
112 #endif /* __TBB_TASK_ISOLATION */
113  __TBB_control_consistency_helper(); // on my_first
114  // There is a first item in the mailbox. See if there is a second.
115  if ( task_proxy* second = curr->next_in_mailbox ) {
116  // There are at least two items, so first item can be popped easily.
117  *prev_ptr = second;
118  } else {
119  // There is only one item. Some care is required to pop it.
120  *prev_ptr = NULL;
121  if ( as_atomic( my_last ).compare_and_swap( prev_ptr, &curr->next_in_mailbox ) == &curr->next_in_mailbox ) {
122  // Successfully transitioned mailbox from having one item to having none.
123  __TBB_ASSERT( !curr->next_in_mailbox, NULL );
124  } else {
125  // Some other thread updated my_last but has not filled in first->next_in_mailbox
126  // Wait until first item points to second item.
127  atomic_backoff backoff;
128  while ( !(second = curr->next_in_mailbox) ) backoff.pause();
129  *prev_ptr = second;
130  }
131  }
132  __TBB_ASSERT( curr, NULL );
133  return curr;
134  }
135 public:
136  friend class mail_inbox;
137 
139 
140  void push( task_proxy* t ) {
141  __TBB_ASSERT(t, NULL);
142  t->next_in_mailbox = NULL;
143  proxy_ptr * const link = (proxy_ptr *)__TBB_FetchAndStoreW(&my_last,(intptr_t)&t->next_in_mailbox);
144  // No release fence required for the next store, because there are no memory operations
145  // between the previous fully fenced atomic operation and the store.
146  __TBB_store_relaxed(*link, t);
147  }
148 
150  bool empty() {
151  return __TBB_load_relaxed(my_first) == NULL;
152  }
153 
155 
158  void construct() {
159  __TBB_ASSERT( sizeof(*this)==NFS_MaxLineSize, NULL );
160  __TBB_ASSERT( !my_first, NULL );
161  __TBB_ASSERT( !my_last, NULL );
162  __TBB_ASSERT( !my_is_idle, NULL );
163  my_last=&my_first;
165  }
166 
168  intptr_t drain() {
169  intptr_t k = 0;
170  // No fences here because other threads have already quit.
171  for( ; task_proxy* t = my_first; ++k ) {
172  my_first = t->next_in_mailbox;
174  }
175  return k;
176  }
177 
180  return my_is_idle;
181  }
182 }; // class mail_outbox
183 
185 class mail_inbox {
188 public:
190  mail_inbox() : my_putter(NULL) {}
191 
193  void attach( mail_outbox& putter ) {
194  my_putter = &putter;
195  }
197  void detach() {
198  __TBB_ASSERT(my_putter,"not attached");
199  my_putter = NULL;
200  }
203  return my_putter->internal_pop( __TBB_ISOLATION_EXPR( isolation ) );
204  }
206  bool empty() {
207  return my_putter->empty();
208  }
210 
211  void set_is_idle( bool value ) {
212  if( my_putter ) {
213  __TBB_ASSERT( my_putter->my_is_idle || value, "attempt to redundantly mark mailbox as not idle" );
214  my_putter->my_is_idle = value;
215  }
216  }
218  bool is_idle_state ( bool value ) const {
219  return !my_putter || my_putter->my_is_idle == value;
220  }
221 
222 #if DO_ITT_NOTIFY
223  void* outbox() const {return my_putter;}
225 #endif /* DO_ITT_NOTIFY */
226 }; // class mail_inbox
227 
228 } // namespace internal
229 } // namespace tbb
230 
231 #endif /* _TBB_mailbox_H */
mail_inbox()
Construct unattached inbox.
Definition: mailbox.h:190
#define __TBB_ISOLATION_EXPR(isolation)
T __TBB_load_relaxed(const volatile T &location)
Definition: tbb_machine.h:738
atomic< T > & as_atomic(T &t)
Definition: atomic.h:572
Pads type T to fill out to a multiple of cache line size.
Definition: tbb_stddef.h:261
task_proxy * internal_pop(__TBB_ISOLATION_EXPR(isolation_tag isolation))
Definition: mailbox.h:98
The graph class.
static task * task_ptr(intptr_t tat)
Returns a pointer to the encapsulated task or NULL.
Definition: mailbox.h:51
task_proxy *__TBB_atomic next_in_mailbox
Pointer to next task_proxy in a mailbox.
Definition: mailbox.h:40
bool empty()
Return true if mailbox is empty.
Definition: mailbox.h:206
Class representing where mail is put.
Definition: mailbox.h:96
static const intptr_t mailbox_bit
Definition: mailbox.h:31
static const intptr_t location_mask
Definition: mailbox.h:32
void pause()
Pause for a while.
Definition: tbb_machine.h:363
#define __TBB_atomic
Definition: tbb_stddef.h:237
T __TBB_load_with_acquire(const volatile T &location)
Definition: tbb_machine.h:712
void set_is_idle(bool value)
Indicate whether thread that reads this mailbox is idle.
Definition: mailbox.h:211
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
intptr_t isolation_tag
A tag for task isolation.
Definition: task.h:132
#define __TBB_control_consistency_helper()
Definition: gcc_generic.h:60
intptr_t drain()
Drain the mailbox.
Definition: mailbox.h:168
void attach(mail_outbox &putter)
Attach inbox to a corresponding outbox.
Definition: mailbox.h:193
bool is_idle_state(bool value) const
Indicate whether thread that reads this mailbox is idle.
Definition: mailbox.h:218
Internal representation of mail_outbox, without padding.
Definition: mailbox.h:80
bool recipient_is_idle()
True if thread that owns this mailbox is looking for work.
Definition: mailbox.h:179
proxy_ptr my_first
Pointer to first task_proxy in mailbox, or NULL if box is empty.
Definition: mailbox.h:85
void __TBB_EXPORTED_FUNC NFS_Free(void *)
Free memory allocated by NFS_Allocate.
const size_t task_prefix_reservation_size
Number of bytes reserved for a task prefix.
const size_t NFS_MaxLineSize
Compile-time constant that is upper bound on cache line/sector size.
Definition: tbb_stddef.h:216
proxy_ptr *__TBB_atomic my_last
Pointer to pointer that will point to next item in the queue. Never NULL.
Definition: mailbox.h:88
static const intptr_t pool_bit
Definition: mailbox.h:30
mail_outbox * my_putter
Corresponding sink where mail that we receive will be put.
Definition: mailbox.h:187
Tag for v3 task_proxy.
Class representing source of mail.
Definition: mailbox.h:185
void suppress_unused_warning(const T1 &)
Utility template function to prevent "unused" warnings by various compilers.
Definition: tbb_stddef.h:398
task_proxy * pop(__TBB_ISOLATION_EXPR(isolation_tag isolation))
Get next piece of mail, or NULL if mailbox is empty.
Definition: mailbox.h:202
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
void __TBB_store_relaxed(volatile T &location, V value)
Definition: tbb_machine.h:742
Class that implements exponential backoff.
Definition: tbb_machine.h:348
bool my_is_idle
Owner of mailbox is not executing a task, and has drained its own task pool.
Definition: mailbox.h:91
void detach()
Detach inbox from its outbox.
Definition: mailbox.h:197
void construct()
Construct *this as a mailbox from zeroed memory.
Definition: mailbox.h:158
static bool is_shared(intptr_t tat)
True if the proxy is stored both in its sender&#39;s pool and in the destination mailbox.
Definition: mailbox.h:46
internal::task_prefix & prefix(internal::version_tag *=NULL) const
Get reference to corresponding task_prefix.
Definition: task.h:991
bool empty()
Return true if mailbox is empty.
Definition: mailbox.h:150
task_proxy *__TBB_atomic proxy_ptr
Definition: mailbox.h:82
const isolation_tag no_isolation
Definition: task.h:133
Base class for user-defined tasks.
Definition: task.h:604
mail_outbox * outbox
Mailbox to which this was mailed.
Definition: mailbox.h:43
void push(task_proxy *t)
Push task_proxy onto the mailbox queue of another thread.
Definition: mailbox.h:140
task * extract_task()
Returns a pointer to the encapsulated task or NULL, and frees proxy if necessary. ...
Definition: mailbox.h:57

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.