Mercurial > hg > freeDiameter
annotate libfdproto/fifo.c @ 1104:757df62cadb6
Merge
author | Sebastien Decugis <sdecugis@freediameter.net> |
---|---|
date | Fri, 10 May 2013 18:49:19 +0800 |
parents | d8591b1c56cd a1d6e1980132 |
children | 1af09cc156d6 |
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) */ |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
72 |
1067
23989d6c8390
Add total count in the statistics of the queues, thanks for the feedback :)
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1060
diff
changeset
|
73 long long total_items; /* Cumulated number of items that went through this fifo (excluding current count), always increasing. */ |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
74 struct timespec total_time; /* Cumulated time all items spent in this queue, including blocking time (always growing, use deltas for monitoring) */ |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
75 struct timespec blocking_time; /* Cumulated time threads trying to post new items were blocked (queue full). */ |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
76 struct timespec last_time; /* For the last element retrieved from the queue, how long it take between posting (including blocking) and poping */ |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
77 |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
78 }; |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
79 |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
80 struct fifo_item { |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
81 struct fd_list item; |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
82 struct timespec posted_on; |
0 | 83 }; |
84 | |
85 /* The eye catcher value */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
86 #define FIFO_EYEC 0xe7ec1130 |
0 | 87 |
88 /* Macro to check a pointer */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
89 #define CHECK_FIFO( _queue ) (( (_queue) != NULL) && ( (_queue)->eyec == FIFO_EYEC) ) |
0 | 90 |
91 | |
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
|
92 /* 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
|
93 int fd_fifo_new ( struct fifo ** queue, int max ) |
0 | 94 { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
95 struct fifo * new; |
0 | 96 |
97 TRACE_ENTRY( "%p", queue ); | |
98 | |
99 CHECK_PARAMS( queue ); | |
100 | |
101 /* Create a new object */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
102 CHECK_MALLOC( new = malloc (sizeof (struct fifo) ) ); |
0 | 103 |
104 /* Initialize the content */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
105 memset(new, 0, sizeof(struct fifo)); |
0 | 106 |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
107 new->eyec = FIFO_EYEC; |
0 | 108 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
|
109 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
|
110 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
|
111 new->max = max; |
0 | 112 |
113 fd_list_init(&new->list, NULL); | |
114 | |
115 /* We're done */ | |
116 *queue = new; | |
117 return 0; | |
118 } | |
119 | |
10
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
120 /* Dump the content of a queue */ |
1085
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
121 DECLARE_FD_DUMP_PROTOTYPE(fd_fifo_dump, char * name, struct fifo * queue, fd_fifo_dump_item_cb dump_item) |
10
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
122 { |
1093
44f3e48dfe27
Align the behavior of all fd_*dump functions wrt final \n
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1085
diff
changeset
|
123 FD_DUMP_HANDLE_OFFSET(); |
10
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
124 |
1085
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
125 if (name) { |
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
126 CHECK_MALLOC_DO( fd_dump_extend( FD_DUMP_STD_PARAMS, "'%s'(@%p): ", name, queue), return NULL); |
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
127 } else { |
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
128 CHECK_MALLOC_DO( fd_dump_extend( FD_DUMP_STD_PARAMS, "{fifo}(@%p): ", queue), return NULL); |
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
129 } |
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
130 |
10
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
131 if (!CHECK_FIFO( queue )) { |
1093
44f3e48dfe27
Align the behavior of all fd_*dump functions wrt final \n
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1085
diff
changeset
|
132 return fd_dump_extend(FD_DUMP_STD_PARAMS, "INVALID/NULL"); |
10
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
133 } |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
134 |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
135 CHECK_POSIX_DO( pthread_mutex_lock( &queue->mtx ), /* continue */ ); |
1093
44f3e48dfe27
Align the behavior of all fd_*dump functions wrt final \n
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1085
diff
changeset
|
136 CHECK_MALLOC_DO( fd_dump_extend( FD_DUMP_STD_PARAMS, "items:%d,%d,%d threads:%d,%d stats:%lld/%ld.%06ld,%ld.%06ld,%ld.%06ld thresholds:%d,%d,%d,%p,%p,%p", |
1085
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
137 queue->count, queue->highest_ever, queue->max, |
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
138 queue->thrs, queue->thrs_push, |
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
139 queue->total_items,(long)queue->total_time.tv_sec,(long)(queue->total_time.tv_nsec/1000),(long)queue->blocking_time.tv_sec,(long)(queue->blocking_time.tv_nsec/1000),(long)queue->last_time.tv_sec,(long)(queue->last_time.tv_nsec/1000), |
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
140 queue->high, queue->low, queue->highest, queue->h_cb, queue->l_cb, queue->data), |
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
141 goto error); |
10
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 if (dump_item) { |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
144 struct fd_list * li; |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
145 int i = 0; |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
146 for (li = queue->list.next; li != &queue->list; li = li->next) { |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
147 struct fifo_item * fi = (struct fifo_item *)li; |
1093
44f3e48dfe27
Align the behavior of all fd_*dump functions wrt final \n
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1085
diff
changeset
|
148 CHECK_MALLOC_DO( fd_dump_extend( FD_DUMP_STD_PARAMS, "\n [#%i](@%p)@%ld.%06ld: ", |
1085
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
149 i++, fi->item.o, (long)fi->posted_on.tv_sec,(long)(fi->posted_on.tv_nsec/1000)), |
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
150 goto error); |
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
151 CHECK_MALLOC_DO( (*dump_item)(FD_DUMP_STD_PARAMS, fi->item.o), goto error); |
10
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
152 } |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
153 } |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
154 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
|
155 |
1085
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
156 return *buf; |
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
157 error: |
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
158 CHECK_POSIX_DO( pthread_mutex_unlock( &queue->mtx ), /* continue */ ); |
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
159 return NULL; |
10
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
160 } |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
161 |
21
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
162 /* Delete a queue. It must be empty. */ |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
163 int fd_fifo_del ( struct fifo ** queue ) |
0 | 164 { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
165 struct fifo * q; |
21
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
166 int loops = 0; |
0 | 167 |
168 TRACE_ENTRY( "%p", queue ); | |
169 | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
170 CHECK_PARAMS( queue && CHECK_FIFO( *queue ) ); |
0 | 171 |
172 q = *queue; | |
173 | |
174 CHECK_POSIX( pthread_mutex_lock( &q->mtx ) ); | |
175 | |
21
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
176 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
|
177 TRACE_DEBUG(INFO, "The queue cannot be destroyed (%d, %p)", q->count, q->data); |
0 | 178 CHECK_POSIX_DO( pthread_mutex_unlock( &q->mtx ), /* no fallback */ ); |
179 return EINVAL; | |
180 } | |
181 | |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
182 /* Ok, now invalidate the queue */ |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
183 q->eyec = 0xdead; |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
184 |
156
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
139
diff
changeset
|
185 /* 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
|
186 while (q->thrs) { |
138
4249a55f859a
Attempt to fix behavior on multi-CPU platforms
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
137
diff
changeset
|
187 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
|
188 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
|
189 usleep(1000); |
139
8b8d11fadc4e
Attempt to fix behavior on multi-CPU platforms, take 2
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
138
diff
changeset
|
190 |
21
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
191 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
|
192 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
|
193 } |
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
194 |
0 | 195 /* sanity check */ |
196 ASSERT(FD_IS_LIST_EMPTY(&q->list)); | |
197 | |
198 /* And destroy it */ | |
199 CHECK_POSIX( pthread_mutex_unlock( &q->mtx ) ); | |
200 | |
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
|
201 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
|
202 |
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
|
203 CHECK_POSIX_DO( pthread_cond_destroy( &q->cond_push ), ); |
0 | 204 |
451
4395961aad27
Some libc seem to have an issue with canceled threads...
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
450
diff
changeset
|
205 CHECK_POSIX_DO( pthread_mutex_destroy( &q->mtx ), ); |
0 | 206 |
207 free(q); | |
208 *queue = NULL; | |
209 | |
210 return 0; | |
211 } | |
212 | |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
213 /* 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
|
214 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
|
215 { |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
216 int loops = 0; |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
217 |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
218 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
|
219 CHECK_PARAMS( CHECK_FIFO( old ) && CHECK_FIFO( new )); |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
220 |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
221 CHECK_PARAMS( ! old->data ); |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
222 if (new->high) { |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
223 TODO("Implement support for thresholds in fd_fifo_move..."); |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
224 } |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
225 |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
226 /* Update loc_update */ |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
227 if (loc_update) |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
228 *loc_update = new; |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
229 |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
230 /* 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
|
231 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
|
232 |
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
|
233 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
|
234 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
|
235 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
|
236 } ); |
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 |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
238 CHECK_POSIX( pthread_mutex_lock( &new->mtx ) ); |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
239 |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
240 /* 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
|
241 old->eyec = 0xdead; |
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
242 while (old->thrs) { |
138
4249a55f859a
Attempt to fix behavior on multi-CPU platforms
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
137
diff
changeset
|
243 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
|
244 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
|
245 usleep(1000); |
139
8b8d11fadc4e
Attempt to fix behavior on multi-CPU platforms, take 2
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
138
diff
changeset
|
246 |
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_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
|
248 ASSERT( loops < 20 ); /* detect infinite loops */ |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
249 } |
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 /* 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
|
252 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
|
253 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
|
254 CHECK_POSIX( pthread_cond_signal(&new->cond_pull) ); |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
255 } |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
256 new->count += old->count; |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
257 |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
258 /* Reset old */ |
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
259 old->count = 0; |
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
260 old->eyec = FIFO_EYEC; |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
261 |
1067
23989d6c8390
Add total count in the statistics of the queues, thanks for the feedback :)
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1060
diff
changeset
|
262 /* Merge the stats in the new queue */ |
23989d6c8390
Add total count in the statistics of the queues, thanks for the feedback :)
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1060
diff
changeset
|
263 new->total_items += old->total_items; |
23989d6c8390
Add total count in the statistics of the queues, thanks for the feedback :)
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1060
diff
changeset
|
264 old->total_items = 0; |
23989d6c8390
Add total count in the statistics of the queues, thanks for the feedback :)
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1060
diff
changeset
|
265 |
23989d6c8390
Add total count in the statistics of the queues, thanks for the feedback :)
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1060
diff
changeset
|
266 new->total_time.tv_nsec += old->total_time.tv_nsec; |
23989d6c8390
Add total count in the statistics of the queues, thanks for the feedback :)
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1060
diff
changeset
|
267 new->total_time.tv_sec += old->total_time.tv_sec + (new->total_time.tv_nsec / 1000000000); |
23989d6c8390
Add total count in the statistics of the queues, thanks for the feedback :)
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1060
diff
changeset
|
268 new->total_time.tv_nsec %= 1000000000; |
23989d6c8390
Add total count in the statistics of the queues, thanks for the feedback :)
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1060
diff
changeset
|
269 old->total_time.tv_nsec = 0; |
23989d6c8390
Add total count in the statistics of the queues, thanks for the feedback :)
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1060
diff
changeset
|
270 old->total_time.tv_sec = 0; |
23989d6c8390
Add total count in the statistics of the queues, thanks for the feedback :)
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1060
diff
changeset
|
271 |
23989d6c8390
Add total count in the statistics of the queues, thanks for the feedback :)
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1060
diff
changeset
|
272 new->blocking_time.tv_nsec += old->blocking_time.tv_nsec; |
23989d6c8390
Add total count in the statistics of the queues, thanks for the feedback :)
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1060
diff
changeset
|
273 new->blocking_time.tv_sec += old->blocking_time.tv_sec + (new->blocking_time.tv_nsec / 1000000000); |
23989d6c8390
Add total count in the statistics of the queues, thanks for the feedback :)
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1060
diff
changeset
|
274 new->blocking_time.tv_nsec %= 1000000000; |
23989d6c8390
Add total count in the statistics of the queues, thanks for the feedback :)
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1060
diff
changeset
|
275 old->blocking_time.tv_nsec = 0; |
23989d6c8390
Add total count in the statistics of the queues, thanks for the feedback :)
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1060
diff
changeset
|
276 old->blocking_time.tv_sec = 0; |
23989d6c8390
Add total count in the statistics of the queues, thanks for the feedback :)
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1060
diff
changeset
|
277 |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
278 /* Unlock, we're done */ |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
279 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
|
280 CHECK_POSIX( pthread_mutex_unlock( &old->mtx ) ); |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
281 |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
282 return 0; |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
283 } |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
284 |
1071
6ca6cadf209c
Implement the fd_stat_getstats function; changed prototype of fd_fifo_length.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1067
diff
changeset
|
285 /* Get the information on the queue */ |
6ca6cadf209c
Implement the fd_stat_getstats function; changed prototype of fd_fifo_length.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1067
diff
changeset
|
286 int fd_fifo_getstats( struct fifo * queue, int * current_count, int * limit_count, int * highest_count, long long * total_count, |
6ca6cadf209c
Implement the fd_stat_getstats function; changed prototype of fd_fifo_length.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1067
diff
changeset
|
287 struct timespec * total, struct timespec * blocking, struct timespec * last) |
0 | 288 { |
1071
6ca6cadf209c
Implement the fd_stat_getstats function; changed prototype of fd_fifo_length.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1067
diff
changeset
|
289 TRACE_ENTRY( "%p %p %p %p %p %p %p %p", queue, current_count, limit_count, highest_count, total_count, total, blocking, last); |
0 | 290 |
291 /* Check the parameters */ | |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
292 CHECK_PARAMS( CHECK_FIFO( queue ) ); |
0 | 293 |
294 /* lock the queue */ | |
295 CHECK_POSIX( pthread_mutex_lock( &queue->mtx ) ); | |
296 | |
1071
6ca6cadf209c
Implement the fd_stat_getstats function; changed prototype of fd_fifo_length.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1067
diff
changeset
|
297 if (current_count) |
6ca6cadf209c
Implement the fd_stat_getstats function; changed prototype of fd_fifo_length.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1067
diff
changeset
|
298 *current_count = queue->count; |
6ca6cadf209c
Implement the fd_stat_getstats function; changed prototype of fd_fifo_length.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1067
diff
changeset
|
299 |
6ca6cadf209c
Implement the fd_stat_getstats function; changed prototype of fd_fifo_length.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1067
diff
changeset
|
300 if (limit_count) |
6ca6cadf209c
Implement the fd_stat_getstats function; changed prototype of fd_fifo_length.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1067
diff
changeset
|
301 *limit_count = queue->max; |
6ca6cadf209c
Implement the fd_stat_getstats function; changed prototype of fd_fifo_length.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1067
diff
changeset
|
302 |
6ca6cadf209c
Implement the fd_stat_getstats function; changed prototype of fd_fifo_length.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1067
diff
changeset
|
303 if (highest_count) |
6ca6cadf209c
Implement the fd_stat_getstats function; changed prototype of fd_fifo_length.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1067
diff
changeset
|
304 *highest_count = queue->highest_ever; |
6ca6cadf209c
Implement the fd_stat_getstats function; changed prototype of fd_fifo_length.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1067
diff
changeset
|
305 |
6ca6cadf209c
Implement the fd_stat_getstats function; changed prototype of fd_fifo_length.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1067
diff
changeset
|
306 if (total_count) |
6ca6cadf209c
Implement the fd_stat_getstats function; changed prototype of fd_fifo_length.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1067
diff
changeset
|
307 *total_count = queue->total_items; |
1067
23989d6c8390
Add total count in the statistics of the queues, thanks for the feedback :)
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1060
diff
changeset
|
308 |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
309 if (total) |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
310 memcpy(total, &queue->total_time, sizeof(struct timespec)); |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
311 |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
312 if (blocking) |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
313 memcpy(blocking, &queue->blocking_time, sizeof(struct timespec)); |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
314 |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
315 if (last) |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
316 memcpy(last, &queue->last_time, sizeof(struct timespec)); |
0 | 317 |
318 /* Unlock */ | |
319 CHECK_POSIX( pthread_mutex_unlock( &queue->mtx ) ); | |
320 | |
321 /* Done */ | |
322 return 0; | |
323 } | |
324 | |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
325 |
0 | 326 /* alternate version with no error checking */ |
1071
6ca6cadf209c
Implement the fd_stat_getstats function; changed prototype of fd_fifo_length.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1067
diff
changeset
|
327 int fd_fifo_length ( struct fifo * queue ) |
0 | 328 { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
329 if ( !CHECK_FIFO( queue ) ) |
0 | 330 return 0; |
331 | |
332 return queue->count; /* Let's hope it's read atomically, since we are not locking... */ | |
333 } | |
334 | |
335 /* Set the thresholds of the queue */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
336 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 | 337 { |
338 TRACE_ENTRY( "%p %p %hu %p %hu %p", queue, data, high, h_cb, low, l_cb ); | |
339 | |
340 /* Check the parameters */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
341 CHECK_PARAMS( CHECK_FIFO( queue ) && (high > low) && (queue->data == NULL) ); |
0 | 342 |
343 /* lock the queue */ | |
344 CHECK_POSIX( pthread_mutex_lock( &queue->mtx ) ); | |
345 | |
346 /* Save the values */ | |
347 queue->high = high; | |
348 queue->low = low; | |
349 queue->data = data; | |
350 queue->h_cb = h_cb; | |
351 queue->l_cb = l_cb; | |
352 | |
353 /* Unlock */ | |
354 CHECK_POSIX( pthread_mutex_unlock( &queue->mtx ) ); | |
355 | |
356 /* Done */ | |
357 return 0; | |
358 } | |
359 | |
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
|
360 |
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
|
361 /* 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
|
362 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
|
363 { |
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
|
364 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
|
365 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
|
366 |
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
|
367 /* 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
|
368 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
|
369 |
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
|
370 /* 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
|
371 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
|
372 |
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
|
373 /* 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
|
374 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
|
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 |
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 |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
378 /* Post a new item in the queue */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
379 int fd_fifo_post_int ( struct fifo * queue, void ** item ) |
0 | 380 { |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
381 struct fifo_item * new; |
0 | 382 int call_cb = 0; |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
383 struct timespec posted_on, queued_on; |
0 | 384 |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
385 TRACE_ENTRY( "%p %p", queue, item ); |
0 | 386 |
387 /* Check the parameters */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
388 CHECK_PARAMS( CHECK_FIFO( queue ) && item && *item ); |
0 | 389 |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
390 /* Get the timing of this call */ |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
391 CHECK_SYS( clock_gettime(CLOCK_REALTIME, &posted_on) ); |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
392 |
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
|
393 /* 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
|
394 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
|
395 |
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
|
396 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
|
397 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
|
398 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
|
399 |
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
|
400 /* 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
|
401 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
|
402 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
|
403 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
|
404 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
|
405 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 |
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 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
|
408 } |
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
|
409 } |
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
|
410 |
0 | 411 /* Create a new list item */ |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
412 CHECK_MALLOC_DO( new = malloc (sizeof (struct fifo_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
|
413 pthread_mutex_unlock( &queue->mtx ); |
1103
d8591b1c56cd
Implemented a few hooks
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1093
diff
changeset
|
414 return ENOMEM; |
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
|
415 } ); |
0 | 416 |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
417 fd_list_init(&new->item, *item); |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
418 *item = NULL; |
0 | 419 |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
420 /* Add the new item at the end */ |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
421 fd_list_insert_before( &queue->list, &new->item); |
0 | 422 queue->count++; |
14
14cf6daf716d
Some progress on peers module
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
11
diff
changeset
|
423 if (queue->highest_ever < queue->count) |
14cf6daf716d
Some progress on peers module
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
11
diff
changeset
|
424 queue->highest_ever = queue->count; |
0 | 425 if (queue->high && ((queue->count % queue->high) == 0)) { |
426 call_cb = 1; | |
427 queue->highest = queue->count; | |
428 } | |
429 | |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
430 /* store timing */ |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
431 memcpy(&new->posted_on, &posted_on, sizeof(struct timespec)); |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
432 |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
433 /* update queue timing info "blocking time" */ |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
434 { |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
435 long long blocked_ns; |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
436 CHECK_SYS( clock_gettime(CLOCK_REALTIME, &queued_on) ); |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
437 blocked_ns = (queued_on.tv_sec - posted_on.tv_sec) * 1000000000; |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
438 blocked_ns += (queued_on.tv_nsec - posted_on.tv_nsec); |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
439 blocked_ns += queue->blocking_time.tv_nsec; |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
440 queue->blocking_time.tv_sec += blocked_ns / 1000000000; |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
441 queue->blocking_time.tv_nsec = blocked_ns % 1000000000; |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
442 } |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
443 |
0 | 444 /* Signal if threads are asleep */ |
445 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
|
446 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
|
447 } |
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 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
|
449 /* 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
|
450 CHECK_POSIX( pthread_cond_signal(&queue->cond_push) ); |
0 | 451 } |
452 | |
453 /* Unlock */ | |
454 CHECK_POSIX( pthread_mutex_unlock( &queue->mtx ) ); | |
455 | |
456 /* Call high-watermark cb as needed */ | |
457 if (call_cb && queue->h_cb) | |
458 (*queue->h_cb)(queue, &queue->data); | |
459 | |
460 /* Done */ | |
461 return 0; | |
462 } | |
463 | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
464 /* Pop the first item from the queue */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
465 static void * mq_pop(struct fifo * queue) |
0 | 466 { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
467 void * ret = NULL; |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
468 struct fifo_item * fi; |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
469 struct timespec now; |
0 | 470 |
471 ASSERT( ! FD_IS_LIST_EMPTY(&queue->list) ); | |
472 | |
1103
d8591b1c56cd
Implemented a few hooks
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1093
diff
changeset
|
473 fi = (struct fifo_item *)(queue->list.next); |
d8591b1c56cd
Implemented a few hooks
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1093
diff
changeset
|
474 ret = fi->item.o; |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
475 fd_list_unlink(&fi->item); |
0 | 476 queue->count--; |
1067
23989d6c8390
Add total count in the statistics of the queues, thanks for the feedback :)
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1060
diff
changeset
|
477 queue->total_items++; |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
478 |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
479 /* Update the timings */ |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
480 CHECK_SYS_DO( clock_gettime(CLOCK_REALTIME, &now), goto skip_timing ); |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
481 { |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
482 long long elapsed = (now.tv_sec - fi->posted_on.tv_sec) * 1000000000; |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
483 elapsed += now.tv_nsec - fi->posted_on.tv_nsec; |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
484 |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
485 queue->last_time.tv_sec = elapsed / 1000000000; |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
486 queue->last_time.tv_nsec = elapsed % 1000000000; |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
487 |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
488 elapsed += queue->total_time.tv_nsec; |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
489 queue->total_time.tv_sec += elapsed / 1000000000; |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
490 queue->total_time.tv_nsec = elapsed % 1000000000; |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
491 } |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
492 skip_timing: |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
493 free(fi); |
0 | 494 |
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
|
495 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
|
496 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
|
497 } |
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
|
498 |
0 | 499 return ret; |
500 } | |
501 | |
502 /* 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
|
503 static __inline__ int test_l_cb(struct fifo * queue) |
0 | 504 { |
505 if ((queue->high == 0) || (queue->low == 0) || (queue->l_cb == 0)) | |
506 return 0; | |
507 | |
508 if (((queue->count % queue->high) == queue->low) && (queue->highest > queue->count)) { | |
509 queue->highest -= queue->high; | |
510 return 1; | |
511 } | |
512 | |
513 return 0; | |
514 } | |
515 | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
516 /* Try poping an item */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
517 int fd_fifo_tryget_int ( struct fifo * queue, void ** item ) |
0 | 518 { |
519 int wouldblock = 0; | |
520 int call_cb = 0; | |
521 | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
522 TRACE_ENTRY( "%p %p", queue, item ); |
0 | 523 |
524 /* Check the parameters */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
525 CHECK_PARAMS( CHECK_FIFO( queue ) && item ); |
0 | 526 |
527 /* lock the queue */ | |
528 CHECK_POSIX( pthread_mutex_lock( &queue->mtx ) ); | |
529 | |
530 /* Check queue status */ | |
531 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
|
532 got_item: |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
533 /* 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
|
534 *item = mq_pop(queue); |
0 | 535 call_cb = test_l_cb(queue); |
536 } 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
|
537 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
|
538 /* 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
|
539 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
|
540 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
|
541 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
|
542 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
|
543 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
|
544 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
|
545 } |
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
|
546 |
0 | 547 wouldblock = 1; |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
548 *item = NULL; |
0 | 549 } |
550 | |
551 /* Unlock */ | |
552 CHECK_POSIX( pthread_mutex_unlock( &queue->mtx ) ); | |
553 | |
554 /* Call low watermark callback as needed */ | |
555 if (call_cb) | |
556 (*queue->l_cb)(queue, &queue->data); | |
557 | |
558 /* Done */ | |
559 return wouldblock ? EWOULDBLOCK : 0; | |
560 } | |
561 | |
562 /* 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
|
563 static void fifo_cleanup(void * queue) |
0 | 564 { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
565 struct fifo * q = (struct fifo *)queue; |
0 | 566 TRACE_ENTRY( "%p", queue ); |
567 | |
568 /* The thread has been cancelled, therefore it does not wait on the queue anymore */ | |
569 q->thrs--; | |
570 | |
571 /* Now unlock the queue, and we're done */ | |
572 CHECK_POSIX_DO( pthread_mutex_unlock( &q->mtx ), /* nothing */ ); | |
573 | |
574 /* End of cleanup handler */ | |
575 return; | |
576 } | |
577 | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
578 /* 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
|
579 static int fifo_tget ( struct fifo * queue, void ** item, int istimed, const struct timespec *abstime) |
0 | 580 { |
581 int call_cb = 0; | |
1059
a1d6e1980132
Fix fifo_tget to propagate any error returned by the posix call
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
582 int ret = 0; |
0 | 583 |
584 /* Check the parameters */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
585 CHECK_PARAMS( CHECK_FIFO( queue ) && item && (abstime || !istimed) ); |
0 | 586 |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
587 /* Initialize the return value */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
588 *item = NULL; |
0 | 589 |
590 /* lock the queue */ | |
591 CHECK_POSIX( pthread_mutex_lock( &queue->mtx ) ); | |
592 | |
593 awaken: | |
594 /* Check queue status */ | |
21
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
595 if (!CHECK_FIFO( queue )) { |
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
596 /* 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
|
597 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
|
598 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
|
599 return EPIPE; |
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
600 } |
1103
d8591b1c56cd
Implemented a few hooks
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1093
diff
changeset
|
601 |
0 | 602 if (queue->count > 0) { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
603 /* 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
|
604 *item = mq_pop(queue); |
0 | 605 call_cb = test_l_cb(queue); |
606 } else { | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
607 /* We have to wait for a new item */ |
0 | 608 queue->thrs++ ; |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
609 pthread_cleanup_push( fifo_cleanup, queue); |
0 | 610 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
|
611 ret = pthread_cond_timedwait( &queue->cond_pull, &queue->mtx, abstime ); |
0 | 612 } 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
|
613 ret = pthread_cond_wait( &queue->cond_pull, &queue->mtx ); |
0 | 614 } |
615 pthread_cleanup_pop(0); | |
616 queue->thrs-- ; | |
617 if (ret == 0) | |
618 goto awaken; /* test for spurious wake-ups */ | |
619 | |
1059
a1d6e1980132
Fix fifo_tget to propagate any error returned by the posix call
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
620 /* otherwise (ETIMEDOUT / other error) just continue */ |
0 | 621 } |
622 | |
623 /* Unlock */ | |
624 CHECK_POSIX( pthread_mutex_unlock( &queue->mtx ) ); | |
625 | |
626 /* Call low watermark callback as needed */ | |
627 if (call_cb) | |
628 (*queue->l_cb)(queue, &queue->data); | |
629 | |
630 /* Done */ | |
1059
a1d6e1980132
Fix fifo_tget to propagate any error returned by the posix call
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
631 return ret; |
0 | 632 } |
633 | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
634 /* 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
|
635 int fd_fifo_get_int ( struct fifo * queue, void ** item ) |
0 | 636 { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
637 TRACE_ENTRY( "%p %p", queue, item ); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
638 return fifo_tget(queue, item, 0, NULL); |
0 | 639 } |
640 | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
641 /* 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
|
642 int fd_fifo_timedget_int ( struct fifo * queue, void ** item, const struct timespec *abstime ) |
0 | 643 { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
644 TRACE_ENTRY( "%p %p %p", queue, item, abstime ); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
645 return fifo_tget(queue, item, 1, abstime); |
0 | 646 } |
647 |