[gcc/testsuite]
[official-gcc.git] / libcilkrts / runtime / local_state.h
blob9b286853d0318a5162b7c54e34060352063069f3
1 /* local_state.h -*-C++-*-
3 *************************************************************************
5 * Copyright (C) 2009-2016, Intel Corporation
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * * Neither the name of Intel Corporation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
32 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
35 * *********************************************************************
37 * PLEASE NOTE: This file is a downstream copy of a file mainitained in
38 * a repository at cilkplus.org. Changes made to this file that are not
39 * submitted through the contribution process detailed at
40 * http://www.cilkplus.org/submit-cilk-contribution will be lost the next
41 * time that a new version is released. Changes only submitted to the
42 * GNU compiler collection or posted to the git repository at
43 * https://bitbucket.org/intelcilkruntime/intel-cilk-runtime.git are
44 * not tracked.
46 * We welcome your contributions to this open source project. Thank you
47 * for your assistance in helping us improve Cilk Plus.
48 **************************************************************************/
50 /**
51 * @file local_state.h
53 * @brief The local_state structure contains additional OS-independent
54 * information that's associated with a worker, but doesn't need to be visible
55 * to the code generated by the compiler.
58 #ifndef INCLUDED_LOCAL_STATE_DOT_H
59 #define INCLUDED_LOCAL_STATE_DOT_H
61 #include <internal/abi.h>
62 #include "worker_mutex.h"
63 #include "global_state.h"
64 #include "record-replay.h"
65 #include "signal_node.h"
67 #include <setjmp.h>
68 #include <stddef.h>
69 #include <stdio.h>
72 #ifndef _WIN32
73 # include <pthread.h>
74 #endif
76 __CILKRTS_BEGIN_EXTERN_C
78 /* Opaque types. */
80 struct full_frame;
81 struct free_list;
82 struct pending_exception_info;
83 /// Opaque type for replay entry.
84 typedef struct replay_entry_t replay_entry_t;
86 /**
87 * @brief Magic numbers for local_state, used for debugging
89 typedef unsigned long long ls_magic_t;
91 /**
92 * @brief Scheduling stack function: A function that is decided on the program stack,
93 * but that must be executed on the scheduling stack.
95 typedef void (*scheduling_stack_fcn_t) (__cilkrts_worker *w,
96 struct full_frame *ff,
97 __cilkrts_stack_frame *sf);
99 /**
100 * @brief Type of this worker.
102 typedef enum cilk_worker_type
104 WORKER_FREE, ///< Unused worker - available to be bound to user threads
105 WORKER_SYSTEM, ///< Worker created by runtime - able to steal from any worker
106 WORKER_USER ///< User thread - able to steal only from team members
107 } cilk_worker_type;
111 * @brief The local_state structure contains additional OS-independent
112 * information that's associated with a worker, but doesn't need to be
113 * visible to the compiler.
115 * No compiler-generated code should need to know the layout of this
116 * structure.
118 * The fields of this struct can be classified as either local or
119 * shared.
121 * Local: This field is only accessed by the thread bound to this
122 * worker struct. Local fields can be freely accessed without
123 * acquiring locks.
125 * Shared: This field may be accessed by multiple worker threads.
126 * Accesses to shared fields usually requires locks, except in
127 * special situations where one can prove that locks are
128 * unnecessary.
130 * The fields of this can also be classified as "read-only" if the
131 * field does not change after it is initialized. Otherwise, the
132 * field is "read/write". Read-only fields do not require locks to
133 * access (ignoring the synchronization that might be needed for
134 * initialization if this can occur in parallel).
136 * Finally, we explicitly classify some fields as "synchronization"
137 * fields if they are used as part of a synchronization protocol in
138 * the runtime. These variables are generally shared and read/write.
139 * Mostly, this category includes lock variables and other variables
140 * that are involved in synchronization protocols (i.e., the THE
141 * protocol).
143 struct local_state /* COMMON_PORTABLE */
145 /** This value should be in the first field in any local_state */
146 # define WORKER_MAGIC_0 ((ls_magic_t)0xe0831a4a940c60b8ULL)
149 * Should be WORKER_MAGIC_0 or the local_state has been corrupted
150 * This magic field is shared because it is read on lock acquisitions.
152 * [shared read-only]
154 ls_magic_t worker_magic_0;
157 * Mutex used to serialize access to the local_state
158 * Synchronization field. [shared read/write]
160 struct mutex lock;
163 * Flag that indicates that the worker is interested in grabbing
164 * LOCK, and thus thieves should leave the worker alone.
165 * Written only by self, may be read by others.
167 * Synchronization field. [shared read/write]
169 int do_not_steal;
172 * Lock that all thieves grab in order to compete for the right
173 * to disturb this worker.
175 * Synchronization field. [shared read/write]
177 struct mutex steal_lock;
180 * Full frame that the worker is working on.
182 * While a worker w is executing, a thief may change
183 * w->l->frame_ff (on a successful steal) after acquiring w's
184 * lock.
186 * Unlocked accesses to w->l->frame_ff are safe (by w itself) when
187 * w's deque is empty, or when stealing from w has been disabled.
189 * [shared read/write]
191 struct full_frame *frame_ff;
194 * Full frame that the worker will be working on next
196 * This field is normally local for a worker w. Another worker v
197 * may modify w->l->next_frame_ff, however, in the special case
198 * when v is returning a frame to a user thread w since w is the
199 * team leader.
201 * [shared read/write]
203 struct full_frame *next_frame_ff;
206 * This is set iff this is a WORKER_USER and there has been a steal. It
207 * points to the first frame that was stolen since the team was last fully
208 * sync'd. Only this worker may continue past a sync in this function.
210 * This field is set by a thief for a victim that is a user
211 * thread, while holding the victim's lock.
212 * It can be cleared without a lock by the worker that will
213 * continue exuecting past the sync.
215 * [shared read/write]
217 struct full_frame *last_full_frame;
220 * Team on which this worker is a participant. When a user worker enters,
221 * its team is its own worker struct and it can never change teams. When a
222 * system worker steals, it adopts the team of its victim.
224 * When a system worker w steals, it reads victim->l->team and
225 * joins this team. w->l->team is constant until the next time w
226 * returns control to the runtime.
227 * We must acquire the worker lock to change w->l->team.
229 * @note This field is 64-byte aligned because it is the first in
230 * the group of shared read-only fields. We want this group to
231 * fall on a different cache line from the previous group, which
232 * is shared read-write.
234 * [shared read-only]
236 __attribute__((aligned(64)))
237 __cilkrts_worker *team;
240 * Type of this worker
242 * This field changes only when a worker binds or unbinds.
243 * Otherwise, the field is read-only while the worker is bound.
245 * [shared read-only]
247 cilk_worker_type type;
250 * Lazy task queue of this worker - an array of pointers to stack frames.
252 * Read-only because deques are a fixed size in the current
253 * implementation.
255 * @note This field is 64-byte aligned because it is the first in
256 * the group of local fields. We want this group to fall on a
257 * different cache line from the previous group, which is shared
258 * read-only.
260 * [local read-only]
262 __attribute__((aligned(64)))
263 __cilkrts_stack_frame **ltq;
266 * Pool of fibers waiting to be reused.
267 * [local read/write]
269 cilk_fiber_pool fiber_pool;
272 * The fiber for the scheduling stacks.
273 * [local read/write]
275 cilk_fiber* scheduling_fiber;
278 * Saved pointer to the leaf node in thread-local storage, when a
279 * user thread is imported. This pointer gets set to a
280 * meaningful value when binding a user thread, and cleared on
281 * unbind.
283 * [local read/write]
285 __cilkrts_pedigree* original_pedigree_leaf;
288 * State of the random number generator
290 * [local read/write]
292 unsigned rand_seed;
295 * Function to execute after transferring onto the scheduling stack.
297 * [local read/write]
299 scheduling_stack_fcn_t post_suspend;
302 * __cilkrts_stack_frame we suspended when we transferred onto the
303 * scheduling stack.
305 * [local read/write]
307 __cilkrts_stack_frame *suspended_stack;
310 * cilk_fiber that should be freed after returning from a
311 * spawn with a stolen parent or after stalling at a sync.
313 * We calculate the stack to free when executing a reduction on
314 * the user stack, but we can not actually release the stack
315 * until control longjmps onto a runtime scheduling stack.
317 * This field is used to pass information to the runtime across
318 * the longjmp onto the scheduling stack.
320 * [local read/write]
322 cilk_fiber* fiber_to_free;
325 * Saved exception object for an exception that is being passed to
326 * our parent
328 * [local read/write]
330 struct pending_exception_info *pending_exception;
333 * Buckets for the memory allocator
335 * [local read/write]
337 struct free_list *free_list[FRAME_MALLOC_NBUCKETS];
340 * Potential function for the memory allocator
342 * [local read/write]
344 size_t bucket_potential[FRAME_MALLOC_NBUCKETS];
347 * Support for statistics
349 * Useful only when CILK_PROFIlE is compiled in.
350 * [local read/write]
352 statistics* stats;
355 * Count indicates number of failures since last successful steal. This is
356 * used by the scheduler to reduce contention on shared flags.
358 * [local read/write]
360 unsigned int steal_failure_count;
363 * Record indicating that the worker stole work at least once.
365 * [local read/write]
367 int has_stolen;
370 * 1 if work was stolen from another worker. When true, this will flag
371 * setup_for_execution_pedigree to increment the pedigree when we resume
372 * execution to match the increment that would have been done on a return
373 * from a spawn helper.
375 * [local read/write]
377 int work_stolen;
380 * File pointer for record or replay
381 * Does FILE * work on Windows?
382 * During record, the file will be opened in write-only mode.
383 * During replay, the file will be opened in read-only mode.
385 * [local read/write]
387 FILE *record_replay_fptr;
390 * Root of array of replay entries - NULL if we're not replaying a log
392 * [local read/write]
394 replay_entry_t *replay_list_root;
397 * Current replay entry - NULL if we're not replaying a log
399 * [local read/write]
401 replay_entry_t *replay_list_entry;
404 * Separate the signal_node from other things in the local_state by the
405 * sizeof a cache line for performance reasons.
407 * unused
409 char buf[64];
412 * Signal object for waking/sleeping the worker. This should be a pointer
413 * to avoid the possibility of caching problems.
415 * [shared read-only]
417 signal_node_t *signal_node;
419 /** This value should be in the last field in any local_state */
420 # define WORKER_MAGIC_1 ((ls_magic_t)0x16164afb0ea0dff9ULL)
423 * Should be WORKER_MAGIC_1 or the local_state has been corrupted
424 * This magic field is shared because it is read on lock acquisitions.
425 * [shared read-only]
427 ls_magic_t worker_magic_1;
431 * Perform cleanup according to the function set before the longjmp().
433 * Call this after longjmp() has completed and the worker is back on a
434 * scheduling stack.
436 * @param w __cilkrts_worker currently executing.
438 void run_scheduling_stack_fcn(__cilkrts_worker *w);
440 __CILKRTS_END_EXTERN_C
442 #endif // ! defined(INCLUDED_LOCAL_STATE_DOT_H)