Memcached源代码多线程在多核系统1.6.6上如何安全

问题描述

我想知道在多核环境中,当许多_stritem或item构造变量的操纵在操纵时似乎没有利用锁时,memcached源代码在多线程环境中是如何安全的(而不是使用refcount来指示有多少个线程)正在使用该物品)。

行:memcached.h中为519

typedef struct _stritem {
    /* Protected by LRU locks */
    struct _stritem *next;
    struct _stritem *prev;
    /* Rest are protected by an item lock */
    struct _stritem *h_next;    /* hash chain next */
    rel_time_t      time;       /* least recent access */
    rel_time_t      exptime;    /* expire time */
    int             nbytes;     /* size of data */
    unsigned short  refcount;
    uint16_t        it_flags;   /* ITEM_* above */
    uint8_t         slabs_clsid;/* which slab class we're in */
    uint8_t         nkey;       /* key length,w/terminating null and padding */
    /* this odd type prevents type-punning issues when we do
     * the little shuffle to save space when not using CAS. */
    union {
        uint64_t cas;
        char end;
    } data[];
    /* if it_flags & ITEM_CAS we have 8 bytes CAS */
    /* then null-terminated key */
    /* then " flags length\r\n" (no terminating null) */
    /* then data with terminating \r\n (no terminating null; it's binary!) */
} item;

memcached.h中的第851行

#define refcount_incr(it) ++(it->refcount)
#define refcount_decr(it) --(it->refcount)

在多线程多核环境中递增/递减变量结构时,是否不缺少对项目锁的锁定?

例如:该变量可以由两个线程获取,并放置在单独的寄存器中,然后在两个寄存器中均递增,然后在其内存位置中替换。这将导致仅增加1。然后,如果减少1个线程,是否无法释放对象,然后导致其他线程出现问题?

编辑:要澄清一下,鉴于memcached已经存在了17年以上,因此该实现很可能是无错误的,但我对memcached如何处理此问题感到好奇。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)