From 8bc57749db19362f49c3cf3a8fafc1f3e5552385 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?IOhannes=20m=20zm=C3=B6lnig?= Date: Fri, 20 Aug 2010 14:32:59 +0000 Subject: reindentated; add some comments svn path=/trunk/externals/iem/iemnet/; revision=13869 --- iemnet_data.c | 153 +++++++++++++++++++++++++++++++--------------------------- 1 file changed, 81 insertions(+), 72 deletions(-) (limited to 'iemnet_data.c') diff --git a/iemnet_data.c b/iemnet_data.c index 50dd97a..9969e9a 100644 --- a/iemnet_data.c +++ b/iemnet_data.c @@ -202,103 +202,112 @@ struct _iemnet_queue { }; +/* push a chunk into the queue + */ int queue_push( - t_iemnet_queue* const _this, - t_iemnet_chunk* const data - ) { + t_iemnet_queue* const _this, + t_iemnet_chunk* const data + ) { t_node* tail; t_node* n=NULL; - int size=-1; - if(_this) { - size=_this->size; - - if(NULL == data) return size; - //fprintf(stderr, "pushing %d bytes\n", data->size); - - n=(t_node*)getbytes(sizeof(t_node)); - - n->next = 0; - n->data = data; - pthread_mutex_lock(&_this->mtx); - if (! (tail = _this->tail)) { - _this->head = n; - } else { - tail->next = n; - } - _this->tail = n; + int size=-1; + if(_this) { + size=_this->size; + + if(NULL == data) return size; + //fprintf(stderr, "pushing %d bytes\n", data->size); - _this->size+=data->size; - size=_this->size; + n=(t_node*)getbytes(sizeof(t_node)); + + n->next = 0; + n->data = data; + pthread_mutex_lock(&_this->mtx); + if (! (tail = _this->tail)) { + _this->head = n; + } else { + tail->next = n; + } + _this->tail = n; + + _this->size+=data->size; + size=_this->size; - //fprintf(stderr, "pushed %d bytes\n", data->size); + //fprintf(stderr, "pushed %d bytes\n", data->size); - pthread_mutex_unlock(&_this->mtx); - pthread_cond_signal(&_this->cond); - } + pthread_mutex_unlock(&_this->mtx); + pthread_cond_signal(&_this->cond); + } return size; } + +/* push a chunk from the queue + * if the queue is empty, this will block until + * something has been pushed + * OR the queue is "done" (in which case NULL is returned) + */ t_iemnet_chunk* queue_pop_block( - t_iemnet_queue* const _this - ) { + t_iemnet_queue* const _this + ) { t_node* head=0; t_iemnet_chunk*data=0; - if(_this) { - pthread_mutex_lock(&_this->mtx); - while (! (head = _this->head)) { - if(_this->done) { - pthread_mutex_unlock(&_this->mtx); - return NULL; - } - else { - pthread_cond_wait(&_this->cond, &_this->mtx); + if(_this) { + pthread_mutex_lock(&_this->mtx); + + /* wait until there is something in the queue or the "done" flag is set */ + while (! (head = _this->head)) { + if(_this->done) { + pthread_mutex_unlock(&_this->mtx); + return NULL; + } else { + pthread_cond_wait(&_this->cond, &_this->mtx); + } } - } - if (! (_this->head = head->next)) { - _this->tail = 0; - } - if(head && head->data) { - _this->size-=head->data->size; - } + if (! (_this->head = head->next)) { + _this->tail = 0; + } + if(head && head->data) { + _this->size-=head->data->size; + } - pthread_mutex_unlock(&_this->mtx); - if(head) { - data=head->data; - freebytes(head, sizeof(t_node)); - head=NULL; + pthread_mutex_unlock(&_this->mtx); + if(head) { + data=head->data; + freebytes(head, sizeof(t_node)); + head=NULL; + } } - } return data; } t_iemnet_chunk* queue_pop_noblock( - t_iemnet_queue* const _this - ) { + t_iemnet_queue* const _this + ) { t_node* head=0; t_iemnet_chunk*data=0; - if(_this) { - pthread_mutex_lock(&_this->mtx); - if (! (head = _this->head)) { - // empty head - pthread_mutex_unlock(&_this->mtx); - return NULL; - } - if (! (_this->head = head->next)) { - _this->tail = 0; - } - if(head && head->data) { - _this->size-=head->data->size; - } + if(_this) { + pthread_mutex_lock(&_this->mtx); + if (! (head = _this->head)) { + // empty head + pthread_mutex_unlock(&_this->mtx); + return NULL; + } + if (! (_this->head = head->next)) { + _this->tail = 0; + } + if(head && head->data) { + _this->size-=head->data->size; + } - pthread_mutex_unlock(&_this->mtx); - if(head) { - data=head->data; - freebytes(head, sizeof(t_node)); - head=NULL; + pthread_mutex_unlock(&_this->mtx); + if(head) { + data=head->data; + freebytes(head, sizeof(t_node)); + head=NULL; + } } - } return data; } -- cgit v1.2.1