如果CoreMutex打开,则会设置全局的锁控制函数
- /* If the xMutexAlloc method has not been set, then the user did not
- ** install a mutex implementation via sqlite3_config() prior to
- ** sqlite3_initialize() being called. This block copies pointers to
- ** the default implementation into the sqlite3GlobalConfig structure.
- */
- sqlite3_mutex_methods const *pFrom;
- sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
-
- if( sqlite3GlobalConfig.bCoreMutex ){
- pFrom = sqlite3DefaultMutex();
- }else{
- pFrom = sqlite3NoopMutex();
- }
- pTo->xMutexInit = pFrom->xMutexInit;
- pTo->xMutexEnd = pFrom->xMutexEnd;
- pTo->xMutexFree = pFrom->xMutexFree;
- pTo->xMutexEnter = pFrom->xMutexEnter;
- pTo->xMutexTry = pFrom->xMutexTry;
- pTo->xMutexLeave = pFrom->xMutexLeave;
- pTo->xMutexHeld = pFrom->xMutexHeld;
- pTo->xMutexNotheld = pFrom->xMutexNotheld;
- sqlite3MemoryBarrier();
- pTo->xMutexAlloc = pFrom->xMutexAlloc;
- 而CoreMutext未打开的话,sqlite3NoopMutex()的实现如下(CoreMutext未打开的话,对应使用的锁函数均为空实现):
- sqlite3_mutex_methods const *sqlite3NoopMutex(void){
- static const sqlite3_mutex_methods sMutex = {
- noopMutexInit,
- noopMutexEnd,
- noopMutexAlloc,
- noopMutexFree,
- noopMutexEnter,
- noopMutexTry,
- noopMutexLeave,
- 0,
- 0,
- };
-
- return &sMutex;
- }
-
- // CoreMutext未打开的话,对应使用的锁函数均为空实现
- static int noopMutexInit(void){ return SQLITE_OK; }
- static int noopMutexEnd(void){ return SQLITE_OK; }
- static sqlite3_mutex *noopMutexAlloc(int id){
- UNUSED_PARAMETER(id);
- return (sqlite3_mutex*)8;
- }
- static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
- static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
- static int noopMutexTry(sqlite3_mutex *p){
- UNUSED_PARAMETER(p);
- return SQLITE_OK;
- }
- static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
粗略看了一下,通过db->mutex(sqlite3_mutex_enter(db->mutex);)保护的逻辑块和函数主要如下列表:
- sqlite3_db_status、sqlite3_finalize、sqlite3_reset、sqlite3_step、sqlite3_exec、
- sqlite3_preppare_v2、column_name、blob操作、sqlite3Close、sqlite3_errmsg...
基本覆盖了所有的读、写、DDL、DML,也包括prepared statement操作;也就是说,在未打开FullMutex的情况下,在一个连接上的所有DB操作必须严格串行执行,包括只读操作。
sqlite3中的mutex操作函数,除了用于操作db->mutex这个成员之外,还主要用于以下逻辑块(主要是影响数据库所有连接的逻辑):
shm操作(index for wal)、内存池操作、内存缓存操作等
(编辑:ASP站长网)
|