Rev 3912 | Rev 3990 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3912 | Rev 3914 | ||
---|---|---|---|
Line 34... | Line 34... | ||
34 | * Packet map and queue implementation. |
34 | * Packet map and queue implementation. |
35 | * This file has to be compiled with both the packet server and the client. |
35 | * This file has to be compiled with both the packet server and the client. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <errno.h> |
38 | #include <errno.h> |
- | 39 | #include <futex.h> |
|
39 | #include <malloc.h> |
40 | #include <malloc.h> |
40 | //#include <stdio.h> |
41 | //#include <stdio.h> |
41 | #include <string.h> |
42 | #include <string.h> |
42 | 43 | ||
43 | #include <sys/mman.h> |
44 | #include <sys/mman.h> |
Line 85... | Line 86... | ||
85 | int packet_destroy( packet_t packet ); |
86 | int packet_destroy( packet_t packet ); |
86 | 87 | ||
87 | /** Packet map global data. |
88 | /** Packet map global data. |
88 | */ |
89 | */ |
89 | static struct{ |
90 | static struct{ |
90 | // TODO lock |
91 | /** Safety lock. |
- | 92 | * Used as a mutex. |
|
- | 93 | */ |
|
- | 94 | futex_t lock; |
|
91 | /** Packet map. |
95 | /** Packet map. |
92 | */ |
96 | */ |
93 | gpm_t packet_map; |
97 | gpm_t packet_map; |
94 | } pm_globals; |
98 | } pm_globals; |
95 | 99 | ||
96 | GENERIC_FIELD_IMPLEMENT( gpm, packet_map_t ); |
100 | GENERIC_FIELD_IMPLEMENT( gpm, packet_map_t ); |
97 | 101 | ||
98 | int packet_destroy( packet_t packet ){ |
102 | int packet_destroy( packet_t packet ){ |
99 | if( ! packet_is_valid( packet )) return EINVAL; |
103 | if( ! packet_is_valid( packet )) return EINVAL; |
100 | return munmap( packet, packet->length ); |
104 | return munmap( packet, packet->length ); |
101 | } |
105 | } |
102 | 106 | ||
103 | int pm_init( void ){ |
107 | int pm_init( void ){ |
- | 108 | ERROR_DECLARE; |
|
- | 109 | ||
- | 110 | // start locked |
|
- | 111 | futex_initialize( & pm_globals.lock, 0 ); |
|
104 | return gpm_initialize( & pm_globals.packet_map ); |
112 | ERROR_PROPAGATE( gpm_initialize( & pm_globals.packet_map )); |
- | 113 | // release the lock |
|
- | 114 | futex_up( & pm_globals.lock ); |
|
- | 115 | return EOK; |
|
105 | } |
116 | } |
106 | 117 | ||
107 | packet_t pm_find( packet_id_t packet_id ){ |
118 | packet_t pm_find( packet_id_t packet_id ){ |
108 | packet_map_ref map; |
119 | packet_map_ref map; |
- | 120 | packet_t packet; |
|
109 | 121 | ||
110 | if( ! packet_id ) return NULL; |
122 | if( ! packet_id ) return NULL; |
- | 123 | futex_down( & pm_globals.lock ); |
|
111 | if( packet_id > PACKET_MAP_SIZE * gpm_count( & pm_globals.packet_map )) return NULL; |
124 | if( packet_id > PACKET_MAP_SIZE * gpm_count( & pm_globals.packet_map )){ |
- | 125 | futex_up( & pm_globals.lock ); |
|
- | 126 | return NULL; |
|
- | 127 | } |
|
112 | map = gpm_get_index( & pm_globals.packet_map, PACKET_MAP_PAGE( packet_id )); |
128 | map = gpm_get_index( & pm_globals.packet_map, PACKET_MAP_PAGE( packet_id )); |
- | 129 | if( ! map ){ |
|
- | 130 | futex_up( & pm_globals.lock ); |
|
113 | if( ! map ) return NULL; |
131 | return NULL; |
- | 132 | } |
|
114 | return ( * map )[ PACKET_MAP_INDEX( packet_id ) ]; |
133 | packet = ( * map )[ PACKET_MAP_INDEX( packet_id ) ]; |
- | 134 | futex_up( & pm_globals.lock ); |
|
- | 135 | return packet; |
|
115 | } |
136 | } |
116 | 137 | ||
117 | int pm_add( packet_t packet ){ |
138 | int pm_add( packet_t packet ){ |
118 | ERROR_DECLARE; |
139 | ERROR_DECLARE; |
119 | 140 | ||
120 | packet_map_ref map; |
141 | packet_map_ref map; |
121 | 142 | ||
122 | if(( ! packet_is_valid( packet )) || ( gpm_count( & pm_globals.packet_map ) < 0 )) return EINVAL; |
143 | if( ! packet_is_valid( packet )) return EINVAL; |
- | 144 | futex_down( & pm_globals.lock ); |
|
123 | if( PACKET_MAP_PAGE( packet->packet_id ) < gpm_count( & pm_globals.packet_map )){ |
145 | if( PACKET_MAP_PAGE( packet->packet_id ) < gpm_count( & pm_globals.packet_map )){ |
124 | map = gpm_get_index( & pm_globals.packet_map, PACKET_MAP_PAGE( packet->packet_id )); |
146 | map = gpm_get_index( & pm_globals.packet_map, PACKET_MAP_PAGE( packet->packet_id )); |
125 | }else{ |
147 | }else{ |
126 | do{ |
148 | do{ |
127 | map = ( packet_map_ref ) malloc( sizeof( packet_map_t )); |
149 | map = ( packet_map_ref ) malloc( sizeof( packet_map_t )); |
- | 150 | if( ! map ){ |
|
- | 151 | futex_up( & pm_globals.lock ); |
|
128 | if( ! map ) return ENOMEM; |
152 | return ENOMEM; |
- | 153 | } |
|
129 | memset( map, 0, sizeof( packet_map_t )); |
154 | memset( map, 0, sizeof( packet_map_t )); |
130 | if(( ERROR_CODE = gpm_add( & pm_globals.packet_map, map )) < 0 ){ |
155 | if(( ERROR_CODE = gpm_add( & pm_globals.packet_map, map )) < 0 ){ |
131 | free( map ); |
156 | free( map ); |
- | 157 | futex_up( & pm_globals.lock ); |
|
132 | return ERROR_CODE; |
158 | return ERROR_CODE; |
133 | } |
159 | } |
134 | }while( PACKET_MAP_PAGE( packet->packet_id ) >= gpm_count( & pm_globals.packet_map )); |
160 | }while( PACKET_MAP_PAGE( packet->packet_id ) >= gpm_count( & pm_globals.packet_map )); |
135 | } |
161 | } |
136 | ( * map )[ PACKET_MAP_INDEX( packet->packet_id ) ] = packet; |
162 | ( * map )[ PACKET_MAP_INDEX( packet->packet_id ) ] = packet; |
- | 163 | futex_up( & pm_globals.lock ); |
|
137 | return EOK; |
164 | return EOK; |
138 | } |
165 | } |
139 | 166 | ||
140 | void pm_destroy( void ){ |
167 | void pm_destroy( void ){ |
141 | int count; |
168 | int count; |
142 | int index; |
169 | int index; |
143 | packet_map_ref map; |
170 | packet_map_ref map; |
144 | packet_t packet; |
171 | packet_t packet; |
145 | 172 | ||
- | 173 | futex_down( & pm_globals.lock ); |
|
146 | count = gpm_count( & pm_globals.packet_map ); |
174 | count = gpm_count( & pm_globals.packet_map ); |
147 | while( count > 0 ){ |
175 | while( count > 0 ){ |
148 | map = gpm_get_index( & pm_globals.packet_map, count - 1 ); |
176 | map = gpm_get_index( & pm_globals.packet_map, count - 1 ); |
149 | for( index = PACKET_MAP_SIZE - 1; index >= 0; -- index ){ |
177 | for( index = PACKET_MAP_SIZE - 1; index >= 0; -- index ){ |
150 | packet = ( * map )[ index ]; |
178 | packet = ( * map )[ index ]; |
Line 152... | Line 180... | ||
152 | munmap( packet, packet->length ); |
180 | munmap( packet, packet->length ); |
153 | } |
181 | } |
154 | } |
182 | } |
155 | } |
183 | } |
156 | gpm_destroy( & pm_globals.packet_map ); |
184 | gpm_destroy( & pm_globals.packet_map ); |
- | 185 | // leave locked |
|
157 | } |
186 | } |
158 | 187 | ||
159 | packet_t pq_add( packet_t first, packet_t packet, int order, size_t metric ){ |
188 | packet_t pq_add( packet_t first, packet_t packet, int order, size_t metric ){ |
160 | packet_t item; |
189 | packet_t item; |
161 | 190 |