4 * @brief Implementation of send_queue_t.
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
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>.
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
25 #include "send_queue.h"
27 #include <utils/allocator.h>
28 #include <utils/linked_list.h>
31 typedef struct private_send_queue_t private_send_queue_t
;
34 * @brief Private Variables and Functions of send_queue class
37 struct private_send_queue_t
{
39 * Public part of the send_queue_t object
44 * The packets are stored in a linked list
49 * access to linked_list is locked through this mutex
51 pthread_mutex_t mutex
;
54 * If the queue is empty a thread has to wait
55 * This condvar is used to wake up such a thread
57 pthread_cond_t condvar
;
62 * implements send_queue_t.get_count
64 static int get_count(private_send_queue_t
*this)
67 pthread_mutex_lock(&(this->mutex
));
68 count
= this->list
->get_count(this->list
);
69 pthread_mutex_unlock(&(this->mutex
));
74 * implements send_queue_t.get
76 static packet_t
*get(private_send_queue_t
*this)
80 pthread_mutex_lock(&(this->mutex
));
81 /* go to wait while no packets available */
83 while(this->list
->get_count(this->list
) == 0)
85 /* add mutex unlock handler for cancellation, enable cancellation */
86 pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock
, (void*)&(this->mutex
));
87 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
88 pthread_cond_wait( &(this->condvar
), &(this->mutex
));
90 /* reset cancellation, remove mutex-unlock handler (without executing) */
91 pthread_setcancelstate(oldstate
, NULL
);
92 pthread_cleanup_pop(0);
94 this->list
->remove_first(this->list
,(void **)&packet
);
95 pthread_mutex_unlock(&(this->mutex
));
100 * implements send_queue_t.add
102 static void add(private_send_queue_t
*this, packet_t
*packet
)
104 pthread_mutex_lock(&(this->mutex
));
105 this->list
->insert_last(this->list
,packet
);
106 pthread_cond_signal( &(this->condvar
));
107 pthread_mutex_unlock(&(this->mutex
));
111 * implements send_queue_t.destroy
113 static void destroy (private_send_queue_t
*this)
116 /* destroy all packets in list before destroying list */
117 while (this->list
->get_count(this->list
) > 0)
120 if (this->list
->remove_first(this->list
,(void *) &packet
) != SUCCESS
)
122 this->list
->destroy(this->list
);
125 packet
->destroy(packet
);
127 this->list
->destroy(this->list
);
129 pthread_mutex_destroy(&(this->mutex
));
131 pthread_cond_destroy(&(this->condvar
));
133 allocator_free(this);
138 * Documented in header
140 send_queue_t
*send_queue_create()
142 private_send_queue_t
*this = allocator_alloc_thing(private_send_queue_t
);
144 this->public.get_count
= (int(*)(send_queue_t
*)) get_count
;
145 this->public.get
= (packet_t
*(*)(send_queue_t
*)) get
;
146 this->public.add
= (void(*)(send_queue_t
*, packet_t
*)) add
;
147 this->public.destroy
= (void(*)(send_queue_t
*)) destroy
;
149 this->list
= linked_list_create();
150 pthread_mutex_init(&(this->mutex
), NULL
);
151 pthread_cond_init(&(this->condvar
), NULL
);
153 return (&this->public);