zoukankan      html  css  js  c++  java
  • 无锁链表

    #ifndef lkf_h
    #define lkf_h
    
    struct lkf_node {
        struct lkf_node* next;
    };
    
    struct lkf_list {
        struct lkf_node root;
        struct lkf_node** tail;
    };
    
    
    #define LKF_INIT(name) {.root = {NULL}, .tail = &(name.root.next)}
    #define LKF_LIST(name) 
    struct lkf_list name = LKF_INIT(name)
    
    #define INIT_LKF(name) 
    do { 
        name->root.next = NULL; 
        name->tail = &(name->root.next); 
    } while (0)
    
    static inline void lkf_node_put(struct lkf_list* list, struct lkf_node* node)
    {
        node->next = NULL;
        struct lkf_node** ptr = __sync_lock_test_and_set(&(list->tail), &(node->next));
        *ptr = node;
    }
    
    static inline struct lkf_node* lkf_node_get_one(struct lkf_list* list)
    {
        struct lkf_node* head = __sync_lock_test_and_set(&(list->root.next), NULL);
        if (head == NULL) {
            return NULL;
        }
    
        struct lkf_node* next = head->next;
        if (next != NULL) {
            list->root.next = next;
            head->next = head;
            return head;
        }
    
        int b = __sync_bool_compare_and_swap(&(list->tail), &(head->next), &(list->root.next));
        if (b) {
            head->next = head;
            return head;
        }
    
        list->root.next = head;
        return NULL;
    }
    
    static inline struct lkf_node* lkf_node_get(struct lkf_list* list)
    {
        struct lkf_node* ptr = __sync_lock_test_and_set(&(list->root.next), NULL);
        if (ptr == NULL) {
            return NULL;
        }
    
        struct lkf_node** last = __sync_lock_test_and_set(&(list->tail), &(list->root.next));
        *last = ptr;
        return *last;
    }
    
    static inline struct lkf_node* lkf_node_next(struct lkf_node* node)
    {
        struct lkf_node* ptr = node->next;
        if (ptr == NULL || ptr->next == NULL) {
            return NULL;
        }
    
        if (ptr == node) {
            return ptr;
        }
    
        node->next = ptr->next;
        ptr->next = NULL;
        return ptr;
    }
    
    #endif
    
  • 相关阅读:
    sql server 函数
    Idea+Maven+Jetty,代码修改后,重新部署,无法获取新内容~
    css优先级
    jquery选择器
    XML学习笔记一
    JVM参数调优实例解析
    JVM中的Stack和Heap--------堆与栈
    svn报错:“Previous operation has not finished; run 'cleanup' if it was interrupted“
    Spark中的CombineKey()详解
    Spark性能优化指南——高级篇
  • 原文地址:https://www.cnblogs.com/zylthinking/p/5975710.html
Copyright © 2011-2022 走看看