1 /* Copyright (C) 2005 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <rth@redhat.com>.
4 This file is part of the GNU OpenMP Library (libgomp).
6 Libgomp is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
16 You should have received a copy of the GNU Lesser General Public License
17 along with libgomp; see the file COPYING.LIB. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 MA 02110-1301, USA. */
21 /* As a special exception, if you link this library with other files, some
22 of which are compiled with GCC, to produce an executable, this library
23 does not by itself cause the resulting executable to be covered by the
24 GNU General Public License. This exception does not however invalidate
25 any other reasons why the executable file might be covered by the GNU
26 General Public License. */
28 /* This file contains routines to manage the work-share queue for a team
36 /* Create a new work share structure. */
38 struct gomp_work_share
*
39 gomp_new_work_share (bool ordered
, unsigned nthreads
)
41 struct gomp_work_share
*ws
;
46 size
+= nthreads
* sizeof (ws
->ordered_team_ids
[0]);
48 ws
= gomp_malloc_cleared (size
);
49 gomp_mutex_init (&ws
->lock
);
50 ws
->ordered_owner
= -1;
56 /* Free a work share structure. */
59 free_work_share (struct gomp_work_share
*ws
)
61 gomp_mutex_destroy (&ws
->lock
);
66 /* The current thread is ready to begin the next work sharing construct.
67 In all cases, thr->ts.work_share is updated to point to the new
68 structure. In all cases the work_share lock is locked. Return true
69 if this was the first thread to reach this point. */
72 gomp_work_share_start (bool ordered
)
74 struct gomp_thread
*thr
= gomp_thread ();
75 struct gomp_team
*team
= thr
->ts
.team
;
76 struct gomp_work_share
*ws
;
77 unsigned ws_index
, ws_gen
;
79 /* Work sharing constructs can be orphaned. */
82 ws
= gomp_new_work_share (ordered
, 1);
83 thr
->ts
.work_share
= ws
;
84 thr
->ts
.static_trip
= 0;
85 gomp_mutex_lock (&ws
->lock
);
89 gomp_mutex_lock (&team
->work_share_lock
);
91 /* This thread is beginning its next generation. */
92 ws_gen
= ++thr
->ts
.work_share_generation
;
94 /* If this next generation is not newer than any other generation in
95 the team, then simply reference the existing construct. */
96 if (ws_gen
- team
->oldest_live_gen
< team
->num_live_gen
)
98 ws_index
= ws_gen
& team
->generation_mask
;
99 ws
= team
->work_shares
[ws_index
];
100 thr
->ts
.work_share
= ws
;
101 thr
->ts
.static_trip
= 0;
103 gomp_mutex_lock (&ws
->lock
);
104 gomp_mutex_unlock (&team
->work_share_lock
);
109 /* Resize the work shares queue if we've run out of space. */
110 if (team
->num_live_gen
++ == team
->generation_mask
)
112 team
->work_shares
= gomp_realloc (team
->work_shares
,
113 2 * team
->num_live_gen
114 * sizeof (*team
->work_shares
));
116 /* Unless oldest_live_gen is zero, the sequence of live elements
117 wraps around the end of the array. If we do nothing, we break
118 lookup of the existing elements. Fix that by unwrapping the
119 data from the front to the end. */
120 if (team
->oldest_live_gen
> 0)
121 memcpy (team
->work_shares
+ team
->num_live_gen
,
123 (team
->oldest_live_gen
& team
->generation_mask
)
124 * sizeof (*team
->work_shares
));
126 team
->generation_mask
= team
->generation_mask
* 2 + 1;
129 ws_index
= ws_gen
& team
->generation_mask
;
130 ws
= gomp_new_work_share (ordered
, team
->nthreads
);
131 thr
->ts
.work_share
= ws
;
132 thr
->ts
.static_trip
= 0;
133 team
->work_shares
[ws_index
] = ws
;
135 gomp_mutex_lock (&ws
->lock
);
136 gomp_mutex_unlock (&team
->work_share_lock
);
142 /* The current thread is done with its current work sharing construct.
143 This version does imply a barrier at the end of the work-share. */
146 gomp_work_share_end (void)
148 struct gomp_thread
*thr
= gomp_thread ();
149 struct gomp_team
*team
= thr
->ts
.team
;
150 struct gomp_work_share
*ws
= thr
->ts
.work_share
;
153 thr
->ts
.work_share
= NULL
;
155 /* Work sharing constructs can be orphaned. */
158 free_work_share (ws
);
162 last
= gomp_barrier_wait_start (&team
->barrier
);
168 ws_index
= thr
->ts
.work_share_generation
& team
->generation_mask
;
169 team
->work_shares
[ws_index
] = NULL
;
170 team
->oldest_live_gen
++;
171 team
->num_live_gen
= 0;
173 free_work_share (ws
);
176 gomp_barrier_wait_end (&team
->barrier
, last
);
180 /* The current thread is done with its current work sharing construct.
181 This version does NOT imply a barrier at the end of the work-share. */
184 gomp_work_share_end_nowait (void)
186 struct gomp_thread
*thr
= gomp_thread ();
187 struct gomp_team
*team
= thr
->ts
.team
;
188 struct gomp_work_share
*ws
= thr
->ts
.work_share
;
191 thr
->ts
.work_share
= NULL
;
193 /* Work sharing constructs can be orphaned. */
196 free_work_share (ws
);
200 #ifdef HAVE_SYNC_BUILTINS
201 completed
= __sync_add_and_fetch (&ws
->threads_completed
, 1);
203 gomp_mutex_lock (&ws
->lock
);
204 completed
= ++ws
->threads_completed
;
205 gomp_mutex_unlock (&ws
->lock
);
208 if (completed
== team
->nthreads
)
212 gomp_mutex_lock (&team
->work_share_lock
);
214 ws_index
= thr
->ts
.work_share_generation
& team
->generation_mask
;
215 team
->work_shares
[ws_index
] = NULL
;
216 team
->oldest_live_gen
++;
217 team
->num_live_gen
--;
219 gomp_mutex_unlock (&team
->work_share_lock
);
221 free_work_share (ws
);