]>
Commit | Line | Data |
---|---|---|
91447636 A |
1 | /* |
2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
37839358 A |
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. | |
91447636 | 11 | * |
37839358 A |
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 | |
91447636 A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
37839358 A |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
91447636 A |
19 | * |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | ||
23 | #include <kern/kalloc.h> | |
24 | #include <libkern/OSAtomic.h> | |
25 | #include <sys/errno.h> | |
26 | #include <net/init.h> | |
27 | #include <libkern/libkern.h> | |
28 | #include <string.h> | |
29 | ||
30 | struct init_list_entry { | |
31 | struct init_list_entry *next; | |
32 | net_init_func_ptr func; | |
33 | }; | |
34 | ||
35 | #define LIST_RAN ((struct init_list_entry*)0xffffffff) | |
36 | static struct init_list_entry *list_head = 0; | |
37 | ||
38 | errno_t | |
39 | net_init_add( | |
40 | net_init_func_ptr init_func) | |
41 | { | |
42 | struct init_list_entry *entry; | |
43 | ||
44 | if (init_func == 0) { | |
45 | return EINVAL; | |
46 | } | |
47 | ||
48 | /* Check if we've already started */ | |
49 | if (list_head == LIST_RAN) { | |
50 | return EALREADY; | |
51 | } | |
52 | ||
53 | entry = kalloc(sizeof(*entry)); | |
54 | if (entry == 0) { | |
55 | printf("net_init_add: no memory\n"); | |
56 | return ENOMEM; | |
57 | } | |
58 | ||
59 | bzero(entry, sizeof(*entry)); | |
60 | entry->func = init_func; | |
61 | ||
62 | do { | |
63 | entry->next = list_head; | |
64 | ||
65 | if (entry->next == LIST_RAN) { | |
66 | /* List already ran, cleanup and call the function */ | |
67 | kfree(entry, sizeof(*entry)); | |
68 | return EALREADY; | |
69 | } | |
70 | } while(!OSCompareAndSwap((UInt32)entry->next, (UInt32)entry, | |
71 | (UInt32*)&list_head)); | |
72 | ||
73 | return 0; | |
74 | } | |
75 | ||
76 | __private_extern__ void | |
77 | net_init_run(void) | |
78 | { | |
79 | struct init_list_entry *backward_head = 0; | |
80 | struct init_list_entry *forward_head = 0; | |
81 | struct init_list_entry *current = 0; | |
82 | ||
83 | /* | |
84 | * Grab the list, replacing the head with 0xffffffff to indicate | |
85 | * that we've already run. | |
86 | */ | |
87 | do { | |
88 | backward_head = list_head; | |
89 | } while (!OSCompareAndSwap((UInt32)backward_head, (UInt32)LIST_RAN, | |
90 | (UInt32*)&list_head)); | |
91 | ||
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; | |
98 | } | |
99 | ||
100 | /* Call each function pointer registered */ | |
101 | while (forward_head != 0) { | |
102 | current = forward_head; | |
103 | forward_head = current->next; | |
104 | current->func(); | |
105 | kfree(current, sizeof(*current)); | |
106 | } | |
107 | } |