2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <kern/kalloc.h>
25 #include <libkern/OSAtomic.h>
26 #include <sys/errno.h>
28 #include <libkern/libkern.h>
31 struct init_list_entry
{
32 struct init_list_entry
*next
;
33 net_init_func_ptr func
;
36 #define LIST_RAN ((struct init_list_entry*)0xffffffff)
37 static struct init_list_entry
*list_head
= 0;
41 net_init_func_ptr init_func
)
43 struct init_list_entry
*entry
;
49 /* Check if we've already started */
50 if (list_head
== LIST_RAN
) {
54 entry
= kalloc(sizeof(*entry
));
56 printf("net_init_add: no memory\n");
60 bzero(entry
, sizeof(*entry
));
61 entry
->func
= init_func
;
64 entry
->next
= list_head
;
66 if (entry
->next
== LIST_RAN
) {
67 /* List already ran, cleanup and call the function */
68 kfree(entry
, sizeof(*entry
));
71 } while(!OSCompareAndSwap((UInt32
)entry
->next
, (UInt32
)entry
,
72 (UInt32
*)&list_head
));
77 __private_extern__
void
80 struct init_list_entry
*backward_head
= 0;
81 struct init_list_entry
*forward_head
= 0;
82 struct init_list_entry
*current
= 0;
85 * Grab the list, replacing the head with 0xffffffff to indicate
86 * that we've already run.
89 backward_head
= list_head
;
90 } while (!OSCompareAndSwap((UInt32
)backward_head
, (UInt32
)LIST_RAN
,
91 (UInt32
*)&list_head
));
93 /* Reverse the order of the list */
94 while (backward_head
!= 0) {
95 current
= backward_head
;
96 backward_head
= current
->next
;
97 current
->next
= forward_head
;
98 forward_head
= current
;
101 /* Call each function pointer registered */
102 while (forward_head
!= 0) {
103 current
= forward_head
;
104 forward_head
= current
->next
;
106 kfree(current
, sizeof(*current
));