2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 #include <kern/kalloc.h>
32 #include <libkern/OSAtomic.h>
33 #include <sys/errno.h>
35 #include <libkern/libkern.h>
38 struct init_list_entry
{
39 struct init_list_entry
*next
;
40 net_init_func_ptr func
;
43 #define LIST_RAN ((struct init_list_entry*)0xffffffff)
44 static struct init_list_entry
*list_head
= 0;
48 net_init_func_ptr init_func
)
50 struct init_list_entry
*entry
;
56 /* Check if we've already started */
57 if (list_head
== LIST_RAN
) {
61 entry
= kalloc(sizeof(*entry
));
63 printf("net_init_add: no memory\n");
67 bzero(entry
, sizeof(*entry
));
68 entry
->func
= init_func
;
71 entry
->next
= list_head
;
73 if (entry
->next
== LIST_RAN
) {
74 /* List already ran, cleanup and call the function */
75 kfree(entry
, sizeof(*entry
));
78 } while(!OSCompareAndSwap((UInt32
)entry
->next
, (UInt32
)entry
,
79 (UInt32
*)&list_head
));
84 __private_extern__
void
87 struct init_list_entry
*backward_head
= 0;
88 struct init_list_entry
*forward_head
= 0;
89 struct init_list_entry
*current
= 0;
92 * Grab the list, replacing the head with 0xffffffff to indicate
93 * that we've already run.
96 backward_head
= list_head
;
97 } while (!OSCompareAndSwap((UInt32
)backward_head
, (UInt32
)LIST_RAN
,
98 (UInt32
*)&list_head
));
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
;
108 /* Call each function pointer registered */
109 while (forward_head
!= 0) {
110 current
= forward_head
;
111 forward_head
= current
->next
;
113 kfree(current
, sizeof(*current
));