anjana_sreekumar@infosys.com | 991c206 | 2020-01-08 11:42:57 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2003-2018, Great Software Laboratory Pvt. Ltd. |
| 3 | * Copyright (c) 2017 Intel Corporation |
| 4 | * Copyright (c) 2019, Infosys Ltd. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <errno.h> |
| 23 | |
| 24 | #include "thread_pool.h" |
| 25 | #include "tpool_queue.h" |
| 26 | |
| 27 | /* allocates memory for node and initialize it */ |
| 28 | static struct node *createnode(void *data) |
| 29 | { |
| 30 | struct node *entry; |
| 31 | |
| 32 | entry = (struct node *)malloc(sizeof(struct node)); |
| 33 | if(entry == NULL) { |
| 34 | #ifdef DEBUG |
| 35 | log_msg(LOG_ERROR, "failed to allocate memory\n"); |
| 36 | #endif |
| 37 | return NULL; |
| 38 | } |
| 39 | |
| 40 | entry->data = data; |
| 41 | entry->next = NULL; |
| 42 | |
| 43 | return entry; |
| 44 | } |
| 45 | |
| 46 | /* push data to queue tail */ |
| 47 | int queue_push_tail(struct Queue *queue, void *data) |
| 48 | { |
| 49 | struct node *entry; |
| 50 | |
| 51 | if(queue == NULL) |
| 52 | return -1; |
| 53 | |
| 54 | entry = createnode(data); |
| 55 | if(entry == NULL) { |
| 56 | return -ENOMEM; |
| 57 | } |
| 58 | |
| 59 | /* For empty queue */ |
| 60 | if(queue->head == NULL) |
| 61 | queue->head = entry; |
| 62 | else |
| 63 | queue->tail->next = entry; |
| 64 | queue->tail = entry; |
| 65 | |
| 66 | /* atomic increment */ |
| 67 | __sync_fetch_and_add(&queue->length, 1); |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | /* removes head and return its data */ |
| 72 | void *queue_pop_head(struct Queue *queue) |
| 73 | { |
| 74 | void *data; |
| 75 | struct node *entry; |
| 76 | |
| 77 | if(queue == NULL || queue->length == 0) |
| 78 | return NULL; |
| 79 | |
| 80 | if(queue->head == NULL) { |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | entry = queue->head; |
| 85 | queue->head = queue->head->next; |
| 86 | data = entry->data; |
| 87 | /* atomic decrement */ |
| 88 | __sync_fetch_and_sub(&queue->length, 1); |
| 89 | free(entry); |
| 90 | |
| 91 | return data; |
| 92 | } |
| 93 | |
| 94 | int queue_get_length(struct Queue *queue) |
| 95 | { |
| 96 | if (queue == NULL) |
| 97 | return 0; |
| 98 | |
| 99 | return queue->length; |
| 100 | } |
| 101 | |
| 102 | struct Queue *queue_new() |
| 103 | { |
| 104 | struct Queue *queue; |
| 105 | |
| 106 | queue = (struct Queue *)malloc(sizeof(struct Queue)); |
| 107 | if(queue == NULL) { |
| 108 | #ifdef DEBUG |
| 109 | log_msg(LOG_ERROR, "failed to allocate memory\n"); |
| 110 | #endif |
| 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | queue->length = 0; |
| 115 | queue->head = NULL; |
| 116 | queue->tail = NULL; |
| 117 | |
| 118 | return queue; |
| 119 | } |
| 120 | |
| 121 | void queue_destroy(struct Queue *queue, QueueDataFreeFunc function) |
| 122 | { |
| 123 | struct node *tmp; |
| 124 | |
| 125 | if(queue == NULL || queue->length == 0) |
| 126 | { |
| 127 | free(queue); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | if(queue->head == NULL) |
| 132 | free(queue); |
| 133 | else { |
| 134 | tmp = queue->head; |
| 135 | while(queue->head != NULL) { |
| 136 | tmp = queue->head->next; |
| 137 | if (function != NULL) |
| 138 | function(queue->head->data); |
| 139 | free(queue->head); |
| 140 | queue->head=tmp; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | } |
| 145 | |
| 146 | |