summaryrefslogtreecommitdiff
path: root/libpthread/linuxthreads/ptfork.c
blob: 74d63083087871d8f8842d8dee3cbd5d4d088f68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* Linuxthreads - a simple clone()-based implementation of Posix        */
/* threads for Linux.                                                   */
/* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr)              */
/*                                                                      */
/* This program is free software; you can redistribute it and/or        */
/* modify it under the terms of the GNU Library General Public License  */
/* as published by the Free Software Foundation; either version 2       */
/* of the License, or (at your option) any later version.               */
/*                                                                      */
/* This program is distributed in the hope that it will be useful,      */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of       */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        */
/* GNU Library General Public License for more details.                 */

/* mods for uClibc: removed strong alias and defined funcs properly */

/* The "atfork" stuff */

#include <errno.h>

#ifdef __ARCH_USE_MMU__

#include <bits/uClibc_mutex.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include "pthread.h"
#include "internals.h"

struct handler_list {
  void (*handler)(void);
  struct handler_list * next;
};

static pthread_mutex_t pthread_atfork_lock = PTHREAD_MUTEX_INITIALIZER;
static struct handler_list * pthread_atfork_prepare = NULL;
static struct handler_list * pthread_atfork_parent = NULL;
static struct handler_list * pthread_atfork_child = NULL;

#ifdef __MALLOC__
__UCLIBC_MUTEX_EXTERN(__malloc_heap_lock);
__UCLIBC_MUTEX_EXTERN(__malloc_sbrk_lock);
#ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
__UCLIBC_MUTEX_EXTERN(__malloc_mmb_heap_lock);
#endif
#elif defined(__MALLOC_STANDARD__) || defined(__MALLOC_SIMPLE__)
__UCLIBC_MUTEX_EXTERN(__malloc_lock);
#endif

static void pthread_insert_list(struct handler_list ** list,
                                void (*handler)(void),
                                struct handler_list * newlist,
                                int at_end)
{
  if (handler == NULL) return;
  if (at_end) {
    while(*list != NULL) list = &((*list)->next);
  }
  newlist->handler = handler;
  newlist->next = *list;
  *list = newlist;
}

struct handler_list_block {
  struct handler_list prepare, parent, child;
};

int pthread_atfork(void (*prepare)(void),
		     void (*parent)(void),
		     void (*child)(void))
{
  struct handler_list_block * block =
    (struct handler_list_block *) malloc(sizeof(struct handler_list_block));
  if (block == NULL) return ENOMEM;
  __pthread_mutex_lock(&pthread_atfork_lock);
  /* "prepare" handlers are called in LIFO */
  pthread_insert_list(&pthread_atfork_prepare, prepare, &block->prepare, 0);
  /* "parent" handlers are called in FIFO */
  pthread_insert_list(&pthread_atfork_parent, parent, &block->parent, 1);
  /* "child" handlers are called in FIFO */
  pthread_insert_list(&pthread_atfork_child, child, &block->child, 1);
  __pthread_mutex_unlock(&pthread_atfork_lock);
  return 0;
}
/*strong_alias (__pthread_atfork, pthread_atfork)*/

static __inline__ void pthread_call_handlers(struct handler_list * list)
{
  for (/*nothing*/; list != NULL; list = list->next) (list->handler)();
}

void __pthread_once_fork_prepare(void);
void __pthread_once_fork_child(void);
void __pthread_once_fork_parent(void);

static pid_t __fork(void)
{
  pid_t pid;
  struct handler_list * prepare, * child, * parent;

  __pthread_mutex_lock(&pthread_atfork_lock);
  prepare = pthread_atfork_prepare;
  child = pthread_atfork_child;
  parent = pthread_atfork_parent;
  pthread_call_handlers(prepare);

  __pthread_once_fork_prepare();
#ifdef __MALLOC__
  __pthread_mutex_lock(&__malloc_sbrk_lock);
  __pthread_mutex_lock(&__malloc_heap_lock);
#ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
  __pthread_mutex_lock(&__malloc_mmb_heap_lock);
#endif
#elif defined(__MALLOC_STANDARD__) || defined(__MALLOC_SIMPLE__)
  __pthread_mutex_lock(&__malloc_lock);
#endif

  pid = __libc_fork();
  if (pid == 0) {
#if defined(__MALLOC_STANDARD__) || defined(__MALLOC_SIMPLE__)
    __libc_lock_init_recursive(__malloc_lock);
#elif defined(__MALLOC__)
#ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
    __libc_lock_init_adaptive(__malloc_mmb_heap_lock);
#endif
    __libc_lock_init_adaptive(__malloc_heap_lock);
    __libc_lock_init(__malloc_sbrk_lock);
#endif
    __libc_lock_init_adaptive(pthread_atfork_lock);
    __pthread_reset_main_thread();
    __fresetlockfiles();
    __pthread_once_fork_child();
    pthread_call_handlers(child);
  } else {
#if defined(__MALLOC_STANDARD__) || defined(__MALLOC_SIMPLE__)
    __pthread_mutex_unlock(&__malloc_lock);
#elif defined(__MALLOC__)
#ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
    __pthread_mutex_unlock(&__malloc_mmb_heap_lock);
#endif
    __pthread_mutex_unlock(&__malloc_heap_lock);
    __pthread_mutex_unlock(&__malloc_sbrk_lock);
#endif
    __pthread_mutex_unlock(&pthread_atfork_lock);
    __pthread_once_fork_parent();
    pthread_call_handlers(parent);
  }
  return pid;
}
strong_alias(__fork,fork)
strong_alias(__fork,vfork)

#endif