1 /* Copyright (C) 2005-2021 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <rth@redhat.com>.
4 This file is part of the GNU Offloading and Multi Processing Library
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 maintenance of threads in response to team
27 creation and termination. */
34 #ifdef LIBGOMP_USE_PTHREADS
35 pthread_attr_t gomp_thread_attr
;
37 /* This key is for the thread destructor. */
38 pthread_key_t gomp_thread_destructor
;
41 /* This is the libgomp per-thread data structure. */
42 #if defined HAVE_TLS || defined USE_EMUTLS
43 __thread
struct gomp_thread gomp_tls_data
;
45 pthread_key_t gomp_tls_key
;
49 /* This structure is used to communicate across pthread_create. */
51 struct gomp_thread_start_data
55 struct gomp_team_state ts
;
56 struct gomp_task
*task
;
57 struct gomp_thread_pool
*thread_pool
;
64 /* This function is a pthread_create entry point. This contains the idle
65 loop in which a thread waits to be called up to become part of a team. */
68 gomp_thread_start (void *xdata
)
70 struct gomp_thread_start_data
*data
= xdata
;
71 struct gomp_thread
*thr
;
72 struct gomp_thread_pool
*pool
;
73 void (*local_fn
) (void *);
76 #if defined HAVE_TLS || defined USE_EMUTLS
79 struct gomp_thread local_thr
;
82 gomp_sem_init (&thr
->release
, 0);
84 /* Extract what we need from data. */
86 local_data
= data
->fn_data
;
87 thr
->thread_pool
= data
->thread_pool
;
89 thr
->task
= data
->task
;
90 thr
->place
= data
->place
;
91 #ifdef GOMP_NEEDS_THREAD_HANDLE
92 thr
->handle
= data
->handle
;
94 #if !(defined HAVE_TLS || defined USE_EMUTLS)
95 pthread_setspecific (gomp_tls_key
, thr
);
98 thr
->ts
.team
->ordered_release
[thr
->ts
.team_id
] = &thr
->release
;
100 /* Make thread pool local. */
101 pool
= thr
->thread_pool
;
105 struct gomp_team
*team
= thr
->ts
.team
;
106 struct gomp_task
*task
= thr
->task
;
108 gomp_barrier_wait (&team
->barrier
);
110 local_fn (local_data
);
111 gomp_team_barrier_wait_final (&team
->barrier
);
112 gomp_finish_task (task
);
113 gomp_barrier_wait_last (&team
->barrier
);
117 pool
->threads
[thr
->ts
.team_id
] = thr
;
119 gomp_simple_barrier_wait (&pool
->threads_dock
);
122 struct gomp_team
*team
= thr
->ts
.team
;
123 struct gomp_task
*task
= thr
->task
;
125 local_fn (local_data
);
126 gomp_team_barrier_wait_final (&team
->barrier
);
127 gomp_finish_task (task
);
129 gomp_simple_barrier_wait (&pool
->threads_dock
);
132 local_data
= thr
->data
;
138 gomp_sem_destroy (&thr
->release
);
139 pthread_detach (pthread_self ());
140 thr
->thread_pool
= NULL
;
146 static inline struct gomp_team
*
147 get_last_team (unsigned nthreads
)
149 struct gomp_thread
*thr
= gomp_thread ();
150 if (thr
->ts
.team
== NULL
)
152 struct gomp_thread_pool
*pool
= gomp_get_thread_pool (thr
, nthreads
);
153 struct gomp_team
*last_team
= pool
->last_team
;
154 if (last_team
!= NULL
&& last_team
->nthreads
== nthreads
)
156 pool
->last_team
= NULL
;
163 /* Create a new team data structure. */
166 gomp_new_team (unsigned nthreads
)
168 struct gomp_team
*team
;
171 team
= get_last_team (nthreads
);
174 size_t extra
= sizeof (team
->ordered_release
[0])
175 + sizeof (team
->implicit_task
[0]);
176 team
= team_malloc (sizeof (*team
) + nthreads
* extra
);
178 #ifndef HAVE_SYNC_BUILTINS
179 gomp_mutex_init (&team
->work_share_list_free_lock
);
181 gomp_barrier_init (&team
->barrier
, nthreads
);
182 gomp_mutex_init (&team
->task_lock
);
184 team
->nthreads
= nthreads
;
187 team
->work_share_chunk
= 8;
188 #ifdef HAVE_SYNC_BUILTINS
189 team
->single_count
= 0;
191 team
->work_shares_to_free
= &team
->work_shares
[0];
192 gomp_init_work_share (&team
->work_shares
[0], 0, nthreads
);
193 team
->work_shares
[0].next_alloc
= NULL
;
194 team
->work_share_list_free
= NULL
;
195 team
->work_share_list_alloc
= &team
->work_shares
[1];
196 for (i
= 1; i
< 7; i
++)
197 team
->work_shares
[i
].next_free
= &team
->work_shares
[i
+ 1];
198 team
->work_shares
[i
].next_free
= NULL
;
200 gomp_sem_init (&team
->master_release
, 0);
201 team
->ordered_release
= (void *) &team
->implicit_task
[nthreads
];
202 team
->ordered_release
[0] = &team
->master_release
;
204 priority_queue_init (&team
->task_queue
);
205 team
->task_count
= 0;
206 team
->task_queued_count
= 0;
207 team
->task_running_count
= 0;
208 team
->work_share_cancelled
= 0;
209 team
->team_cancelled
= 0;
211 team
->task_detach_count
= 0;
217 /* Free a team data structure. */
220 free_team (struct gomp_team
*team
)
222 #ifndef HAVE_SYNC_BUILTINS
223 gomp_mutex_destroy (&team
->work_share_list_free_lock
);
225 gomp_barrier_destroy (&team
->barrier
);
226 gomp_mutex_destroy (&team
->task_lock
);
227 priority_queue_free (&team
->task_queue
);
232 gomp_free_pool_helper (void *thread_pool
)
234 struct gomp_thread
*thr
= gomp_thread ();
235 struct gomp_thread_pool
*pool
236 = (struct gomp_thread_pool
*) thread_pool
;
237 gomp_simple_barrier_wait_last (&pool
->threads_dock
);
238 gomp_sem_destroy (&thr
->release
);
239 thr
->thread_pool
= NULL
;
241 #ifdef LIBGOMP_USE_PTHREADS
242 pthread_detach (pthread_self ());
244 #elif defined(__nvptx__)
246 #elif defined(__AMDGCN__)
247 asm ("s_dcache_wb\n\t"
250 #error gomp_free_pool_helper must terminate the thread
254 /* Free a thread pool and release its threads. */
257 gomp_free_thread (void *arg
__attribute__((unused
)))
259 struct gomp_thread
*thr
= gomp_thread ();
260 struct gomp_thread_pool
*pool
= thr
->thread_pool
;
263 if (pool
->threads_used
> 0)
266 for (i
= 1; i
< pool
->threads_used
; i
++)
268 struct gomp_thread
*nthr
= pool
->threads
[i
];
269 nthr
->fn
= gomp_free_pool_helper
;
272 /* This barrier undocks threads docked on pool->threads_dock. */
273 gomp_simple_barrier_wait (&pool
->threads_dock
);
274 /* And this waits till all threads have called gomp_barrier_wait_last
275 in gomp_free_pool_helper. */
276 gomp_simple_barrier_wait (&pool
->threads_dock
);
277 /* Now it is safe to destroy the barrier and free the pool. */
278 gomp_simple_barrier_destroy (&pool
->threads_dock
);
280 #ifdef HAVE_SYNC_BUILTINS
281 __sync_fetch_and_add (&gomp_managed_threads
,
282 1L - pool
->threads_used
);
284 gomp_mutex_lock (&gomp_managed_threads_lock
);
285 gomp_managed_threads
-= pool
->threads_used
- 1L;
286 gomp_mutex_unlock (&gomp_managed_threads_lock
);
290 free_team (pool
->last_team
);
292 team_free (pool
->threads
);
295 thr
->thread_pool
= NULL
;
297 if (thr
->ts
.level
== 0 && __builtin_expect (thr
->ts
.team
!= NULL
, 0))
299 if (thr
->task
!= NULL
)
301 struct gomp_task
*task
= thr
->task
;
309 #ifdef LIBGOMP_USE_PTHREADS
311 gomp_team_start (void (*fn
) (void *), void *data
, unsigned nthreads
,
312 unsigned flags
, struct gomp_team
*team
,
313 struct gomp_taskgroup
*taskgroup
)
315 struct gomp_thread_start_data
*start_data
= NULL
;
316 struct gomp_thread
*thr
, *nthr
;
317 struct gomp_task
*task
;
318 struct gomp_task_icv
*icv
;
320 struct gomp_thread_pool
*pool
;
321 unsigned i
, n
, old_threads_used
= 0;
322 pthread_attr_t thread_attr
, *attr
;
323 unsigned long nthreads_var
;
325 unsigned int s
= 0, rest
= 0, p
= 0, k
= 0;
326 unsigned int affinity_count
= 0;
327 struct gomp_thread
**affinity_thr
= NULL
;
328 bool force_display
= false;
330 thr
= gomp_thread ();
331 nested
= thr
->ts
.level
;
332 pool
= thr
->thread_pool
;
334 icv
= task
? &task
->icv
: &gomp_global_icv
;
335 if (__builtin_expect (gomp_places_list
!= NULL
, 0) && thr
->place
== 0)
337 gomp_init_affinity ();
338 if (__builtin_expect (gomp_display_affinity_var
, 0) && nthreads
== 1)
339 gomp_display_affinity_thread (gomp_thread_self (), &thr
->ts
,
343 /* Always save the previous state, even if this isn't a nested team.
344 In particular, we should save any work share state from an outer
345 orphaned work share construct. */
346 team
->prev_ts
= thr
->ts
;
352 ++thr
->ts
.active_level
;
353 thr
->ts
.work_share
= &team
->work_shares
[0];
354 thr
->ts
.last_work_share
= NULL
;
355 #ifdef HAVE_SYNC_BUILTINS
356 thr
->ts
.single_count
= 0;
358 thr
->ts
.static_trip
= 0;
359 thr
->task
= &team
->implicit_task
[0];
360 #ifdef GOMP_NEEDS_THREAD_HANDLE
361 thr
->handle
= pthread_self ();
363 nthreads_var
= icv
->nthreads_var
;
364 if (__builtin_expect (gomp_nthreads_var_list
!= NULL
, 0)
365 && thr
->ts
.level
< gomp_nthreads_var_list_len
)
366 nthreads_var
= gomp_nthreads_var_list
[thr
->ts
.level
];
367 bind_var
= icv
->bind_var
;
368 if (bind_var
!= omp_proc_bind_false
&& (flags
& 7) != omp_proc_bind_false
)
369 bind_var
= flags
& 7;
371 if (__builtin_expect (gomp_bind_var_list
!= NULL
, 0)
372 && thr
->ts
.level
< gomp_bind_var_list_len
)
373 bind_var
= gomp_bind_var_list
[thr
->ts
.level
];
374 gomp_init_task (thr
->task
, task
, icv
);
375 thr
->task
->taskgroup
= taskgroup
;
376 team
->implicit_task
[0].icv
.nthreads_var
= nthreads_var
;
377 team
->implicit_task
[0].icv
.bind_var
= bind_var
;
384 if (__builtin_expect (gomp_places_list
!= NULL
, 0))
386 /* Depending on chosen proc_bind model, set subpartition
387 for the master thread and initialize helper variables
388 P and optionally S, K and/or REST used by later place
389 computation for each additional thread. */
393 case omp_proc_bind_true
:
394 case omp_proc_bind_close
:
395 if (nthreads
> thr
->ts
.place_partition_len
)
397 /* T > P. S threads will be placed in each place,
398 and the final REM threads placed one by one
399 into the already occupied places. */
400 s
= nthreads
/ thr
->ts
.place_partition_len
;
401 rest
= nthreads
% thr
->ts
.place_partition_len
;
407 case omp_proc_bind_master
:
408 /* Each thread will be bound to master's place. */
410 case omp_proc_bind_spread
:
411 if (nthreads
<= thr
->ts
.place_partition_len
)
413 /* T <= P. Each subpartition will have in between s
414 and s+1 places (subpartitions starting at or
415 after rest will have s places, earlier s+1 places),
416 each thread will be bound to the first place in
417 its subpartition (except for the master thread
418 that can be bound to another place in its
420 s
= thr
->ts
.place_partition_len
/ nthreads
;
421 rest
= thr
->ts
.place_partition_len
% nthreads
;
422 rest
= (s
+ 1) * rest
+ thr
->ts
.place_partition_off
;
425 p
-= (p
- thr
->ts
.place_partition_off
) % (s
+ 1);
426 thr
->ts
.place_partition_len
= s
+ 1;
431 thr
->ts
.place_partition_len
= s
;
433 thr
->ts
.place_partition_off
= p
;
437 /* T > P. Each subpartition will have just a single
438 place and we'll place between s and s+1
439 threads into each subpartition. */
440 s
= nthreads
/ thr
->ts
.place_partition_len
;
441 rest
= nthreads
% thr
->ts
.place_partition_len
;
442 thr
->ts
.place_partition_off
= p
;
443 thr
->ts
.place_partition_len
= 1;
450 bind
= omp_proc_bind_false
;
452 /* We only allow the reuse of idle threads for non-nested PARALLEL
453 regions. This appears to be implied by the semantics of
454 threadprivate variables, but perhaps that's reading too much into
455 things. Certainly it does prevent any locking problems, since
456 only the initial program thread will modify gomp_threads. */
459 old_threads_used
= pool
->threads_used
;
461 if (nthreads
<= old_threads_used
)
463 else if (old_threads_used
== 0)
466 gomp_simple_barrier_init (&pool
->threads_dock
, nthreads
);
470 n
= old_threads_used
;
472 /* Increase the barrier threshold to make sure all new
473 threads arrive before the team is released. */
474 gomp_simple_barrier_reinit (&pool
->threads_dock
, nthreads
);
477 /* Not true yet, but soon will be. We're going to release all
478 threads from the dock, and those that aren't part of the
480 pool
->threads_used
= nthreads
;
482 /* If necessary, expand the size of the gomp_threads array. It is
483 expected that changes in the number of threads are rare, thus we
484 make no effort to expand gomp_threads_size geometrically. */
485 if (nthreads
>= pool
->threads_size
)
487 pool
->threads_size
= nthreads
+ 1;
489 = gomp_realloc (pool
->threads
,
491 * sizeof (struct gomp_thread
*));
492 /* Add current (master) thread to threads[]. */
493 pool
->threads
[0] = thr
;
496 /* Release existing idle threads. */
499 unsigned int place_partition_off
= thr
->ts
.place_partition_off
;
500 unsigned int place_partition_len
= thr
->ts
.place_partition_len
;
501 unsigned int place
= 0;
502 if (__builtin_expect (gomp_places_list
!= NULL
, 0))
506 case omp_proc_bind_true
:
507 case omp_proc_bind_close
:
511 if (p
== (team
->prev_ts
.place_partition_off
512 + team
->prev_ts
.place_partition_len
))
513 p
= team
->prev_ts
.place_partition_off
;
515 if (i
== nthreads
- rest
)
521 case omp_proc_bind_master
:
523 case omp_proc_bind_spread
:
531 if (p
== (team
->prev_ts
.place_partition_off
532 + team
->prev_ts
.place_partition_len
))
533 p
= team
->prev_ts
.place_partition_off
;
534 place_partition_off
= p
;
536 place_partition_len
= s
+ 1;
538 place_partition_len
= s
;
546 if (p
== (team
->prev_ts
.place_partition_off
547 + team
->prev_ts
.place_partition_len
))
548 p
= team
->prev_ts
.place_partition_off
;
550 if (i
== nthreads
- rest
)
555 place_partition_off
= p
;
556 place_partition_len
= 1;
560 if (affinity_thr
!= NULL
561 || (bind
!= omp_proc_bind_true
562 && pool
->threads
[i
]->place
!= p
+ 1)
563 || pool
->threads
[i
]->place
<= place_partition_off
564 || pool
->threads
[i
]->place
> (place_partition_off
565 + place_partition_len
))
568 force_display
= true;
569 if (affinity_thr
== NULL
)
573 if (team
->prev_ts
.place_partition_len
> 64)
575 = gomp_malloc (team
->prev_ts
.place_partition_len
576 * sizeof (struct gomp_thread
*));
579 = gomp_alloca (team
->prev_ts
.place_partition_len
580 * sizeof (struct gomp_thread
*));
581 memset (affinity_thr
, '\0',
582 team
->prev_ts
.place_partition_len
583 * sizeof (struct gomp_thread
*));
584 for (j
= i
; j
< old_threads_used
; j
++)
586 if (pool
->threads
[j
]->place
587 > team
->prev_ts
.place_partition_off
588 && (pool
->threads
[j
]->place
589 <= (team
->prev_ts
.place_partition_off
590 + team
->prev_ts
.place_partition_len
)))
592 l
= pool
->threads
[j
]->place
- 1
593 - team
->prev_ts
.place_partition_off
;
594 pool
->threads
[j
]->data
= affinity_thr
[l
];
595 affinity_thr
[l
] = pool
->threads
[j
];
597 pool
->threads
[j
] = NULL
;
599 if (nthreads
> old_threads_used
)
600 memset (&pool
->threads
[old_threads_used
],
601 '\0', ((nthreads
- old_threads_used
)
602 * sizeof (struct gomp_thread
*)));
604 affinity_count
= old_threads_used
- i
;
606 if (affinity_count
== 0)
609 if (affinity_thr
[l
- team
->prev_ts
.place_partition_off
]
612 if (bind
!= omp_proc_bind_true
)
614 for (l
= place_partition_off
;
615 l
< place_partition_off
+ place_partition_len
;
617 if (affinity_thr
[l
- team
->prev_ts
.place_partition_off
]
620 if (l
== place_partition_off
+ place_partition_len
)
623 nthr
= affinity_thr
[l
- team
->prev_ts
.place_partition_off
];
624 affinity_thr
[l
- team
->prev_ts
.place_partition_off
]
625 = (struct gomp_thread
*) nthr
->data
;
627 pool
->threads
[i
] = nthr
;
630 nthr
= pool
->threads
[i
];
634 nthr
= pool
->threads
[i
];
635 nthr
->ts
.team
= team
;
636 nthr
->ts
.work_share
= &team
->work_shares
[0];
637 nthr
->ts
.last_work_share
= NULL
;
638 nthr
->ts
.team_id
= i
;
639 nthr
->ts
.level
= team
->prev_ts
.level
+ 1;
640 nthr
->ts
.active_level
= thr
->ts
.active_level
;
641 nthr
->ts
.place_partition_off
= place_partition_off
;
642 nthr
->ts
.place_partition_len
= place_partition_len
;
643 nthr
->ts
.def_allocator
= thr
->ts
.def_allocator
;
644 #ifdef HAVE_SYNC_BUILTINS
645 nthr
->ts
.single_count
= 0;
647 nthr
->ts
.static_trip
= 0;
648 nthr
->task
= &team
->implicit_task
[i
];
650 gomp_init_task (nthr
->task
, task
, icv
);
651 team
->implicit_task
[i
].icv
.nthreads_var
= nthreads_var
;
652 team
->implicit_task
[i
].icv
.bind_var
= bind_var
;
653 nthr
->task
->taskgroup
= taskgroup
;
656 team
->ordered_release
[i
] = &nthr
->release
;
659 if (__builtin_expect (affinity_thr
!= NULL
, 0))
661 /* If AFFINITY_THR is non-NULL just because we had to
662 permute some threads in the pool, but we've managed
663 to find exactly as many old threads as we'd find
664 without affinity, we don't need to handle this
665 specially anymore. */
666 if (nthreads
<= old_threads_used
667 ? (affinity_count
== old_threads_used
- nthreads
)
668 : (i
== old_threads_used
))
670 if (team
->prev_ts
.place_partition_len
> 64)
678 /* We are going to compute the places/subpartitions
679 again from the beginning. So, we need to reinitialize
680 vars modified by the switch (bind) above inside
681 of the loop, to the state they had after the initial
685 case omp_proc_bind_true
:
686 case omp_proc_bind_close
:
687 if (nthreads
> thr
->ts
.place_partition_len
)
688 /* T > P. S has been changed, so needs
690 s
= nthreads
/ thr
->ts
.place_partition_len
;
694 case omp_proc_bind_master
:
695 /* No vars have been changed. */
697 case omp_proc_bind_spread
:
698 p
= thr
->ts
.place_partition_off
;
702 s
= nthreads
/ team
->prev_ts
.place_partition_len
;
708 /* Increase the barrier threshold to make sure all new
709 threads and all the threads we're going to let die
710 arrive before the team is released. */
712 gomp_simple_barrier_reinit (&pool
->threads_dock
,
713 nthreads
+ affinity_count
);
722 if (__builtin_expect (nthreads
+ affinity_count
> old_threads_used
, 0))
724 long diff
= (long) (nthreads
+ affinity_count
) - (long) old_threads_used
;
726 if (old_threads_used
== 0)
729 #ifdef HAVE_SYNC_BUILTINS
730 __sync_fetch_and_add (&gomp_managed_threads
, diff
);
732 gomp_mutex_lock (&gomp_managed_threads_lock
);
733 gomp_managed_threads
+= diff
;
734 gomp_mutex_unlock (&gomp_managed_threads_lock
);
738 attr
= &gomp_thread_attr
;
739 if (__builtin_expect (gomp_places_list
!= NULL
, 0))
742 pthread_attr_init (&thread_attr
);
743 if (! pthread_attr_getstacksize (&gomp_thread_attr
, &stacksize
))
744 pthread_attr_setstacksize (&thread_attr
, stacksize
);
748 start_data
= gomp_alloca (sizeof (struct gomp_thread_start_data
)
751 /* Launch new threads. */
752 for (; i
< nthreads
; ++i
)
756 start_data
->ts
.place_partition_off
= thr
->ts
.place_partition_off
;
757 start_data
->ts
.place_partition_len
= thr
->ts
.place_partition_len
;
758 start_data
->place
= 0;
759 if (__builtin_expect (gomp_places_list
!= NULL
, 0))
763 case omp_proc_bind_true
:
764 case omp_proc_bind_close
:
768 if (p
== (team
->prev_ts
.place_partition_off
769 + team
->prev_ts
.place_partition_len
))
770 p
= team
->prev_ts
.place_partition_off
;
772 if (i
== nthreads
- rest
)
778 case omp_proc_bind_master
:
780 case omp_proc_bind_spread
:
788 if (p
== (team
->prev_ts
.place_partition_off
789 + team
->prev_ts
.place_partition_len
))
790 p
= team
->prev_ts
.place_partition_off
;
791 start_data
->ts
.place_partition_off
= p
;
793 start_data
->ts
.place_partition_len
= s
+ 1;
795 start_data
->ts
.place_partition_len
= s
;
803 if (p
== (team
->prev_ts
.place_partition_off
804 + team
->prev_ts
.place_partition_len
))
805 p
= team
->prev_ts
.place_partition_off
;
807 if (i
== nthreads
- rest
)
812 start_data
->ts
.place_partition_off
= p
;
813 start_data
->ts
.place_partition_len
= 1;
817 start_data
->place
= p
+ 1;
818 if (affinity_thr
!= NULL
&& pool
->threads
[i
] != NULL
)
820 gomp_init_thread_affinity (attr
, p
);
824 start_data
->fn_data
= data
;
825 start_data
->ts
.team
= team
;
826 start_data
->ts
.work_share
= &team
->work_shares
[0];
827 start_data
->ts
.last_work_share
= NULL
;
828 start_data
->ts
.team_id
= i
;
829 start_data
->ts
.level
= team
->prev_ts
.level
+ 1;
830 start_data
->ts
.active_level
= thr
->ts
.active_level
;
831 start_data
->ts
.def_allocator
= thr
->ts
.def_allocator
;
832 #ifdef HAVE_SYNC_BUILTINS
833 start_data
->ts
.single_count
= 0;
835 start_data
->ts
.static_trip
= 0;
836 start_data
->task
= &team
->implicit_task
[i
];
837 gomp_init_task (start_data
->task
, task
, icv
);
838 team
->implicit_task
[i
].icv
.nthreads_var
= nthreads_var
;
839 team
->implicit_task
[i
].icv
.bind_var
= bind_var
;
840 start_data
->task
->taskgroup
= taskgroup
;
841 start_data
->thread_pool
= pool
;
842 start_data
->nested
= nested
;
844 attr
= gomp_adjust_thread_attr (attr
, &thread_attr
);
845 err
= pthread_create (&start_data
->handle
, attr
, gomp_thread_start
,
849 gomp_fatal ("Thread creation failed: %s", strerror (err
));
852 if (__builtin_expect (attr
== &thread_attr
, 0))
853 pthread_attr_destroy (&thread_attr
);
857 gomp_barrier_wait (&team
->barrier
);
859 gomp_simple_barrier_wait (&pool
->threads_dock
);
861 /* Decrease the barrier threshold to match the number of threads
862 that should arrive back at the end of this team. The extra
863 threads should be exiting. Note that we arrange for this test
864 to never be true for nested teams. If AFFINITY_COUNT is non-zero,
865 the barrier as well as gomp_managed_threads was temporarily
866 set to NTHREADS + AFFINITY_COUNT. For NTHREADS < OLD_THREADS_COUNT,
867 AFFINITY_COUNT if non-zero will be always at least
868 OLD_THREADS_COUNT - NTHREADS. */
869 if (__builtin_expect (nthreads
< old_threads_used
, 0)
870 || __builtin_expect (affinity_count
, 0))
872 long diff
= (long) nthreads
- (long) old_threads_used
;
875 diff
= -affinity_count
;
877 gomp_simple_barrier_reinit (&pool
->threads_dock
, nthreads
);
879 #ifdef HAVE_SYNC_BUILTINS
880 __sync_fetch_and_add (&gomp_managed_threads
, diff
);
882 gomp_mutex_lock (&gomp_managed_threads_lock
);
883 gomp_managed_threads
+= diff
;
884 gomp_mutex_unlock (&gomp_managed_threads_lock
);
887 if (__builtin_expect (gomp_display_affinity_var
, 0))
890 || nthreads
!= old_threads_used
893 gomp_display_affinity_thread (gomp_thread_self (), &thr
->ts
,
897 start_data
-= nthreads
- 1;
898 for (i
= 1; i
< nthreads
; ++i
)
900 gomp_display_affinity_thread (
901 #ifdef LIBGOMP_USE_PTHREADS
913 for (i
= 1; i
< nthreads
; ++i
)
915 gomp_thread_handle handle
916 = gomp_thread_to_pthread_t (pool
->threads
[i
]);
917 gomp_display_affinity_thread (handle
, &pool
->threads
[i
]->ts
,
918 pool
->threads
[i
]->place
);
923 if (__builtin_expect (affinity_thr
!= NULL
, 0)
924 && team
->prev_ts
.place_partition_len
> 64)
930 /* Terminate the current team. This is only to be called by the master
931 thread. We assume that we must wait for the other threads. */
936 struct gomp_thread
*thr
= gomp_thread ();
937 struct gomp_team
*team
= thr
->ts
.team
;
939 /* This barrier handles all pending explicit threads.
940 As #pragma omp cancel parallel might get awaited count in
941 team->barrier in a inconsistent state, we need to use a different
943 gomp_team_barrier_wait_final (&team
->barrier
);
944 if (__builtin_expect (team
->team_cancelled
, 0))
946 struct gomp_work_share
*ws
= team
->work_shares_to_free
;
949 struct gomp_work_share
*next_ws
= gomp_ptrlock_get (&ws
->next_ws
);
951 gomp_ptrlock_set (&ws
->next_ws
, ws
);
952 gomp_fini_work_share (ws
);
958 gomp_fini_work_share (thr
->ts
.work_share
);
961 thr
->ts
= team
->prev_ts
;
963 if (__builtin_expect (thr
->ts
.level
!= 0, 0))
965 #ifdef HAVE_SYNC_BUILTINS
966 __sync_fetch_and_add (&gomp_managed_threads
, 1L - team
->nthreads
);
968 gomp_mutex_lock (&gomp_managed_threads_lock
);
969 gomp_managed_threads
-= team
->nthreads
- 1L;
970 gomp_mutex_unlock (&gomp_managed_threads_lock
);
972 /* This barrier has gomp_barrier_wait_last counterparts
973 and ensures the team can be safely destroyed. */
974 gomp_barrier_wait (&team
->barrier
);
977 if (__builtin_expect (team
->work_shares
[0].next_alloc
!= NULL
, 0))
979 struct gomp_work_share
*ws
= team
->work_shares
[0].next_alloc
;
982 struct gomp_work_share
*next_ws
= ws
->next_alloc
;
988 gomp_sem_destroy (&team
->master_release
);
990 if (__builtin_expect (thr
->ts
.team
!= NULL
, 0)
991 || __builtin_expect (team
->nthreads
== 1, 0))
995 struct gomp_thread_pool
*pool
= thr
->thread_pool
;
997 free_team (pool
->last_team
);
998 pool
->last_team
= team
;
999 gomp_release_thread_pool (pool
);
1003 #ifdef LIBGOMP_USE_PTHREADS
1005 /* Constructors for this file. */
1007 static void __attribute__((constructor
))
1008 initialize_team (void)
1010 #if !defined HAVE_TLS && !defined USE_EMUTLS
1011 static struct gomp_thread initial_thread_tls_data
;
1013 pthread_key_create (&gomp_tls_key
, NULL
);
1014 pthread_setspecific (gomp_tls_key
, &initial_thread_tls_data
);
1017 if (pthread_key_create (&gomp_thread_destructor
, gomp_free_thread
) != 0)
1018 gomp_fatal ("could not create thread pool destructor.");
1021 static void __attribute__((destructor
))
1022 team_destructor (void)
1024 /* Without this dlclose on libgomp could lead to subsequent
1026 pthread_key_delete (gomp_thread_destructor
);
1029 /* Similar to gomp_free_pool_helper, but don't detach itself,
1030 gomp_pause_host will pthread_join those threads. */
1033 gomp_pause_pool_helper (void *thread_pool
)
1035 struct gomp_thread
*thr
= gomp_thread ();
1036 struct gomp_thread_pool
*pool
1037 = (struct gomp_thread_pool
*) thread_pool
;
1038 gomp_simple_barrier_wait_last (&pool
->threads_dock
);
1039 gomp_sem_destroy (&thr
->release
);
1040 thr
->thread_pool
= NULL
;
1042 pthread_exit (NULL
);
1045 /* Free a thread pool and release its threads. Return non-zero on
1049 gomp_pause_host (void)
1051 struct gomp_thread
*thr
= gomp_thread ();
1052 struct gomp_thread_pool
*pool
= thr
->thread_pool
;
1057 if (pool
->threads_used
> 0)
1061 = gomp_alloca (sizeof (pthread_t
) * pool
->threads_used
);
1062 for (i
= 1; i
< pool
->threads_used
; i
++)
1064 struct gomp_thread
*nthr
= pool
->threads
[i
];
1065 nthr
->fn
= gomp_pause_pool_helper
;
1067 thrs
[i
] = gomp_thread_to_pthread_t (nthr
);
1069 /* This barrier undocks threads docked on pool->threads_dock. */
1070 gomp_simple_barrier_wait (&pool
->threads_dock
);
1071 /* And this waits till all threads have called gomp_barrier_wait_last
1072 in gomp_pause_pool_helper. */
1073 gomp_simple_barrier_wait (&pool
->threads_dock
);
1074 /* Now it is safe to destroy the barrier and free the pool. */
1075 gomp_simple_barrier_destroy (&pool
->threads_dock
);
1077 #ifdef HAVE_SYNC_BUILTINS
1078 __sync_fetch_and_add (&gomp_managed_threads
,
1079 1L - pool
->threads_used
);
1081 gomp_mutex_lock (&gomp_managed_threads_lock
);
1082 gomp_managed_threads
-= pool
->threads_used
- 1L;
1083 gomp_mutex_unlock (&gomp_managed_threads_lock
);
1085 for (i
= 1; i
< pool
->threads_used
; i
++)
1086 pthread_join (thrs
[i
], NULL
);
1088 if (pool
->last_team
)
1089 free_team (pool
->last_team
);
1091 team_free (pool
->threads
);
1094 thr
->thread_pool
= NULL
;
1100 struct gomp_task_icv
*
1103 struct gomp_thread
*thr
= gomp_thread ();
1104 struct gomp_task
*task
= gomp_malloc (sizeof (struct gomp_task
));
1105 gomp_init_task (task
, NULL
, &gomp_global_icv
);
1107 #ifdef LIBGOMP_USE_PTHREADS
1108 pthread_setspecific (gomp_thread_destructor
, thr
);