lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Papastefanos Serafeim */
|
| 2 | /* Ulopoihsh sunarthsewn ouras */
|
| 3 |
|
| 4 | #include <cwmp/cwmp.h>
|
| 5 | #include "cwmp/queue.h"
|
| 6 | #include "cwmp/log.h"
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 | void queue_add(queue_t *q, void * data, int type, int priority, void * arg1, void *arg2) {
|
| 11 | qnode_t *node;
|
| 12 |
|
| 13 | node = (qnode_t *)MALLOC(sizeof(qnode_t));
|
| 14 |
|
| 15 | if(node == NULL) {
|
| 16 | cwmp_log_error("malloc null");
|
| 17 | return ;
|
| 18 | }
|
| 19 |
|
| 20 | node->data = data;
|
| 21 | node->arg1 = arg1;
|
| 22 | node->arg2 = arg2;
|
| 23 | node->datatype = type;
|
| 24 | node->priority = priority;
|
| 25 | node->next = NULL;
|
| 26 |
|
| 27 | pthread_mutex_lock(&q->mutex);
|
| 28 |
|
| 29 | q->size += 1;
|
| 30 |
|
| 31 | if(q->first==NULL) {
|
| 32 | q->first = node;
|
| 33 | q->last = node;
|
| 34 | } else {
|
| 35 | qnode_t * first = q->first;
|
| 36 | if(priority >= first->priority)
|
| 37 | {
|
| 38 | node->next = first->next;
|
| 39 | q->first = node;
|
| 40 | }
|
| 41 | else
|
| 42 | {
|
| 43 | q->last->next = node;
|
| 44 | q->last = node;
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | pthread_mutex_unlock(& q->mutex);
|
| 49 |
|
| 50 | }
|
| 51 |
|
| 52 | void queue_push(queue_t *q, void * data, int type) {
|
| 53 | qnode_t *node;
|
| 54 |
|
| 55 | node = (qnode_t *)MALLOC(sizeof(qnode_t));
|
| 56 |
|
| 57 | if(node == NULL) {
|
| 58 | cwmp_log_error("malloc null");
|
| 59 | return ;
|
| 60 | }
|
| 61 |
|
| 62 | node->data = data;
|
| 63 | node->arg1 = NULL;
|
| 64 | node->arg2 = NULL;
|
| 65 | node->datatype = type;
|
| 66 | node->priority = QUEUE_PRIORITY_COMMON;
|
| 67 | node->next = NULL;
|
| 68 |
|
| 69 | pthread_mutex_lock(&q->mutex);
|
| 70 |
|
| 71 | q->size += 1;
|
| 72 |
|
| 73 | if(q->first==NULL) {
|
| 74 | q->first = node;
|
| 75 | q->last = node;
|
| 76 | } else {
|
| 77 | q->last->next = node;
|
| 78 | q->last = node;
|
| 79 |
|
| 80 | }
|
| 81 |
|
| 82 | pthread_mutex_unlock(& q->mutex);
|
| 83 |
|
| 84 | }
|
| 85 |
|
| 86 |
|
| 87 | void queue_view(queue_t *q) {
|
| 88 | qnode_t *p;
|
| 89 | p=q->first;
|
| 90 | if(p==NULL) {
|
| 91 | cwmp_log_debug("queue is empty.");
|
| 92 | return;
|
| 93 | } else {
|
| 94 | cwmp_log_debug("queue size = %d. ", q->size);
|
| 95 | while(p->next!=NULL) {
|
| 96 | cwmp_log_debug(" %s ", (char *)p->data);
|
| 97 | p=p->next;
|
| 98 | }
|
| 99 | cwmp_log_debug(" %s ", (char *)p->data);
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 |
|
| 104 | int queue_pop(queue_t *q, void ** data) {
|
| 105 | if(q->first == NULL) {
|
| 106 | cwmp_log_debug("queue is empty.");
|
| 107 | return -1;
|
| 108 | }
|
| 109 | qnode_t *p;
|
| 110 | int type ;
|
| 111 | pthread_mutex_lock(& q->mutex);
|
| 112 |
|
| 113 | void *c=q->first->data;
|
| 114 | p=q->first;
|
| 115 | type = p->datatype;
|
| 116 | q->first=q->first->next;
|
| 117 | if(q->first == NULL) q->last = NULL;
|
| 118 | free(p);
|
| 119 | q->size--;
|
| 120 | pthread_mutex_unlock(& q->mutex);
|
| 121 |
|
| 122 | *data = c;
|
| 123 | return type;
|
| 124 | }
|
| 125 |
|
| 126 |
|
| 127 | queue_t *queue_create(pool_t * pool) {
|
| 128 | queue_t *queue = MALLOC(sizeof(queue_t));//(queue_t *)pool_pcalloc(pool, sizeof(queue_t) );
|
| 129 | if(queue == NULL) return NULL;
|
| 130 | queue->first = NULL;
|
| 131 | queue->last = NULL;
|
| 132 | queue->size = 0;
|
| 133 |
|
| 134 |
|
| 135 | pthread_mutex_init(& queue->mutex ,NULL);
|
| 136 |
|
| 137 | return queue;
|
| 138 | }
|
| 139 |
|
| 140 | /* Elegxei an h oura einai adeia */
|
| 141 | int queue_is_empty(queue_t *q) {
|
| 142 | return (q->first == NULL);
|
| 143 | }
|
| 144 |
|
| 145 | void queue_free(pool_t * pool, queue_t *q) {
|
| 146 | pthread_mutex_lock(& q->mutex);
|
| 147 | qnode_t *p = q->first;
|
| 148 | while(p->next != NULL) {
|
| 149 | qnode_t *r = p;
|
| 150 | p=p->next;
|
| 151 | free(r);
|
| 152 | }
|
| 153 | pthread_mutex_unlock(& q->mutex);
|
| 154 | pool_pfree(pool, q);
|
| 155 | }
|