Added a simple blocking queue around linked_list_t
This commit is contained in:
@@ -25,8 +25,8 @@ processing/jobs/callback_job.c processing/processor.c processing/scheduler.c \
|
|||||||
selectors/traffic_selector.c threading/thread.c threading/thread_value.c \
|
selectors/traffic_selector.c threading/thread.c threading/thread_value.c \
|
||||||
threading/mutex.c threading/semaphore.c threading/rwlock.c threading/spinlock.c \
|
threading/mutex.c threading/semaphore.c threading/rwlock.c threading/spinlock.c \
|
||||||
utils.c utils/host.c utils/packet.c utils/identification.c utils/lexparser.c \
|
utils.c utils/host.c utils/packet.c utils/identification.c utils/lexparser.c \
|
||||||
utils/linked_list.c utils/hashtable.c utils/enumerator.c utils/optionsfrom.c \
|
utils/linked_list.c utils/blocking_queue.c utils/hashtable.c utils/enumerator.c \
|
||||||
utils/capabilities.c utils/backtrace.c
|
utils/optionsfrom.c utils/capabilities.c utils/backtrace.c
|
||||||
|
|
||||||
# adding the plugin source files
|
# adding the plugin source files
|
||||||
|
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ processing/jobs/callback_job.c processing/processor.c processing/scheduler.c \
|
|||||||
selectors/traffic_selector.c threading/thread.c threading/thread_value.c \
|
selectors/traffic_selector.c threading/thread.c threading/thread_value.c \
|
||||||
threading/mutex.c threading/semaphore.c threading/rwlock.c threading/spinlock.c \
|
threading/mutex.c threading/semaphore.c threading/rwlock.c threading/spinlock.c \
|
||||||
utils.c utils/host.c utils/packet.c utils/identification.c utils/lexparser.c \
|
utils.c utils/host.c utils/packet.c utils/identification.c utils/lexparser.c \
|
||||||
utils/linked_list.c utils/hashtable.c utils/enumerator.c utils/optionsfrom.c \
|
utils/linked_list.c utils/blocking_queue.c utils/hashtable.c utils/enumerator.c \
|
||||||
utils/capabilities.c utils/backtrace.c
|
utils/optionsfrom.c utils/capabilities.c utils/backtrace.c
|
||||||
|
|
||||||
if USE_DEV_HEADERS
|
if USE_DEV_HEADERS
|
||||||
strongswan_includedir = ${dev_headers}
|
strongswan_includedir = ${dev_headers}
|
||||||
@@ -58,8 +58,8 @@ selectors/traffic_selector.h threading/thread.h threading/thread_value.h \
|
|||||||
threading/mutex.h threading/condvar.h threading/spinlock.h threading/semaphore.h \
|
threading/mutex.h threading/condvar.h threading/spinlock.h threading/semaphore.h \
|
||||||
threading/rwlock.h threading/lock_profiler.h utils.h utils/host.h \
|
threading/rwlock.h threading/lock_profiler.h utils.h utils/host.h \
|
||||||
utils/packet.h utils/identification.h utils/lexparser.h utils/linked_list.h \
|
utils/packet.h utils/identification.h utils/lexparser.h utils/linked_list.h \
|
||||||
utils/hashtable.h utils/enumerator.h utils/optionsfrom.h utils/capabilities.h \
|
utils/blocking_queue.h utils/hashtable.h utils/enumerator.h utils/optionsfrom.h \
|
||||||
utils/backtrace.h
|
utils/capabilities.h utils/backtrace.h
|
||||||
endif
|
endif
|
||||||
|
|
||||||
library.lo : $(top_builddir)/config.status
|
library.lo : $(top_builddir)/config.status
|
||||||
|
|||||||
@@ -0,0 +1,129 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2012 Tobias Brunner
|
||||||
|
* Copyright (C) 2012 Giuliano Grassi
|
||||||
|
* Copyright (C) 2012 Ralf Sager
|
||||||
|
* Hochschule fuer Technik Rapperswil
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation; either version 2 of the License, or (at your
|
||||||
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "blocking_queue.h"
|
||||||
|
|
||||||
|
#include <threading/mutex.h>
|
||||||
|
#include <threading/thread.h>
|
||||||
|
#include <threading/condvar.h>
|
||||||
|
#include <utils/linked_list.h>
|
||||||
|
|
||||||
|
typedef struct private_blocking_queue_t private_blocking_queue_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Private data of a blocking_queue_t object.
|
||||||
|
*/
|
||||||
|
struct private_blocking_queue_t {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Public part
|
||||||
|
*/
|
||||||
|
blocking_queue_t public;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Linked list containing all items in the queue
|
||||||
|
*/
|
||||||
|
linked_list_t *list;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mutex used to synchronize access to the queue
|
||||||
|
*/
|
||||||
|
mutex_t *mutex;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Condvar used to wait for items
|
||||||
|
*/
|
||||||
|
condvar_t *condvar;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
METHOD(blocking_queue_t, enqueue, void,
|
||||||
|
private_blocking_queue_t *this, void *item)
|
||||||
|
{
|
||||||
|
this->mutex->lock(this->mutex);
|
||||||
|
this->list->insert_first(this->list, item);
|
||||||
|
this->condvar->signal(this->condvar);
|
||||||
|
this->mutex->unlock(this->mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
METHOD(blocking_queue_t, dequeue, void*,
|
||||||
|
private_blocking_queue_t *this)
|
||||||
|
{
|
||||||
|
bool oldstate;
|
||||||
|
void *item;
|
||||||
|
|
||||||
|
|
||||||
|
this->mutex->lock(this->mutex);
|
||||||
|
thread_cleanup_push((thread_cleanup_t)this->mutex->unlock, this->mutex);
|
||||||
|
/* ensure that a canceled thread does not dequeue any items */
|
||||||
|
thread_cancellation_point();
|
||||||
|
while (this->list->remove_last(this->list, &item) != SUCCESS)
|
||||||
|
{
|
||||||
|
oldstate = thread_cancelability(TRUE);
|
||||||
|
this->condvar->wait(this->condvar, this->mutex);
|
||||||
|
thread_cancelability(oldstate);
|
||||||
|
}
|
||||||
|
thread_cleanup_pop(TRUE);
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
METHOD(blocking_queue_t, destroy, void,
|
||||||
|
private_blocking_queue_t *this)
|
||||||
|
{
|
||||||
|
this->list->destroy(this->list);
|
||||||
|
this->condvar->destroy(this->condvar);
|
||||||
|
this->mutex->destroy(this->mutex);
|
||||||
|
free(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
METHOD(blocking_queue_t, destroy_offset, void,
|
||||||
|
private_blocking_queue_t *this, size_t offset)
|
||||||
|
{
|
||||||
|
this->list->invoke_offset(this->list, offset);
|
||||||
|
destroy(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
METHOD(blocking_queue_t, destroy_function, void,
|
||||||
|
private_blocking_queue_t *this, void (*fn)(void*))
|
||||||
|
{
|
||||||
|
this->list->invoke_function(this->list, (linked_list_invoke_t)fn);
|
||||||
|
destroy(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Described in header.
|
||||||
|
*/
|
||||||
|
blocking_queue_t *blocking_queue_create()
|
||||||
|
{
|
||||||
|
private_blocking_queue_t *this;
|
||||||
|
|
||||||
|
INIT(this,
|
||||||
|
.public = {
|
||||||
|
.enqueue = _enqueue,
|
||||||
|
.dequeue = _dequeue,
|
||||||
|
.destroy = _destroy,
|
||||||
|
.destroy_offset = _destroy_offset,
|
||||||
|
.destroy_function = _destroy_function,
|
||||||
|
},
|
||||||
|
.list = linked_list_create(),
|
||||||
|
.mutex = mutex_create(MUTEX_TYPE_DEFAULT),
|
||||||
|
.condvar = condvar_create(CONDVAR_TYPE_DEFAULT),
|
||||||
|
);
|
||||||
|
|
||||||
|
return &this->public;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,97 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2012 Tobias Brunner
|
||||||
|
* Copyright (C) 2012 Giuliano Grassi
|
||||||
|
* Copyright (C) 2012 Ralf Sager
|
||||||
|
* Hochschule fuer Technik Rapperswil
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation; either version 2 of the License, or (at your
|
||||||
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @defgroup blocking_queue blocking_queue
|
||||||
|
* @{ @ingroup utils
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BLOCKING_QUEUE_H_
|
||||||
|
#define BLOCKING_QUEUE_H_
|
||||||
|
|
||||||
|
typedef struct blocking_queue_t blocking_queue_t;
|
||||||
|
|
||||||
|
#include <library.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class implementing a synchronized blocking queue based on linked_list_t
|
||||||
|
*/
|
||||||
|
struct blocking_queue_t {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inserts a new item at the tail of the queue
|
||||||
|
*
|
||||||
|
* @param item item to insert in queue
|
||||||
|
*/
|
||||||
|
void (*enqueue)(blocking_queue_t *this, void *item);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the first item in the queue and returns its value.
|
||||||
|
* If the queue is empty, this call blocks until a new item is inserted.
|
||||||
|
*
|
||||||
|
* @note This is a thread cancellation point
|
||||||
|
*
|
||||||
|
* @return removed item
|
||||||
|
*/
|
||||||
|
void *(*dequeue)(blocking_queue_t *this);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destroys a blocking_queue_t object.
|
||||||
|
*
|
||||||
|
* @note No thread must wait in dequeue() when this function is called
|
||||||
|
*/
|
||||||
|
void (*destroy)(blocking_queue_t *this);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destroys a queue and its objects using the given destructor.
|
||||||
|
*
|
||||||
|
* If a queue and the contained objects should be destroyed, use
|
||||||
|
* destroy_offset. The supplied offset specifies the destructor to
|
||||||
|
* call on each object. The offset may be calculated using the offsetof
|
||||||
|
* macro, e.g.: queue->destroy_offset(queue, offsetof(object_t, destroy));
|
||||||
|
*
|
||||||
|
* @note No thread must wait in dequeue() when this function is called
|
||||||
|
*
|
||||||
|
* @param offset offset of the objects destructor
|
||||||
|
*/
|
||||||
|
void (*destroy_offset)(blocking_queue_t *this, size_t offset);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destroys a queue and its objects using a cleanup function.
|
||||||
|
*
|
||||||
|
* If a queue and its contents should get destroyed using a specific
|
||||||
|
* cleanup function, use destroy_function. This is useful when the
|
||||||
|
* list contains malloc()-ed blocks which should get freed,
|
||||||
|
* e.g.: queue->destroy_function(queue, free);
|
||||||
|
*
|
||||||
|
* @note No thread must wait in dequeue() when this function is called
|
||||||
|
*
|
||||||
|
* @param function function to call on each object
|
||||||
|
*/
|
||||||
|
void (*destroy_function)(blocking_queue_t *this, void (*)(void*));
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an empty queue object.
|
||||||
|
*
|
||||||
|
* @return blocking_queue_t object.
|
||||||
|
*/
|
||||||
|
blocking_queue_t *blocking_queue_create();
|
||||||
|
|
||||||
|
#endif /** BLOCKING_QUEUE_H_ @}*/
|
||||||
|
|
||||||
Reference in New Issue
Block a user