首页 数据库 mysql教程 BDB事务共享区域

BDB事务共享区域

Jun 07, 2016 pm 04:26 PM
事务 共享 区域 我们 最后

最后我们来看一下事务共享区域。 首先仍然是每个进程都有的一个数据结构__db_txnmgr{}: /** The transaction manager encapsulates the transaction system. It contains* references to the log and lock managers as well as the state that keeps* track

最后我们来看一下事务共享区域。

首先仍然是每个进程都有的一个数据结构__db_txnmgr{}:

<code>/*
* The transaction manager encapsulates the transaction system.  It contains
* references to the log and lock managers as well as the state that keeps
* track of the shared memory region.
*/
struct __db_txnmgr {
/* These fields need to be protected for multi-threaded support. */
    db_mutex_t    *mutexp;    /* Synchronization. */
                    /* list of active transactions */
    TAILQ_HEAD(_chain, __db_txn)    txn_chain;
/* These fields are not protected. */
    REGINFO        reginfo;    /* Region information. */
    DB_ENV        *dbenv;        /* Environment. */
    int (*recover)            /* Recovery dispatch routine */
        __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
    u_int32_t     flags;        /* DB_TXN_NOSYNC, DB_THREAD */
    DB_TXNREGION    *region;    /* address of shared memory region */
    void        *mem;        /* address of the shalloc space */
};
</code>
登录后复制

同样拥有一个指向描述共享区域的reginfo字段和指向共享区域的指针mem。

<code>/*
* Layout of the shared memory region.
* The region consists of a DB_TXNREGION structure followed by a large
* pool of shalloc'd memory which is used to hold TXN_DETAIL structures
* and thread mutexes (which are dynamically allocated).
*/
struct __db_txnregion {
    RLAYOUT        hdr;        /* Shared memory region header. */
    u_int32_t    magic;        /* transaction magic number */
    u_int32_t    version;    /* version number */
    u_int32_t    maxtxns;    /* maximum number of active txns */
    u_int32_t    last_txnid;    /* last transaction id given out */
    DB_LSN        pending_ckp;    /* last checkpoint did not finish */
    DB_LSN        last_ckp;    /* lsn of the last checkpoint */
    time_t        time_ckp;    /* time of last checkpoint */
    u_int32_t    logtype;    /* type of logging */
    u_int32_t    locktype;    /* lock type */
    u_int32_t    naborts;    /* number of aborted transactions */
    u_int32_t    ncommits;    /* number of committed transactions */
    u_int32_t    nbegins;    /* number of begun transactions */
    SH_TAILQ_HEAD(_active) active_txn;    /* active transaction list */
};
</code>
登录后复制

共享区域除了有个公用的头部hdr字段以外,最重要的是有一个所有活动事务的列表active_txn字段。

现在让我们看一下这些数据结构和共享区域的创建或打开以及初始化操作。

在db_appinit()函数中调用txn_open()函数打开或连接一个事务共享区域:

<code>if (LF_ISSET(DB_INIT_TXN) && (ret = txn_open(NULL,
    LF_ISSET(DB_CREATE | DB_THREAD | DB_TXN_NOSYNC),
    mode, dbenv, &dbenv->tx_info)) != 0)
    goto err;
</code>
登录后复制

其中txn_open定义如下:

