简体   繁体   English

为什么在Redis源代码中我没有看到内存障碍?

[英]Why in Redis source code I didn't see memory barriers?

In Linux kernel source code, there are many memory barrier(smp_mb() and so on). 在Linux内核源代码中,有许多内存屏障(smp_mb()等)。

But in redis's source,I didn't see it. 但在redis的来源中,我没有看到它。 In the Makefile of redis, gcc optimize option is -O2, so it should be reorder these instruction. 在redis的Makefile中,gcc optimize选项是-O2,所以应该重新排序这些指令。 Why it not use mb() for insure the correct behavior? 为什么不使用mb()来确保正确的行为?

Added: 添加:

For example: In Linux kernel's kfifo: 例如:在Linux内核的kfifo中:

unsigned int __kfifo_put(struct kfifo *fifo,unsigned char *buffer, unsigned int len)   
{   
  unsigned int l;   
  len = min(len, fifo->size - fifo->in + fifo->out);
  smp_mb();
  l = min(len, fifo->size - (fifo->in & (fifo->size - 1)));   
  memcpy(fifo->buffer + (fifo->in & (fifo->size - 1)), buffer, l);
  ...
  smp_wmb();
  fifo->in += len;
  ...
}

In Redis source, I research the whole project, cann't find memory barriers: for example: 在Redis源代码中,我研究整个项目,找不到内存障碍:例如:

zskiplistNode *zslInsert(zskiplist *zsl, double score, robj *obj) {
  zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
  unsigned int rank[ZSKIPLIST_MAXLEVEL];
  int i, level;
  ...
  level = zslRandomLevel();
  if (level > zsl->level) {
    for (i = zsl->level; i < level; i++) {
      rank[i] = 0;
      update[i] = zsl->header;
/////need a mb() ???
      update[i]->level[i].span = zsl->length;
    }
    zsl->level = level;
  }
  ...
}

is it special that why in redis there is no memory barriers? 难道为什么在redis中没有记忆障碍?
I think it probably that my understand for mb() is immature, thanks for comment... 我认为可能我对mb()的理解还不成熟,感谢评论......

added: 添加:

But in above two pieces of code displayed, the kfifo in linux kernel uses mb(). 但是在上面显示的两段代码中,linux内核中的kfifo使用了mb()。 it just changes variables allocated in thread's stack space and also user mb() between r/w operation. 它只是改变了线程堆栈空间中分配的变量,也改变了r / w操作之间的用户mb()。 So it should not totally relevent to multi-threading... (although redis is single thread) 所以它不应该完全相关到多线程...(虽然redis是单线程)

It may be helpful to realize that very little code requires explicit memory barriers. 认识到很少的代码需要明确的内存障碍可能会有所帮助。 Some examples of such code include OS kernels, threading libraries and lock-free data structures. 此类代码的一些示例包括OS内核,线程库和无锁数据结构。

Most other code would either: 大多数其他代码将:

  1. not directly interact with threading, rendering memory barriers unnecessary; 不直接与线程交互,不必渲染内存障碍;
  2. use OS-provided threading libraries (eg pthreads ), and rely on the memory ordering guarantees provided by such libraries. 使用OS提供的线程库(例如pthreads ),并依赖这些库提供的内存排序保证

If, when asking this question, you had a specific part of Redis's code base in mind, please show us the relevant code. 如果在提出这个问题时,您考虑到了Redis代码库的特定部分,请向我们展示相关代码。

Redis is single threaded, so there's no need for memory barriers. Redis是单线程的,因此不需要内存屏障。

They're only relevant if you have multiple execution paths (eg in a multithreaded application). 如果您有多个执行路径(例如在多线程应用程序中),它们只是相关的。 Even with multithreaded user space applications, you would normally not need your own memory barriers, as libraries (eg pthreads) contains memory barriers in the synchronization APIs, (such as mutexes, semaphores, condition variables and so on). 即使使用多线程用户空间应用程序,您通常也不需要自己的内存屏障,因为库(例如pthread)在同步API中包含内存障碍(例如互斥锁,信号量,条件变量等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM