]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/platform-bionic.git/blob - libc/bionic/pthread_atfork.cpp
Merge "Remove more BSD cruft from the public headers."
[android-sdk/platform-bionic.git] / libc / bionic / pthread_atfork.cpp
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
29 #include <errno.h>
30 #include <pthread.h>
31 #include <stdlib.h>
33 struct atfork_t {
34   atfork_t* next;
35   atfork_t* prev;
37   void (*prepare)(void);
38   void (*child)(void);
39   void (*parent)(void);
40 };
42 struct atfork_list_t {
43   atfork_t* first;
44   atfork_t* last;
45 };
47 static pthread_mutex_t g_atfork_list_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
48 static atfork_list_t g_atfork_list = { NULL, NULL };
50 void __bionic_atfork_run_prepare() {
51   // We lock the atfork list here, unlock it in the parent, and reset it in the child.
52   // This ensures that nobody can modify the handler array between the calls
53   // to the prepare and parent/child handlers.
54   //
55   // TODO: If a handler tries to mutate the list, they'll block. We should probably copy
56   // the list before forking, and have prepare, parent, and child all work on the consistent copy.
57   pthread_mutex_lock(&g_atfork_list_mutex);
59   // Call pthread_atfork() prepare handlers. POSIX states that the prepare
60   // handlers should be called in the reverse order of the parent/child
61   // handlers, so we iterate backwards.
62   for (atfork_t* it = g_atfork_list.last; it != NULL; it = it->prev) {
63     if (it->prepare != NULL) {
64       it->prepare();
65     }
66   }
67 }
69 void __bionic_atfork_run_child() {
70   for (atfork_t* it = g_atfork_list.first; it != NULL; it = it->next) {
71     if (it->child != NULL) {
72       it->child();
73     }
74   }
76   g_atfork_list_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
77 }
79 void __bionic_atfork_run_parent() {
80   for (atfork_t* it = g_atfork_list.first; it != NULL; it = it->next) {
81     if (it->parent != NULL) {
82       it->parent();
83     }
84   }
86   pthread_mutex_unlock(&g_atfork_list_mutex);
87 }
89 int pthread_atfork(void (*prepare)(void), void (*parent)(void), void(*child)(void)) {
90   atfork_t* entry = reinterpret_cast<atfork_t*>(malloc(sizeof(atfork_t)));
91   if (entry == NULL) {
92     return ENOMEM;
93   }
95   entry->prepare = prepare;
96   entry->parent = parent;
97   entry->child = child;
99   pthread_mutex_lock(&g_atfork_list_mutex);
101   // Append 'entry' to the list.
102   entry->next = NULL;
103   entry->prev = g_atfork_list.last;
104   if (entry->prev != NULL) {
105     entry->prev->next = entry;
106   }
107   if (g_atfork_list.first == NULL) {
108     g_atfork_list.first = entry;
109   }
110   g_atfork_list.last = entry;
112   pthread_mutex_unlock(&g_atfork_list_mutex);
114   return 0;