Home ⌂Doc Index ◂Up ▴
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-2020 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 
86 
89 
91  tbb::atomic<int> my_task_count;
92 
94  bool my_is_idle;
95 };
96 
98 
99 class mail_outbox : padded<unpadded_mail_outbox> {
100  static const int mailbox_task_limit = 32;
101 
104  if ( !curr )
105  return NULL;
106  task_proxy **prev_ptr = &my_first;
107 #if __TBB_TASK_ISOLATION
108  if ( isolation != no_isolation ) {
109  while ( curr->prefix().isolation != isolation ) {
110  prev_ptr = &curr->next_in_mailbox;
111  curr = curr->next_in_mailbox;
112  if ( !curr )
113  return NULL;
114  }
115  }
116 #endif /* __TBB_TASK_ISOLATION */
117  __TBB_control_consistency_helper(); // on my_first
118  // There is a first item in the mailbox. See if there is a second.
119  if ( task_proxy* second = curr->next_in_mailbox ) {
120  // There are at least two items, so first item can be popped easily.
121  *prev_ptr = second;
122  } else {
123  // There is only one item. Some care is required to pop it.
124  *prev_ptr = NULL;
125  if ( as_atomic( my_last ).compare_and_swap( prev_ptr, &curr->next_in_mailbox ) == &curr->next_in_mailbox ) {
126  // Successfully transitioned mailbox from having one item to having none.
127  __TBB_ASSERT( !curr->next_in_mailbox, NULL );
128  } else {
129  // Some other thread updated my_last but has not filled in first->next_in_mailbox
130  // Wait until first item points to second item.
131  atomic_backoff backoff;
132  while ( !(second = curr->next_in_mailbox) ) backoff.pause();
133  *prev_ptr = second;
134  }
135  }
136  --my_task_count;
137  __TBB_ASSERT( my_task_count >= 0, NULL );
138  __TBB_ASSERT( curr, NULL );
139  return curr;
140  }
141 public:
142  friend class mail_inbox;
143 
145 
147  bool push( task_proxy* t ) {
149  return false;
150  ++my_task_count;
151  __TBB_ASSERT(t, NULL);
152  t->next_in_mailbox = NULL;
153  proxy_ptr * const link = (proxy_ptr *)__TBB_FetchAndStoreW(&my_last,(intptr_t)&t->next_in_mailbox);
154  // No release fence required for the next store, because there are no memory operations
155  // between the previous fully fenced atomic operation and the store.
156  __TBB_store_relaxed(*link, t);
157  return true;
158  }
159 
161  bool empty() {
162  return __TBB_load_relaxed(my_first) == NULL;
163  }
164 
166 
169  void construct() {
170  __TBB_ASSERT( sizeof(*this)==NFS_MaxLineSize, NULL );
171  __TBB_ASSERT( !my_first, NULL );
172  __TBB_ASSERT( !my_last, NULL );
173  __TBB_ASSERT( !my_is_idle, NULL );
174  my_last=&my_first;
176  }
177 
179  intptr_t drain() {
180  intptr_t k = 0;
181  // No fences here because other threads have already quit.
182  for( ; task_proxy* t = my_first; ++k ) {
185  }
186  return k;
187  }
188 
191  return my_is_idle;
192  }
193 }; // class mail_outbox
194 
196 class mail_inbox {
199 public:
201  mail_inbox() : my_putter(NULL) {}
202 
204  void attach( mail_outbox& putter ) {
205  my_putter = &putter;
206  }
208  void detach() {
209  __TBB_ASSERT(my_putter,"not attached");
210  my_putter = NULL;
211  }
214  return my_putter->internal_pop( __TBB_ISOLATION_EXPR( isolation ) );
215  }
217  bool empty() {
218  return my_putter->empty();
219  }
221 
222  void set_is_idle( bool value ) {
223  if( my_putter ) {
224  __TBB_ASSERT( my_putter->my_is_idle || value, "attempt to redundantly mark mailbox as not idle" );
226  }
227  }
229  bool is_idle_state ( bool value ) const {
230  return !my_putter || my_putter->my_is_idle == value;
231  }
232 
233 #if DO_ITT_NOTIFY
234  void* outbox() const {return my_putter;}
236 #endif /* DO_ITT_NOTIFY */
237 }; // class mail_inbox
238 
239 } // namespace internal
240 } // namespace tbb
241 
242 #endif /* _TBB_mailbox_H */
task_proxy *__TBB_atomic next_in_mailbox
Pointer to next task_proxy in a mailbox.
Definition: mailbox.h:40
const isolation_tag no_isolation
Definition: task.h:144
void set_is_idle(bool value)
Indicate whether thread that reads this mailbox is idle.
Definition: mailbox.h:222
const size_t task_prefix_reservation_size
Number of bytes reserved for a task prefix.
intptr_t drain()
Drain the mailbox.
Definition: mailbox.h:179
tbb::atomic< int > my_task_count
Approximate number of tasks in mailbox to prevent an unlimited grow when the owner is not available.
Definition: mailbox.h:91
#define __TBB_ISOLATION_EXPR(isolation)
bool push(task_proxy *t)
Push task_proxy onto the mailbox queue of another thread.
Definition: mailbox.h:147
proxy_ptr *__TBB_atomic my_last
Pointer to pointer that will point to next item in the queue. Never NULL.
Definition: mailbox.h:88
internal::task_prefix & prefix(internal::version_tag *=NULL) const
Get reference to corresponding task_prefix.
Definition: task.h:1002
Base class for user-defined tasks.
Definition: task.h:615
bool is_idle_state(bool value) const
Indicate whether thread that reads this mailbox is idle.
Definition: mailbox.h:229
task_proxy * pop(__TBB_ISOLATION_EXPR(isolation_tag isolation))
Get next piece of mail, or NULL if mailbox is empty.
Definition: mailbox.h:213
mail_outbox * outbox
Mailbox to which this was mailed.
Definition: mailbox.h:43
task_proxy *__TBB_atomic proxy_ptr
Definition: mailbox.h:82
Internal representation of mail_outbox, without padding.
Definition: mailbox.h:80
void pause()
Pause for a while.
Definition: tbb_machine.h:360
void __TBB_store_relaxed(volatile T &location, V value)
Definition: tbb_machine.h:739
Class representing source of mail.
Definition: mailbox.h:196
static const intptr_t pool_bit
Definition: mailbox.h:30
void suppress_unused_warning(const T1 &)
Utility template function to prevent "unused" warnings by various compilers.
Definition: tbb_stddef.h:398
bool empty()
Return true if mailbox is empty.
Definition: mailbox.h:161
mail_inbox()
Construct unattached inbox.
Definition: mailbox.h:201
bool recipient_is_idle()
True if thread that owns this mailbox is looking for work.
Definition: mailbox.h:190
#define __TBB_atomic
Definition: tbb_stddef.h:237
intptr_t isolation_tag
A tag for task isolation.
Definition: task.h:143
proxy_ptr my_first
Pointer to first task_proxy in mailbox, or NULL if box is empty.
Definition: mailbox.h:85
Pads type T to fill out to a multiple of cache line size.
Definition: tbb_stddef.h:261
#define __TBB_control_consistency_helper()
Definition: gcc_generic.h:60
static const intptr_t mailbox_bit
Definition: mailbox.h:31
Class representing where mail is put.
Definition: mailbox.h:99
static const int mailbox_task_limit
Definition: mailbox.h:100
void __TBB_EXPORTED_FUNC NFS_Free(void *)
Free memory allocated by NFS_Allocate.
static const intptr_t location_mask
Definition: mailbox.h:32
const size_t NFS_MaxLineSize
Compile-time constant that is upper bound on cache line/sector size.
Definition: tbb_stddef.h:216
task * extract_task()
Returns a pointer to the encapsulated task or NULL, and frees proxy if necessary.
Definition: mailbox.h:57
Class that implements exponential backoff.
Definition: tbb_machine.h:345
void attach(mail_outbox &putter)
Attach inbox to a corresponding outbox.
Definition: mailbox.h:204
bool my_is_idle
Owner of mailbox is not executing a task, and has drained its own task pool.
Definition: mailbox.h:94
void detach()
Detach inbox from its outbox.
Definition: mailbox.h:208
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
static task * task_ptr(intptr_t tat)
Returns a pointer to the encapsulated task or NULL.
Definition: mailbox.h:51
mail_outbox * my_putter
Corresponding sink where mail that we receive will be put.
Definition: mailbox.h:198
T __TBB_load_relaxed(const volatile T &location)
Definition: tbb_machine.h:735
atomic< T > & as_atomic(T &t)
Definition: atomic.h:572
bool empty()
Return true if mailbox is empty.
Definition: mailbox.h:217
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
Tag for v3 task_proxy.
The graph class.
T __TBB_load_with_acquire(const volatile T &location)
Definition: tbb_machine.h:709
void construct()
Construct *this as a mailbox from zeroed memory.
Definition: mailbox.h:169
task_proxy * internal_pop(__TBB_ISOLATION_EXPR(isolation_tag isolation))
Definition: mailbox.h:102
static bool is_shared(intptr_t tat)
True if the proxy is stored both in its sender's pool and in the destination mailbox.
Definition: mailbox.h:46

Copyright © 2005-2020 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.