<code>int
txn_open(path, flags, mode, dbenv, mgrpp)
    const char *path;
    u_int32_t flags;
    int mode;
    DB_ENV *dbenv;
    DB_TXNMGR **mgrpp;
{
    DB_TXNMGR *tmgrp;
    u_int32_t maxtxns;
    int ret;
    /* Validate arguments. */
    首先仍然是验证参数
    然后用malloc创建每个进程都有的事务管理器结构
    /* Now, create the transaction manager structure and set its fields. */
    if ((ret = __os_calloc(1, sizeof(DB_TXNMGR), &tmgrp)) != 0)
        return (ret);
    并初始化事务管理器结构
    /* Initialize the transaction manager structure. */
    tmgrp->mutexp = NULL;
    tmgrp->dbenv = dbenv;
    tmgrp->recover =
        dbenv->tx_recover == NULL ? __db_dispatch : dbenv->tx_recover;
    tmgrp->flags = LF_ISSET(DB_TXN_NOSYNC | DB_THREAD);
    TAILQ_INIT(&tmgrp->txn_chain);
    现在开始创建或连接到一个事务共享区域
    /* Join/create the txn region. */
    首先还是一样,填充区域描述信息REGINFO{},然后调用__db_rattach()函数创建或打开一个共享区域
    tmgrp->reginfo.dbenv = dbenv;
    tmgrp->reginfo.appname = DB_APP_NONE;
    if (path == NULL)
        tmgrp->reginfo.path = NULL;
    else
        if ((ret = __os_strdup(path, &tmgrp->reginfo.path)) != 0)
            goto err;
    tmgrp->reginfo.file = DEFAULT_TXN_FILE;
    tmgrp->reginfo.mode = mode;
    tmgrp->reginfo.size = TXN_REGION_SIZE(maxtxns);
    tmgrp->reginfo.dbflags = flags;
    tmgrp->reginfo.addr = NULL;
    tmgrp->reginfo.fd = -1;
    tmgrp->reginfo.flags = dbenv->tx_max == 0 ? REGION_SIZEDEF : 0;
    if ((ret = __db_rattach(&tmgrp->reginfo)) != 0)
        goto err;
    /* Fill in region-related fields. */
    tmgrp->region = tmgrp->reginfo.addr;
    tmgrp->mem = &tmgrp->region[1];
    if (F_ISSET(&tmgrp->reginfo, REGION_CREATED)) {
        tmgrp->region->maxtxns = maxtxns;
        if ((ret = __txn_init(tmgrp->region)) != 0)
            goto err;
    } else if (tmgrp->region->magic != DB_TXNMAGIC) {
        /* Check if valid region. */
        __db_err(dbenv, "txn_open: Bad magic number");
        ret = EINVAL;
        goto err;
    }
    // 如果这是一个新的事务共享区域,初始化之。这里又和MPOOL一样,
    // 使用__shmalloc函数管理共享区域内存
    if (LF_ISSET(DB_THREAD)) {
        if ((ret = __db_shalloc(tmgrp->mem, sizeof(db_mutex_t),
            MUTEX_ALIGNMENT, &tmgrp->mutexp)) == 0)
            /*
            * Since we only get here if threading is turned on, we
            * know that we have spinlocks, so the offset is going
            * to be ignored.  We put 0 here as a valid placeholder.
            */
            __db_mutex_init(tmgrp->mutexp, 0);
        if (ret != 0)
            goto err;
    }
    UNLOCK_TXNREGION(tmgrp);
    *mgrpp = tmgrp;
    return (0);
err:    if (tmgrp->reginfo.addr != NULL) {
        if (tmgrp->mutexp != NULL)
            __db_shalloc_free(tmgrp->mem, tmgrp->mutexp);
        UNLOCK_TXNREGION(tmgrp);
        (void)__db_rdetach(&tmgrp->reginfo);
        if (F_ISSET(&tmgrp->reginfo, REGION_CREATED))
            (void)txn_unlink(path, 1, dbenv);
    }
    if (tmgrp->reginfo.path != NULL)
        __os_freestr(tmgrp->reginfo.path);
    __os_free(tmgrp, sizeof(*tmgrp));
    return (ret);
}
</code>
登录后复制

这个函数就是txn_open()调用来初始化事务共享区域的函数:

<code>/*
* This file contains the top level routines of the transaction library.
* It assumes that a lock manager and log manager that conform to the db_log(3)
* and db_lock(3) interfaces exist.
*
* Initialize a transaction region in shared memory.
* Return 0 on success, errno on failure.
*/
static int
__txn_init(txn_region)
    DB_TXNREGION *txn_region;
{
    time_t now;
    (void)time(&now);
    /* maxtxns is already initialized. */
    txn_region->magic = DB_TXNMAGIC;
    txn_region->version = DB_TXNVERSION;
    txn_region->last_txnid = TXN_MINIMUM;
    /*
    * XXX
    * If we ever do more types of locking and logging, this changes.
    */
    txn_region->logtype = 0;
    txn_region->locktype = 0;
    txn_region->time_ckp = now;
    ZERO_LSN(txn_region->last_ckp);
    ZERO_LSN(txn_region->pending_ckp);
    SH_TAILQ_INIT(&txn_region->active_txn);
    __db_shalloc_init((void *)&txn_region[1],
        TXN_REGION_SIZE(txn_region->maxtxns) - sizeof(DB_TXNREGION));
    return (0);
}
</code>
登录后复制

