1 /* Copyright (C) 2005, 2006, 2007, 2008, 2009, 2011, 2012
2 Free Software Foundation, Inc.
3 Contributed by Richard Henderson <rth@redhat.com>.
5 This file is part of the GNU OpenMP Library (libgomp).
7 Libgomp is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 /* This file handles the maintainence of threads in response to team
27 creation and termination. */
33 /* This attribute contains PTHREAD_CREATE_DETACHED. */
34 pthread_attr_t gomp_thread_attr
;
36 /* This key is for the thread destructor. */
37 pthread_key_t gomp_thread_destructor
;
40 /* This is the libgomp per-thread data structure. */
42 __thread
struct gomp_thread gomp_tls_data
;
44 pthread_key_t gomp_tls_key
;
48 /* This structure is used to communicate across pthread_create. */
50 struct gomp_thread_start_data
54 struct gomp_team_state ts
;
55 struct gomp_task
*task
;
56 struct gomp_thread_pool
*thread_pool
;
61 /* This function is a pthread_create entry point. This contains the idle
62 loop in which a thread waits to be called up to become part of a team. */
65 gomp_thread_start (void *xdata
)
67 struct gomp_thread_start_data
*data
= xdata
;
68 struct gomp_thread
*thr
;
69 struct gomp_thread_pool
*pool
;
70 void (*local_fn
) (void *);
76 struct gomp_thread local_thr
;
78 pthread_setspecific (gomp_tls_key
, thr
);
80 gomp_sem_init (&thr
->release
, 0);
82 /* Extract what we need from data. */
84 local_data
= data
->fn_data
;
85 thr
->thread_pool
= data
->thread_pool
;
87 thr
->task
= data
->task
;
89 thr
->ts
.team
->ordered_release
[thr
->ts
.team_id
] = &thr
->release
;
91 /* Make thread pool local. */
92 pool
= thr
->thread_pool
;
96 struct gomp_team
*team
= thr
->ts
.team
;
97 struct gomp_task
*task
= thr
->task
;
99 gomp_barrier_wait (&team
->barrier
);
101 local_fn (local_data
);
102 gomp_team_barrier_wait (&team
->barrier
);
103 gomp_finish_task (task
);
104 gomp_barrier_wait_last (&team
->barrier
);
108 pool
->threads
[thr
->ts
.team_id
] = thr
;
110 gomp_barrier_wait (&pool
->threads_dock
);
113 struct gomp_team
*team
= thr
->ts
.team
;
114 struct gomp_task
*task
= thr
->task
;
116 local_fn (local_data
);
117 gomp_team_barrier_wait (&team
->barrier
);
118 gomp_finish_task (task
);
120 gomp_barrier_wait (&pool
->threads_dock
);
123 local_data
= thr
->data
;
129 gomp_sem_destroy (&thr
->release
);
134 /* Create a new team data structure. */
137 gomp_new_team (unsigned nthreads
)
139 struct gomp_team
*team
;
143 size
= sizeof (*team
) + nthreads
* (sizeof (team
->ordered_release
[0])
144 + sizeof (team
->implicit_task
[0]));
145 team
= gomp_malloc (size
);
147 team
->work_share_chunk
= 8;
148 #ifdef HAVE_SYNC_BUILTINS
149 team
->single_count
= 0;
151 gomp_mutex_init (&team
->work_share_list_free_lock
);
153 gomp_init_work_share (&team
->work_shares
[0], false, nthreads
);
154 team
->work_shares
[0].next_alloc
= NULL
;
155 team
->work_share_list_free
= NULL
;
156 team
->work_share_list_alloc
= &team
->work_shares
[1];
157 for (i
= 1; i
< 7; i
++)
158 team
->work_shares
[i
].next_free
= &team
->work_shares
[i
+ 1];
159 team
->work_shares
[i
].next_free
= NULL
;
161 team
->nthreads
= nthreads
;
162 gomp_barrier_init (&team
->barrier
, nthreads
);
164 gomp_sem_init (&team
->master_release
, 0);
165 team
->ordered_release
= (void *) &team
->implicit_task
[nthreads
];
166 team
->ordered_release
[0] = &team
->master_release
;
168 gomp_mutex_init (&team
->task_lock
);
169 team
->task_queue
= NULL
;
170 team
->task_count
= 0;
171 team
->task_running_count
= 0;
177 /* Free a team data structure. */
180 free_team (struct gomp_team
*team
)
182 gomp_barrier_destroy (&team
->barrier
);
183 gomp_mutex_destroy (&team
->task_lock
);
187 /* Allocate and initialize a thread pool. */
189 static struct gomp_thread_pool
*gomp_new_thread_pool (void)
191 struct gomp_thread_pool
*pool
192 = gomp_malloc (sizeof(struct gomp_thread_pool
));
193 pool
->threads
= NULL
;
194 pool
->threads_size
= 0;
195 pool
->threads_used
= 0;
196 pool
->last_team
= NULL
;
201 gomp_free_pool_helper (void *thread_pool
)
203 struct gomp_thread_pool
*pool
204 = (struct gomp_thread_pool
*) thread_pool
;
205 gomp_barrier_wait_last (&pool
->threads_dock
);
206 gomp_sem_destroy (&gomp_thread ()->release
);
210 /* Free a thread pool and release its threads. */
213 gomp_free_thread (void *arg
__attribute__((unused
)))
215 struct gomp_thread
*thr
= gomp_thread ();
216 struct gomp_thread_pool
*pool
= thr
->thread_pool
;
219 if (pool
->threads_used
> 0)
222 for (i
= 1; i
< pool
->threads_used
; i
++)
224 struct gomp_thread
*nthr
= pool
->threads
[i
];
225 nthr
->fn
= gomp_free_pool_helper
;
228 /* This barrier undocks threads docked on pool->threads_dock. */
229 gomp_barrier_wait (&pool
->threads_dock
);
230 /* And this waits till all threads have called gomp_barrier_wait_last
231 in gomp_free_pool_helper. */
232 gomp_barrier_wait (&pool
->threads_dock
);
233 /* Now it is safe to destroy the barrier and free the pool. */
234 gomp_barrier_destroy (&pool
->threads_dock
);
236 #ifdef HAVE_SYNC_BUILTINS
237 __sync_fetch_and_add (&gomp_managed_threads
,
238 1L - pool
->threads_used
);
240 gomp_mutex_lock (&gomp_remaining_threads_lock
);
241 gomp_managed_threads
-= pool
->threads_used
- 1L;
242 gomp_mutex_unlock (&gomp_remaining_threads_lock
);
245 free (pool
->threads
);
247 free_team (pool
->last_team
);
249 thr
->thread_pool
= NULL
;
251 if (thr
->task
!= NULL
)
253 struct gomp_task
*task
= thr
->task
;
262 gomp_team_start (void (*fn
) (void *), void *data
, unsigned nthreads
,
263 struct gomp_team
*team
)
265 struct gomp_thread_start_data
*start_data
;
266 struct gomp_thread
*thr
, *nthr
;
267 struct gomp_task
*task
;
268 struct gomp_task_icv
*icv
;
270 struct gomp_thread_pool
*pool
;
271 unsigned i
, n
, old_threads_used
= 0;
272 pthread_attr_t thread_attr
, *attr
;
273 unsigned long nthreads_var
;
275 thr
= gomp_thread ();
276 nested
= thr
->ts
.team
!= NULL
;
277 if (__builtin_expect (thr
->thread_pool
== NULL
, 0))
279 thr
->thread_pool
= gomp_new_thread_pool ();
280 pthread_setspecific (gomp_thread_destructor
, thr
);
282 pool
= thr
->thread_pool
;
284 icv
= task
? &task
->icv
: &gomp_global_icv
;
286 /* Always save the previous state, even if this isn't a nested team.
287 In particular, we should save any work share state from an outer
288 orphaned work share construct. */
289 team
->prev_ts
= thr
->ts
;
295 ++thr
->ts
.active_level
;
296 thr
->ts
.work_share
= &team
->work_shares
[0];
297 thr
->ts
.last_work_share
= NULL
;
298 #ifdef HAVE_SYNC_BUILTINS
299 thr
->ts
.single_count
= 0;
301 thr
->ts
.static_trip
= 0;
302 thr
->task
= &team
->implicit_task
[0];
303 nthreads_var
= icv
->nthreads_var
;
304 if (__builtin_expect (gomp_nthreads_var_list
!= NULL
, 0)
305 && thr
->ts
.level
< gomp_nthreads_var_list_len
)
306 nthreads_var
= gomp_nthreads_var_list
[thr
->ts
.level
];
307 gomp_init_task (thr
->task
, task
, icv
);
308 team
->implicit_task
[0].icv
.nthreads_var
= nthreads_var
;
315 /* We only allow the reuse of idle threads for non-nested PARALLEL
316 regions. This appears to be implied by the semantics of
317 threadprivate variables, but perhaps that's reading too much into
318 things. Certainly it does prevent any locking problems, since
319 only the initial program thread will modify gomp_threads. */
322 old_threads_used
= pool
->threads_used
;
324 if (nthreads
<= old_threads_used
)
326 else if (old_threads_used
== 0)
329 gomp_barrier_init (&pool
->threads_dock
, nthreads
);
333 n
= old_threads_used
;
335 /* Increase the barrier threshold to make sure all new
336 threads arrive before the team is released. */
337 gomp_barrier_reinit (&pool
->threads_dock
, nthreads
);
340 /* Not true yet, but soon will be. We're going to release all
341 threads from the dock, and those that aren't part of the
343 pool
->threads_used
= nthreads
;
345 /* Release existing idle threads. */
348 nthr
= pool
->threads
[i
];
349 nthr
->ts
.team
= team
;
350 nthr
->ts
.work_share
= &team
->work_shares
[0];
351 nthr
->ts
.last_work_share
= NULL
;
352 nthr
->ts
.team_id
= i
;
353 nthr
->ts
.level
= team
->prev_ts
.level
+ 1;
354 nthr
->ts
.active_level
= thr
->ts
.active_level
;
355 #ifdef HAVE_SYNC_BUILTINS
356 nthr
->ts
.single_count
= 0;
358 nthr
->ts
.static_trip
= 0;
359 nthr
->task
= &team
->implicit_task
[i
];
360 gomp_init_task (nthr
->task
, task
, icv
);
361 team
->implicit_task
[i
].icv
.nthreads_var
= nthreads_var
;
364 team
->ordered_release
[i
] = &nthr
->release
;
370 /* If necessary, expand the size of the gomp_threads array. It is
371 expected that changes in the number of threads are rare, thus we
372 make no effort to expand gomp_threads_size geometrically. */
373 if (nthreads
>= pool
->threads_size
)
375 pool
->threads_size
= nthreads
+ 1;
377 = gomp_realloc (pool
->threads
,
379 * sizeof (struct gomp_thread_data
*));
383 if (__builtin_expect (nthreads
> old_threads_used
, 0))
385 long diff
= (long) nthreads
- (long) old_threads_used
;
387 if (old_threads_used
== 0)
390 #ifdef HAVE_SYNC_BUILTINS
391 __sync_fetch_and_add (&gomp_managed_threads
, diff
);
393 gomp_mutex_lock (&gomp_remaining_threads_lock
);
394 gomp_managed_threads
+= diff
;
395 gomp_mutex_unlock (&gomp_remaining_threads_lock
);
399 attr
= &gomp_thread_attr
;
400 if (__builtin_expect (gomp_cpu_affinity
!= NULL
, 0))
403 pthread_attr_init (&thread_attr
);
404 pthread_attr_setdetachstate (&thread_attr
, PTHREAD_CREATE_DETACHED
);
405 if (! pthread_attr_getstacksize (&gomp_thread_attr
, &stacksize
))
406 pthread_attr_setstacksize (&thread_attr
, stacksize
);
410 start_data
= gomp_alloca (sizeof (struct gomp_thread_start_data
)
413 /* Launch new threads. */
414 for (; i
< nthreads
; ++i
, ++start_data
)
420 start_data
->fn_data
= data
;
421 start_data
->ts
.team
= team
;
422 start_data
->ts
.work_share
= &team
->work_shares
[0];
423 start_data
->ts
.last_work_share
= NULL
;
424 start_data
->ts
.team_id
= i
;
425 start_data
->ts
.level
= team
->prev_ts
.level
+ 1;
426 start_data
->ts
.active_level
= thr
->ts
.active_level
;
427 #ifdef HAVE_SYNC_BUILTINS
428 start_data
->ts
.single_count
= 0;
430 start_data
->ts
.static_trip
= 0;
431 start_data
->task
= &team
->implicit_task
[i
];
432 gomp_init_task (start_data
->task
, task
, icv
);
433 team
->implicit_task
[i
].icv
.nthreads_var
= nthreads_var
;
434 start_data
->thread_pool
= pool
;
435 start_data
->nested
= nested
;
437 if (gomp_cpu_affinity
!= NULL
)
438 gomp_init_thread_affinity (attr
);
440 err
= pthread_create (&pt
, attr
, gomp_thread_start
, start_data
);
442 gomp_fatal ("Thread creation failed: %s", strerror (err
));
445 if (__builtin_expect (gomp_cpu_affinity
!= NULL
, 0))
446 pthread_attr_destroy (&thread_attr
);
449 gomp_barrier_wait (nested
? &team
->barrier
: &pool
->threads_dock
);
451 /* Decrease the barrier threshold to match the number of threads
452 that should arrive back at the end of this team. The extra
453 threads should be exiting. Note that we arrange for this test
454 to never be true for nested teams. */
455 if (__builtin_expect (nthreads
< old_threads_used
, 0))
457 long diff
= (long) nthreads
- (long) old_threads_used
;
459 gomp_barrier_reinit (&pool
->threads_dock
, nthreads
);
461 #ifdef HAVE_SYNC_BUILTINS
462 __sync_fetch_and_add (&gomp_managed_threads
, diff
);
464 gomp_mutex_lock (&gomp_remaining_threads_lock
);
465 gomp_managed_threads
+= diff
;
466 gomp_mutex_unlock (&gomp_remaining_threads_lock
);
472 /* Terminate the current team. This is only to be called by the master
473 thread. We assume that we must wait for the other threads. */
478 struct gomp_thread
*thr
= gomp_thread ();
479 struct gomp_team
*team
= thr
->ts
.team
;
481 /* This barrier handles all pending explicit threads. */
482 gomp_team_barrier_wait (&team
->barrier
);
483 gomp_fini_work_share (thr
->ts
.work_share
);
486 thr
->ts
= team
->prev_ts
;
488 if (__builtin_expect (thr
->ts
.team
!= NULL
, 0))
490 #ifdef HAVE_SYNC_BUILTINS
491 __sync_fetch_and_add (&gomp_managed_threads
, 1L - team
->nthreads
);
493 gomp_mutex_lock (&gomp_remaining_threads_lock
);
494 gomp_managed_threads
-= team
->nthreads
- 1L;
495 gomp_mutex_unlock (&gomp_remaining_threads_lock
);
497 /* This barrier has gomp_barrier_wait_last counterparts
498 and ensures the team can be safely destroyed. */
499 gomp_barrier_wait (&team
->barrier
);
502 if (__builtin_expect (team
->work_shares
[0].next_alloc
!= NULL
, 0))
504 struct gomp_work_share
*ws
= team
->work_shares
[0].next_alloc
;
507 struct gomp_work_share
*next_ws
= ws
->next_alloc
;
513 gomp_sem_destroy (&team
->master_release
);
514 #ifndef HAVE_SYNC_BUILTINS
515 gomp_mutex_destroy (&team
->work_share_list_free_lock
);
518 if (__builtin_expect (thr
->ts
.team
!= NULL
, 0)
519 || __builtin_expect (team
->nthreads
== 1, 0))
523 struct gomp_thread_pool
*pool
= thr
->thread_pool
;
525 free_team (pool
->last_team
);
526 pool
->last_team
= team
;
531 /* Constructors for this file. */
533 static void __attribute__((constructor
))
534 initialize_team (void)
536 struct gomp_thread
*thr
;
539 static struct gomp_thread initial_thread_tls_data
;
541 pthread_key_create (&gomp_tls_key
, NULL
);
542 pthread_setspecific (gomp_tls_key
, &initial_thread_tls_data
);
545 if (pthread_key_create (&gomp_thread_destructor
, gomp_free_thread
) != 0)
546 gomp_fatal ("could not create thread pool destructor.");
549 thr
= &gomp_tls_data
;
551 thr
= &initial_thread_tls_data
;
553 gomp_sem_init (&thr
->release
, 0);
556 static void __attribute__((destructor
))
557 team_destructor (void)
559 /* Without this dlclose on libgomp could lead to subsequent
561 pthread_key_delete (gomp_thread_destructor
);
564 struct gomp_task_icv
*
567 struct gomp_thread
*thr
= gomp_thread ();
568 struct gomp_task
*task
= gomp_malloc (sizeof (struct gomp_task
));
569 gomp_init_task (task
, NULL
, &gomp_global_icv
);
571 pthread_setspecific (gomp_thread_destructor
, thr
);