Mercurial > hg > freeDiameter
annotate libfdproto/fifo.c @ 1393:357e9cec520f
libfdproto: add fd_fifo_set_max
This function sets the maximum size for a FIFO.
author | Thomas Klausner <tk@giga.or.at> |
---|---|
date | Fri, 15 Nov 2019 11:29:15 +0100 |
parents | 4968bc47a1a5 |
children | 3cbe458fbfa9 |
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 * * |
1127
1af09cc156d6
Updated copyright information
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1104
diff
changeset
|
5 * Copyright (c) 2013, 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 */ |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
53 |
0 | 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 */ |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
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) */ |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
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 */ |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
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) */ |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
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 */ |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
77 |
1060
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; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
96 |
0 | 97 TRACE_ENTRY( "%p", queue ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
98 |
0 | 99 CHECK_PARAMS( queue ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
100 |
0 | 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) ) ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
103 |
0 | 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)); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
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; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
112 |
0 | 113 fd_list_init(&new->list, NULL); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
114 |
0 | 115 /* We're done */ |
116 *queue = new; | |
117 return 0; | |
118 } | |
119 | |
1393
357e9cec520f
libfdproto: add fd_fifo_set_max
Thomas Klausner <tk@giga.or.at>
parents:
1385
diff
changeset
|
120 int fd_fifo_set_max (struct fifo * queue, int max) |
357e9cec520f
libfdproto: add fd_fifo_set_max
Thomas Klausner <tk@giga.or.at>
parents:
1385
diff
changeset
|
121 { |
357e9cec520f
libfdproto: add fd_fifo_set_max
Thomas Klausner <tk@giga.or.at>
parents:
1385
diff
changeset
|
122 queue->max = max; |
357e9cec520f
libfdproto: add fd_fifo_set_max
Thomas Klausner <tk@giga.or.at>
parents:
1385
diff
changeset
|
123 return 0; |
357e9cec520f
libfdproto: add fd_fifo_set_max
Thomas Klausner <tk@giga.or.at>
parents:
1385
diff
changeset
|
124 } |
357e9cec520f
libfdproto: add fd_fifo_set_max
Thomas Klausner <tk@giga.or.at>
parents:
1385
diff
changeset
|
125 |
357e9cec520f
libfdproto: add fd_fifo_set_max
Thomas Klausner <tk@giga.or.at>
parents:
1385
diff
changeset
|
126 |
10
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
127 /* Dump the content of a queue */ |
1085
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
128 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
|
129 { |
1093
44f3e48dfe27
Align the behavior of all fd_*dump functions wrt final \n
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1085
diff
changeset
|
130 FD_DUMP_HANDLE_OFFSET(); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
131 |
1085
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
132 if (name) { |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
133 CHECK_MALLOC_DO( fd_dump_extend( FD_DUMP_STD_PARAMS, "'%s'(@%p): ", name, queue), return NULL); |
1085
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
134 } else { |
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
135 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
|
136 } |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
137 |
10
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
138 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
|
139 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
|
140 } |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
141 |
10
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
142 CHECK_POSIX_DO( pthread_mutex_lock( &queue->mtx ), /* continue */ ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
143 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
|
144 queue->count, queue->highest_ever, queue->max, |
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
145 queue->thrs, queue->thrs_push, |
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
146 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), |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
147 queue->high, queue->low, queue->highest, queue->h_cb, queue->l_cb, queue->data), |
1085
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
148 goto error); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
149 |
10
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
150 if (dump_item) { |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
151 struct fd_list * li; |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
152 int i = 0; |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
153 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
|
154 struct fifo_item * fi = (struct fifo_item *)li; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
155 CHECK_MALLOC_DO( fd_dump_extend( FD_DUMP_STD_PARAMS, "\n [#%i](@%p)@%ld.%06ld: ", |
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
156 i++, fi->item.o, (long)fi->posted_on.tv_sec,(long)(fi->posted_on.tv_nsec/1000)), |
1085
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
157 goto error); |
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
158 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
|
159 } |
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 CHECK_POSIX_DO( pthread_mutex_unlock( &queue->mtx ), /* continue */ ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
162 |
1085
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
163 return *buf; |
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
164 error: |
7d7266115a34
Cleaning of the traces in progress
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1073
diff
changeset
|
165 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
|
166 return NULL; |
10
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
167 } |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
168 |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
169 /* Delete a queue. It must be empty. */ |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
170 int fd_fifo_del ( struct fifo ** queue ) |
0 | 171 { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
172 struct fifo * q; |
21
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
173 int loops = 0; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
174 |
0 | 175 TRACE_ENTRY( "%p", queue ); |
176 | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
177 if (queue && *queue == NULL) { |
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
178 /* Queue already (in the process of being) deleted */ |
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
179 return 0; |
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
180 } |
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
181 |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
182 CHECK_PARAMS( queue && CHECK_FIFO( *queue ) ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
183 |
0 | 184 q = *queue; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
185 |
0 | 186 CHECK_POSIX( pthread_mutex_lock( &q->mtx ) ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
187 |
21
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
188 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
|
189 TRACE_DEBUG(INFO, "The queue cannot be destroyed (%d, %p)", q->count, q->data); |
0 | 190 CHECK_POSIX_DO( pthread_mutex_unlock( &q->mtx ), /* no fallback */ ); |
191 return EINVAL; | |
192 } | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
193 |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
194 /* Ok, now invalidate the queue */ |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
195 q->eyec = 0xdead; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
196 |
156
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
139
diff
changeset
|
197 /* 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
|
198 while (q->thrs) { |
138
4249a55f859a
Attempt to fix behavior on multi-CPU platforms
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
137
diff
changeset
|
199 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
|
200 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
|
201 usleep(1000); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
202 |
21
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
203 CHECK_POSIX( pthread_mutex_lock( &q->mtx ) ); |
1384
2131d6bdf32a
Be more lenient in infinite loop check, 20 is too low for some setups.
Thomas Klausner <tk@giga.or.at>
parents:
1377
diff
changeset
|
204 ASSERT( ++loops < 200 ); /* detect infinite loops */ |
21
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
205 } |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
206 |
0 | 207 /* sanity check */ |
208 ASSERT(FD_IS_LIST_EMPTY(&q->list)); | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
209 |
0 | 210 /* And destroy it */ |
211 CHECK_POSIX( pthread_mutex_unlock( &q->mtx ) ); | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
212 |
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
|
213 CHECK_POSIX_DO( pthread_cond_destroy( &q->cond_pull ), ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
214 |
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 CHECK_POSIX_DO( pthread_cond_destroy( &q->cond_push ), ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
216 |
451
4395961aad27
Some libc seem to have an issue with canceled threads...
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
450
diff
changeset
|
217 CHECK_POSIX_DO( pthread_mutex_destroy( &q->mtx ), ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
218 |
0 | 219 free(q); |
220 *queue = NULL; | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
221 |
0 | 222 return 0; |
223 } | |
224 | |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
225 /* 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
|
226 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
|
227 { |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
228 int loops = 0; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
229 |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
230 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
|
231 CHECK_PARAMS( CHECK_FIFO( old ) && CHECK_FIFO( new )); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
232 |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
233 CHECK_PARAMS( ! old->data ); |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
234 if (new->high) { |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
235 TODO("Implement support for thresholds in fd_fifo_move..."); |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
236 } |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
237 |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
238 /* Update loc_update */ |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
239 if (loc_update) |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
240 *loc_update = new; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
241 |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
242 /* 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
|
243 CHECK_POSIX( pthread_mutex_lock( &old->mtx ) ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
244 |
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
|
245 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
|
246 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
|
247 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
|
248 } ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
249 |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
250 CHECK_POSIX( pthread_mutex_lock( &new->mtx ) ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
251 |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
252 /* 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
|
253 old->eyec = 0xdead; |
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
254 while (old->thrs) { |
138
4249a55f859a
Attempt to fix behavior on multi-CPU platforms
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
137
diff
changeset
|
255 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
|
256 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
|
257 usleep(1000); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
258 |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
259 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
|
260 ASSERT( loops < 20 ); /* detect infinite loops */ |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
261 } |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
262 |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
263 /* 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
|
264 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
|
265 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
|
266 CHECK_POSIX( pthread_cond_signal(&new->cond_pull) ); |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
267 } |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
268 new->count += old->count; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
269 |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
270 /* Reset old */ |
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
271 old->count = 0; |
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
272 old->eyec = FIFO_EYEC; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
273 |
1067
23989d6c8390
Add total count in the statistics of the queues, thanks for the feedback :)
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1060
diff
changeset
|
274 /* 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
|
275 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
|
276 old->total_items = 0; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
277 |
1067
23989d6c8390
Add total count in the statistics of the queues, thanks for the feedback :)
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1060
diff
changeset
|
278 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
|
279 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
|
280 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
|
281 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
|
282 old->total_time.tv_sec = 0; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
283 |
1067
23989d6c8390
Add total count in the statistics of the queues, thanks for the feedback :)
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1060
diff
changeset
|
284 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
|
285 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
|
286 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
|
287 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
|
288 old->blocking_time.tv_sec = 0; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
289 |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
25
diff
changeset
|
290 /* Unlock, we're done */ |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
291 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
|
292 CHECK_POSIX( pthread_mutex_unlock( &old->mtx ) ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
293 |
25
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
294 return 0; |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
295 } |
67ca08d5bc48
Completed connection context files
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
21
diff
changeset
|
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 /* Get the information on the queue */ |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
298 int fd_fifo_getstats( struct fifo * queue, int * current_count, int * limit_count, int * highest_count, long long * total_count, |
1071
6ca6cadf209c
Implement the fd_stat_getstats function; changed prototype of fd_fifo_length.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1067
diff
changeset
|
299 struct timespec * total, struct timespec * blocking, struct timespec * last) |
0 | 300 { |
1071
6ca6cadf209c
Implement the fd_stat_getstats function; changed prototype of fd_fifo_length.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1067
diff
changeset
|
301 TRACE_ENTRY( "%p %p %p %p %p %p %p %p", queue, current_count, limit_count, highest_count, total_count, total, blocking, last); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
302 |
1385
4968bc47a1a5
fd_fifo_getstats: it is not an error if a queue does not exist
Thomas Klausner <tk@giga.or.at>
parents:
1384
diff
changeset
|
303 if (queue == NULL) { |
4968bc47a1a5
fd_fifo_getstats: it is not an error if a queue does not exist
Thomas Klausner <tk@giga.or.at>
parents:
1384
diff
changeset
|
304 /* It is not an error if the queue is not available; happens e.g. when peers disappear */ |
4968bc47a1a5
fd_fifo_getstats: it is not an error if a queue does not exist
Thomas Klausner <tk@giga.or.at>
parents:
1384
diff
changeset
|
305 return 0; |
4968bc47a1a5
fd_fifo_getstats: it is not an error if a queue does not exist
Thomas Klausner <tk@giga.or.at>
parents:
1384
diff
changeset
|
306 } |
4968bc47a1a5
fd_fifo_getstats: it is not an error if a queue does not exist
Thomas Klausner <tk@giga.or.at>
parents:
1384
diff
changeset
|
307 |
0 | 308 /* Check the parameters */ |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
309 CHECK_PARAMS( CHECK_FIFO( queue ) ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
310 |
0 | 311 /* lock the queue */ |
312 CHECK_POSIX( pthread_mutex_lock( &queue->mtx ) ); | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
313 |
1071
6ca6cadf209c
Implement the fd_stat_getstats function; changed prototype of fd_fifo_length.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1067
diff
changeset
|
314 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
|
315 *current_count = queue->count; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
316 |
1071
6ca6cadf209c
Implement the fd_stat_getstats function; changed prototype of fd_fifo_length.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1067
diff
changeset
|
317 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
|
318 *limit_count = queue->max; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
319 |
1071
6ca6cadf209c
Implement the fd_stat_getstats function; changed prototype of fd_fifo_length.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1067
diff
changeset
|
320 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
|
321 *highest_count = queue->highest_ever; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
322 |
1071
6ca6cadf209c
Implement the fd_stat_getstats function; changed prototype of fd_fifo_length.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1067
diff
changeset
|
323 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
|
324 *total_count = queue->total_items; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
325 |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
326 if (total) |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
327 memcpy(total, &queue->total_time, sizeof(struct timespec)); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
328 |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
329 if (blocking) |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
330 memcpy(blocking, &queue->blocking_time, sizeof(struct timespec)); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
331 |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
332 if (last) |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
333 memcpy(last, &queue->last_time, sizeof(struct timespec)); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
334 |
0 | 335 /* Unlock */ |
336 CHECK_POSIX( pthread_mutex_unlock( &queue->mtx ) ); | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
337 |
0 | 338 /* Done */ |
339 return 0; | |
340 } | |
341 | |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
342 |
0 | 343 /* 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
|
344 int fd_fifo_length ( struct fifo * queue ) |
0 | 345 { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
346 if ( !CHECK_FIFO( queue ) ) |
0 | 347 return 0; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
348 |
0 | 349 return queue->count; /* Let's hope it's read atomically, since we are not locking... */ |
350 } | |
351 | |
352 /* Set the thresholds of the queue */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
353 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 | 354 { |
355 TRACE_ENTRY( "%p %p %hu %p %hu %p", queue, data, high, h_cb, low, l_cb ); | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
356 |
0 | 357 /* Check the parameters */ |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
358 CHECK_PARAMS( CHECK_FIFO( queue ) && (high > low) && (queue->data == NULL) ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
359 |
0 | 360 /* lock the queue */ |
361 CHECK_POSIX( pthread_mutex_lock( &queue->mtx ) ); | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
362 |
0 | 363 /* Save the values */ |
364 queue->high = high; | |
365 queue->low = low; | |
366 queue->data = data; | |
367 queue->h_cb = h_cb; | |
368 queue->l_cb = l_cb; | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
369 |
0 | 370 /* Unlock */ |
371 CHECK_POSIX( pthread_mutex_unlock( &queue->mtx ) ); | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
372 |
0 | 373 /* Done */ |
374 return 0; | |
375 } | |
376 | |
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
|
377 |
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 /* 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
|
379 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
|
380 { |
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
|
381 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
|
382 TRACE_ENTRY( "%p", queue ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
383 |
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
|
384 /* 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
|
385 q->thrs_push--; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
386 |
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
|
387 /* 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
|
388 CHECK_POSIX_DO( pthread_mutex_unlock( &q->mtx ), /* nothing */ ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
389 |
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
|
390 /* 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
|
391 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
|
392 } |
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 |
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 |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
395 /* Post a new item in the queue */ |
1188
f40de74bd1c7
Don't block PSM during failover
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1186
diff
changeset
|
396 int fd_fifo_post_internal ( struct fifo * queue, void ** item, int skip_max ) |
0 | 397 { |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
398 struct fifo_item * new; |
0 | 399 int call_cb = 0; |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
400 struct timespec posted_on, queued_on; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
401 |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
402 /* 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
|
403 CHECK_SYS( clock_gettime(CLOCK_REALTIME, &posted_on) ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
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 /* 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
|
406 CHECK_POSIX( pthread_mutex_lock( &queue->mtx ) ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
407 |
1188
f40de74bd1c7
Don't block PSM during failover
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1186
diff
changeset
|
408 if ((!skip_max) && (queue->max)) { |
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
|
409 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
|
410 int ret = 0; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
411 |
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
|
412 /* 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
|
413 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
|
414 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
|
415 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
|
416 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
|
417 queue->thrs_push-- ; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
418 |
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
|
419 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
|
420 } |
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
|
421 } |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
422 |
0 | 423 /* 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
|
424 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
|
425 pthread_mutex_unlock( &queue->mtx ); |
1103
d8591b1c56cd
Implemented a few hooks
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1093
diff
changeset
|
426 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
|
427 } ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
428 |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
429 fd_list_init(&new->item, *item); |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
430 *item = NULL; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
431 |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
432 /* 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
|
433 fd_list_insert_before( &queue->list, &new->item); |
0 | 434 queue->count++; |
14
14cf6daf716d
Some progress on peers module
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
11
diff
changeset
|
435 if (queue->highest_ever < queue->count) |
14cf6daf716d
Some progress on peers module
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
11
diff
changeset
|
436 queue->highest_ever = queue->count; |
0 | 437 if (queue->high && ((queue->count % queue->high) == 0)) { |
438 call_cb = 1; | |
439 queue->highest = queue->count; | |
440 } | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
441 |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
442 /* store timing */ |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
443 memcpy(&new->posted_on, &posted_on, sizeof(struct timespec)); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
444 |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
445 /* 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
|
446 { |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
447 long long blocked_ns; |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
448 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
|
449 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
|
450 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
|
451 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
|
452 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
|
453 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
|
454 } |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
455 |
0 | 456 /* Signal if threads are asleep */ |
457 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
|
458 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
|
459 } |
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
|
460 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
|
461 /* 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
|
462 CHECK_POSIX( pthread_cond_signal(&queue->cond_push) ); |
0 | 463 } |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
464 |
0 | 465 /* Unlock */ |
466 CHECK_POSIX( pthread_mutex_unlock( &queue->mtx ) ); | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
467 |
0 | 468 /* Call high-watermark cb as needed */ |
469 if (call_cb && queue->h_cb) | |
470 (*queue->h_cb)(queue, &queue->data); | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
471 |
0 | 472 /* Done */ |
473 return 0; | |
474 } | |
475 | |
1188
f40de74bd1c7
Don't block PSM during failover
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1186
diff
changeset
|
476 /* Post a new item in the queue */ |
f40de74bd1c7
Don't block PSM during failover
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1186
diff
changeset
|
477 int fd_fifo_post_int ( struct fifo * queue, void ** item ) |
f40de74bd1c7
Don't block PSM during failover
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1186
diff
changeset
|
478 { |
f40de74bd1c7
Don't block PSM during failover
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1186
diff
changeset
|
479 TRACE_ENTRY( "%p %p", queue, item ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
480 |
1188
f40de74bd1c7
Don't block PSM during failover
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1186
diff
changeset
|
481 /* Check the parameters */ |
f40de74bd1c7
Don't block PSM during failover
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1186
diff
changeset
|
482 CHECK_PARAMS( CHECK_FIFO( queue ) && item && *item ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
483 |
1188
f40de74bd1c7
Don't block PSM during failover
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1186
diff
changeset
|
484 return fd_fifo_post_internal ( queue,item, 0 ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
485 |
1188
f40de74bd1c7
Don't block PSM during failover
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1186
diff
changeset
|
486 } |
f40de74bd1c7
Don't block PSM during failover
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1186
diff
changeset
|
487 |
f40de74bd1c7
Don't block PSM during failover
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1186
diff
changeset
|
488 /* Post a new item in the queue, not blocking */ |
f40de74bd1c7
Don't block PSM during failover
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1186
diff
changeset
|
489 int fd_fifo_post_noblock ( struct fifo * queue, void ** item ) |
f40de74bd1c7
Don't block PSM during failover
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1186
diff
changeset
|
490 { |
f40de74bd1c7
Don't block PSM during failover
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1186
diff
changeset
|
491 TRACE_ENTRY( "%p %p", queue, item ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
492 |
1188
f40de74bd1c7
Don't block PSM during failover
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1186
diff
changeset
|
493 /* Check the parameters */ |
f40de74bd1c7
Don't block PSM during failover
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1186
diff
changeset
|
494 CHECK_PARAMS( CHECK_FIFO( queue ) && item && *item ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
495 |
1188
f40de74bd1c7
Don't block PSM during failover
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1186
diff
changeset
|
496 return fd_fifo_post_internal ( queue,item, 1 ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
497 |
1188
f40de74bd1c7
Don't block PSM during failover
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1186
diff
changeset
|
498 } |
f40de74bd1c7
Don't block PSM during failover
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1186
diff
changeset
|
499 |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
500 /* Pop the first item from the queue */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
501 static void * mq_pop(struct fifo * queue) |
0 | 502 { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
503 void * ret = NULL; |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
504 struct fifo_item * fi; |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
505 struct timespec now; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
506 |
0 | 507 ASSERT( ! FD_IS_LIST_EMPTY(&queue->list) ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
508 |
1103
d8591b1c56cd
Implemented a few hooks
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1093
diff
changeset
|
509 fi = (struct fifo_item *)(queue->list.next); |
d8591b1c56cd
Implemented a few hooks
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1093
diff
changeset
|
510 ret = fi->item.o; |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
511 fd_list_unlink(&fi->item); |
0 | 512 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
|
513 queue->total_items++; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
514 |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
515 /* Update the timings */ |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
516 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
|
517 { |
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
518 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
|
519 elapsed += now.tv_nsec - fi->posted_on.tv_nsec; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
520 |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
521 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
|
522 queue->last_time.tv_nsec = elapsed % 1000000000; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
523 |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
524 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
|
525 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
|
526 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
|
527 } |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
528 skip_timing: |
1060
13fc3fc9c789
New feature in fd_fifo to get timing statistics
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
529 free(fi); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
530 |
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
|
531 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
|
532 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
|
533 } |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
534 |
0 | 535 return ret; |
536 } | |
537 | |
538 /* 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
|
539 static __inline__ int test_l_cb(struct fifo * queue) |
0 | 540 { |
541 if ((queue->high == 0) || (queue->low == 0) || (queue->l_cb == 0)) | |
542 return 0; | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
543 |
0 | 544 if (((queue->count % queue->high) == queue->low) && (queue->highest > queue->count)) { |
545 queue->highest -= queue->high; | |
546 return 1; | |
547 } | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
548 |
0 | 549 return 0; |
550 } | |
551 | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
552 /* Try poping an item */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
553 int fd_fifo_tryget_int ( struct fifo * queue, void ** item ) |
0 | 554 { |
555 int wouldblock = 0; | |
556 int call_cb = 0; | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
557 |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
558 TRACE_ENTRY( "%p %p", queue, item ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
559 |
0 | 560 /* Check the parameters */ |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
561 CHECK_PARAMS( CHECK_FIFO( queue ) && item ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
562 |
0 | 563 /* lock the queue */ |
564 CHECK_POSIX( pthread_mutex_lock( &queue->mtx ) ); | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
565 |
0 | 566 /* Check queue status */ |
567 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
|
568 got_item: |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
569 /* 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
|
570 *item = mq_pop(queue); |
0 | 571 call_cb = test_l_cb(queue); |
572 } 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
|
573 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
|
574 /* 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
|
575 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
|
576 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
|
577 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
|
578 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
|
579 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
|
580 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
|
581 } |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
582 |
0 | 583 wouldblock = 1; |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
584 *item = NULL; |
0 | 585 } |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
586 |
0 | 587 /* Unlock */ |
588 CHECK_POSIX( pthread_mutex_unlock( &queue->mtx ) ); | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
589 |
0 | 590 /* Call low watermark callback as needed */ |
591 if (call_cb) | |
592 (*queue->l_cb)(queue, &queue->data); | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
593 |
0 | 594 /* Done */ |
595 return wouldblock ? EWOULDBLOCK : 0; | |
596 } | |
597 | |
598 /* 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
|
599 static void fifo_cleanup(void * queue) |
0 | 600 { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
601 struct fifo * q = (struct fifo *)queue; |
0 | 602 TRACE_ENTRY( "%p", queue ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
603 |
0 | 604 /* The thread has been cancelled, therefore it does not wait on the queue anymore */ |
605 q->thrs--; | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
606 |
0 | 607 /* Now unlock the queue, and we're done */ |
608 CHECK_POSIX_DO( pthread_mutex_unlock( &q->mtx ), /* nothing */ ); | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
609 |
0 | 610 /* End of cleanup handler */ |
611 return; | |
612 } | |
613 | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
614 /* 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
|
615 static int fifo_tget ( struct fifo * queue, void ** item, int istimed, const struct timespec *abstime) |
0 | 616 { |
617 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
|
618 int ret = 0; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
619 |
0 | 620 /* Check the parameters */ |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
621 CHECK_PARAMS( CHECK_FIFO( queue ) && item && (abstime || !istimed) ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
622 |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
623 /* Initialize the return value */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
624 *item = NULL; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
625 |
0 | 626 /* lock the queue */ |
627 CHECK_POSIX( pthread_mutex_lock( &queue->mtx ) ); | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
628 |
0 | 629 awaken: |
630 /* Check queue status */ | |
21
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
631 if (!CHECK_FIFO( queue )) { |
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
632 /* 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
|
633 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
|
634 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
|
635 return EPIPE; |
bef197f6826f
Backup before week-end, cnxctx and server in progress
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
14
diff
changeset
|
636 } |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
637 |
0 | 638 if (queue->count > 0) { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
639 /* 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
|
640 *item = mq_pop(queue); |
0 | 641 call_cb = test_l_cb(queue); |
642 } else { | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
643 /* We have to wait for a new item */ |
0 | 644 queue->thrs++ ; |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
645 pthread_cleanup_push( fifo_cleanup, queue); |
0 | 646 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
|
647 ret = pthread_cond_timedwait( &queue->cond_pull, &queue->mtx, abstime ); |
0 | 648 } 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
|
649 ret = pthread_cond_wait( &queue->cond_pull, &queue->mtx ); |
0 | 650 } |
651 pthread_cleanup_pop(0); | |
652 queue->thrs-- ; | |
653 if (ret == 0) | |
654 goto awaken; /* test for spurious wake-ups */ | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
655 |
1059
a1d6e1980132
Fix fifo_tget to propagate any error returned by the posix call
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
656 /* otherwise (ETIMEDOUT / other error) just continue */ |
0 | 657 } |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
658 |
0 | 659 /* Unlock */ |
660 CHECK_POSIX( pthread_mutex_unlock( &queue->mtx ) ); | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
661 |
0 | 662 /* Call low watermark callback as needed */ |
663 if (call_cb) | |
664 (*queue->l_cb)(queue, &queue->data); | |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
665 |
0 | 666 /* Done */ |
1059
a1d6e1980132
Fix fifo_tget to propagate any error returned by the posix call
Sebastien Decugis <sdecugis@freediameter.net>
parents:
974
diff
changeset
|
667 return ret; |
0 | 668 } |
669 | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
670 /* 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
|
671 int fd_fifo_get_int ( struct fifo * queue, void ** item ) |
0 | 672 { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
673 TRACE_ENTRY( "%p %p", queue, item ); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
674 return fifo_tget(queue, item, 0, NULL); |
0 | 675 } |
676 | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
677 /* 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
|
678 int fd_fifo_timedget_int ( struct fifo * queue, void ** item, const struct timespec *abstime ) |
0 | 679 { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
680 TRACE_ENTRY( "%p %p %p", queue, item, abstime ); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
681 return fifo_tget(queue, item, 1, abstime); |
0 | 682 } |
683 | |
1186
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
684 /* Test if data is available in the queue, without pulling it */ |
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
685 int fd_fifo_select ( struct fifo * queue, const struct timespec *abstime ) |
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
686 { |
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
687 int ret = 0; |
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
688 TRACE_ENTRY( "%p %p", queue, abstime ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
689 |
1186
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
690 CHECK_PARAMS_DO( CHECK_FIFO( queue ), return -EINVAL ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
691 |
1186
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
692 /* lock the queue */ |
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
693 CHECK_POSIX_DO( pthread_mutex_lock( &queue->mtx ), return -__ret__ ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
694 |
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
695 awaken: |
1186
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
696 ret = (queue->count > 0 ) ? queue->count : 0; |
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
697 if ((ret == 0) && (abstime != NULL)) { |
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
698 /* We have to wait for a new item */ |
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
699 queue->thrs++ ; |
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
700 pthread_cleanup_push( fifo_cleanup, queue); |
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
701 ret = pthread_cond_timedwait( &queue->cond_pull, &queue->mtx, abstime ); |
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
702 pthread_cleanup_pop(0); |
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
703 queue->thrs-- ; |
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
704 if (ret == 0) |
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
705 goto awaken; /* test for spurious wake-ups */ |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
706 |
1186
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
707 if (ret == ETIMEDOUT) |
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
708 ret = 0; |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
709 else |
1186
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
710 ret = -ret; |
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
711 } |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
712 |
1186
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
713 /* Unlock */ |
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
714 CHECK_POSIX_DO( pthread_mutex_unlock( &queue->mtx ), return -__ret__ ); |
1377
ce257e43085d
fd_fifo_del: check if queue is already (being) destroyed and return success in that case.
Thomas Klausner <tk@giga.or.at>
parents:
1188
diff
changeset
|
715 |
1186
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
716 return ret; |
56c36d1007b4
Further preparation of the DTLS integration. Some cleanups in the GNUTLS handling.
Sebastien Decugis <sdecugis@freediameter.net>
parents:
1127
diff
changeset
|
717 } |