首頁 資料庫 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脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 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)

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1321
25
PHP教程
1269
29
C# 教程
1249
24
企業微信如何分享螢幕 企業微信如何分享螢幕 Feb 28, 2024 pm 12:43 PM

越來越多的企業選擇使用專屬的企業微信,這不僅便於企業與客戶、合作夥伴之間的溝通和交流,也大大提高了工作效率。企業微信功能豐富,其中,共享螢幕功能備受歡迎。在會議過程中,透過分享螢幕,與會者可以更直觀地展示內容,從而更有效率地協作。那麼究竟該如何在企業微信中高效的共享自己的螢幕呢,還不了解的用戶們,這篇教程攻略就將為大家帶來詳細的內容介紹,希望能幫助到大家!企業微信怎麼共享螢幕? 1.在企業微信主介面的左側藍色區域內可以看到有一列功能,我們找到「會議」這個圖標,點擊進入之後,就會出現三種會議模式

wallpaperengine如何分享給好友 wallpaperengine如何分享給好友 Mar 18, 2024 pm 10:00 PM

用戶在使用wallpaperengine時可以將獲得的壁紙共享給好友,有很多用戶不知道wallpaperengine如何共享給好友,可以將自己喜歡的壁紙保存到本地之後再通過社交軟體的方式分享給朋友。 wallpaperengine如何分享給好友答:在儲存到本地之後分享給朋友。 1.建議大家可​​以將自己喜歡的桌布保存到本地之後再透過社群軟體的方式分享給朋友。 2.也可以透過資料夾的方式上傳到電腦端,然後在電腦端用創意工坊的功能點擊分享。 3.在電腦端使用Wallpaperengine,打開創意工坊的選項列找到

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

快速共享可以節省三星用戶在裝置間傳輸檔案的大量時間。但是三星Galaxy用戶抱怨手機上的快速分享功能面臨問題。通常,是快速共享中的可見性問題導致了此問題。因此,這是您對Galaxy裝置上的快速共用功能進行故障排除所需的唯一指南。修復1–更改快速共享可見性設定切換手機上的快速共享可見性設定。快速共享可能設置為錯誤的設置,從而導致此問題。步驟1–首先,向上滑動一次以打開應用程式抽屜。步驟2–在那裡,打開“設定”.第3步–進入“設定”頁面,打開“連接的設備”選項卡。步驟4–開啟「快速共享」功能。步驟5

如何在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僅適用於發送新的聯絡人

Windows 11 資料夾共用指南:輕鬆分享你的檔案和資料 Windows 11 資料夾共用指南:輕鬆分享你的檔案和資料 Mar 13, 2024 am 11:49 AM

在日常生活和工作中,我們經常需要在不同裝置之間共用檔案和資料夾。 Windows11系統提供了方便的內建資料夾共用功能,讓我們可以輕鬆地在同一網路內安全地與他人分享所需內容,同時保護個人檔案的隱私。這項功能使文件共享變得簡單而高效,不必擔心洩露私人資訊。透過Windows11系統的資料夾共享功能,我們可以更方便地進行合作、交流和協作,提高工作效率和生活便利性。為了順利配置共用資料夾,我們首先需要滿足以下條件:所有(參與共享的)設備都連接到同一個網路。啟用「網路發現」並配置好共享。知道目標設備中的

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,該錯誤表示事務等待逾時。這個錯誤通常發生在並且

共用印表機無法連接到印表機 共用印表機無法連接到印表機 Feb 22, 2024 pm 01:09 PM

隨著數位時代的發展,共享印表機成為現代辦公環境中不可或缺的一部分。然而,有時我們可能會遇到共用印表機無法連接到印表機的問題,這不僅會影響工作效率,還會帶來一系列麻煩。本文旨在探討共用印表機無法連接到印表機的原因和解決方法。共用印表機無法連接到印表機的原因有很多,其中最常見的原因是網路問題。如果共用印表機與印表機之間的網路連線不穩定或中斷,那麼就無法進行正常

如何在iPhone上保護聯絡人照片和海報的隱私 如何在iPhone上保護聯絡人照片和海報的隱私 Sep 18, 2023 am 10:49 AM

誰可以在iPhone上查看您的聯絡人照片和海報? Apple提供了一些選項,用於個性化您在致電或發送訊息時在某人的iPhone上的顯示方式。這些選項包括擬我表情、簡單文字或帶有效果的自定照片作為您的聯絡人照片和顯示圖像。您可以隨時自由更改這些選擇,並在聯絡人卡片上在不同設定檔之間轉換。此外,Apple還可讓您控制誰可以在iOS17上查看和存取您選擇的照片或顯示影像。您可以決定與儲存在聯絡人清單中的個人分享這些內容,也可以將iPhone設定為每次與聯絡人互動時提示您。如果您願意,也可以永久停用名稱

See all articles