zoukankan      html  css  js  c++  java
  • dpdk 自旋锁

    #ifndef __SPIN_LOCK__
    #define __SPIN_LOCK__
    
    typedef struct {
        volatile int locked; /**< lock status 0 = unlocked, 1 = locked */
    } rte_spinlock_t;
    
    void rte_pause(){
            ;
    }
    
    //spinlock的初始化,lock=0,标识无锁状态
    static inline void
    rte_spinlock_init(rte_spinlock_t *sl)
    {
        sl->locked = 0;
    }
    
    //加锁,也就是locked置1
    static inline void
    rte_spinlock_lock(rte_spinlock_t *sl)
    {
        while (__sync_lock_test_and_set(&sl->locked, 1))
            while(sl->locked)
                rte_pause();
    }
    
    //解锁
    static inline void
    rte_spinlock_unlock (rte_spinlock_t *sl)
    {
        __sync_lock_release(&sl->locked);
    }
    
    // 尝试加锁,如果当前状态是无锁,func返回1,标识加锁成功,失败func返回0
    static inline int
    rte_spinlock_trylock (rte_spinlock_t *sl)
    {
        return (__sync_lock_test_and_set(&sl->locked,1) == 0);
    }
    
    //返回当前锁的status
    static inline int rte_spinlock_is_locked (rte_spinlock_t *sl)
    {
        return sl->locked;
    }
    
    #endif
    #include "spinlock.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    
    static int num =0; 
    rte_spinlock_t retlock;
    
    void *testfunc(void *arg)
    {
        int i=0,t;
        for(i=0;i<20000;i++)
        {   
            rte_spinlock_lock(&retlock);
            num++;
            rte_spinlock_unlock(&retlock);
        }
    }
    
    int main()
    {
            rte_spinlock_init(&retlock);
    
        pthread_t thread[20];
        int i;
        for(i=0;i<20;i++)
        {
            pthread_create(&thread[i],NULL,testfunc,NULL);
        }
        for(i=0;i<20;i++)
        {
            pthread_join(thread[i],NULL);
        }
        printf("%d
    ",num);
     }
    root@ubuntu:~/dpdk-function# g++ -pthread  -o spinlock   spinlock_test.c
    root@ubuntu:~/dpdk-function# ./spinlock 
    400000
    root@ubuntu:~/dpdk-function# 

    定义静态函数:在函数返回类型前加上static关键字,函数即被定义为静态函数,其特点如下:

    a.静态函数只能在本源文件中使用

    b.在文件作用域中声明的inline函数默认为static类型

    /* file1.c */
    #include <stdio.h>
    
    static void fun(void)
    {
        printf("hello from fun.
    ");
    }
    
    int main(void)
    {
        fun();
        fun1();
    
        return 0;
    }
    
    /* file2.c */
    #include <stdio.h>
    
    static void fun1(void)
    {
        printf("hello from static fun1.
    ");
    }
    /tmp/cc2VMzGR.o:在函数‘main’中:
    static_fun.c:(.text+0x20):对‘fun1’未定义的引用
    collect2: error: ld returned 1 exit status

    总结:用static定义的全局和局部静态变量的区别是,全局的静态变量的作用域和可见域都是从文件的定义开始到整个文件结束;而局部的静态变量可见域是从文件的定义开始到整个文件结束,作用域是从该语句块的定义开始到该语句块结束。

  • 相关阅读:
    hashCode()相同,equals()也一定为true吗?
    什么是装箱?什么是拆箱?装箱和拆箱的执行过程?常见问题?
    LOJ114_k 大异或和_线性基
    BZOJ_4459_[Jsoi2013]丢番图_数学+分解质因数
    BZOJ_4184_shallot_线段树按时间分治维护线性基
    BZOJ_2844_albus就是要第一个出场_线性基
    BZOJ_3105_[cqoi2013]新Nim游戏_线性基+博弈论
    BZOJ_1195_[HNOI2006]最短母串_AC自动机+BFS+分层图
    BZOJ_3881_[Coci2015]Divljak_AC自动机+dfs序+树状数组
    BZOJ_1532_[POI2005]Kos-Dicing_二分+网络流
  • 原文地址:https://www.cnblogs.com/dream397/p/14717266.html
Copyright © 2011-2022 走看看