drm/i915: Set GPU freq to idle_freq initially
[dragonfly.git] / lib / libc_r / uthread / uthread_gc.c
bloba89c7efd90f3b588d2c53ae47b1efa66d35afba4
1 /*
2 * Copyright (c) 1998 John Birrell <jb@cimlogic.com.au>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by John Birrell.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * $FreeBSD: src/lib/libc_r/uthread/uthread_gc.c,v 1.11.2.5 2002/10/22 14:44:03 fjoe Exp $
34 * Garbage collector thread. Frees memory allocated for dead threads.
37 #include <errno.h>
38 #include <time.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <sys/types.h>
42 #include <sys/mman.h>
43 #include <pthread.h>
44 #include "pthread_private.h"
46 pthread_addr_t
47 _thread_gc(pthread_addr_t arg)
49 struct pthread *curthread = _get_curthread();
50 int f_debug;
51 int f_done = 0;
52 int ret;
53 sigset_t mask;
54 pthread_t pthread;
55 pthread_t pthread_cln;
56 struct timespec abstime;
57 void *p_stack;
59 /* Block all signals */
60 sigfillset(&mask);
61 pthread_sigmask(SIG_BLOCK, &mask, NULL);
63 /* Mark this thread as a library thread (not a user thread). */
64 curthread->flags |= PTHREAD_FLAGS_PRIVATE;
66 /* Set a debug flag based on an environment variable. */
67 f_debug = (getenv("LIBC_R_DEBUG") != NULL);
69 /* Set the name of this thread. */
70 pthread_set_name_np(curthread,"GC");
72 while (!f_done) {
73 /* Check if debugging this application. */
74 if (f_debug)
75 /* Dump thread info to file. */
76 _thread_dump_info();
79 * Defer signals to protect the scheduling queues from
80 * access by the signal handler:
82 _thread_kern_sig_defer();
84 /* Check if this is the last running thread: */
85 if (TAILQ_FIRST(&_thread_list) == curthread &&
86 TAILQ_NEXT(curthread, tle) == NULL)
88 * This is the last thread, so it can exit
89 * now.
91 f_done = 1;
94 * Undefer and handle pending signals, yielding if
95 * necessary:
97 _thread_kern_sig_undefer();
100 * Lock the garbage collector mutex which ensures that
101 * this thread sees another thread exit:
103 if (pthread_mutex_lock(&_gc_mutex) != 0)
104 PANIC("Cannot lock gc mutex");
106 /* No stack of thread structure to free yet: */
107 p_stack = NULL;
108 pthread_cln = NULL;
111 * Enter a loop to search for the first dead thread that
112 * has memory to free.
114 for (pthread = TAILQ_FIRST(&_dead_list);
115 p_stack == NULL && pthread_cln == NULL && pthread != NULL;
116 pthread = TAILQ_NEXT(pthread, dle)) {
117 /* Check if the initial thread: */
118 if (pthread == _thread_initial) {
119 /* Don't destroy the initial thread. */
122 * Check if this thread has detached:
124 else if ((pthread->attr.flags &
125 PTHREAD_DETACHED) != 0) {
126 /* Remove this thread from the dead list: */
127 TAILQ_REMOVE(&_dead_list, pthread, dle);
130 * Check if the stack was not specified by
131 * the caller to pthread_create() and has not
132 * been destroyed yet:
134 if (pthread->attr.stackaddr_attr == NULL &&
135 pthread->stack != NULL) {
136 if (pthread->attr.stacksize_attr
137 == PTHREAD_STACK_DEFAULT) {
139 * Default-size stack. Cache
140 * it:
142 struct stack *spare_stack;
144 spare_stack
145 = (pthread->stack
146 + PTHREAD_STACK_DEFAULT
147 - sizeof(struct stack));
148 SLIST_INSERT_HEAD(&_stackq,
149 spare_stack,
150 qe);
151 } else {
153 * Non-standard stack size.
154 * free() it outside the locks.
156 p_stack = pthread->stack;
161 * Point to the thread structure that must
162 * be freed outside the locks:
164 pthread_cln = pthread;
166 } else {
168 * This thread has not detached, so do
169 * not destroy it.
171 * Check if the stack was not specified by
172 * the caller to pthread_create() and has not
173 * been destroyed yet:
175 if (pthread->attr.stackaddr_attr == NULL &&
176 pthread->stack != NULL) {
177 if (pthread->attr.stacksize_attr
178 == PTHREAD_STACK_DEFAULT) {
180 * Default-size stack. Cache
181 * it:
183 struct stack *spare_stack;
185 spare_stack
186 = (pthread->stack
187 + PTHREAD_STACK_DEFAULT
188 - sizeof(struct stack));
189 SLIST_INSERT_HEAD(&_stackq,
190 spare_stack,
191 qe);
192 } else {
194 * Non-standard stack size.
195 * free() it outside the locks:
197 p_stack = pthread->stack;
201 * NULL the stack pointer now
202 * that the memory has been freed:
204 pthread->stack = NULL;
210 * Check if this is not the last thread and there is no
211 * memory to free this time around.
213 if (!f_done && p_stack == NULL && pthread_cln == NULL) {
214 /* Get the current time. */
215 if (clock_gettime(CLOCK_REALTIME,&abstime) != 0)
216 PANIC("gc cannot get time");
219 * Do a backup poll in 10 seconds if no threads
220 * die before then.
222 abstime.tv_sec += 10;
225 * Wait for a signal from a dying thread or a
226 * timeout (for a backup poll).
228 if ((ret = pthread_cond_timedwait(&_gc_cond,
229 &_gc_mutex, &abstime)) != 0 && ret != ETIMEDOUT)
230 PANIC("gc cannot wait for a signal");
233 /* Unlock the garbage collector mutex: */
234 if (pthread_mutex_unlock(&_gc_mutex) != 0)
235 PANIC("Cannot unlock gc mutex");
238 * If there is memory to free, do it now. The call to
239 * free() might block, so this must be done outside the
240 * locks.
242 if (p_stack != NULL)
243 free(p_stack);
244 if (pthread_cln != NULL) {
245 if (pthread_cln->name != NULL) {
246 /* Free the thread name string. */
247 free(pthread_cln->name);
249 _rtld_free_tls(pthread_cln->tcb);
251 * Free the memory allocated for the thread
252 * structure.
254 free(pthread_cln);
257 return (NULL);