BDB事务共享区域
最后我们来看一下事务共享区域。 首先仍然是每个进程都有的一个数据结构__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>
原文地址:BDB事务共享区域, 感谢原作者分享。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Users can share the wallpapers they obtain with friends when using WallpaperEngine. Many users do not know how to share WallpaperEngine with friends. They can save their favorite wallpapers locally and then share them with friends through social software. How to share wallpaperengine with friends Answer: Save it locally and share it with friends. 1. It is recommended that you save your favorite wallpapers locally and then share them with friends through social software. 2. You can also upload it to the computer through a folder, and then click Share using the creative workshop function on the computer. 3. Use Wallpaperengine on the computer, open the options bar of the creative workshop and find

More and more enterprises choose to use exclusive enterprise WeChat, which not only facilitates communication between enterprises and customers and partners, but also greatly improves work efficiency. Enterprise WeChat has rich functions, among which the screen sharing function is very popular. During the meeting, by sharing the screen, participants can display content more intuitively and collaborate more efficiently. So how to share your screen efficiently in WeChat Enterprise? For users who don’t know yet, this tutorial guide will give you a detailed introduction. I hope it can help you! How to share screen on WeChat Enterprise? 1. In the blue area on the left side of the main interface of Enterprise WeChat, you can see a list of functions. We find the "Conference" icon. After clicking to enter, three conference modes will appear.

Quick Share can save Samsung users a lot of time transferring files between devices. But Samsung Galaxy users have complained about facing issues with the Quick Share feature on their phones. Typically, visibility issues in quick sharing cause this issue. So, this is the only guide you need to troubleshoot the Quick Share feature on your Galaxy device. Fix 1 – Change Quick Share Visibility Settings Toggle the Quick Share visibility setting on your phone. Quick Share might be set to the wrong settings, causing this issue. Step 1 – First, swipe up once to open the app drawer. Step 2 – Once there, open Settings. Step 3 – Go to the Settings page and open the Connected Devices tab. Step 4 – Turn on the “Quick Share” feature. Step 5

In daily life and work, we often need to share files and folders between different devices. Windows 11 system provides convenient built-in folder sharing functions, allowing us to easily and safely share the content we need with others within the same network while protecting the privacy of personal files. This feature makes file sharing simple and efficient without worrying about leaking private information. Through the folder sharing function of Windows 11 system, we can cooperate, communicate and collaborate more conveniently, improving work efficiency and life convenience. In order to successfully configure a shared folder, we first need to meet the following conditions: All devices (participating in sharing) are connected to the same network. Enable Network Discovery and configure sharing. Know the target device

With the launch of the new Apple iPhone15 series mobile phones and the launch of the latest iOS17 mobile operating system, a wealth of new features, adjustments and enhancements have been brought to Apple devices. Users may be wondering how to use the new NameDrop feature on iPhone and iOS17. This guide will provide a brief overview of how to share your contact information quickly and efficiently using the new NameDrop system available on iOS17. NameDrop is a feature that allows iPhone users to quickly share their contact information with others. It's a convenient tool for social events, business meetings or social gatherings where you need to exchange contact details with new friends. However, it's important to note that NameDrop only works for sending new contacts

With the development of the digital era, shared printers have become an indispensable part of the modern office environment. However, sometimes we may encounter the problem that the shared printer cannot be connected to the printer, which will not only affect work efficiency, but also cause a series of troubles. This article aims to explore the reasons and solutions for why a shared printer cannot connect to the printer. There are many reasons why a shared printer cannot connect to the printer, the most common of which is network issues. If the network connection between the shared printer and the printer is unstable or interrupted, normal operation will not be possible.

Who can view your contact photos and posters on iPhone? Apple offers options for personalizing how you appear on someone's iPhone when they call or send a message. The options include Memoji, simple text, or a custom photo with effects as your contact photo and display image. You are free to change these selections at any time and switch between profiles on the contact card. Additionally, Apple is giving you the ability to control who can view and access photos or display images of your choice on iOS17. You can decide to share these with individuals saved in your contact list, or you can set your iPhone to prompt you every time you interact with a contact. If you wish, you can also disable the name permanently

Lockwaittimeoutexceeded;tryrestartingtransaction - How to solve the MySQL error: transaction wait timeout. When using the MySQL database, you may sometimes encounter a common error: Lockwaittimeoutexceeded;tryrestartingtransaction. This error indicates that the transaction wait timeout. This error usually occurs when
