zoukankan      html  css  js  c++  java
  • C开发机顶盒实战代码之队列

    #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;
    }

  • 相关阅读:
    自动登录跳板机->开发机
    关于写代码的一下规范
    vscode 配置 GOPATH
    thinkphp6.0 nginx 配置
    vue-cli 3.x 构建项目,webpack没有了?
    Laravel6.0 使用 Jwt-auth 实现多用户接口认证
    怎么在 localhost 下访问多个 Laravel 项目,通过一个IP访问多个项目(不仅仅是改变端口哦)
    laravel 5.8 实现消息推送
    vs code 设置 保存自动格式化vue代码
    项目开发规范(编码规范、命名规范、安全规范、前端优化、源码提交规范、代码维护规范、产品发布规范)
  • 原文地址:https://www.cnblogs.com/fx2008/p/2174545.html
Copyright © 2011-2022 走看看