1/* pedigrees.c                  -*-C-*-
2 *
3 *************************************************************************
4 *
5 *  @copyright
6 *  Copyright (C) 2007-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:
13 *
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.
23 *
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 *
38 **************************************************************************/
39
40#include "pedigrees.h"
41#include "local_state.h"
42
43/*************************************************************
44  Pedigree API code.
45*************************************************************/
46
47/*
48 * C99 requires that every inline function with external linkage have one
49 * extern declaration in the program (with the inline definition in scope).
50 */
51COMMON_PORTABLE
52extern void update_pedigree_on_leave_frame(__cilkrts_worker *w,
53					   __cilkrts_stack_frame *sf);
54
55void __cilkrts_set_pedigree_leaf(__cilkrts_pedigree *leaf)
56{
57    __cilkrts_set_tls_pedigree_leaf(leaf);
58}
59
60void load_pedigree_leaf_into_user_worker(__cilkrts_worker *w)
61{
62    __cilkrts_pedigree *pedigree_leaf;
63    CILK_ASSERT(w->l->type == WORKER_USER);
64    pedigree_leaf = __cilkrts_get_tls_pedigree_leaf(1);
65    w->pedigree = *pedigree_leaf;
66
67    // Save a pointer to the old leaf.
68    // We'll need to restore it later.
69    CILK_ASSERT(w->l->original_pedigree_leaf == NULL);
70    w->l->original_pedigree_leaf = pedigree_leaf;
71
72    __cilkrts_set_tls_pedigree_leaf(&w->pedigree);
73
74    // Check that this new pedigree root has at least two values.
75    CILK_ASSERT(w->pedigree.parent);
76    CILK_ASSERT(w->pedigree.parent->parent == NULL);
77}
78
79void save_pedigree_leaf_from_user_worker(__cilkrts_worker *w)
80{
81    CILK_ASSERT(w->l->type == WORKER_USER);
82
83    // Existing leaf in tls should be for the current worker.
84    // This assert is expensive to check though.
85    // CILK_ASSERT(&w->pedigree == __cilkrts_get_tls_pedigree_leaf(0));
86    CILK_ASSERT(w->l->original_pedigree_leaf);
87
88    // w should finish with a pedigree node that points to
89    // the same root that we just looked up.
90
91    // TODO: This assert should be valid.
92    // But we are removing it now to make exceptions (without pedigrees) work.
93    // Currently, reading the pedigree after an exception is caught
94    // fails because the pedigree chain not restored correctly.
95    // CILK_ASSERT(w->l->original_pedigree_leaf->next == w->pedigree.parent);
96    w->l->original_pedigree_leaf->rank = w->pedigree.rank;
97
98    // Save that leaf pointer back into tls.
99    __cilkrts_set_tls_pedigree_leaf(w->l->original_pedigree_leaf);
100    // Null out worker's leaf for paranoia.
101    w->l->original_pedigree_leaf = NULL;
102}
103
104
105
106/*
107  Local Variables: **
108  c-file-style:"bsd" **
109  c-basic-offset:4 **
110  indent-tabs-mode:nil **
111  End: **
112*/
113