ptfork.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* Linuxthreads - a simple clone()-based implementation of Posix */
  2. /* threads for Linux. */
  3. /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
  4. /* */
  5. /* This program is free software; you can redistribute it and/or */
  6. /* modify it under the terms of the GNU Library General Public License */
  7. /* as published by the Free Software Foundation; either version 2 */
  8. /* of the License, or (at your option) any later version. */
  9. /* */
  10. /* This program 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 */
  13. /* GNU Library General Public License for more details. */
  14. /* mods for uClibc: removed strong alias and defined funcs properly */
  15. /* The "atfork" stuff */
  16. #include <errno.h>
  17. #ifdef __ARCH_USE_MMU__
  18. #include <bits/uClibc_mutex.h>
  19. #include <stddef.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include "pthread.h"
  23. #include "internals.h"
  24. struct handler_list {
  25. void (*handler)(void);
  26. struct handler_list * next;
  27. };
  28. static pthread_mutex_t pthread_atfork_lock = PTHREAD_MUTEX_INITIALIZER;
  29. static struct handler_list * pthread_atfork_prepare = NULL;
  30. static struct handler_list * pthread_atfork_parent = NULL;
  31. static struct handler_list * pthread_atfork_child = NULL;
  32. #ifdef __MALLOC__
  33. __UCLIBC_MUTEX_EXTERN(__malloc_heap_lock);
  34. __UCLIBC_MUTEX_EXTERN(__malloc_sbrk_lock);
  35. #ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
  36. __UCLIBC_MUTEX_EXTERN(__malloc_mmb_heap_lock);
  37. #endif
  38. #elif defined(__MALLOC_STANDARD__) || defined(__MALLOC_SIMPLE__)
  39. __UCLIBC_MUTEX_EXTERN(__malloc_lock);
  40. #endif
  41. static void pthread_insert_list(struct handler_list ** list,
  42. void (*handler)(void),
  43. struct handler_list * newlist,
  44. int at_end)
  45. {
  46. if (handler == NULL) return;
  47. if (at_end) {
  48. while(*list != NULL) list = &((*list)->next);
  49. }
  50. newlist->handler = handler;
  51. newlist->next = *list;
  52. *list = newlist;
  53. }
  54. struct handler_list_block {
  55. struct handler_list prepare, parent, child;
  56. };
  57. int pthread_atfork(void (*prepare)(void),
  58. void (*parent)(void),
  59. void (*child)(void))
  60. {
  61. struct handler_list_block * block =
  62. (struct handler_list_block *) malloc(sizeof(struct handler_list_block));
  63. if (block == NULL) return ENOMEM;
  64. __pthread_mutex_lock(&pthread_atfork_lock);
  65. /* "prepare" handlers are called in LIFO */
  66. pthread_insert_list(&pthread_atfork_prepare, prepare, &block->prepare, 0);
  67. /* "parent" handlers are called in FIFO */
  68. pthread_insert_list(&pthread_atfork_parent, parent, &block->parent, 1);
  69. /* "child" handlers are called in FIFO */
  70. pthread_insert_list(&pthread_atfork_child, child, &block->child, 1);
  71. __pthread_mutex_unlock(&pthread_atfork_lock);
  72. return 0;
  73. }
  74. /*strong_alias (__pthread_atfork, pthread_atfork)*/
  75. static __inline__ void pthread_call_handlers(struct handler_list * list)
  76. {
  77. for (/*nothing*/; list != NULL; list = list->next) (list->handler)();
  78. }
  79. void __pthread_once_fork_prepare(void);
  80. void __pthread_once_fork_child(void);
  81. void __pthread_once_fork_parent(void);
  82. static pid_t __fork(void)
  83. {
  84. pid_t pid;
  85. struct handler_list * prepare, * child, * parent;
  86. __pthread_mutex_lock(&pthread_atfork_lock);
  87. prepare = pthread_atfork_prepare;
  88. child = pthread_atfork_child;
  89. parent = pthread_atfork_parent;
  90. pthread_call_handlers(prepare);
  91. __pthread_once_fork_prepare();
  92. #ifdef __MALLOC__
  93. __pthread_mutex_lock(&__malloc_sbrk_lock);
  94. __pthread_mutex_lock(&__malloc_heap_lock);
  95. #ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
  96. __pthread_mutex_lock(&__malloc_mmb_heap_lock);
  97. #endif
  98. #elif defined(__MALLOC_STANDARD__) || defined(__MALLOC_SIMPLE__)
  99. __pthread_mutex_lock(&__malloc_lock);
  100. #endif
  101. pid = __libc_fork();
  102. if (pid == 0) {
  103. #if defined(__MALLOC_STANDARD__) || defined(__MALLOC_SIMPLE__)
  104. __libc_lock_init_recursive(__malloc_lock);
  105. #elif defined(__MALLOC__)
  106. #ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
  107. __libc_lock_init_adaptive(__malloc_mmb_heap_lock);
  108. #endif
  109. __libc_lock_init_adaptive(__malloc_heap_lock);
  110. __libc_lock_init(__malloc_sbrk_lock);
  111. #endif
  112. __libc_lock_init_adaptive(pthread_atfork_lock);
  113. __pthread_reset_main_thread();
  114. __fresetlockfiles();
  115. __pthread_once_fork_child();
  116. pthread_call_handlers(child);
  117. } else {
  118. #if defined(__MALLOC_STANDARD__) || defined(__MALLOC_SIMPLE__)
  119. __pthread_mutex_unlock(&__malloc_lock);
  120. #elif defined(__MALLOC__)
  121. #ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
  122. __pthread_mutex_unlock(&__malloc_mmb_heap_lock);
  123. #endif
  124. __pthread_mutex_unlock(&__malloc_heap_lock);
  125. __pthread_mutex_unlock(&__malloc_sbrk_lock);
  126. #endif
  127. __pthread_mutex_unlock(&pthread_atfork_lock);
  128. __pthread_once_fork_parent();
  129. pthread_call_handlers(parent);
  130. }
  131. return pid;
  132. }
  133. strong_alias(__fork,fork)
  134. strong_alias(__fork,vfork)
  135. #else
  136. /* We can't support pthread_atfork without MMU, since we don't have
  137. fork(), and we can't offer the correct semantics for vfork().
  138. But instead of failing, just provide a dummy */
  139. int pthread_atfork(void (*prepare)(void),
  140. void (*parent)(void),
  141. void (*child)(void))
  142. {
  143. return 0;
  144. }
  145. #endif