这一次我们以锁共享区域分析。 /** the lock table is the per-process cookie returned from a lock_open call.*/struct __db_locktab { db_env *dbenv; /* environment. */ reginfo reginfo; /* region information. */ db_lockregion *region; /* address
这一次我们以锁共享区域分析。
/*
* the lock table is the per-process cookie returned from a lock_open call.
*/
struct __db_locktab {
db_env *dbenv; /* environment. */
reginfo reginfo; /* region information. */
db_lockregion *region; /* address of shared memory region. */
db_hashtab *hashtab; /* beginning of hash table. */
void *mem; /* beginning of string space. */
u_int8_t *conflicts; /* pointer to conflict matrix. */
};
与mpool一样,有一个每进程都有的数据结构__db_locktab{},其中reginfo字段指向共享内存区域描述信息结构reginfo{}。
region字段指向分配在共享缓冲区的,用于描述该缓冲区的db_lockregion{}结构的起始地址。该结构位于共享区域的开始。
/*
* the lock region consists of:
* the db_lockregion structure (sizeof(db_lockregion)).
* the conflict matrix of nmodes * nmodes bytes (nmodes * nmodes).
* the hash table for object lookup (hashsize * sizeof(db_obj *)).
* the locks themselves (maxlocks * sizeof(struct __db_lock).
* the objects being locked (maxlocks * sizeof(db_obj)).
* string space to represent the dbts that are the objects being locked.
*/
struct __db_lockregion {
rlayout hdr; /* shared region header. */
u_int32_t magic; /* lock magic number */
u_int32_t version; /* version number */
u_int32_t id; /* unique id generator */
u_int32_t need_dd; /* flag for deadlock detector */
u_int32_t detect; /* run dd on every conflict */
sh_tailq_head(lock_header) free_locks; /* free lock header */
sh_tailq_head(obj_header) free_objs; /* free obj header */
u_int32_t maxlocks; /* maximum number of locks in table */
u_int32_t table_size; /* size of hash table */
u_int32_t nmodes; /* number of lock modes */
u_int32_t numobjs; /* number of objects */
u_int32_t nlockers; /* number of lockers */
size_t increment; /* how much to grow region */
size_t hash_off; /* offset of hash table */
size_t mem_off; /* offset of memory region */
size_t mem_bytes; /* number of bytes in memory region */
u_int32_t nconflicts; /* number of lock conflicts */
u_int32_t nrequests; /* number of lock gets */
u_int32_t nreleases; /* number of lock puts */
u_int32_t ndeadlocks; /* number of deadlocks */
};
每一个共享区域都有一个存放通用信息的区域头rlayout{}。 其他的信息都是针对共享区域起始地址的偏移量,这是因为共享区域在各个进程的地址空间映射地址是各不相同的(即addr各不相同),因此不能使用相对进程的内存地址。
我们来看一下锁区域是怎样创建的。
在db_appinit()函数中调用lock_open():
if (lf_isset(db_init_lock) && (ret = lock_open(null,
lf_isset(db_create | db_thread),
mode, dbenv, &dbenv->lk_info)) != 0)
我们来看一下lock_open()函数:
int
lock_open(path, flags, mode, dbenv, ltp)
const char *path;
u_int32_t flags;
int mode;
db_env *dbenv;
db_locktab **ltp;
{
db_locktab *lt;
u_int32_t lock_modes, maxlocks, regflags;
int ret;
/* 首先验证参数的合法性 */
/* validate arguments. */
......
/* 然后使用malloc创建每个进程都有的锁表数据结构 */
/* create the lock table structure. */
if ((ret = __os_calloc(1, sizeof(db_locktab), <)) != 0)
return (ret);
lt->dbenv = dbenv;
/* grab the values that we need to compute the region size. */
lock_modes = db_lock_rw_n;
maxlocks = db_lock_default_n;
regflags = region_sizedef;
if (dbenv != null) {
if (dbenv->lk_modes != 0) {
lock_modes = dbenv->lk_modes;
regflags = 0;
}
if (dbenv->lk_max != 0) {
maxlocks = dbenv->lk_max;
regflags = 0;
}
}
/* 现在可以创建或链接到一个共享锁区域 */
/* join/create the lock region. */
/* 填充reginfo信息以调用底层的__db_rattach()函数 */
lt->reginfo.dbenv = dbenv;
lt->reginfo.appname = db_app_none;
if (path == null)
lt->reginfo.path = null;
else
if ((ret = __os_strdup(path, <->reginfo.path)) != 0)
goto err;
lt->reginfo.file = db_default_lock_file;
lt->reginfo.mode = mode;
lt->reginfo.size =
lock_region_size(lock_modes, maxlocks, __db_tablesize(maxlocks));
lt->reginfo.dbflags = flags;
lt->reginfo.addr = null;
lt->reginfo.fd = -1;
lt->reginfo.flags = regflags;
/* 调用__db_rattach()函数创建或打开一个共享区域 */
if ((ret = __db_rattach(<->reginfo)) != 0)
goto err;
/* now set up the pointer to the region. */
lt->region = lt->reginfo.addr;
/* 如果是该共享区域是新创建的,初始化之 */
/* initialize the region if we created it. */
if (f_isset(<->reginfo, region_created)) {
lt->region->maxlocks = maxlocks;
lt->region->nmodes = lock_modes;
if ((ret = __lock_tabinit(dbenv, lt->region)) != 0)
goto err;
} else {
/* check for an unexpected region. */
if (lt->region->magic != db_lockmagic) {
__db_err(dbenv,
"lock_open: %s: bad magic number", path);
ret = einval;
goto err;
}
}
/* check for automatic deadlock detection. */
if (dbenv != null && dbenv->lk_detect != db_lock_norun) {
if (lt->region->detect != db_lock_norun &&
dbenv->lk_detect != db_lock_default &&
lt->region->detect != dbenv->lk_detect) {
__db_err(dbenv,
"lock_open: incompatible deadlock detector mode");
ret = einval;
goto err;
}
if (lt->region->detect == db_lock_norun)
lt->region->detect = dbenv->lk_detect;
}
/* 填充区域的指针字段:注意我们前面说过的映射问题。这里进程利用公共db_lockregion类型字段region的相对地址在自己的锁表中填写绝对地址 */
/* set up remaining pointers into region. */
lt->conflicts = (u_int8_t *)lt->region + sizeof(db_lockregion);
lt->hashtab =
(db_hashtab *)((u_int8_t *)lt->region + lt->region->hash_off);
lt->mem = (void *)((u_int8_t *)lt->region + lt->region->mem_off);
unlock_lockregion(lt);
*ltp = lt;
return (0);
err: if (lt->reginfo.addr != null) {
unlock_lockregion(lt);
(void)__db_rdetach(<->reginfo);
if (f_isset(<->reginfo, region_created))
(void)lock_unlink(path, 1, dbenv);
}
if (lt->reginfo.path != null)
__os_freestr(lt->reginfo.path);
__os_free(lt, sizeof(*lt));
return (ret);
}
/*
* __lock_panic --
* panic a lock region.
*
* public: void __lock_panic __p((db_env *));
*/
void
__lock_panic(dbenv)
db_env *dbenv;
{
if (dbenv->lk_info != null)
dbenv->lk_info->region->hdr.panic = 1;
}
当第一个创建共享区域时,使用__lock_tabinit初始化锁区域(定义在lock_region.c文件中) 分为两部分:初始化描述共享区域的db_lockreginfo{}结构和共享区域本身。事实上,正如mpool共享区域, 这两部分都是进程共享的对象,因此都在共享区域中,而且在实现上是紧挨在一起的:curaddr = (u_int8_t *)lrp + sizeof(db_lockregion);一个值得注意的地方就是在共享区域中的指针必须以相对起始共享区域起始位置的偏移量存储,
原因我们在前面已经说明了:lrp->hash_off = curaddr - (u_int8_t *)lrp;另一个需要注意的地方是内存对齐问题。
/*
* __lock_tabinit --
* initialize the lock region.
*/
static int
__lock_tabinit(dbenv, lrp)
db_env *dbenv;
db_lockregion *lrp;
{
struct __db_lock *lp;
struct lock_header *tq_head;
struct obj_header *obj_head;
db_lockobj *op;
u_int32_t i, nelements;
const u_int8_t *conflicts;
u_int8_t *curaddr;
conflicts = dbenv == null || dbenv->lk_conflicts == null ?
db_rw_conflicts : dbenv->lk_conflicts;
lrp->table_size = __db_tablesize(lrp->maxlocks);
lrp->magic = db_lockmagic;
lrp->version = db_lockversion;
lrp->id = 0;
/*
* these fields (lrp->maxlocks, lrp->nmodes) are initialized
* in the caller, since we had to grab those values to size
* the region.
*/
lrp->need_dd = 0;
lrp->detect = db_lock_norun;
lrp->numobjs = lrp->maxlocks;
lrp->nlockers = 0;
lrp->mem_bytes = align(string_size(lrp->maxlocks), sizeof(size_t));
lrp->increment = lrp->hdr.size / 2;
lrp->nconflicts = 0;
lrp->nrequests = 0;
lrp->nreleases = 0;
lrp->ndeadlocks = 0;
/*
* as we write the region, we've got to maintain the alignment
* for the structures that follow each chunk. this information
* ends up being encapsulated both in here as well as in the
* lock.h file for the xxx_size macros.
*/
/* initialize conflict matrix. */
curaddr = (u_int8_t *)lrp + sizeof(db_lockregion);
memcpy(curaddr, conflicts, lrp->nmodes * lrp->nmodes);
curaddr += lrp->nmodes * lrp->nmodes;
/*
* initialize hash table.
*/
curaddr = (u_int8_t *)alignp(curaddr, lock_hash_align);
lrp->hash_off = curaddr - (u_int8_t *)lrp;
nelements = lrp->table_size;
__db_hashinit(curaddr, nelements);
curaddr += nelements * sizeof(db_hashtab);
/*
* initialize locks onto a free list. since locks contains mutexes,
* we need to make sure that each lock is aligned on a mutex_alignment
* boundary.
*/
curaddr = (u_int8_t *)alignp(curaddr, mutex_alignment);
tq_head = &lrp->free_locks;
sh_tailq_init(tq_head);
for (i = 0; i++ < lrp->maxlocks;
curaddr += align(sizeof(struct __db_lock), mutex_alignment)) {
lp = (struct __db_lock *)curaddr;
lp->status = db_lstat_free;
sh_tailq_insert_head(tq_head, lp, links, __db_lock);
}
/* initialize objects onto a free list. */
obj_head = &lrp->free_objs;
sh_tailq_init(obj_head);
for (i = 0; i++ < lrp->maxlocks; curaddr += sizeof(db_lockobj)) {
op = (db_lockobj *)curaddr;
sh_tailq_insert_head(obj_head, op, links, __db_lockobj);
}
/*
* initialize the string space; as for all shared memory allocation
* regions, this requires size_t alignment, since we store the
* lengths of malloc'd areas in the area.
*/
curaddr = (u_int8_t *)alignp(curaddr, sizeof(size_t));
lrp->mem_off = curaddr - (u_int8_t *)lrp;
__db_shalloc_init(curaddr, lrp->mem_bytes);
return (0);
}
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!