2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #include <kern/kalloc.h>
24 #include <libkern/OSAtomic.h>
25 #include <sys/errno.h>
27 #include <libkern/libkern.h>
30 struct init_list_entry
{
31 struct init_list_entry
*next
;
32 net_init_func_ptr func
;
35 #define LIST_RAN ((struct init_list_entry*)0xffffffff)
36 static struct init_list_entry
*list_head
= 0;
40 net_init_func_ptr init_func
)
42 struct init_list_entry
*entry
;
48 /* Check if we've already started */
49 if (list_head
== LIST_RAN
) {
53 entry
= kalloc(sizeof(*entry
));
55 printf("net_init_add: no memory\n");
59 bzero(entry
, sizeof(*entry
));
60 entry
->func
= init_func
;
63 entry
->next
= list_head
;
65 if (entry
->next
== LIST_RAN
) {
66 /* List already ran, cleanup and call the function */
67 kfree(entry
, sizeof(*entry
));
70 } while(!OSCompareAndSwap((UInt32
)entry
->next
, (UInt32
)entry
,
71 (UInt32
*)&list_head
));
76 __private_extern__
void
79 struct init_list_entry
*backward_head
= 0;
80 struct init_list_entry
*forward_head
= 0;
81 struct init_list_entry
*current
= 0;
84 * Grab the list, replacing the head with 0xffffffff to indicate
85 * that we've already run.
88 backward_head
= list_head
;
89 } while (!OSCompareAndSwap((UInt32
)backward_head
, (UInt32
)LIST_RAN
,
90 (UInt32
*)&list_head
));
92 /* Reverse the order of the list */
93 while (backward_head
!= 0) {
94 current
= backward_head
;
95 backward_head
= current
->next
;
96 current
->next
= forward_head
;
97 forward_head
= current
;
100 /* Call each function pointer registered */
101 while (forward_head
!= 0) {
102 current
= forward_head
;
103 forward_head
= current
->next
;
105 kfree(current
, sizeof(*current
));