ptfork.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. extern __typeof(fork) __libc_fork;
  83. pid_t __fork(void) attribute_hidden;
  84. pid_t __fork(void)
  85. {
  86. pid_t pid;
  87. struct handler_list * prepare, * child, * parent;
  88. __pthread_mutex_lock(&pthread_atfork_lock);
  89. prepare = pthread_atfork_prepare;
  90. child = pthread_atfork_child;
  91. parent = pthread_atfork_parent;
  92. pthread_call_handlers(prepare);
  93. __pthread_once_fork_prepare();
  94. #ifdef __MALLOC__
  95. __pthread_mutex_lock(&__malloc_sbrk_lock);
  96. __pthread_mutex_lock(&__malloc_heap_lock);
  97. #ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
  98. __pthread_mutex_lock(&__malloc_mmb_heap_lock);
  99. #endif
  100. #elif defined(__MALLOC_STANDARD__) || defined(__MALLOC_SIMPLE__)
  101. __pthread_mutex_lock(&__malloc_lock);
  102. #endif
  103. pid = __libc_fork();
  104. if (pid == 0) {
  105. #if defined(__MALLOC_STANDARD__) || defined(__MALLOC_SIMPLE__)
  106. __libc_lock_init_recursive(__malloc_lock);
  107. #elif defined(__MALLOC__)
  108. #ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
  109. __libc_lock_init_adaptive(__malloc_mmb_heap_lock);
  110. #endif
  111. __libc_lock_init_adaptive(__malloc_heap_lock);
  112. __libc_lock_init(__malloc_sbrk_lock);
  113. #endif
  114. __libc_lock_init_adaptive(pthread_atfork_lock);
  115. __pthread_reset_main_thread();
  116. __fresetlockfiles();
  117. __pthread_once_fork_child();
  118. pthread_call_handlers(child);
  119. } else {
  120. #if defined(__MALLOC_STANDARD__) || defined(__MALLOC_SIMPLE__)
  121. __pthread_mutex_unlock(&__malloc_lock);
  122. #elif defined(__MALLOC__)
  123. #ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
  124. __pthread_mutex_unlock(&__malloc_mmb_heap_lock);
  125. #endif
  126. __pthread_mutex_unlock(&__malloc_heap_lock);
  127. __pthread_mutex_unlock(&__malloc_sbrk_lock);
  128. #endif
  129. __pthread_mutex_unlock(&pthread_atfork_lock);
  130. __pthread_once_fork_parent();
  131. pthread_call_handlers(parent);
  132. }
  133. return pid;
  134. }
  135. strong_alias(__fork,fork)
  136. pid_t vfork(void)
  137. {
  138. return __fork();
  139. }
  140. #else
  141. /* We can't support pthread_atfork without MMU, since we don't have
  142. fork(), and we can't offer the correct semantics for vfork(). */
  143. int pthread_atfork(void (*prepare)(void),
  144. void (*parent)(void),
  145. void (*child)(void))
  146. {
  147. /* ENOMEM is probably pushing it a little bit.
  148. Take it as `no *virtual* memory' :-) */
  149. errno = ENOMEM;
  150. return -1;
  151. }
  152. #endif