changed enum and structs names to _t
[strongswan.git] / Source / charon / queues / 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
27 #include <utils/allocator.h>
28 #include <utils/linked_list.h>
29
30
31 typedef struct private_send_queue_t private_send_queue_t;
32
33 /**
34 * @brief Private Variables and Functions of send_queue class
35 *
36 */
37 struct private_send_queue_t {
38 /**
39 * Public part of the send_queue_t object
40 */
41 send_queue_t public;
42
43 /**
44 * The packets are stored in a linked list
45 */
46 linked_list_t *list;
47
48 /**
49 * access to linked_list is locked through this mutex
50 */
51 pthread_mutex_t mutex;
52
53 /**
54 * If the queue is empty a thread has to wait
55 * This condvar is used to wake up such a thread
56 */
57 pthread_cond_t condvar;
58 };
59
60
61 /**
62 * @brief implements function get_count of send_queue_t
63 */
64 static int get_count(private_send_queue_t *this)
65 {
66 int count;
67 pthread_mutex_lock(&(this->mutex));
68 count = this->list->get_count(this->list);
69 pthread_mutex_unlock(&(this->mutex));
70 return count;
71 }
72
73 /**
74 * @brief implements function get of send_queue_t
75 */
76 static status_t get(private_send_queue_t *this, packet_t **packet)
77 {
78 int oldstate;
79 pthread_mutex_lock(&(this->mutex));
80 /* go to wait while no packets available */
81
82 while(this->list->get_count(this->list) == 0)
83 {
84 /* add mutex unlock handler for cancellation, enable cancellation */
85 pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock, (void*)&(this->mutex));
86 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
87 pthread_cond_wait( &(this->condvar), &(this->mutex));
88
89 /* reset cancellation, remove mutex-unlock handler (without executing) */
90 pthread_setcancelstate(oldstate, NULL);
91 pthread_cleanup_pop(0);
92 }
93 this->list->remove_first(this->list,(void **) packet);
94 pthread_mutex_unlock(&(this->mutex));
95 return SUCCESS;
96 }
97
98 /**
99 * @brief implements function add of send_queue_t
100 */
101 static status_t add(private_send_queue_t *this, packet_t *packet)
102 {
103 pthread_mutex_lock(&(this->mutex));
104 this->list->insert_last(this->list,packet);
105 pthread_cond_signal( &(this->condvar));
106 pthread_mutex_unlock(&(this->mutex));
107 return SUCCESS;
108 }
109
110 /**
111 * @brief implements function destroy of send_queue_t
112 *
113 */
114 static status_t destroy (private_send_queue_t *this)
115 {
116
117 /* destroy all packets in list before destroying list */
118 while (this->list->get_count(this->list) > 0)
119 {
120 packet_t *packet;
121 if (this->list->remove_first(this->list,(void *) &packet) != SUCCESS)
122 {
123 this->list->destroy(this->list);
124 break;
125 }
126 packet->destroy(packet);
127 }
128 this->list->destroy(this->list);
129
130 pthread_mutex_destroy(&(this->mutex));
131
132 pthread_cond_destroy(&(this->condvar));
133
134 allocator_free(this);
135 return SUCCESS;
136 }
137
138 /*
139 *
140 * Documented in header
141 */
142 send_queue_t *send_queue_create()
143 {
144 linked_list_t *linked_list = linked_list_create();
145 if (linked_list == NULL)
146 {
147 return NULL;
148 }
149
150 private_send_queue_t *this = allocator_alloc_thing(private_send_queue_t);
151 if (this == NULL)
152 {
153 linked_list->destroy(linked_list);
154 return NULL;
155 }
156
157 this->public.get_count = (int(*)(send_queue_t*)) get_count;
158 this->public.get = (status_t(*)(send_queue_t*, packet_t**)) get;
159 this->public.add = (status_t(*)(send_queue_t*, packet_t*)) add;
160 this->public.destroy = (status_t(*)(send_queue_t*)) destroy;
161
162 this->list = linked_list;
163 pthread_mutex_init(&(this->mutex), NULL);
164 pthread_cond_init(&(this->condvar), NULL);
165
166 return (&this->public);
167 }