]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/init.c
5fdb45400aaf5edefddb06724450f3de71aa2d4b
[apple/xnu.git] / bsd / net / init.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30
31 #include <kern/kalloc.h>
32 #include <libkern/OSAtomic.h>
33 #include <sys/errno.h>
34 #include <net/init.h>
35 #include <libkern/libkern.h>
36 #include <string.h>
37
38 struct init_list_entry {
39 struct init_list_entry *next;
40 net_init_func_ptr func;
41 };
42
43 #define LIST_RAN ((struct init_list_entry*)0xffffffff)
44 static struct init_list_entry *list_head = 0;
45
46 errno_t
47 net_init_add(
48 net_init_func_ptr init_func)
49 {
50 struct init_list_entry *entry;
51
52 if (init_func == 0) {
53 return EINVAL;
54 }
55
56 /* Check if we've already started */
57 if (list_head == LIST_RAN) {
58 return EALREADY;
59 }
60
61 entry = kalloc(sizeof(*entry));
62 if (entry == 0) {
63 printf("net_init_add: no memory\n");
64 return ENOMEM;
65 }
66
67 bzero(entry, sizeof(*entry));
68 entry->func = init_func;
69
70 do {
71 entry->next = list_head;
72
73 if (entry->next == LIST_RAN) {
74 /* List already ran, cleanup and call the function */
75 kfree(entry, sizeof(*entry));
76 return EALREADY;
77 }
78 } while(!OSCompareAndSwap((UInt32)entry->next, (UInt32)entry,
79 (UInt32*)&list_head));
80
81 return 0;
82 }
83
84 __private_extern__ void
85 net_init_run(void)
86 {
87 struct init_list_entry *backward_head = 0;
88 struct init_list_entry *forward_head = 0;
89 struct init_list_entry *current = 0;
90
91 /*
92 * Grab the list, replacing the head with 0xffffffff to indicate
93 * that we've already run.
94 */
95 do {
96 backward_head = list_head;
97 } while (!OSCompareAndSwap((UInt32)backward_head, (UInt32)LIST_RAN,
98 (UInt32*)&list_head));
99
100 /* Reverse the order of the list */
101 while (backward_head != 0) {
102 current = backward_head;
103 backward_head = current->next;
104 current->next = forward_head;
105 forward_head = current;
106 }
107
108 /* Call each function pointer registered */
109 while (forward_head != 0) {
110 current = forward_head;
111 forward_head = current->next;
112 current->func();
113 kfree(current, sizeof(*current));
114 }
115 }