PostgreSQL源码解读(218)-spinlock的实现

本节介绍了spinlock在不同平台(主要是X86_64和aarch74)下的实现.

成都创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站制作、成都做网站、涿鹿网络推广、小程序设计、涿鹿网络营销、涿鹿企业策划、涿鹿品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联为所有大学生创业者提供涿鹿建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com

/*-------------------------------------------------------------------------
 *
 * s_lock.h
 *     Hardware-dependent implementation of spinlocks.
 ...

一、实现

X86_64
TAS意思是Test And Set.在X86_64平台下,spinlock的实现使用了汇编语言.

#ifdef __x86_64__       /* AMD Opteron, Intel EM64T */
#define HAS_TEST_AND_SET
typedef unsigned char slock_t;
#define TAS(lock) tas(lock)
/*
 * On Intel EM64T, it's a win to use a non-locking test before the xchg proper,
 * but only when spinning.
 *
 * See also Implementing Scalable Atomic Locks for Multi-Core Intel(tm) EM64T
 * and IA32, by Michael Chynoweth and Mary R. Lee. As of this writing, it is
 * available at:
 * http://software.intel.com/en-us/articles/implementing-scalable-atomic-locks-for-multi-core-intel-em64t-and-ia32-architectures
 */
#define TAS_SPIN(lock)    (*(lock) ? 1 : TAS(lock))
static __inline__ int
tas(volatile slock_t *lock)
{
    register slock_t _res = 1;
    __asm__ __volatile__(
        "   lock            \n"
        "   xchgb   %0,%1   \n"
:       "+q"(_res), "+m"(*lock)
:       /* no inputs */
:       "memory", "cc");
    return (int) _res;
}
#define SPIN_DELAY() spin_delay()
static __inline__ void
spin_delay(void)
{
    /*
     * Adding a PAUSE in the spin delay loop is demonstrably a no-op on
     * Opteron, but it may be of some use on EM64T, so we keep it.
     */
    __asm__ __volatile__(
        " rep; nop          \n");
}
#endif   /* __x86_64__ */

aarch74
在aarch74(ARM64)下,使用了__sync_lock_test_and_set函数实现(如可用的情况下)

/*
 * On ARM and ARM64, we use __sync_lock_test_and_set(int *, int) if available.
 *
 * We use the int-width variant of the builtin because it works on more chips
 * than other widths.
 */
#if defined(__arm__) || defined(__arm) || defined(__aarch74__) || defined(__aarch74)
#ifdef HAVE_GCC__SYNC_INT32_TAS
#define HAS_TEST_AND_SET
#define TAS(lock) tas(lock)
typedef int slock_t;
static __inline__ int
tas(volatile slock_t *lock)
{
    return __sync_lock_test_and_set(lock, 1);
}
#define S_UNLOCK(lock) __sync_lock_release(lock)
#endif   /* HAVE_GCC__SYNC_INT32_TAS */
#endif   /* __arm__ || __arm || __aarch74__ || __aarch74 */

二、参考资料

s_lock.h
PostgreSQL中的锁

网站标题:PostgreSQL源码解读(218)-spinlock的实现
链接分享:https://www.cdcxhl.com/article16/pgojgg.html

成都网站建设公司_创新互联,为您提供定制网站网站维护云服务器关键词优化品牌网站建设自适应网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都定制网站网页设计