SQLite3线程锁分析

sqlite3三种锁:
1. 单线程模式
2. 多线程模式
3. 串行模式

 

动态配置流程:

1. sqlite3_config函数配置
	......
	case SQLITE_CONFIG_SINGLETHREAD: {
      /* Disable all mutexing */
      sqlite3GlobalConfig.bCoreMutex = 0;
      sqlite3GlobalConfig.bFullMutex = 0;
      break;
    }
    case SQLITE_CONFIG_MULTITHREAD: {
      /* Disable mutexing of database connections */
      /* Enable mutexing of core data structures */
      sqlite3GlobalConfig.bCoreMutex = 1;
      sqlite3GlobalConfig.bFullMutex = 0;
      break;
    }
    case SQLITE_CONFIG_SERIALIZED: {
      /* Enable all mutexing */
      sqlite3GlobalConfig.bCoreMutex = 1;
      sqlite3GlobalConfig.bFullMutex = 1;
      break;
    }
	......

 

2. sqlite3_open_v2(...)->openDatabase(...)	
	......
	if( sqlite3GlobalConfig.bCoreMutex==0 ){ //注意,如果第一步配置成了0,那么在当前步骤flags配置成了SQLITE_OPEN_NOMUTEX或SQLITE_OPEN_FULLMUTEX是不生效的了
		isThreadsafe = 0;
	}else if( flags & SQLITE_OPEN_NOMUTEX ){
		isThreadsafe = 0;
	}else if( flags & SQLITE_OPEN_FULLMUTEX ){
		isThreadsafe = 1;
	}else{
		isThreadsafe = sqlite3GlobalConfig.bFullMutex;
	}
	......  
	
	if( isThreadsafe ){
		db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
		if( db->mutex==0 ){
			sqlite3_free(db);
			db = 0;
			goto opendb_out;
		}
	}
	......
	if( db ){
		assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 );
		sqlite3_mutex_leave(db->mutex);
	}
	  

 

你可能感兴趣的:(sqlite)