- send_queue written, but not tested
[strongswan.git] / Source / charon / send_queue.c
1 /**
2 * @file send_queue.c
3 *
4 * @brief Send-Queue based on linked_list_t
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <pthread.h>
24
25 #include "send_queue.h"
26 #include "linked_list.h"
27
28 /**
29 * @brief Private Variables and Functions of send_queue class
30 *
31 */
32 typedef struct private_send_queue_s private_send_queue_t;
33
34
35 struct private_send_queue_s {
36 /**
37 * Public part of the send_queue_t object
38 */
39 send_queue_t public;
40
41 /**
42 * The packets are stored in a linked list
43 */
44 linked_list_t *list;
45
46 /**
47 * access to linked_list is locked through this mutex
48 */
49 pthread_mutex_t mutex;
50
51 /**
52 * If the queue is empty a thread has to wait
53 * This condvar is used to wake up such a thread
54 */
55 pthread_cond_t condvar;
56 };
57
58
59 /**
60 * @brief implements function get_count of send_queue_t
61 */
62 static status_t get_count(private_send_queue_t *this, int *count)
63 {
64 pthread_mutex_lock(&(this->mutex));
65 this->list->get_count(this->list,count);
66 pthread_mutex_unlock(&(this->mutex));
67 return SUCCESS;
68 }
69
70 /**
71 * @brief implements function get of send_queue_t
72 */
73 static status_t get(private_send_queue_t *this, packet_t **packet)
74 {
75 int count;
76 int oldstate;
77 pthread_mutex_lock(&(this->mutex));
78 /* go to wait while no packets available */
79 this->list->get_count(this->list,&count);
80 while(count == 0)
81 {
82 /* add mutex unlock handler for cancellation, enable cancellation */
83 pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock, (void*)&(this->mutex));
84 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
85 pthread_cond_wait( &(this->condvar), &(this->mutex));
86
87 /* reset cancellation, remove mutex-unlock handler (without executing) */
88 pthread_setcancelstate(oldstate, NULL);
89 pthread_cleanup_pop(0);
90 this->list->get_count(this->list,&count);
91 }
92 this->list->remove_first(this->list,(void **) packet);
93 pthread_mutex_unlock(&(this->mutex));
94 return SUCCESS;
95 }
96
97 /**
98 * @brief implements function add of send_queue_t
99 */
100 static status_t add(private_send_queue_t *this, packet_t *packet)
101 {
102 pthread_mutex_lock(&(this->mutex));
103 this->list->insert_last(this->list,packet);
104 pthread_cond_signal( &(this->condvar));
105 pthread_mutex_unlock(&(this->mutex));
106 return SUCCESS;
107 }
108
109 /**
110 * @brief implements function destroy of send_queue_t
111 *
112 */
113 static status_t destroy (private_send_queue_t *this)
114 {
115 int count;
116 this->list->get_count(this->list,&count);
117
118 /* destroy all packets in list before destroying list */
119 while (count > 0)
120 {
121 packet_t *packet;
122 if (this->list->remove_first(this->list,(void *) &packet) != SUCCESS)
123 {
124 this->list->destroy(this->list);
125 break;
126 }
127 packet->destroy(packet);
128 this->list->get_count(this->list,&count);
129 }
130 this->list->destroy(this->list);
131
132 pthread_mutex_destroy(&(this->mutex));
133
134 pthread_cond_destroy(&(this->condvar));
135
136 pfree(this);
137 return SUCCESS;
138 }
139
140 /*
141 *
142 * Documented in header
143 */
144 send_queue_t *send_queue_create()
145 {
146 linked_list_t *linked_list = linked_list_create();
147 if (linked_list == NULL)
148 {
149 return NULL;
150 }
151
152 private_send_queue_t *this = alloc_thing(private_send_queue_t, "private_send_queue_t");
153 if (this == NULL)
154 {
155 linked_list->destroy(linked_list);
156 return NULL;
157 }
158
159 this->public.get_count = (status_t(*)(send_queue_t*, int*)) get_count;
160 this->public.get = (status_t(*)(send_queue_t*, packet_t**)) get;
161 this->public.add = (status_t(*)(send_queue_t*, packet_t*)) add;
162 this->public.destroy = (status_t(*)(send_queue_t*)) destroy;
163
164 this->list = linked_list;
165 pthread_mutex_init(&(this->mutex), NULL);
166 pthread_cond_init(&(this->condvar), NULL);
167
168 return (&this->public);
169 }