其中DB_TXN{}是每个事务的描述符(定义在db_int.h文件中):

<code>/* The structure allocated for every transaction. */
struct __db_txn {
    DB_TXNMGR    *mgrp;        /* Pointer to transaction manager. */
    DB_TXN        *parent;    /* Pointer to transaction's parent. */
    DB_LSN        last_lsn;    /* Lsn of last log write. */
    u_int32_t    txnid;        /* Unique transaction id. */
    size_t        off;        /* Detail structure within region. */
    TAILQ_ENTRY(__db_txn) links;    /* Links transactions off manager. */
    TAILQ_HEAD(__kids, __db_txn) kids; /* Child transactions. */
    TAILQ_ENTRY(__db_txn) klinks;    /* Links child transactions. */
#define    TXN_MALLOC    0x01        /* Structure allocated by TXN system. */
    u_int32_t    flags;
};
typedef struct __txn_detail {
    u_int32_t txnid;        /* current transaction id
                    used to link free list also */
    DB_LSN    last_lsn;        /* last lsn written for this txn */
    DB_LSN    begin_lsn;        /* lsn of begin record */
    size_t    last_lock;        /* offset in lock region of last lock
                    for this transaction. */
    size_t    parent;            /* Offset of transaction's parent. */
#define    TXN_UNALLOC    0
#define    TXN_RUNNING    1
#define    TXN_ABORTED    2
#define    TXN_PREPARED    3
#define    TXN_COMMITTED    4
    u_int32_t status;        /* status of the transaction */
    SH_TAILQ_ENTRY    links;        /* free/active list */
#define    TXN_XA_ABORTED        1
#define    TXN_XA_DEADLOCKED    2
#define    TXN_XA_ENDED        3
#define    TXN_XA_PREPARED        4
#define    TXN_XA_STARTED        5
#define    TXN_XA_SUSPENDED    6
    u_int32_t xa_status;        /* XA status */
    /*
    * XID (xid_t) structure: because these fields are logged, the
    * sizes have to be explicit.
    */
    DB_XID xid;            /* XA global transaction id */
    u_int32_t bqual;        /* bqual_length from XID */
    u_int32_t gtrid;        /* gtrid_length from XID */
    int32_t format;            /* XA format */
} TXN_DETAIL;
</code>
登录后复制
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

wallpaperengine如何共享给好友 wallpaperengine如何共享给好友 Mar 18, 2024 pm 10:00 PM

用户在使用wallpaperengine时可以将获得的壁纸共享给好友,有很多用户不知道wallpaperengine如何共享给好友,可以将自己喜欢的壁纸保存到本地之后再通过社交软件的方式分享给朋友。wallpaperengine如何共享给好友答:保存到本地之后分享给朋友。1、建议大家可以将自己喜欢的壁纸保存到本地之后再通过社交软件的方式分享给朋友。2、也可以通过文件夹的方式上传到电脑端,然后在电脑端用创意工坊的功能点击分享。3、在电脑端使用Wallpaperengine,打开创意工坊的选项栏找到

企业微信如何共享屏幕 企业微信如何共享屏幕 Feb 28, 2024 pm 12:43 PM

越来越多的企业选择使用专属的企业微信,这不仅便于企业与客户、合作伙伴之间的沟通和交流,还极大地提高了工作效率。企业微信功能丰富,其中,共享屏幕功能备受欢迎。在会议过程中,通过共享屏幕,与会者可以更加直观地展示内容,从而更加高效地协作。那么究竟该如何在企业微信中高效的共享自己的屏幕呢,还不了解的用户们,这篇教程攻略就将为大家带来详细的内容介绍,希望能帮助到大家!企业微信怎么共享屏幕?1、在企业微信主界面的左侧蓝色区域内可以看到有一列功能,我们找到“会议”这个图标,点击进入之后,就会出现三种会议模式

快速共享功能在三星手机上不起作用 – 修复 快速共享功能在三星手机上不起作用 – 修复 Sep 19, 2023 pm 04:25 PM

快速共享可以节省三星用户在设备间传输文件的大量时间。但是三星Galaxy用户抱怨手机上的快速共享功能面临问题。通常,是快速共享中的可见性问题导致了此问题。因此,这是您对Galaxy设备上的快速共享功能进行故障排除所需的唯一指南。修复1–更改快速共享可见性设置切换手机上的快速共享可见性设置。快速共享可能设置为错误的设置,从而导致此问题。步骤1–首先,向上滑动一次以打开应用程序抽屉。步骤2–在那里,打开“设置”.第3步–进入“设置”页面,打开“连接的设备”选项卡。第4步–打开“快速共享”功能。步骤5

Windows 11 文件夹共享指南:轻松分享你的文件和资料 Windows 11 文件夹共享指南:轻松分享你的文件和资料 Mar 13, 2024 am 11:49 AM

在日常生活和工作中,我们经常需要在不同设备之间共享文件和文件夹。Windows11系统提供了方便的内建文件夹共享功能,让我们可以轻松地在同一网络内安全地与他人分享所需内容,同时保护个人文件的隐私。这项功能使文件共享变得简单而高效,不必担心泄露私人信息。通过Windows11系统的文件夹共享功能,我们可以更加便捷地进行合作、交流和协作,提高工作效率和生活便利性。为了顺利配置共享文件夹,我们首先需要满足以下条件:所有(参与共享的)设备都连接到同一个网络。启用「网络发现」并配置好共享。知道目标设备中的

如何在iPhone iOS 17上使用NameDrop 如何在iPhone iOS 17上使用NameDrop Sep 22, 2023 pm 11:41 PM

随着新款苹果iPhone15系列手机的推出和最新的iOS17移动操作系统的推出,为苹果设备带来了丰富的新功能,调整和增强功能。用户可能想知道如何在iPhone和iOS17上使用新的NameDrop功能。本指南将简要概述如何使用iOS17上提供的新NameDrop系统快速有效地共享您的联系信息。NameDrop是一项功能,允许iPhone用户快速与他人共享他们的联系信息。它是社交活动、商务会议或社交聚会的便捷工具,您需要与新朋友交换联系方式。但是,请务必注意,NameDrop仅适用于发送新的联系人

共享打印机无法连接到打印机 共享打印机无法连接到打印机 Feb 22, 2024 pm 01:09 PM

随着数字化时代的发展,共享打印机成为现代办公环境中不可或缺的一部分。然而,有时我们可能会遇到共享打印机无法连接到打印机的问题,这不仅会影响工作效率,还会带来一系列麻烦。本文旨在探讨共享打印机无法连接到打印机的原因和解决方法。共享打印机无法连接到打印机的原因有很多,其中最常见的原因是网络问题。如果共享打印机与打印机之间的网络连接不稳定或中断,那么就无法进行正常

如何在iPhone上保护联系人照片和海报的隐私 如何在iPhone上保护联系人照片和海报的隐私 Sep 18, 2023 am 10:49 AM

谁可以在iPhone上查看您的联系人照片和海报?Apple提供了一些选项,用于个性化您在致电或发消息时在某人的iPhone上的显示方式。这些选项包括拟我表情、简单文本或带有效果的自定照片作为您的联系人照片和显示图像。您可以随时自由更改这些选择,并在联系人卡片上在不同配置文件之间转换。此外,Apple还使您能够控制谁可以在iOS17上查看和访问您选择的照片或显示图像。您可以决定与保存在联系人列表中的个人共享这些内容,也可以将iPhone设置为每次与联系人交互时提示您。如果您愿意,还可以永久禁用名称

Lock wait timeout exceeded; try restarting transaction - 如何解决MySQL报错:事务等待超时 Lock wait timeout exceeded; try restarting transaction - 如何解决MySQL报错:事务等待超时 Oct 05, 2023 am 08:46 AM

Lockwaittimeoutexceeded;tryrestartingtransaction-如何解决MySQL报错:事务等待超时在使用MySQL数据库时,有时可能会遇到一个常见的错误:Lockwaittimeoutexceeded;tryrestartingtransaction,该错误表示事务等待超时。这个错误通常发生在并

See all articles