+/* Allocate and initialize a thread pool. */
+
+static struct gomp_thread_pool *gomp_new_thread_pool (void)
+{
+ struct gomp_thread_pool *pool
+ = gomp_malloc (sizeof(struct gomp_thread_pool));
+ pool->threads = NULL;
+ pool->threads_size = 0;
+ pool->threads_used = 0;
+ pool->last_team = NULL;
+ return pool;
+}
+
+static void
+gomp_free_pool_helper (void *thread_pool)
+{
+ struct gomp_thread_pool *pool
+ = (struct gomp_thread_pool *) thread_pool;
+ gomp_barrier_wait_last (&pool->threads_dock);
+ gomp_sem_destroy (&gomp_thread ()->release);
+ pthread_exit (NULL);
+}
+
+/* Free a thread pool and release its threads. */
+
+static void
+gomp_free_thread (void *arg __attribute__((unused)))
+{
+ struct gomp_thread *thr = gomp_thread ();
+ struct gomp_thread_pool *pool = thr->thread_pool;
+ if (pool)
+ {
+ if (pool->threads_used > 0)
+ {
+ int i;
+ for (i = 1; i < pool->threads_used; i++)
+ {
+ struct gomp_thread *nthr = pool->threads[i];
+ nthr->fn = gomp_free_pool_helper;
+ nthr->data = pool;
+ }
+ /* This barrier undocks threads docked on pool->threads_dock. */
+ gomp_barrier_wait (&pool->threads_dock);
+ /* And this waits till all threads have called gomp_barrier_wait_last
+ in gomp_free_pool_helper. */
+ gomp_barrier_wait (&pool->threads_dock);
+ /* Now it is safe to destroy the barrier and free the pool. */
+ gomp_barrier_destroy (&pool->threads_dock);
+
+#ifdef HAVE_SYNC_BUILTINS
+ __sync_fetch_and_add (&gomp_managed_threads,
+ 1L - pool->threads_used);
+#else
+ gomp_mutex_lock (&gomp_remaining_threads_lock);
+ gomp_managed_threads -= pool->threads_used - 1L;
+ gomp_mutex_unlock (&gomp_remaining_threads_lock);
+#endif
+ }
+ free (pool->threads);
+ if (pool->last_team)
+ free_team (pool->last_team);
+ free (pool);
+ thr->thread_pool = NULL;
+ }
+ if (thr->task != NULL)
+ {
+ struct gomp_task *task = thr->task;
+ gomp_end_task ();
+ free (task);
+ }
+}