wait queue 5

来源:互联网 发布:mac os x 重装 超慢 编辑:程序博客网 时间:2024/06/05 17:36
在使用bit_waitqueue的时候一般通过下面的函数得到wait_queue_head_t
wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
其中bit_wait_table是一个cacheline 对其的静态数组
static wait_queue_head_t bit_wait_table[WAIT_TABLE_SIZE] __cacheline_aligned;

wait_queue_head_t *bit_waitqueue(void *word, int bit)
{
    const int shift = BITS_PER_LONG == 32 ? 5 : 6;
    unsigned long val = (unsigned long)word << shift | bit;

    return bit_wait_table + hash_long(val, WAIT_TABLE_BITS);
}
总共支持256元素
#define WAIT_TABLE_BITS 8
#define WAIT_TABLE_SIZE (1 << WAIT_TABLE_BITS)
用户调用bit_waitqueue 的到wait queue的时候,起始就是更加word和要检测的bit产生一个唯一的hash
值,当时这个值必须在256 之内,这点是由hash_long(val, WAIT_TABLE_BITS)的WAIT_TABLE_BITS 来决定的
#if BITS_PER_LONG == 32
#define GOLDEN_RATIO_PRIME GOLDEN_RATIO_32
#define hash_long(val, bits) hash_32(val, bits)
#elif BITS_PER_LONG == 64
#define hash_long(val, bits) hash_64(val, bits)
#define GOLDEN_RATIO_PRIME GOLDEN_RATIO_64
#else
#error Wordsize not 32 or 64
#endif
我们BITS_PER_LONG == 32 因此hash_long == hash_32
这个函数在算出32 bit的hash值后,会右移动24bit(因为WAIT_TABLE_BITS==8),因此就保证hash_32_generic 返回的值在256之内.
static inline u32 hash_32_generic(u32 val, unsigned int bits)
{
    /* High bits are more random, so use them. */
    return __hash_32(val) >> (32 - bits);
}
以后自己写driver的时候也可以考虑采用hash table这种方法.


0 0
原创粉丝点击