最后我们来看一下事务共享区域。 首先仍然是每个进程都有的一个数据结构__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{}:
/* * 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 */ };同样拥有一个指向描述共享区域的reginfo字段和指向共享区域的指针mem。
/* * 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 */ };共享区域除了有个公用的头部hdr字段以外,最重要的是有一个所有活动事务的列表active_txn字段。
现在让我们看一下这些数据结构和共享区域的创建或打开以及初始化操作。
在db_appinit()函数中调用txn_open()函数打开或连接一个事务共享区域:
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;其中txn_open定义如下:
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); }这个函数就是txn_open()调用来初始化事务共享区域的函数:
/* * 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); }其中db_txn{}是每个事务的描述符(定义在db_int.h文件中):
/* 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;【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!