1 /* System dependent pthreads code.  Hurd version.
2    Copyright (C) 2000-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 <assert.h>
20 #include <stddef.h>
21 #include <stdint.h>
22 
23 #include <mach.h>
24 #include <mach/mig_support.h>
25 
26 #include <pt-internal.h>
27 #include <pthreadP.h>
28 
29 __thread struct __pthread *___pthread_self;
30 
31 static void
reset_pthread_total(void)32 reset_pthread_total (void)
33 {
34   /* Only current thread remains */
35   __pthread_total = 1;
36 }
37 
38 /* This function is called from the Hurd-specific startup code.  It
39    should return a new stack pointer for the main thread.  The caller
40    will switch to this new stack before doing anything serious.  */
41 static void
_init_routine(void * stack)42 _init_routine (void *stack)
43 {
44   struct __pthread *thread;
45   int err;
46   pthread_attr_t attr, *attrp = 0;
47 
48   if (GL (dl_pthread_threads) != NULL)
49     /* Already initialized */
50     return;
51 
52   /* Initialize the library.  */
53   ___pthread_init ();
54 
55   if (stack != NULL)
56     {
57       /* We are getting initialized due to dlopening a library using libpthread
58          while the main program was not linked against libpthread.  */
59       /* Avoid allocating another stack */
60       attrp = &attr;
61       __pthread_attr_init (attrp);
62       __pthread_attr_setstack (attrp, stack, __vm_page_size);
63     }
64 
65   /* Create the pthread structure for the main thread (i.e. us).  */
66   err = __pthread_create_internal (&thread, attrp, 0, 0);
67   assert_perror (err);
68 
69   /* XXX The caller copies the command line arguments and the environment
70      to the new stack.  Pretend it wasn't allocated so that it remains
71      valid if the main thread terminates.  */
72   thread->stack = 0;
73   thread->tcb = THREAD_SELF;
74 
75 #ifndef PAGESIZE
76   __pthread_default_attr.__guardsize = __vm_page_size;
77 #endif
78 
79   ___pthread_self = thread;
80 
81   /* Decrease the number of threads, to take into account that the
82      signal thread (which will be created by the glibc startup code
83      when we return from here) shouldn't be seen as a user thread.  */
84   __pthread_total--;
85 
86   __pthread_atfork (NULL, NULL, reset_pthread_total);
87 
88   GL(dl_init_static_tls) = &__pthread_init_static_tls;
89 
90   /* Make MiG code thread aware.  */
91   __mig_init (thread->stackaddr);
92 }
93 
94 void
__pthread_initialize_minimal(void)95 __pthread_initialize_minimal (void)
96 {
97   _init_routine (__libc_stack_end);
98 }
99 
100 #ifdef SHARED
101 __attribute__ ((constructor))
102 static void
dynamic_init_routine(void)103 dynamic_init_routine (void)
104 {
105   _init_routine (__libc_stack_end);
106 }
107 #endif
108