1 /* full_frame.c -*-C++-*-
3 *************************************************************************
6 * Copyright (C) 2010-2013, Intel Corporation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
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.
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.
38 **************************************************************************/
40 #include "full_frame.h"
45 #include "frame_malloc.h"
48 full_frame
*__cilkrts_make_full_frame(__cilkrts_worker
*w
,
49 __cilkrts_stack_frame
*sf
)
53 START_INTERVAL(w
, INTERVAL_ALLOC_FULL_FRAME
) {
54 ff
= (full_frame
*)__cilkrts_frame_malloc(w
, sizeof(*ff
));
55 __cilkrts_mutex_init(&ff
->lock
);
57 ff
->full_frame_magic_0
= FULL_FRAME_MAGIC_0
;
60 ff
->rightmost_child
= 0;
61 ff
->left_sibling
= ff
->right_sibling
= 0;
63 ff
->is_call_child
= 0;
64 ff
->simulated_stolen
= 0;
65 ff
->children_reducer_map
= ff
->right_reducer_map
= 0;
66 ff
->pending_exception
=
67 ff
->child_pending_exception
=
68 ff
->right_pending_exception
= NULL
;
73 ff
->trylevel
= (unsigned long)-1;
82 /*__cilkrts_init_full_frame_sysdep(w, ff);*/
83 ff
->full_frame_magic_1
= FULL_FRAME_MAGIC_1
;
84 } STOP_INTERVAL(w
, INTERVAL_ALLOC_FULL_FRAME
);
88 COMMON_PORTABLE
void __cilkrts_put_stack(full_frame
*ff
,
89 __cilkrts_stack_frame
*sf
)
91 /* When suspending frame ff prior to stealing it, __cilkrts_put_stack is
92 * used to store the stack pointer for eventual sync. When suspending
93 * frame ff prior to a sync, __cilkrts_put_stack is called to re-establish
94 * the sync stack pointer, offsetting it by any change in the stack depth
95 * that occured between the spawn and the sync.
96 * Although it is not usually meaningful to add two pointers, the value of
97 * ff->sync_sp at the time of this call is really an integer, not a
100 ptrdiff_t sync_sp_i
= (ptrdiff_t) ff
->sync_sp
;
101 char* sp
= (char*) __cilkrts_get_sp(sf
);
103 ff
->sync_sp
= sp
+ sync_sp_i
;
105 DBGPRINTF("%d- __cilkrts_put_stack - adjust (+) sync "
106 "stack of full frame %p (+sp: %p) to %p\n",
107 __cilkrts_get_tls_worker()->self
, ff
, sp
, ff
->sync_sp
);
110 COMMON_PORTABLE
void __cilkrts_take_stack(full_frame
*ff
, void *sp
)
112 /* When resuming the parent after a steal, __cilkrts_take_stack is used to
113 * subtract the new stack pointer from the current stack pointer, storing
114 * the offset in ff->sync_sp. When resuming after a sync,
115 * __cilkrts_take_stack is used to subtract the new stack pointer from
116 * itself, leaving ff->sync_sp at zero (null). Although the pointers being
117 * subtracted are not part of the same contiguous chunk of memory, the
118 * flat memory model allows us to subtract them and get a useable offset.
120 ptrdiff_t sync_sp_i
= ff
->sync_sp
- (char*) sp
;
122 ff
->sync_sp
= (char *) sync_sp_i
;
124 DBGPRINTF("%d- __cilkrts_take_stack - adjust (-) sync "
125 "stack of full frame %p to %p (-sp: %p)\n",
126 __cilkrts_get_tls_worker()->self
, ff
, ff
->sync_sp
, sp
);
129 COMMON_PORTABLE
void __cilkrts_adjust_stack(full_frame
*ff
, size_t size
)
131 /* When resuming the parent after a steal, __cilkrts_take_stack is used to
132 * subtract the new stack pointer from the current stack pointer, storing
133 * the offset in ff->sync_sp. When resuming after a sync,
134 * __cilkrts_take_stack is used to subtract the new stack pointer from
135 * itself, leaving ff->sync_sp at zero (null). Although the pointers being
136 * subtracted are not part of the same contiguous chunk of memory, the
137 * flat memory model allows us to subtract them and get a useable offset.
139 * __cilkrts_adjust_stack() is used to deallocate a Variable Length Array
140 * by adding it's size to ff->sync_sp.
142 ff
->sync_sp
= ff
->sync_sp
+ size
;
144 DBGPRINTF("%d- __cilkrts_adjust_stack - adjust (+) sync "
145 "stack of full frame %p to %p (+ size: 0x%x)\n",
146 __cilkrts_get_tls_worker()->self
, ff
, ff
->sync_sp
, size
);
150 void __cilkrts_destroy_full_frame(__cilkrts_worker
*w
, full_frame
*ff
)
152 validate_full_frame(ff
);
153 CILK_ASSERT(ff
->children_reducer_map
== 0);
154 CILK_ASSERT(ff
->right_reducer_map
== 0);
155 CILK_ASSERT(NULL
== ff
->pending_exception
);
156 CILK_ASSERT(NULL
== ff
->child_pending_exception
);
157 CILK_ASSERT(NULL
== ff
->right_pending_exception
);
158 __cilkrts_mutex_destroy(w
, &ff
->lock
);
159 __cilkrts_frame_free(w
, ff
, sizeof(*ff
));
162 COMMON_PORTABLE
void validate_full_frame(full_frame
*ff
)
164 /* check the magic numbers, for debugging purposes */
165 if (ff
->full_frame_magic_0
!= FULL_FRAME_MAGIC_0
||
166 ff
->full_frame_magic_1
!= FULL_FRAME_MAGIC_1
)
167 abort_because_rts_is_corrupted();
170 void __cilkrts_frame_lock(__cilkrts_worker
*w
, full_frame
*ff
)
172 validate_full_frame(ff
);
173 __cilkrts_mutex_lock(w
, &ff
->lock
);
176 void __cilkrts_frame_unlock(__cilkrts_worker
*w
, full_frame
*ff
)
178 __cilkrts_mutex_unlock(w
, &ff
->lock
);
181 /* End full_frame.c */