1 /* A thread attribute with a small stack.
2    Copyright (C) 2019-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <support/xthread.h>
20 #include <allocate_once.h>
21 
22 static void *
allocate(void * closure)23 allocate (void *closure)
24 {
25   pthread_attr_t *result = malloc (sizeof (*result));
26   xpthread_attr_init (result);
27   support_set_small_thread_stack_size (result);
28   return result;
29 }
30 
31 static void
deallocate(void * ptr,void * closure)32 deallocate (void *ptr, void *closure)
33 {
34   xpthread_attr_destroy (ptr);
35   free (ptr);
36 }
37 
38 static void *small_stack_attr;
39 
40 pthread_attr_t *
support_small_stack_thread_attribute(void)41 support_small_stack_thread_attribute (void)
42 {
43   return allocate_once (&small_stack_attr, allocate, deallocate, NULL);
44 }
45 
46 static void __attribute__ ((destructor))
fini(void)47 fini (void)
48 {
49   if (small_stack_attr != NULL)
50     {
51       deallocate (small_stack_attr, NULL);
52       small_stack_attr = NULL;
53     }
54 }
55