#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>
#include "queue.h"
static resend_queue* head;
static resend_queue* now;
static resend_queue* tail;
static resend_queue* temp;
static resend_queue* del;
static int queue_num;
static pthread_mutex_t lock_mute;
bool queue_init()
{
now = tail = head = NULL;
queue_num = 0;
pthread_mutex_init(&lock_mute, NULL);
}
bool queue_add(long data_count)
{
pthread_mutex_lock(&lock_mute);
if( head == NULL )
{
head = (resend_queue*)malloc(sizeof(resend_queue));
head->next = NULL;
head->data_count = data_count;
now = tail = head;
queue_num = 1;
//printf("queue-- begin add queue_num is %d, data_count is %d\n", queue_num, data_count);
pthread_mutex_unlock(&lock_mute);
return true;
}
if( tail == NULL )
{
//printf("tail is NULL\n");
}
//tail->next = (resend_queue*)malloc(sizeof(resend_queue));
//tail->next = (char*)malloc(sizeof(resend_queue));
tail->next = (pqueue)malloc(sizeof(resend_queue));//yuan
tail->next->data_count = data_count;
tail->next->next = NULL;
tail = tail->next;
queue_num++;
//printf("queue-- add queue_num is %d, data_count is %d\n", queue_num, data_count);
pthread_mutex_unlock(&lock_mute);
return true;
}
bool queue_delete(long data_count)
{
pthread_mutex_lock(&lock_mute);
if(head == NULL || queue_num == 0)
{
pthread_mutex_unlock(&lock_mute);
return false;
}
if(head->data_count == data_count)
{
del = head;
if( queue_num > 1 )
{
if( head->next == NULL )
{
pthread_mutex_unlock(&lock_mute);
queue_clear();
return false;
}
if( now == head )
{
now = head->next;
}
head = head->next;
queue_num--;
free(del);
del = NULL;
}
else if( queue_num == 1 )
{
free(del);
del = NULL;
now = tail = head = NULL;
queue_num = 0;
//printf("queue-- last one delete remain %d\n", queue_num);
pthread_mutex_unlock(&lock_mute);
return true;
}
//printf("queue-- delete1 queue_num is %d\n", queue_num);
pthread_mutex_unlock(&lock_mute);
return true;
}
temp = head;
while(1)
{
if(temp->next == NULL)
{
//printf("queue-- delete anything!\n");
pthread_mutex_unlock(&lock_mute);
return false;
}
if(temp->next->data_count == data_count)
{
del = temp->next;
if( temp->next->next != NULL )
{
temp->next = temp->next->next;
}
else
{
temp->next = NULL;
tail = temp;
}
if( del == now )
{
if( head == NULL )
{
now = tail = NULL;
}
else
{
now = head;
}
}
free(del);
del = NULL;
queue_num--;
//printf("queue-- delete2 data_count is %d remain %d\n", data_count, queue_num );
pthread_mutex_unlock(&lock_mute);
return true;
}
temp = temp->next;
}
pthread_mutex_unlock(&lock_mute);
return false;
}
bool queue_if_in(long data_count)
{
pthread_mutex_lock(&lock_mute);
if(head == NULL || queue_num == 0)
{
pthread_mutex_unlock(&lock_mute);
return false;
}
temp = head;
while(1)
{
if(temp->data_count == data_count)
{
pthread_mutex_unlock(&lock_mute);
//printf("queue-- quit detect if in queue true\n");
return true;
}
if(temp->next == NULL)
{
pthread_mutex_unlock(&lock_mute);
//printf("queue-- quit detect if in queue false\n");
return false;
}
temp = temp->next;
}
pthread_mutex_unlock(&lock_mute);
return false;
}
int queue_total()
{
return queue_num;
}
bool queue_if_tail()
{
pthread_mutex_lock(&lock_mute);
if( now == tail && tail != NULL )
{
pthread_mutex_unlock(&lock_mute);
return true;
}
pthread_mutex_unlock(&lock_mute);
return false;
}
long queue_get_value()
{
pthread_mutex_lock(&lock_mute);
if( now != NULL )
{
pthread_mutex_unlock(&lock_mute);
return now->data_count;
}
pthread_mutex_unlock(&lock_mute);
return -1;
}
bool queue_get_head()
{
pthread_mutex_lock(&lock_mute);
if( head != NULL )
{
now = head;
pthread_mutex_unlock(&lock_mute);
return true;
}
pthread_mutex_unlock(&lock_mute);
return false;
}
bool queue_get_next()
{
pthread_mutex_lock(&lock_mute);
if((now != NULL) && (now->next != NULL))
{
now = now->next;
pthread_mutex_unlock(&lock_mute);
return true;
}
pthread_mutex_unlock(&lock_mute);
return false;
}
bool queue_clear()
{
pthread_mutex_lock(&lock_mute);
if( head == NULL )
{
pthread_mutex_unlock(&lock_mute);
return false;
}
now = temp = head;
if( now->next == NULL )
{
free(head);
now = tail = head = NULL;
queue_num = 0;
pthread_mutex_unlock(&lock_mute);
return true;
}
while(now->next != NULL)
{
now = now->next;
if( now->next != NULL )
{
temp = now->next;
head->next = temp;
free(now);
now = temp;
}
else
{
free(now);
free(head);
}
}
now = tail = head = NULL;
queue_num = 0;
pthread_mutex_unlock(&lock_mute);
return true;
}
bool queue_release()
{
pthread_mutex_destroy(lock_mute);
return true;
}