Mercurial > hg > freeDiameter
annotate libfdproto/fifo.c @ 882:ae96ae28f3dd 1.1.5
Limit python version to avoid using python3 (not compatible)
author | Sebastien Decugis <sdecugis@freediameter.net> |
---|---|
date | Sat, 03 Nov 2012 18:45:41 +0100 |
parents | c47c16436f71 |
children | 2091bf698fb1 |
rev | line source |
---|---|
0 | 1 /********************************************************************************************************* |
2 * Software License Agreement (BSD License) * | |
740
4a9f08d6b6ba
Updated my mail address
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
662
diff
changeset
|
3 * Author: Sebastien Decugis <sdecugis@freediameter.net> * |
0 | 4 * * |
662
2e94ef0515d7
Updated copyright information
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
658
diff
changeset
|
5 * Copyright (c) 2011, WIDE Project and NICT * |
0 | 6 * All rights reserved. * |
7 * * | |
8 * Redistribution and use of this software in source and binary forms, with or without modification, are * | |
9 * permitted provided that the following conditions are met: * | |
10 * * | |
11 * * Redistributions of source code must retain the above * | |
12 * copyright notice, this list of conditions and the * | |
13 * following disclaimer. * | |
14 * * | |
15 * * Redistributions in binary form must reproduce the above * | |
16 * copyright notice, this list of conditions and the * | |
17 * following disclaimer in the documentation and/or other * | |
18 * materials provided with the distribution. * | |
19 * * | |
20 * * Neither the name of the WIDE Project or NICT nor the * | |
21 * names of its contributors may be used to endorse or * | |
22 * promote products derived from this software without * | |
23 * specific prior written permission of WIDE Project and * | |
24 * NICT. * | |
25 * * | |
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * | |
27 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * | |
28 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * | |
29 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * | |
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * | |
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * | |
32 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * | |
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * | |
34 *********************************************************************************************************/ | |
35 | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
36 /* FIFO queues module. |
0 | 37 * |
38 * The threads that call these functions must be in the cancellation state PTHREAD_CANCEL_ENABLE and type PTHREAD_CANCEL_DEFERRED. | |
39 * This is the default state and type on thread creation. | |
40 * | |
41 * In order to destroy properly a queue, the application must: | |
42 * -> shutdown any process that can add into the queue first. | |
43 * -> pthread_cancel any thread that could be waiting on the queue. | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
44 * -> consume any element that is in the queue, using fd_qu_tryget_int. |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
45 * -> then destroy the queue using fd_mq_del. |
0 | 46 */ |
47 | |
658
f198d16fa7f4
Initial commit for 1.1.0:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
451
diff
changeset
|
48 #include "fdproto-internal.h" |
0 | 49 |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
50 /* Definition of a FIFO queue object */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
51 struct fifo { |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
52 int eyec; /* An eye catcher, also used to check a queue is valid. FIFO_EYEC */ |
0 | 53 |
54 pthread_mutex_t mtx; /* Mutex protecting this queue */ | |
767
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
55 pthread_cond_t cond_pull; /* condition variable for pulling threads */ |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
56 pthread_cond_t cond_push; /* condition variable for pushing threads */ |
0 | 57 |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
58 struct fd_list list; /* sentinel for the list of elements */ |
0 | 59 int count; /* number of objects in the list */ |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
60 int thrs; /* number of threads waiting for a new element (when count is 0) */ |
0 | 61 |
767
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
62 int max; /* maximum number of items to accept if not 0 */ |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
63 int thrs_push; /* number of threads waitnig to push an item */ |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
64 |
1
bafb831ba688
Fix names to proper case for freeDiameter
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
0
diff
changeset
|
65 uint16_t high; /* High level threshold (see libfreeDiameter.h for details) */ |
0 | 66 uint16_t low; /* Low level threshhold */ |
67 void *data; /* Opaque pointer for threshold callbacks */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
68 void (*h_cb)(struct fifo *, void **); /* The callbacks */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
69 void (*l_cb)(struct fifo *, void **); |
0 | 70 int highest;/* The highest count value for which h_cb has been called */ |
14
14cf6daf716d
Some progress on peers module
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
11
diff
changeset
|
71 int highest_ever; /* The max count value this queue has reached (for tweaking) */ |
0 | 72 }; |
73 | |
74 /* The eye catcher value */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
75 #define FIFO_EYEC 0xe7ec1130 |
0 | 76 |
77 /* Macro to check a pointer */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
78 #define CHECK_FIFO( _queue ) (( (_queue) != NULL) && ( (_queue)->eyec == FIFO_EYEC) ) |
0 | 79 |
80 | |
767
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
81 /* Create a new queue, with max number of items -- use 0 for no max */ |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
82 int fd_fifo_new ( struct fifo ** queue, int max ) |
0 | 83 { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
84 struct fifo * new; |
0 | 85 |
86 TRACE_ENTRY( "%p", queue ); | |
87 | |
88 CHECK_PARAMS( queue ); | |
89 | |
90 /* Create a new object */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
91 CHECK_MALLOC( new = malloc (sizeof (struct fifo) ) ); |
0 | 92 |
93 /* Initialize the content */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
94 memset(new, 0, sizeof(struct fifo)); |
0 | 95 |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
96 new->eyec = FIFO_EYEC; |
0 | 97 CHECK_POSIX( pthread_mutex_init(&new->mtx, NULL) ); |
767
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
98 CHECK_POSIX( pthread_cond_init(&new->cond_pull, NULL) ); |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
99 CHECK_POSIX( pthread_cond_init(&new->cond_push, NULL) ); |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
100 new->max = max; |
0 | 101 |
102 fd_list_init(&new->list, NULL); | |
103 | |
104 /* We're done */ | |
105 *queue = new; | |
106 return 0; | |
107 } | |
108 | |
10
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
109 /* Dump the content of a queue */ |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
110 void fd_fifo_dump(int level, char * name, struct fifo * queue, void (*dump_item)(int level, void * item)) |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
111 { |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
112 TRACE_ENTRY("%i %p %p %p", level, name, queue, dump_item); |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
113 |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
114 if (!TRACE_BOOL(level)) |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
115 return; |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
116 |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
117 fd_log_debug("Dumping queue '%s' (%p):\n", name ?: "?", queue); |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
118 if (!CHECK_FIFO( queue )) { |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
119 fd_log_debug(" Queue invalid!\n"); |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
120 if (queue) |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
121 fd_log_debug(" (%x != %x)\n", queue->eyec, FIFO_EYEC); |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
122 return; |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
123 } |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
124 |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
125 CHECK_POSIX_DO( pthread_mutex_lock( &queue->mtx ), /* continue */ ); |
11
6576ef5e01eb
Added some support for peers
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
10
diff
changeset
|
126 fd_log_debug(" %d elements in queue / %d threads waiting\n", queue->count, queue->thrs); |
767
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
127 fd_log_debug(" %d elements max / %d threads waiting to push\n", queue->max, queue->thrs_push); |
14
14cf6daf716d
Some progress on peers module
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
11
diff
changeset
|
128 fd_log_debug(" thresholds: %d / %d (h:%d), cb: %p,%p (%p), highest: %d\n", |
14cf6daf716d
Some progress on peers module
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
11
diff
changeset
|
129 queue->high, queue->low, queue->highest, |
10
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
130 queue->h_cb, queue->l_cb, queue->data, |
14
14cf6daf716d
Some progress on peers module
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
11
diff
changeset
|
131 queue->highest_ever); |
10
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
132 |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
133 if (dump_item) { |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
134 struct fd_list * li; |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
135 int i = 0; |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
136 for (li = queue->list.next; li != &queue->list; li = li->next) { |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
137 fd_log_debug(" [%i] item %p in fifo %p:\n", i++, li->o, queue); |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
138 (*dump_item)(level, li->o); |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
139 } |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
140 } |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
141 CHECK_POSIX_DO( pthread_mutex_unlock( &queue->mtx ), /* continue */ ); |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
142 |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
143 } |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
144 |
21
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
145 /* Delete a queue. It must be empty. */ |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
146 int fd_fifo_del ( struct fifo ** queue ) |
0 | 147 { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
148 struct fifo * q; |
21
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
149 int loops = 0; |
0 | 150 |
151 TRACE_ENTRY( "%p", queue ); | |
152 | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
153 CHECK_PARAMS( queue && CHECK_FIFO( *queue ) ); |
0 | 154 |
155 q = *queue; | |
156 | |
157 CHECK_POSIX( pthread_mutex_lock( &q->mtx ) ); | |
158 | |
21
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
159 if ((q->count != 0) || (q->data != NULL)) { |
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
160 TRACE_DEBUG(INFO, "The queue cannot be destroyed (%d, %p)", q->count, q->data); |
0 | 161 CHECK_POSIX_DO( pthread_mutex_unlock( &q->mtx ), /* no fallback */ ); |
162 return EINVAL; | |
163 } | |
164 | |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
165 /* Ok, now invalidate the queue */ |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
166 q->eyec = 0xdead; |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
167 |
156
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
139
diff
changeset
|
168 /* Have all waiting threads return an error */ |
21
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
169 while (q->thrs) { |
138
4249a55f859a
Attempt to fix behavior on multi-CPU platforms
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
137
diff
changeset
|
170 CHECK_POSIX( pthread_mutex_unlock( &q->mtx )); |
767
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
171 CHECK_POSIX( pthread_cond_signal(&q->cond_pull) ); |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
172 usleep(1000); |
139
8b8d11fadc4e
Attempt to fix behavior on multi-CPU platforms, take 2
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
138
diff
changeset
|
173 |
21
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
174 CHECK_POSIX( pthread_mutex_lock( &q->mtx ) ); |
139
8b8d11fadc4e
Attempt to fix behavior on multi-CPU platforms, take 2
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
138
diff
changeset
|
175 ASSERT( ++loops < 20 ); /* detect infinite loops */ |
21
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
176 } |
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
177 |
0 | 178 /* sanity check */ |
179 ASSERT(FD_IS_LIST_EMPTY(&q->list)); | |
180 | |
181 /* And destroy it */ | |
182 CHECK_POSIX( pthread_mutex_unlock( &q->mtx ) ); | |
183 | |
767
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
184 CHECK_POSIX_DO( pthread_cond_destroy( &q->cond_pull ), ); |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
185 |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
186 CHECK_POSIX_DO( pthread_cond_destroy( &q->cond_push ), ); |
0 | 187 |
451
4395961aad27
Some libc seem to have an issue with canceled threads...
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
450
diff
changeset
|
188 CHECK_POSIX_DO( pthread_mutex_destroy( &q->mtx ), ); |
0 | 189 |
190 free(q); | |
191 *queue = NULL; | |
192 | |
193 return 0; | |
194 } | |
195 | |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
196 /* Move the content of old into new, and update loc_update atomically. We leave the old queue empty but valid */ |
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
197 int fd_fifo_move ( struct fifo * old, struct fifo * new, struct fifo ** loc_update ) |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
198 { |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
199 int loops = 0; |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
200 |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
201 TRACE_ENTRY("%p %p %p", old, new, loc_update); |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
202 CHECK_PARAMS( CHECK_FIFO( old ) && CHECK_FIFO( new )); |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
203 |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
204 CHECK_PARAMS( ! old->data ); |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
205 if (new->high) { |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
206 TODO("Implement support for thresholds in fd_fifo_move..."); |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
207 } |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
208 |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
209 /* Update loc_update */ |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
210 if (loc_update) |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
211 *loc_update = new; |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
212 |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
213 /* Lock the queues */ |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
214 CHECK_POSIX( pthread_mutex_lock( &old->mtx ) ); |
767
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
215 |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
216 CHECK_PARAMS_DO( (! old->thrs_push), { |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
217 pthread_mutex_unlock( &old->mtx ); |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
218 return EINVAL; |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
219 } ); |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
220 |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
221 CHECK_POSIX( pthread_mutex_lock( &new->mtx ) ); |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
222 |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
223 /* Any waiting thread on the old queue returns an error */ |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
224 old->eyec = 0xdead; |
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
225 while (old->thrs) { |
138
4249a55f859a
Attempt to fix behavior on multi-CPU platforms
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
137
diff
changeset
|
226 CHECK_POSIX( pthread_mutex_unlock( &old->mtx )); |
767
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
227 CHECK_POSIX( pthread_cond_signal( &old->cond_pull ) ); |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
228 usleep(1000); |
139
8b8d11fadc4e
Attempt to fix behavior on multi-CPU platforms, take 2
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
138
diff
changeset
|
229 |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
230 CHECK_POSIX( pthread_mutex_lock( &old->mtx ) ); |
767
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
231 ASSERT( loops < 20 ); /* detect infinite loops */ |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
232 } |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
233 |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
234 /* Move all data from old to new */ |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
235 fd_list_move_end( &new->list, &old->list ); |
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
236 if (old->count && (!new->count)) { |
767
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
237 CHECK_POSIX( pthread_cond_signal(&new->cond_pull) ); |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
238 } |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
239 new->count += old->count; |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
240 |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
241 /* Reset old */ |
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
242 old->count = 0; |
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
243 old->eyec = FIFO_EYEC; |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
244 |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
245 /* Unlock, we're done */ |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
246 CHECK_POSIX( pthread_mutex_unlock( &new->mtx ) ); |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
247 CHECK_POSIX( pthread_mutex_unlock( &old->mtx ) ); |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
248 |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
249 return 0; |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
250 } |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
251 |
0 | 252 /* Get the length of the queue */ |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
253 int fd_fifo_length ( struct fifo * queue, int * length ) |
0 | 254 { |
255 TRACE_ENTRY( "%p %p", queue, length ); | |
256 | |
257 /* Check the parameters */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
258 CHECK_PARAMS( CHECK_FIFO( queue ) && length ); |
0 | 259 |
260 /* lock the queue */ | |
261 CHECK_POSIX( pthread_mutex_lock( &queue->mtx ) ); | |
262 | |
263 /* Retrieve the count */ | |
264 *length = queue->count; | |
265 | |
266 /* Unlock */ | |
267 CHECK_POSIX( pthread_mutex_unlock( &queue->mtx ) ); | |
268 | |
269 /* Done */ | |
270 return 0; | |
271 } | |
272 | |
273 /* alternate version with no error checking */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
274 int fd_fifo_length_noerr ( struct fifo * queue ) |
0 | 275 { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
276 if ( !CHECK_FIFO( queue ) ) |
0 | 277 return 0; |
278 | |
279 return queue->count; /* Let's hope it's read atomically, since we are not locking... */ | |
280 } | |
281 | |
282 /* Set the thresholds of the queue */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
283 int fd_fifo_setthrhd ( struct fifo * queue, void * data, uint16_t high, void (*h_cb)(struct fifo *, void **), uint16_t low, void (*l_cb)(struct fifo *, void **) ) |
0 | 284 { |
285 TRACE_ENTRY( "%p %p %hu %p %hu %p", queue, data, high, h_cb, low, l_cb ); | |
286 | |
287 /* Check the parameters */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
288 CHECK_PARAMS( CHECK_FIFO( queue ) && (high > low) && (queue->data == NULL) ); |
0 | 289 |
290 /* lock the queue */ | |
291 CHECK_POSIX( pthread_mutex_lock( &queue->mtx ) ); | |
292 | |
293 /* Save the values */ | |
294 queue->high = high; | |
295 queue->low = low; | |
296 queue->data = data; | |
297 queue->h_cb = h_cb; | |
298 queue->l_cb = l_cb; | |
299 | |
300 /* Unlock */ | |
301 CHECK_POSIX( pthread_mutex_unlock( &queue->mtx ) ); | |
302 | |
303 /* Done */ | |
304 return 0; | |
305 } | |
306 | |
767
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
307 |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
308 /* This handler is called when a thread is blocked on a queue, and cancelled */ |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
309 static void fifo_cleanup_push(void * queue) |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
310 { |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
311 struct fifo * q = (struct fifo *)queue; |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
312 TRACE_ENTRY( "%p", queue ); |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
313 |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
314 /* The thread has been cancelled, therefore it does not wait on the queue anymore */ |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
315 q->thrs_push--; |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
316 |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
317 /* Now unlock the queue, and we're done */ |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
318 CHECK_POSIX_DO( pthread_mutex_unlock( &q->mtx ), /* nothing */ ); |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
319 |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
320 /* End of cleanup handler */ |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
321 return; |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
322 } |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
323 |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
324 |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
325 /* Post a new item in the queue */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
326 int fd_fifo_post_int ( struct fifo * queue, void ** item ) |
0 | 327 { |
328 struct fd_list * new; | |
329 int call_cb = 0; | |
330 | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
331 TRACE_ENTRY( "%p %p", queue, item ); |
0 | 332 |
333 /* Check the parameters */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
334 CHECK_PARAMS( CHECK_FIFO( queue ) && item && *item ); |
0 | 335 |
767
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
336 /* lock the queue */ |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
337 CHECK_POSIX( pthread_mutex_lock( &queue->mtx ) ); |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
338 |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
339 if (queue->max) { |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
340 while (queue->count >= queue->max) { |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
341 int ret = 0; |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
342 |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
343 /* We have to wait for an item to be pulled */ |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
344 queue->thrs_push++ ; |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
345 pthread_cleanup_push( fifo_cleanup_push, queue); |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
346 ret = pthread_cond_wait( &queue->cond_push, &queue->mtx ); |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
347 pthread_cleanup_pop(0); |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
348 queue->thrs_push-- ; |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
349 |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
350 ASSERT( ret == 0 ); |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
351 } |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
352 } |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
353 |
0 | 354 /* Create a new list item */ |
767
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
355 CHECK_MALLOC_DO( new = malloc (sizeof (struct fd_list)) , { |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
356 pthread_mutex_unlock( &queue->mtx ); |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
357 } ); |
0 | 358 |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
359 fd_list_init(new, *item); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
360 *item = NULL; |
0 | 361 |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
362 /* Add the new item at the end */ |
0 | 363 fd_list_insert_before( &queue->list, new); |
364 queue->count++; | |
14
14cf6daf716d
Some progress on peers module
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
11
diff
changeset
|
365 if (queue->highest_ever < queue->count) |
14cf6daf716d
Some progress on peers module
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
11
diff
changeset
|
366 queue->highest_ever = queue->count; |
0 | 367 if (queue->high && ((queue->count % queue->high) == 0)) { |
368 call_cb = 1; | |
369 queue->highest = queue->count; | |
370 } | |
371 | |
372 /* Signal if threads are asleep */ | |
373 if (queue->thrs > 0) { | |
767
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
374 CHECK_POSIX( pthread_cond_signal(&queue->cond_pull) ); |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
375 } |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
376 if (queue->thrs_push > 0) { |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
377 /* cascade */ |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
378 CHECK_POSIX( pthread_cond_signal(&queue->cond_push) ); |
0 | 379 } |
380 | |
381 /* Unlock */ | |
382 CHECK_POSIX( pthread_mutex_unlock( &queue->mtx ) ); | |
383 | |
384 /* Call high-watermark cb as needed */ | |
385 if (call_cb && queue->h_cb) | |
386 (*queue->h_cb)(queue, &queue->data); | |
387 | |
388 /* Done */ | |
389 return 0; | |
390 } | |
391 | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
392 /* Pop the first item from the queue */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
393 static void * mq_pop(struct fifo * queue) |
0 | 394 { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
395 void * ret = NULL; |
0 | 396 struct fd_list * li; |
397 | |
398 ASSERT( ! FD_IS_LIST_EMPTY(&queue->list) ); | |
399 | |
400 fd_list_unlink(li = queue->list.next); | |
401 queue->count--; | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
402 ret = li->o; |
0 | 403 free(li); |
404 | |
767
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
405 if (queue->thrs_push) { |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
406 CHECK_POSIX_DO( pthread_cond_signal( &queue->cond_push ), ); |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
407 } |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
408 |
0 | 409 return ret; |
410 } | |
411 | |
412 /* Check if the low watermark callback must be called. */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
413 static __inline__ int test_l_cb(struct fifo * queue) |
0 | 414 { |
415 if ((queue->high == 0) || (queue->low == 0) || (queue->l_cb == 0)) | |
416 return 0; | |
417 | |
418 if (((queue->count % queue->high) == queue->low) && (queue->highest > queue->count)) { | |
419 queue->highest -= queue->high; | |
420 return 1; | |
421 } | |
422 | |
423 return 0; | |
424 } | |
425 | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
426 /* Try poping an item */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
427 int fd_fifo_tryget_int ( struct fifo * queue, void ** item ) |
0 | 428 { |
429 int wouldblock = 0; | |
430 int call_cb = 0; | |
431 | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
432 TRACE_ENTRY( "%p %p", queue, item ); |
0 | 433 |
434 /* Check the parameters */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
435 CHECK_PARAMS( CHECK_FIFO( queue ) && item ); |
0 | 436 |
437 /* lock the queue */ | |
438 CHECK_POSIX( pthread_mutex_lock( &queue->mtx ) ); | |
439 | |
440 /* Check queue status */ | |
441 if (queue->count > 0) { | |
767
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
442 got_item: |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
443 /* There are elements in the queue, so pick the first one */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
444 *item = mq_pop(queue); |
0 | 445 call_cb = test_l_cb(queue); |
446 } else { | |
767
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
447 if (queue->thrs_push > 0) { |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
448 /* A thread is trying to push something, let's give it a chance */ |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
449 CHECK_POSIX( pthread_mutex_unlock( &queue->mtx ) ); |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
450 CHECK_POSIX( pthread_cond_signal( &queue->cond_push ) ); |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
451 usleep(1000); |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
452 CHECK_POSIX( pthread_mutex_lock( &queue->mtx ) ); |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
453 if (queue->count > 0) |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
454 goto got_item; |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
455 } |
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
456 |
0 | 457 wouldblock = 1; |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
458 *item = NULL; |
0 | 459 } |
460 | |
461 /* Unlock */ | |
462 CHECK_POSIX( pthread_mutex_unlock( &queue->mtx ) ); | |
463 | |
464 /* Call low watermark callback as needed */ | |
465 if (call_cb) | |
466 (*queue->l_cb)(queue, &queue->data); | |
467 | |
468 /* Done */ | |
469 return wouldblock ? EWOULDBLOCK : 0; | |
470 } | |
471 | |
472 /* This handler is called when a thread is blocked on a queue, and cancelled */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
473 static void fifo_cleanup(void * queue) |
0 | 474 { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
475 struct fifo * q = (struct fifo *)queue; |
0 | 476 TRACE_ENTRY( "%p", queue ); |
477 | |
478 /* The thread has been cancelled, therefore it does not wait on the queue anymore */ | |
479 q->thrs--; | |
480 | |
481 /* Now unlock the queue, and we're done */ | |
482 CHECK_POSIX_DO( pthread_mutex_unlock( &q->mtx ), /* nothing */ ); | |
483 | |
484 /* End of cleanup handler */ | |
485 return; | |
486 } | |
487 | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
488 /* The internal function for fd_fifo_timedget and fd_fifo_get */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
489 static int fifo_tget ( struct fifo * queue, void ** item, int istimed, const struct timespec *abstime) |
0 | 490 { |
491 int timedout = 0; | |
492 int call_cb = 0; | |
493 | |
494 /* Check the parameters */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
495 CHECK_PARAMS( CHECK_FIFO( queue ) && item && (abstime || !istimed) ); |
0 | 496 |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
497 /* Initialize the return value */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
498 *item = NULL; |
0 | 499 |
500 /* lock the queue */ | |
501 CHECK_POSIX( pthread_mutex_lock( &queue->mtx ) ); | |
502 | |
503 awaken: | |
504 /* Check queue status */ | |
21
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
505 if (!CHECK_FIFO( queue )) { |
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
506 /* The queue is being destroyed */ |
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
507 CHECK_POSIX( pthread_mutex_unlock( &queue->mtx ) ); |
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
508 TRACE_DEBUG(FULL, "The queue is being destroyed -> EPIPE"); |
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
509 return EPIPE; |
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
510 } |
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
511 |
0 | 512 if (queue->count > 0) { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
513 /* There are items in the queue, so pick the first one */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
514 *item = mq_pop(queue); |
0 | 515 call_cb = test_l_cb(queue); |
516 } else { | |
517 int ret = 0; | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
518 /* We have to wait for a new item */ |
0 | 519 queue->thrs++ ; |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
520 pthread_cleanup_push( fifo_cleanup, queue); |
0 | 521 if (istimed) { |
767
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
522 ret = pthread_cond_timedwait( &queue->cond_pull, &queue->mtx, abstime ); |
0 | 523 } else { |
767
c47c16436f71
Added a limit on fifo queues to avoid memory exaustion when messages are received faster than handled
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
524 ret = pthread_cond_wait( &queue->cond_pull, &queue->mtx ); |
0 | 525 } |
526 pthread_cleanup_pop(0); | |
527 queue->thrs-- ; | |
528 if (ret == 0) | |
529 goto awaken; /* test for spurious wake-ups */ | |
530 | |
531 if (istimed && (ret == ETIMEDOUT)) { | |
532 timedout = 1; | |
533 } else { | |
534 /* Unexpected error condition (means we need to debug) */ | |
535 ASSERT( ret == 0 /* never true */ ); | |
536 } | |
537 } | |
538 | |
539 /* Unlock */ | |
540 CHECK_POSIX( pthread_mutex_unlock( &queue->mtx ) ); | |
541 | |
542 /* Call low watermark callback as needed */ | |
543 if (call_cb) | |
544 (*queue->l_cb)(queue, &queue->data); | |
545 | |
546 /* Done */ | |
547 return timedout ? ETIMEDOUT : 0; | |
548 } | |
549 | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
550 /* Get the next available item, block until there is one */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
551 int fd_fifo_get_int ( struct fifo * queue, void ** item ) |
0 | 552 { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
553 TRACE_ENTRY( "%p %p", queue, item ); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
554 return fifo_tget(queue, item, 0, NULL); |
0 | 555 } |
556 | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
557 /* Get the next available item, block until there is one, or the timeout expires */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
558 int fd_fifo_timedget_int ( struct fifo * queue, void ** item, const struct timespec *abstime ) |
0 | 559 { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
560 TRACE_ENTRY( "%p %p %p", queue, item, abstime ); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
561 return fifo_tget(queue, item, 1, abstime); |
0 | 562 } |
563 |