Fix expected messages in test
[official-gcc.git] / libcilkrts / runtime / cilk_fiber.h
blob2671f924681af9fd0b32d61bc670fecbc933eb48
1 /* cilk_fiber.h -*-C++-*-
3 *************************************************************************
5 * @copyright
6 * Copyright (C) 2012-2013, Intel Corporation
7 * All rights reserved.
8 *
9 * @copyright
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 * * Neither the name of Intel Corporation nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * @copyright
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
32 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
33 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
35 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 **************************************************************************/
39 /**
40 * @file cilk_fiber.h
42 * @brief Abstraction of a "fiber": A coprocess-like stack and auxiliary data
45 #ifndef INCLUDED_CILK_FIBER_DOT_H
46 #define INCLUDED_CILK_FIBER_DOT_H
48 #include <cilk/common.h>
49 #ifdef __cplusplus
50 # include <cstddef>
51 #else
52 # include <stddef.h>
53 #endif
55 #include "bug.h"
56 #include "cilk-tbb-interop.h"
57 #include "spin_mutex.h"
58 #include "internal/abi.h" // Define __cilkrts_stack_frame
60 /**
61 * @brief Debugging level for Cilk fiber code.
63 * A value of 0 means no debugging.
64 * Higher values generate more debugging output.
66 #define FIBER_DEBUG 0
68 /**
69 * @brief Flag for validating reference counts.
71 * Set to 1 to assert that fiber reference counts are reasonable.
73 #define FIBER_CHECK_REF_COUNTS 1
75 /**
76 * @brief Flag to determine whether fibers support reference counting.
77 * We require reference counting only on Windows, for exception
78 * processing. Unix does not need reference counting.
80 #if defined(_WIN32)
81 # define NEED_FIBER_REF_COUNTS 1
82 #endif
84 /**
85 * @brief Flag to enable support for the
86 * cilk_fiber_get_current_fiber() method.
88 * I'd like this flag to be 0. However, the cilk_fiber test depends
89 * on being able to call this method.
91 #if !defined(SUPPORT_GET_CURRENT_FIBER)
92 # define SUPPORT_GET_CURRENT_FIBER 0
93 #endif
95 /**
96 * @brief Switch for enabling "fast path" check for fibers, which
97 * doesn't go to the heap or OS until checking the ancestors first.
99 * Doing this check seems to make the stress test in
100 * cilk_fiber_pool.t.cpp run faster. But it doesn't seem to make much
101 * difference in other benchmarks, so it is disabled by default.
103 #define USE_FIBER_TRY_ALLOCATE_FROM_POOL 0
106 __CILKRTS_BEGIN_EXTERN_C
108 /// @brief Forward reference to fiber pool.
109 typedef struct cilk_fiber_pool cilk_fiber_pool;
111 /** @brief Opaque data structure representing a fiber */
112 typedef struct cilk_fiber cilk_fiber;
114 /** @brief Function pointer type for use as a fiber's "main" procedure */
115 typedef void (*cilk_fiber_proc)(cilk_fiber*);
117 /** @brief Data structure associated with each fiber. */
118 typedef struct cilk_fiber_data
120 __STDNS size_t stack_size; /**< Size of stack for fiber */
121 __cilkrts_worker* owner; /**< Worker using this fiber */
122 __cilkrts_stack_frame* resume_sf; /**< Stack frame to resume */
123 __cilk_tbb_pfn_stack_op stack_op_routine; /**< Cilk/TBB interop callback */
124 void* stack_op_data; /**< Data for Cilk/TBB callback */
125 void* client_data; /**< Data managed by client */
127 #ifdef _WIN32
128 char *initial_sp; /**< Initalized in fiber_stub */
129 # ifdef _WIN64
130 char *steal_frame_sp; /**< RSP for frame stealing work */
131 // Needed for exception handling so we can
132 // identify when about to unwind off stack
133 # endif
134 #endif
136 } cilk_fiber_data;
138 /** @brief Pool of cilk_fiber for fiber reuse
140 * Pools form a hierarchy, with each pool pointing to its parent. When the
141 * pool undeflows, it gets a fiber from its parent. When a pool overflows,
142 * it returns some fibers to its parent. If the root pool underflows, it
143 * allocates and initializes a new fiber from the heap but only if the total
144 * is less than max_size; otherwise, fiber creation fails.
146 struct cilk_fiber_pool
148 spin_mutex* lock; ///< Mutual exclusion for pool operations
149 __STDNS size_t stack_size; ///< Size of stacks for fibers in this pool.
150 cilk_fiber_pool* parent; ///< @brief Parent pool.
151 ///< If this pool is empty, get from parent
153 // Describes inactive fibers stored in the pool.
154 cilk_fiber** fibers; ///< Array of max_size fiber pointers
155 unsigned max_size; ///< Limit on number of fibers in pool
156 unsigned size; ///< Number of fibers currently in the pool
158 // Statistics on active fibers that were allocated from this pool,
159 // but no longer in the pool.
160 int total; ///< @brief Fibers allocated - fiber deallocated from pool
161 ///< total may be negative for non-root pools.
162 int high_water; ///< High water mark of total fibers
163 int alloc_max; ///< Limit on number of fibers allocated from the heap/OS
166 /** @brief Initializes a cilk_fiber_pool structure
168 * @param pool - The address of the pool that is to be initialized
169 * @param parent - The address of this pool's parent, or NULL for root pool
170 * @param stack_size - Size of stacks for fibers allocated from this pool.
171 * @param buffer_size - The maximum number of fibers that may be pooled.
172 * @param alloc_max - Limit on # of fibers this pool can allocate from the heap.
173 * @param is_shared - True if accessing this pool needs a lock, false otherwise.
175 void cilk_fiber_pool_init(cilk_fiber_pool* pool,
176 cilk_fiber_pool* parent,
177 size_t stack_size,
178 unsigned buffer_size,
179 int alloc_max,
180 int is_shared);
182 /** @brief Sets the maximum number of fibers to allocate from a root pool.
184 * @param root_pool - A root fiber pool
185 * @param max_fibers_to_allocate - The limit on # of fibers to allocate.
187 * Sets the maximum number of fibers that can be allocated from this
188 * pool and all its descendants. This pool must be a root pool.
190 void cilk_fiber_pool_set_fiber_limit(cilk_fiber_pool* root_pool,
191 unsigned max_fibers_to_allocate);
193 /** @brief De-initalizes a cilk_fiber_pool
195 * @param pool - The address of the pool that is to be destroyed
197 void cilk_fiber_pool_destroy(cilk_fiber_pool* pool);
199 /** @brief Allocates a new cilk_fiber.
201 * If the specified pool is empty, this method may choose to either
202 * allocate a fiber from the heap (if pool->total < pool->alloc_max),
203 * or retrieve a fiber from the parent pool.
205 * @note If a non-null fiber is returned, @c cilk_fiber_reset_state
206 * should be called on this fiber before using it.
208 * An allocated fiber begins with a reference count of 1.
209 * This method may lock @c pool or one of its ancestors.
211 * @pre pool should not be NULL.
213 * @param pool The fiber pool from which to retrieve a fiber.
214 * @return An allocated fiber, or NULL if failed to allocate.
216 cilk_fiber* cilk_fiber_allocate(cilk_fiber_pool* pool);
218 /** @brief Allocate and initialize a new cilk_fiber using memory from
219 * the heap and/or OS.
221 * The allocated fiber begins with a reference count of 1.
223 * @param stack_size The size (in bytes) to be allocated for the fiber's
224 * stack.
225 * @return An initialized fiber. This method should not return NULL
226 * unless some exceptional condition has occurred.
228 cilk_fiber* cilk_fiber_allocate_from_heap(size_t stack_size);
231 /** @brief Resets an fiber object just allocated from a pool with the
232 * specified proc.
234 * After this call, cilk_fiber_data object associated with this fiber
235 * is filled with zeros.
237 * This function can be called only on a fiber that has been allocated
238 * from a pool, but never used.
240 * @param fiber The fiber to reset and initialize.
241 * @param start_proc The function to run when switching to the fiber. If
242 * null, the fiber can be used with cilk_fiber_run_proc()
243 * but not with cilk_fiber_resume().
245 void cilk_fiber_reset_state(cilk_fiber* fiber,
246 cilk_fiber_proc start_proc);
248 /** @brief Remove a reference from this fiber, possibly deallocating it.
250 * This fiber is deallocated only when there are no other references
251 * to it. Deallocation happens either by returning the fiber to the
252 * specified pool, or returning it to the heap.
254 * A fiber that is currently executing should not remove the last
255 * reference to itself.
257 * When a fiber is deallocated, destructors are not called for the
258 * objects (if any) still on its stack. The fiber's stack and fiber
259 * data is returned to the stack pool but the client fiber data is not
260 * deallocated.
262 * If the pool overflows because of a deallocation, then some fibers
263 * will be returned to the parent pool. If the root pool overflows,
264 * then the fiber is returned to the heap.
266 * @param fiber The Cilk fiber to remove a reference to.
267 * @param pool The fiber pool to which the fiber should be returned. The
268 * caller is assumed to have exclusive access to the pool
269 * either because there is no contention for it or because
270 * its lock has been acquired. If pool is NULL, any
271 * deallocated fiber is destroyed and returned to the
272 * heap.
274 * @return Final reference count. If the count is 0, the fiber was
275 * returned to a pool or the heap.
277 int cilk_fiber_remove_reference(cilk_fiber *fiber, cilk_fiber_pool *pool);
279 /** @brief Allocates and intializes this thread's main fiber
281 * Each thread has an "implicit" main fiber that control's the
282 * thread's initial stack. This function makes this fiber visible to
283 * the client and allocates the Cilk-specific aspects of the implicit
284 * fiber. A call to this function must be paired with a call to
285 * cilk_fiber_deallocate_fiber_from_thread()
286 * or a memory leak (or worse) will result.
288 * A fiber allocated from a thread begins with a reference count of 2.
289 * One is for being allocated, and one is for being active.
290 * (A fiber created from a thread is automatically currently executing.)
291 * The matching calls above each decrement the reference count by 1.
293 * @return A fiber for the currently executing thread.
295 cilk_fiber* cilk_fiber_allocate_from_thread(void);
297 /** @brief Remove a fiber created from a thread,
298 * possibly deallocating it.
300 * Same as cilk_fiber_remove_reference, except that it works on fibers
301 * created via cilk_fiber_allocate_from_thread().
303 * Fibers created from a thread are never returned to a pool.
305 * @param fiber The Cilk fiber to remove a reference from.
306 * @return Final reference count. If the count is 0, the fiber was
307 * returned to the heap.
309 int cilk_fiber_remove_reference_from_thread(cilk_fiber *fiber);
311 /** @brief Deallocate a fiber created from a thread,
312 * possibly destroying it.
314 * This method decrements the reference count of the fiber by 2, and
315 * destroys the fiber struct if the reference count is 0.
317 * OS-specific cleanup for the fiber executes unconditionally with
318 * this method. The destruction of the actual object, however, does
319 * not occur unless the reference count is 0.
321 * @param fiber The cilk_fiber to deallocate from a thread.
322 * @return Final reference count. If the count is 0, the fiber was
323 * returned to the heap.
325 int cilk_fiber_deallocate_from_thread(cilk_fiber *fiber);
327 /** @brief Returns true if this fiber is allocated from a thread.
329 int cilk_fiber_is_allocated_from_thread(cilk_fiber *fiber);
332 /** @brief Suspend execution on current fiber resumes other fiber.
334 * Suspends the current fiber and transfers control to a new fiber. Execution
335 * on the new fiber resumes from the point at which fiber suspended itself to
336 * run a different fiber. If fiber was freshly allocated, then runs the
337 * start_proc function specified at allocation. This function returns when
338 * another fiber resumes the self fiber. Note that the state of the
339 * floating-point control register (i.e., the register that controls rounding
340 * mode, etc.) is valid but indeterminate on return -- different
341 * implementations will have different results.
343 * When the @c self fiber is resumed, execution proceeds as though
344 * this function call returns.
346 * This operation increments the reference count of @p other.
347 * This operation decrements the reference count of @p self.
349 * @param self Fiber to switch from. Must equal current fiber.
350 * @param other Fiber to switch to.
352 void cilk_fiber_suspend_self_and_resume_other(cilk_fiber* self,
353 cilk_fiber* other);
355 /** @brief Removes a reference from the currently executing fiber and
356 * resumes other fiber.
358 * Removes a reference from @p self and transfer control to @p other
359 * fiber. Execution on @p other resumes from the point at which @p
360 * other suspended itself to run a different fiber. If @p other fiber
361 * was freshly allocated, then runs the function specified at
362 * creation.
365 * This operation increments the reference count of @p other.
367 * This operation conceptually decrements the reference count of
368 * @p self twice, once to suspend it, and once to remove a reference to
369 * it. Then, if the count is 0, it is returned to the specified pool
370 * or destroyed.
372 * @pre @p self is the currently executing fiber.
374 * @param self Fiber to remove reference switch from.
375 * @param self_pool Pool to which the current fiber should be returned
376 * @param other Fiber to switch to.
378 NORETURN
379 cilk_fiber_remove_reference_from_self_and_resume_other(cilk_fiber* self,
380 cilk_fiber_pool* self_pool,
381 cilk_fiber* other);
383 /** @brief Set the proc method to execute immediately after a switch
384 * to this fiber.
386 * The @c post_switch_proc method executes immediately after switching
387 * away form @p self fiber to some other fiber, but before @c self
388 * gets cleaned up.
390 * @note A fiber can have only one post_switch_proc method at a time.
391 * If this method is called multiple times before switching to the
392 * fiber, only the last proc method will execute.
394 * @param self Fiber.
395 * @param post_switch_proc Proc method to execute immediately after switching to this fiber.
397 void cilk_fiber_set_post_switch_proc(cilk_fiber* self, cilk_fiber_proc post_switch_proc);
399 /** @brief Invoke TBB stack op for this fiber.
401 * @param fiber Fiber to invoke stack op for.
402 * @param op The stack op to invoke
404 void cilk_fiber_invoke_tbb_stack_op(cilk_fiber* fiber, __cilk_tbb_stack_op op);
406 /** @brief Returns the fiber data associated with the specified fiber.
408 * The returned struct is owned by the fiber and is deallocated automatically
409 * when the fiber is destroyed. However, the client_data field is owned by
410 * the client and must be deallocated separately. When called for a
411 * newly-allocated fiber, the returned data is zero-filled.
413 * @param fiber The fiber for which data is being requested.
414 * @return The fiber data for the specified fiber
416 cilk_fiber_data* cilk_fiber_get_data(cilk_fiber* fiber);
418 /** @brief Retrieve the owner field from the fiber.
420 * This method is provided for convenience. One can also get the
421 * fiber data, and then get the owner field.
423 __CILKRTS_INLINE
424 __cilkrts_worker* cilk_fiber_get_owner(cilk_fiber* fiber)
426 // TBD: We really want a static assert here, that this cast is
427 // doing the right thing.
428 cilk_fiber_data* fdata = (cilk_fiber_data*)fiber;
429 return fdata->owner;
432 /** @brief Sets the owner field of a fiber.
434 * This method is provided for convenience. One can also get the
435 * fiber data, and then get the owner field.
437 __CILKRTS_INLINE
438 void cilk_fiber_set_owner(cilk_fiber* fiber, __cilkrts_worker* owner)
440 // TBD: We really want a static assert here, that this cast is
441 // doing the right thing.
442 cilk_fiber_data* fdata = (cilk_fiber_data*)fiber;
443 fdata->owner = owner;
446 /** @brief Returns true if this fiber is resumable.
448 * A fiber is considered resumable when it is not currently being
449 * executed.
451 * This function is used by Windows exception code.
452 * @param fiber The fiber to check.
453 * @return Nonzero value if fiber is resumable.
455 int cilk_fiber_is_resumable(cilk_fiber* fiber);
458 * @brief Returns the base of this fiber's stack.
460 * On some platforms (e.g., Windows), the fiber must have started
461 * running before we can get this information.
463 * @param fiber The fiber to get the stack pointer from.
464 * @return The base of the stack, or NULL if this
465 * information is not available yet.
467 char* cilk_fiber_get_stack_base(cilk_fiber* fiber);
470 /****************************************************************************
471 * TBB interop functions
472 * **************************************************************************/
474 * @brief Set the TBB callback information for a stack
476 * @param fiber The fiber to set the TBB callback information for
477 * @param o The TBB callback thunk. Specifies the callback address and
478 * context value.
480 void cilk_fiber_set_stack_op(cilk_fiber *fiber,
481 __cilk_tbb_stack_op_thunk o);
484 * @brief Save the TBB callback address and context value in
485 * thread-local storage.
487 * We'll use it later when the thread binds to a worker.
489 * @param o The TBB callback thunk which is to be saved.
491 void cilk_fiber_tbb_interop_save_stack_op_info(__cilk_tbb_stack_op_thunk o);
494 * @brief Move TBB stack-op info from thread-local storage and store
495 * it into the fiber.
497 * Called when we bind a thread to the runtime. If there is any TBB
498 * interop information in thread-local storage, bind it to the stack
499 * now.
501 * @pre \c fiber should not be NULL.
502 * @param fiber The fiber that should take over the TBB interop information.
504 void cilk_fiber_tbb_interop_use_saved_stack_op_info(cilk_fiber *fiber);
507 * @brief Free any TBB interop information saved in thread-local storage
509 void cilk_fiber_tbb_interop_free_stack_op_info(void);
512 * @brief Migrate any TBB interop information from a cilk_fiber to
513 * thread-local storage.
515 * Returns immediately if no TBB interop information has been
516 * associated with the stack.
518 * @param fiber The cilk_fiber who's TBB interop information should be
519 * saved in thread-local storage.
521 void cilk_fiber_tbb_interop_save_info_from_stack(cilk_fiber* fiber);
524 #if SUPPORT_GET_CURRENT_FIBER
525 /** @brief Returns the fiber associated with the currently executing thread
527 * @note This function is currently used only for testing the Cilk
528 * runtime.
530 * @return Fiber associated with the currently executing thread or NULL if no
531 * fiber was associated with this thread.
533 cilk_fiber* cilk_fiber_get_current_fiber(void);
534 #endif
537 #if NEED_FIBER_REF_COUNTS
538 /** @brief Returns true if this fiber has reference count > 0.
540 * @param fiber The fiber to check for references.
541 * @return Nonzero value if the fiber has references.
543 int cilk_fiber_has_references(cilk_fiber *fiber);
545 /** @brief Returns the value of the reference count.
547 * @param fiber The fiber to check for references.
548 * @return The value of the reference count of fiber.
550 int cilk_fiber_get_ref_count(cilk_fiber *fiber);
552 /** @brief Adds a reference to this fiber.
554 * Increments the reference count of a current fiber. Fibers with
555 * nonzero reference count will not be freed or returned to a fiber
556 * pool.
558 * @param fiber The fiber to add a reference to.
560 void cilk_fiber_add_reference(cilk_fiber *fiber);
562 #endif // NEED_FIBER_REF_COUNTS
564 __CILKRTS_END_EXTERN_C
566 #ifdef __cplusplus
567 // Some C++ implementation details
569 /// Opaque declaration of a cilk_fiber_sysdep object.
570 struct cilk_fiber_sysdep;
573 * cilk_fiber is a base-class for system-dependent fiber implementations.
575 struct cilk_fiber : protected cilk_fiber_data
577 protected:
578 // This is a rare acceptable use of protected inheritence and protected
579 // variable access: when the base class and derived class collaborate
580 // tightly to comprise a single component.
582 /// For overloading constructor of cilk_fiber.
583 enum from_thread_t { from_thread = 1 };
585 // Boolean flags capturing the status of the fiber.
586 // Each one can be set independently.
587 // A default fiber is constructed with a flag value of 0.
588 static const int RESUMABLE = 0x01; ///< True if the fiber is in a suspended state and can be resumed.
589 static const int ALLOCATED_FROM_THREAD = 0x02; ///< True if fiber was allocated from a thread.
591 cilk_fiber_proc m_start_proc; ///< Function to run on start up/reset
592 cilk_fiber_proc m_post_switch_proc; ///< Function that executes when we first switch to a new fiber from a different one.
594 cilk_fiber* m_pending_remove_ref;///< Fiber to possibly delete on start up or resume
595 cilk_fiber_pool* m_pending_pool; ///< Pool where m_pending_remove_ref should go if it is deleted.
596 unsigned m_flags; ///< Captures the status of this fiber.
598 #if NEED_FIBER_REF_COUNTS
599 volatile long m_outstanding_references; ///< Counts references to this fiber.
600 #endif
602 /// Creates a fiber with NULL data.
603 cilk_fiber();
606 * @brief Creates a fiber with user-specified arguments.
608 * @param stack_size Size of stack to use for this fiber.
610 cilk_fiber(std::size_t stack_size);
612 /// Empty destructor.
613 ~cilk_fiber();
616 * @brief Performs any actions that happen after switching from
617 * one fiber to another.
619 * These actions are:
620 * 1. Execute m_post_switch_proc on a fiber.
621 * 2. Do any pending deallocations from the previous fiber.
623 void do_post_switch_actions();
626 *@brief Helper method that converts a @c cilk_fiber object into a
627 * @c cilk_fiber_sysdep object.
629 * The @c cilk_fiber_sysdep object contains the system-dependent parts
630 * of the implementation of a @\c cilk_fiber.
632 * We could have @c cilk_fiber_sysdep inherit from @c cilk_fiber and
633 * then use virtual functions. But since a given platform only uses
634 * one definition of @c cilk_fiber_sysdep at a time, we statically
635 * cast between them.
637 inline cilk_fiber_sysdep* sysdep();
640 * @brief Set resumable flag to specified state.
642 inline void set_resumable(bool state) {
643 m_flags = state ? (m_flags | RESUMABLE) : (m_flags & (~RESUMABLE));
647 *@brief Set the allocated_from_thread flag.
649 inline void set_allocated_from_thread(bool state) {
650 m_flags = state ? (m_flags | ALLOCATED_FROM_THREAD) : (m_flags & (~ALLOCATED_FROM_THREAD));
653 public:
656 * @brief Allocates and initializes a new cilk_fiber, either from
657 * the specified pool or from the heap.
659 * @pre pool should not be NULL.
661 static cilk_fiber* allocate(cilk_fiber_pool* pool);
664 * @brief Allocates a fiber from the heap.
666 static cilk_fiber* allocate_from_heap(size_t stack_size);
669 * @brief Return a fiber to the heap.
671 void deallocate_to_heap();
674 * @brief Reset the state of a fiber just allocated from a pool.
676 void reset_state(cilk_fiber_proc start_proc);
679 * @brief Remove a reference from this fiber, possibly
680 * deallocating it if the reference count becomes 0.
682 * @param pool The fiber pool to which this fiber should be returned.
683 * @return The final reference count.
685 int remove_reference(cilk_fiber_pool* pool);
688 * @brief Deallocate the fiber by returning it to the pool.
689 * @pre This method should only be called if the reference count
690 * is 0.
692 * @param pool The fiber pool to return this fiber to. If NULL,
693 * fiber is returned to the heap.
695 void deallocate_self(cilk_fiber_pool *pool);
697 /** @brief Allocates and intializes this thread's main fiber. */
698 static cilk_fiber* allocate_from_thread();
700 /** @brief Deallocate a fiber created from a thread,
701 * possibly destroying it.
703 * This method decrements the reference count of this fiber by 2,
704 * and destroys the fiber if the reference count is 0.
706 * OS-specific cleanup for the fiber executes unconditionally with for
707 * this method. The destruction of the actual object, however, does
708 * not occur unless the reference count is 0.
710 * @return Final reference count. If the count is 0, the fiber was
711 * returned to the heap.
713 int deallocate_from_thread();
715 /** @brief Removes a reference from this fiber.
717 * This method deallocates this fiber if the reference count
718 * becomes 0.
720 * @pre This fiber must be allocated from a thread.
721 * @return The final reference count of this fiber.
723 int remove_reference_from_thread();
725 #if SUPPORT_GET_CURRENT_FIBER
726 /** @brief Get the current fiber from TLS.
728 * @note This function is only used for testing the runtime.
730 static cilk_fiber* get_current_fiber();
731 #endif
733 /** @brief Suspend execution on current fiber resumes other fiber.
735 * Control returns after resuming execution of the self fiber.
737 void suspend_self_and_resume_other(cilk_fiber* other);
740 /** @brief Removes a reference from the currently executing fiber
741 * and resumes other fiber.
743 * This fiber may be returned to a pool or deallocated.
745 NORETURN remove_reference_from_self_and_resume_other(cilk_fiber_pool* self_pool,
746 cilk_fiber* other);
748 /** @brief Set the proc method to execute immediately after a switch
749 * to this fiber.
751 * @param post_switch_proc Proc method to execute immediately
752 * after switching to this fiber.
754 inline void set_post_switch_proc(cilk_fiber_proc post_switch_proc) {
755 m_post_switch_proc = post_switch_proc;
758 /** @brief Returns true if this fiber is resumable.
760 * A fiber is considered resumable when it is not currently being
761 * executed.
763 inline bool is_resumable(void) {
764 return (m_flags & RESUMABLE);
767 /** @brief Returns true if fiber was allocated from a thread. */
768 inline bool is_allocated_from_thread(void) {
769 return (m_flags & ALLOCATED_FROM_THREAD);
773 *@brief Get the address at the base of the stack for this fiber.
775 inline char* get_stack_base();
777 /** @brief Return the data for this fiber. */
778 cilk_fiber_data* get_data() { return this; }
780 /** @brief Return the data for this fiber. */
781 cilk_fiber_data const* get_data() const { return this; }
784 #if NEED_FIBER_REF_COUNTS
785 /** @brief Verifies that this fiber's reference count equals v. */
786 inline void assert_ref_count_equals(long v) {
787 #if FIBER_CHECK_REF_COUNTS
788 CILK_ASSERT(m_outstanding_references >= v);
789 #endif
792 /** @brief Verifies that this fiber's reference count is at least v. */
793 inline void assert_ref_count_at_least(long v) {
794 #if FIBER_CHECK_REF_COUNTS
795 CILK_ASSERT(m_outstanding_references >= v);
796 #endif
799 /** @brief Get reference count. */
800 inline long get_ref_count() { return m_outstanding_references; }
802 /** @brief Initialize reference count.
803 * Operation is not atomic.
805 inline void init_ref_count(long v) { m_outstanding_references = v; }
807 // For Windows, updates to the fiber reference count need to be
808 // atomic, because exceptions can live on a stack that we are not
809 // currently executing on. Thus, we can update the reference
810 // count of a fiber we are not currently executing on.
812 /** @brief Increment reference count for this fiber [Windows]. */
813 inline void inc_ref_count() { atomic_inc_ref_count(); }
815 /** @brief Decrement reference count for this fiber [Windows]. */
816 inline long dec_ref_count() { return atomic_dec_ref_count(); }
818 /** @brief Subtract v from the reference count for this fiber [Windows]. */
819 inline long sub_from_ref_count(long v) { return atomic_sub_from_ref_count(v); }
820 #else // NEED_FIBER_REF_COUNTS
822 // Without reference counting, we have placeholder methods.
823 inline void init_ref_count(long v) { }
825 inline void inc_ref_count() { }
827 // With no reference counting, dec_ref_count always return 0.
828 // Thus, anyone checking is always the "last" one.
829 inline long dec_ref_count() { return 0; }
830 inline long sub_from_ref_count(long v) { return 0; }
832 // The assert methods do nothing.
833 inline void assert_ref_count_equals(long v) { }
834 inline void assert_ref_count_at_least(long v) { }
835 #endif
838 * @brief Call TBB to tell it about an "interesting" event.
840 * @param op Value specifying the event to track.
842 void invoke_tbb_stack_op(__cilk_tbb_stack_op op);
844 private:
847 * @brief Helper method: try to allocate a fiber from this pool or
848 * its ancestors without going to the OS / heap.
850 * Returns allocated pool, or NULL if no pool is found.
852 * If pool contains a suitable fiber. Return it. Otherwise, try to
853 * recursively grab a fiber from the parent pool, if there is one.
855 * This method will not allocate a fiber from the heap.
857 static cilk_fiber* try_allocate_from_pool_recursive(cilk_fiber_pool* pool);
860 #if NEED_FIBER_REF_COUNTS
862 * @brief Atomic increment of reference count.
864 void atomic_inc_ref_count();
867 * @brief Atomic decrement of reference count.
869 long atomic_dec_ref_count();
872 * @brief Atomic subtract of v from reference count.
873 * @param v Value to subtract.
875 long atomic_sub_from_ref_count(long v);
876 #endif // NEED_FIBER_REF_COUNTS
880 #endif // __cplusplus
882 #endif // ! defined(INCLUDED_CILK_FIBER_DOT_H)