public static RC UnregisterVfs(VSystem vfs) { var mutex = MutexEx.Alloc(MutexEx.MUTEX.STATIC_MASTER); MutexEx.Enter(mutex); UnlinkVfs(vfs); MutexEx.Leave(mutex); return(RC.OK); }
public static VSystem FindVfs(string name) { VSystem vfs = null; var mutex = MutexEx.Alloc(MutexEx.MUTEX.STATIC_MASTER); MutexEx.Enter(mutex); for (vfs = _vfsList; vfs != null && name != vfs.Name; vfs = vfs.Next) { } MutexEx.Leave(mutex); return(vfs); }
public static void ResetAutoExtension() { #if !OMIT_AUTOINIT if (Initialize() == RC.OK) #endif { #if THREADSAFE MutexEx mutex = MutexEx.Alloc(MutexEx.MUTEX.STATIC_MASTER); #endif MutexEx.Enter(mutex); g_autoext.Exts = null; g_autoext.ExtsLength = 0; MutexEx.Leave(mutex); } }
public static void PutRandom(int length, ref long bufferIdx) { bufferIdx = 0; byte[] b = new byte[length]; #if THREADSAFE MutexEx mutex = MutexEx.Alloc(MutexEx.MUTEX.STATIC_PRNG); MutexEx.Enter(mutex); #endif while (length-- > 0) { bufferIdx = (uint)((bufferIdx << 8) + RandomByte()); } #if THREADSAFE MutexEx.Leave(mutex); #endif }
public static void PutRandom(int length, byte[] buffer, int offset) { long bufferIdx = System.DateTime.Now.Ticks; #if THREADSAFE MutexEx mutex = MutexEx.Alloc(MutexEx.MUTEX.STATIC_PRNG); MutexEx.Enter(mutex); #endif while (length-- > 0) { bufferIdx = (uint)((bufferIdx << 8) + RandomByte()); buffer[offset++] = (byte)bufferIdx; } #if THREADSAFE MutexEx.Leave(mutex); #endif }
public static RC RegisterVfs(VSystem vfs, bool default_, Func <VFile> createOsFile) { var mutex = MutexEx.Alloc(MutexEx.MUTEX.STATIC_MASTER); MutexEx.Enter(mutex); UnlinkVfs(vfs); vfs.CreateOsFile = createOsFile; if (default_ || _vfsList == null) { vfs.Next = _vfsList; _vfsList = vfs; } else { vfs.Next = _vfsList.Next; _vfsList.Next = vfs; } Debug.Assert(_vfsList != null); MutexEx.Leave(mutex); return(RC.OK); }
internal static void UnlinkVfs(VSystem vfs) { Debug.Assert(MutexEx.Held(MutexEx.Alloc(MutexEx.MUTEX.STATIC_MASTER))); if (vfs == null) { } else if (_vfsList == vfs) { _vfsList = vfs.Next; } else if (_vfsList != null) { var p = _vfsList; while (p.Next != null && p.Next != vfs) { p = p.Next; } if (p.Next == vfs) { p.Next = vfs.Next; } } }
public static void AutoLoadExtensions(Context ctx) { if (g_autoext.ExtsLength == 0) { return; // Common case: early out without every having to acquire a mutex } bool go = true; for (int i = 0; go; i++) { string errmsg = null; #if THREADSAFE MutexEx mutex = MutexEx.Alloc(MutexEx.MUTEX.STATIC_MASTER); #endif MutexEx.Enter(mutex); Func <Context, string, core_api_routines, RC> init; if (i >= g_autoext.ExtsLength) { init = null; go = false; } else { init = g_autoext.Exts[i]; } MutexEx.Leave(mutex); errmsg = null; RC rc; if (init != null && (rc = init(ctx, errmsg, g_apis)) != 0) { Error(ctx, rc, "automatic extension loading failed: %s", errmsg); go = false; } C._tagfree(ctx, ref errmsg); } }
static RC AutoExtension(Func <Context, string, core_api_routines, RC> init) { RC rc = RC.OK; #if !OMIT_AUTOINIT rc = Initialize(); if (rc != 0) { return(rc); } else #endif { #if THREADSAFE MutexEx mutex = MutexEx.Alloc(MutexEx.MUTEX.STATIC_MASTER); #endif MutexEx.Enter(mutex); int i; for (i = 0; i < g_autoext.ExtsLength; i++) { if (g_autoext.Exts[i] == init) { break; } } if (i == g_autoext.ExtsLength) { Array.Resize(ref g_autoext.Exts, g_autoext.ExtsLength + 1); g_autoext.Exts[g_autoext.ExtsLength] = init; g_autoext.ExtsLength++; } MutexEx.Leave(mutex); Debug.Assert((rc & (RC)0xff) == rc); return(rc); } }
public static RC PreInitialize(out MutexEx masterMutex) { masterMutex = default(MutexEx); // If SQLite is already completely initialized, then this call to sqlite3_initialize() should be a no-op. But the initialization // must be complete. So isInit must not be set until the very end of this routine. if (_GlobalStatics.IsInit) { return(RC.OK); } // The following is just a sanity check to make sure SQLite has been compiled correctly. It is important to run this code, but // we don't want to run it too often and soak up CPU cycles for no reason. So we run it once during initialization. #if !NDEBUG && !OMIT_FLOATING_POINT // This section of code's only "output" is via assert() statements. //ulong x = (((ulong)1)<<63)-1; //double y; //Debug.Assert(sizeof(ulong) == 8); //Debug.Assert(sizeof(ulong) == sizeof(double)); //_memcpy<void>(&y, &x, 8); //Debug.Assert(double.IsNaN(y)); #endif RC rc; #if ENABLE_SQLLOG { Init_Sqllog(); } #endif // Make sure the mutex subsystem is initialized. If unable to initialize the mutex subsystem, return early with the error. // If the system is so sick that we are unable to allocate a mutex, there is not much SQLite is going to be able to do. // The mutex subsystem must take care of serializing its own initialization. rc = RC.OK; //_mutex_init(); if (rc != 0) { return(rc); } // Initialize the malloc() system and the recursive pInitMutex mutex. This operation is protected by the STATIC_MASTER mutex. Note that // MutexAlloc() is called for a static mutex prior to initializing the malloc subsystem - this implies that the allocation of a static // mutex must not require support from the malloc subsystem. masterMutex = MutexEx.Alloc(MutexEx.MUTEX.STATIC_MASTER); // The main static mutex MutexEx.Enter(masterMutex); _GlobalStatics.IsMutexInit = true; //if (!SysEx_GlobalStatics.IsMallocInit) // rc = sqlite3MallocInit(); if (rc == RC.OK) { _GlobalStatics.IsMallocInit = true; if (_GlobalStatics.InitMutex.Tag == null) { _GlobalStatics.InitMutex = MutexEx.Alloc(MutexEx.MUTEX.RECURSIVE); if (_GlobalStatics.CoreMutex && _GlobalStatics.InitMutex.Tag == null) { rc = RC.NOMEM; } } } if (rc == RC.OK) { _GlobalStatics.InitMutexRefs++; } MutexEx.Leave(masterMutex); // If rc is not SQLITE_OK at this point, then either the malloc subsystem could not be initialized or the system failed to allocate // the pInitMutex mutex. Return an error in either case. if (rc != RC.OK) { return(rc); } // Do the rest of the initialization under the recursive mutex so that we will be able to handle recursive calls into // sqlite3_initialize(). The recursive calls normally come through sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other // recursive calls might also be possible. // // IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls to the xInit method, so the xInit method need not be threadsafe. // // The following mutex is what serializes access to the appdef pcache xInit methods. The sqlite3_pcache_methods.xInit() all is embedded in the // call to sqlite3PcacheInitialize(). MutexEx.Enter(_GlobalStatics.InitMutex); if (!_GlobalStatics.IsInit && !_GlobalStatics.InProgress) { _GlobalStatics.InProgress = true; rc = VSystem.Initialize(); } if (rc != RC.OK) { MutexEx.Leave(_GlobalStatics.InitMutex); } return(rc); }