aboutsummaryrefslogtreecommitdiff
path: root/iemnet_data.c
diff options
context:
space:
mode:
authorIOhannes m zmölnig <zmoelnig@users.sourceforge.net>2010-08-20 14:32:59 +0000
committerIOhannes m zmölnig <zmoelnig@users.sourceforge.net>2010-08-20 14:32:59 +0000
commit8bc57749db19362f49c3cf3a8fafc1f3e5552385 (patch)
treecce6289e9d375d59bfc5c3ef7a0b5de9d735b88e /iemnet_data.c
parent2e68fe6ba7fb250f692c5200676d7452338d652e (diff)
reindentated; add some comments
svn path=/trunk/externals/iem/iemnet/; revision=13869
Diffstat (limited to 'iemnet_data.c')
-rw-r--r--iemnet_data.c153
1 files changed, 81 insertions, 72 deletions
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;
}