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