// Code generated by modernc.org/undup from the per-target sqlite_*.go files; DO NOT EDIT.

//go:build (linux && amd64) || (linux && arm64) || (linux && loong64) || (linux && ppc64le) || (linux && riscv64) || (linux && s390x)

package sqlite3

import (
	"unsafe"

	"modernc.org/libc"
)

const F_GETLK = 5

const F_GETLK64 = 5

const F_SETLK = 6

const F_SETLK64 = 6

const F_SETLKW = 7

const F_SETLKW64 = 7

const SIOCGSTAMP = 35078

const SIOCGSTAMPNS = 35079

type Tcpu_set_t = struct {
	F__bits [16]uint64
}

type Tfd_set = struct {
	Ffds_bits [16]uint64
}

type Tpthread_attr_t = struct {
	F__u struct {
		F__vi [0][14]int32
		F__s  [0][7]uint64
		F__i  [14]int32
	}
}

type Tpthread_barrier_t = struct {
	F__u struct {
		F__vi [0][8]int32
		F__p  [0][4]uintptr
		F__i  [8]int32
	}
}

type Tpthread_cond_t = struct {
	F__u struct {
		F__vi [0][12]int32
		F__p  [0][6]uintptr
		F__i  [12]int32
	}
}

type Tpthread_mutex_t = struct {
	F__u struct {
		F__vi [0][10]int32
		F__p  [0][5]uintptr
		F__i  [10]int32
	}
}

type Tpthread_rwlock_t = struct {
	F__u struct {
		F__vi [0][14]int32
		F__p  [0][7]uintptr
		F__i  [14]int32
	}
}

type Tsched_param = struct {
	Fsched_priority int32
	F__reserved1    int32
	F__reserved2    [2]struct {
		F__reserved1 Ttime_t
		F__reserved2 int64
	}
	F__reserved3 int32
}

type Tsigset_t = struct {
	F__bits [16]uint64
}

// C documentation
//
//	/*
//	** Initialize SQLite.
//	**
//	** This routine must be called to initialize the memory allocation,
//	** VFS, and mutex subsystems prior to doing any serious work with
//	** SQLite.  But as long as you do not compile with SQLITE_OMIT_AUTOINIT
//	** this routine will be called automatically by key routines such as
//	** sqlite3_open().
//	**
//	** This routine is a no-op except on its very first call for the process,
//	** or for the first call after a call to sqlite3_shutdown.
//	**
//	** The first thread to call this routine runs the initialization to
//	** completion.  If subsequent threads call this routine before the first
//	** thread has finished the initialization process, then the subsequent
//	** threads must block until the first thread finishes with the initialization.
//	**
//	** The first thread might call this routine recursively.  Recursive
//	** calls to this routine should not block, of course.  Otherwise the
//	** initialization process would never complete.
//	**
//	** Let X be the first thread to enter this routine.  Let Y be some other
//	** thread.  Then while the initial invocation of this routine by X is
//	** incomplete, it is required that:
//	**
//	**    *  Calls to this routine from Y must block until the outer-most
//	**       call by X completes.
//	**
//	**    *  Recursive calls to this routine from thread X return immediately
//	**       without blocking.
//	*/
func Xsqlite3_initialize(tls *libc.TLS) (r int32) {
	var pMainMtx uintptr
	var rc int32
	_, _ = pMainMtx, rc /* Result code */
	/* If the following assert() fails on some obscure processor/compiler
	 ** combination, the work-around is to set the correct pointer
	 ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
	/* 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 libc.AtomicLoadPInt32(uintptr(unsafe.Pointer(&_sqlite3Config))+340) != 0 {
		_sqlite3MemoryBarrier(tls)
		return SQLITE_OK
	}
	/* 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 = _sqlite3MutexInit(tls)
	if rc != 0 {
		return rc
	}
	/* Initialize the malloc() system and the recursive pInitMutex mutex.
	 ** This operation is protected by the STATIC_MAIN 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.
	 */
	pMainMtx = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_MAIN))
	Xsqlite3_mutex_enter(tls, pMainMtx)
	_sqlite3Config.FisMutexInit = int32(1)
	if !(_sqlite3Config.FisMallocInit != 0) {
		rc = _sqlite3MallocInit(tls)
	}
	if rc == SQLITE_OK {
		_sqlite3Config.FisMallocInit = int32(1)
		if !(_sqlite3Config.FpInitMutex != 0) {
			_sqlite3Config.FpInitMutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_RECURSIVE))
			if _sqlite3Config.FbCoreMutex != 0 && !(_sqlite3Config.FpInitMutex != 0) {
				rc = int32(SQLITE_NOMEM)
			}
		}
	}
	if rc == SQLITE_OK {
		_sqlite3Config.FnRefInitMutex = _sqlite3Config.FnRefInitMutex + 1
	}
	Xsqlite3_mutex_leave(tls, pMainMtx)
	/* 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 != SQLITE_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().
	 */
	Xsqlite3_mutex_enter(tls, _sqlite3Config.FpInitMutex)
	if libc.AtomicLoadPInt32(uintptr(unsafe.Pointer(&_sqlite3Config))+340) == 0 && _sqlite3Config.FinProgress == 0 {
		_sqlite3Config.FinProgress = int32(1)
		libc.Xmemset(tls, uintptr(unsafe.Pointer(&_sqlite3BuiltinFunctions)), 0, uint64(184))
		_sqlite3RegisterBuiltinFunctions(tls)
		if _sqlite3Config.FisPCacheInit == 0 {
			rc = _sqlite3PcacheInitialize(tls)
		}
		if rc == SQLITE_OK {
			_sqlite3Config.FisPCacheInit = int32(1)
			rc = _sqlite3OsInit(tls)
		}
		if rc == SQLITE_OK {
			rc = _sqlite3MemdbInit(tls)
		}
		if rc == SQLITE_OK {
			_sqlite3PCacheBufferSetup(tls, _sqlite3Config.FpPage, _sqlite3Config.FszPage, _sqlite3Config.FnPage)
		}
		if rc == SQLITE_OK {
			_sqlite3MemoryBarrier(tls)
			libc.AtomicStorePInt32(uintptr(unsafe.Pointer(&_sqlite3Config))+340, int32(1))
		}
		_sqlite3Config.FinProgress = 0
	}
	Xsqlite3_mutex_leave(tls, _sqlite3Config.FpInitMutex)
	/* Go back under the static mutex and clean up the recursive
	 ** mutex to prevent a resource leak.
	 */
	Xsqlite3_mutex_enter(tls, pMainMtx)
	_sqlite3Config.FnRefInitMutex = _sqlite3Config.FnRefInitMutex - 1
	if _sqlite3Config.FnRefInitMutex <= 0 {
		Xsqlite3_mutex_free(tls, _sqlite3Config.FpInitMutex)
		_sqlite3Config.FpInitMutex = uintptr(0)
	}
	Xsqlite3_mutex_leave(tls, pMainMtx)
	/* 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.
	 */
	/* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
	 ** compile-time option.
	 */
	return rc
}

const __INT_FAST16_MAX__ = 9223372036854775807

const __INT_FAST16_WIDTH__ = 64

const __INT_FAST32_MAX__ = 9223372036854775807

const __INT_FAST32_WIDTH__ = 64

const __UINT_FAST16_MAX__ = 18446744073709551615

const __UINT_FAST32_MAX__ = 18446744073709551615

// C documentation
//
//	/*
//	** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
//	** must be either NO_LOCK or SHARED_LOCK.
//	**
//	** If the locking level of the file descriptor is already at or below
//	** the requested locking level, this routine is a no-op.
//	**
//	** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
//	** the byte range is divided into 2 parts and the first part is unlocked then
//	** set to a read lock, then the other part is simply unlocked.  This works
//	** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
//	** remove the write lock on a region when a read lock is set.
//	*/
func _posixUnlock(tls *libc.TLS, id uintptr, eFileLock int32, handleNFSUnlock int32) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var pFile, pInode uintptr
	var rc int32
	var v1 Toff_t
	var _ /* lock at bp+0 */ Tflock
	_, _, _, _ = pFile, pInode, rc, v1
	pFile = id
	rc = SQLITE_OK
	if libc.Int32FromUint8((*TunixFile)(unsafe.Pointer(pFile)).FeFileLock) <= eFileLock {
		return SQLITE_OK
	}
	pInode = (*TunixFile)(unsafe.Pointer(pFile)).FpInode
	Xsqlite3_mutex_enter(tls, (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpLockMutex)
	if libc.Int32FromUint8((*TunixFile)(unsafe.Pointer(pFile)).FeFileLock) > int32(SHARED_LOCK) {
		/* downgrading to a shared lock on NFS involves clearing the write lock
		 ** before establishing the readlock - to avoid a race condition we downgrade
		 ** the lock in 2 blocks, so that part of the range will be covered by a
		 ** write lock until the rest is covered by a read lock:
		 **  1:   [WWWWW]
		 **  2:   [....W]
		 **  3:   [RRRRW]
		 **  4:   [RRRR.]
		 */
		if eFileLock == int32(SHARED_LOCK) {
			_ = handleNFSUnlock
			(**(**Tflock)(__ccgo_up(bp))).Fl_type = F_RDLCK
			(**(**Tflock)(__ccgo_up(bp))).Fl_whence = 0
			(**(**Tflock)(__ccgo_up(bp))).Fl_start = int64(_sqlite3PendingByte + libc.Int32FromInt32(2))
			(**(**Tflock)(__ccgo_up(bp))).Fl_len = int64(SHARED_SIZE)
			if _unixFileLock(tls, pFile, bp) != 0 {
				/* In theory, the call to unixFileLock() cannot fail because another
				 ** process is holding an incompatible lock. If it does, this
				 ** indicates that the other process is not following the locking
				 ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
				 ** SQLITE_BUSY would confuse the upper layer (in practice it causes
				 ** an assert to fail). */
				rc = libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(9)<<libc.Int32FromInt32(8)
				_storeLastErrno(tls, pFile, **(**int32)(__ccgo_up(libc.X__errno_location(tls))))
				goto end_unlock
			}
		}
		(**(**Tflock)(__ccgo_up(bp))).Fl_type = int16(F_UNLCK)
		(**(**Tflock)(__ccgo_up(bp))).Fl_whence = 0
		(**(**Tflock)(__ccgo_up(bp))).Fl_start = int64(_sqlite3PendingByte)
		(**(**Tflock)(__ccgo_up(bp))).Fl_len = int64(2)
		if _unixFileLock(tls, pFile, bp) == 0 {
			(*TunixInodeInfo)(unsafe.Pointer(pInode)).FeFileLock = uint8(SHARED_LOCK)
		} else {
			rc = libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(8)<<libc.Int32FromInt32(8)
			_storeLastErrno(tls, pFile, **(**int32)(__ccgo_up(libc.X__errno_location(tls))))
			goto end_unlock
		}
	}
	if eFileLock == NO_LOCK {
		/* Decrement the shared lock counter.  Release the lock using an
		 ** OS call only when all threads in this same process have released
		 ** the lock.
		 */
		(*TunixInodeInfo)(unsafe.Pointer(pInode)).FnShared = (*TunixInodeInfo)(unsafe.Pointer(pInode)).FnShared - 1
		if (*TunixInodeInfo)(unsafe.Pointer(pInode)).FnShared == 0 {
			(**(**Tflock)(__ccgo_up(bp))).Fl_type = int16(F_UNLCK)
			(**(**Tflock)(__ccgo_up(bp))).Fl_whence = 0
			v1 = libc.Int64FromInt64(0)
			(**(**Tflock)(__ccgo_up(bp))).Fl_len = v1
			(**(**Tflock)(__ccgo_up(bp))).Fl_start = v1
			if _unixFileLock(tls, pFile, bp) == 0 {
				(*TunixInodeInfo)(unsafe.Pointer(pInode)).FeFileLock = uint8(NO_LOCK)
			} else {
				rc = libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(8)<<libc.Int32FromInt32(8)
				_storeLastErrno(tls, pFile, **(**int32)(__ccgo_up(libc.X__errno_location(tls))))
				(*TunixInodeInfo)(unsafe.Pointer(pInode)).FeFileLock = uint8(NO_LOCK)
				(*TunixFile)(unsafe.Pointer(pFile)).FeFileLock = uint8(NO_LOCK)
			}
		}
		/* Decrement the count of locks against this same file.  When the
		 ** count reaches zero, close any other file descriptors whose close
		 ** was deferred because of outstanding locks.
		 */
		(*TunixInodeInfo)(unsafe.Pointer(pInode)).FnLock = (*TunixInodeInfo)(unsafe.Pointer(pInode)).FnLock - 1
		if (*TunixInodeInfo)(unsafe.Pointer(pInode)).FnLock == 0 {
			_closePendingFds(tls, pFile)
		}
	}
	goto end_unlock
end_unlock:
	;
	Xsqlite3_mutex_leave(tls, (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpLockMutex)
	if rc == SQLITE_OK {
		(*TunixFile)(unsafe.Pointer(pFile)).FeFileLock = libc.Uint8FromInt32(eFileLock)
	}
	return rc
}

// C documentation
//
//	/*
//	** The sqlite3_mutex_alloc() routine allocates a new
//	** mutex and returns a pointer to it.  If it returns NULL
//	** that means that a mutex could not be allocated.  SQLite
//	** will unwind its stack and return an error.  The argument
//	** to sqlite3_mutex_alloc() is one of these integer constants:
//	**
//	** <ul>
//	** <li>  SQLITE_MUTEX_FAST
//	** <li>  SQLITE_MUTEX_RECURSIVE
//	** <li>  SQLITE_MUTEX_STATIC_MAIN
//	** <li>  SQLITE_MUTEX_STATIC_MEM
//	** <li>  SQLITE_MUTEX_STATIC_OPEN
//	** <li>  SQLITE_MUTEX_STATIC_PRNG
//	** <li>  SQLITE_MUTEX_STATIC_LRU
//	** <li>  SQLITE_MUTEX_STATIC_PMEM
//	** <li>  SQLITE_MUTEX_STATIC_APP1
//	** <li>  SQLITE_MUTEX_STATIC_APP2
//	** <li>  SQLITE_MUTEX_STATIC_APP3
//	** <li>  SQLITE_MUTEX_STATIC_VFS1
//	** <li>  SQLITE_MUTEX_STATIC_VFS2
//	** <li>  SQLITE_MUTEX_STATIC_VFS3
//	** </ul>
//	**
//	** The first two constants cause sqlite3_mutex_alloc() to create
//	** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
//	** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
//	** The mutex implementation does not need to make a distinction
//	** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
//	** not want to.  But SQLite will only request a recursive mutex in
//	** cases where it really needs one.  If a faster non-recursive mutex
//	** implementation is available on the host platform, the mutex subsystem
//	** might return such a mutex in response to SQLITE_MUTEX_FAST.
//	**
//	** The other allowed parameters to sqlite3_mutex_alloc() each return
//	** a pointer to a static preexisting mutex.  Six static mutexes are
//	** used by the current version of SQLite.  Future versions of SQLite
//	** may add additional static mutexes.  Static mutexes are for internal
//	** use by SQLite only.  Applications that use SQLite mutexes should
//	** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
//	** SQLITE_MUTEX_RECURSIVE.
//	**
//	** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
//	** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
//	** returns a different mutex on every call.  But for the static
//	** mutex types, the same mutex is returned on every call that has
//	** the same type number.
//	*/
func _pthreadMutexAlloc(tls *libc.TLS, iType int32) (r uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var p uintptr
	var _ /* recursiveAttr at bp+0 */ Tpthread_mutexattr_t
	_ = p
	switch iType {
	case int32(SQLITE_MUTEX_RECURSIVE):
		p = _sqlite3MallocZero(tls, uint64(40))
		if p != 0 {
			libc.Xpthread_mutexattr_init(tls, bp)
			libc.Xpthread_mutexattr_settype(tls, bp, int32(PTHREAD_MUTEX_RECURSIVE))
			libc.Xpthread_mutex_init(tls, p, bp)
			libc.Xpthread_mutexattr_destroy(tls, bp)
		}
	case SQLITE_MUTEX_FAST:
		p = _sqlite3MallocZero(tls, uint64(40))
		if p != 0 {
			libc.Xpthread_mutex_init(tls, p, uintptr(0))
		}
	default:
		p = uintptr(unsafe.Pointer(&_staticMutexes)) + uintptr(iType-int32(2))*40
		break
	}
	return p
}

// C documentation
//
//	/*
//	** Seek to the offset passed as the second argument, then read cnt
//	** bytes into pBuf. Return the number of bytes actually read.
//	**
//	** To avoid stomping the errno value on a failed read the lastErrno value
//	** is set before returning.
//	*/
func _seekAndRead(tls *libc.TLS, id uintptr, offset Tsqlite3_int64, pBuf uintptr, cnt int32) (r int32) {
	var got, prior int32
	_, _ = got, prior
	prior = 0
	for cond := true; cond; cond = got > 0 {
		got = int32((*(*func(*libc.TLS, int32, uintptr, Tsize_t, Toff_t) Tssize_t)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(9)].FpCurrent})))(tls, (*TunixFile)(unsafe.Pointer(id)).Fh, pBuf, libc.Uint64FromInt32(cnt), offset))
		if got == cnt {
			break
		}
		if got < 0 {
			if **(**int32)(__ccgo_up(libc.X__errno_location(tls))) == int32(EINTR) {
				got = int32(1)
				continue
			}
			prior = 0
			_storeLastErrno(tls, id, **(**int32)(__ccgo_up(libc.X__errno_location(tls))))
			break
		} else {
			if got > 0 {
				cnt = cnt - got
				offset = offset + int64(got)
				prior = prior + got
				pBuf = uintptr(got) + pBuf
			}
		}
	}
	return got + prior
}

// C documentation
//
//	/*
//	** Attempt to seek the file-descriptor passed as the first argument to
//	** absolute offset iOff, then attempt to write nBuf bytes of data from
//	** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
//	** return the actual number of bytes written (which may be less than
//	** nBuf).
//	*/
func _seekAndWriteFd(tls *libc.TLS, fd int32, iOff Ti64, pBuf uintptr, nBuf int32, piErrno uintptr) (r int32) {
	var rc int32
	_ = rc
	rc = 0 /* Value returned by system call */
	nBuf = nBuf & int32(0x1ffff)
	for cond := true; cond; cond = rc < 0 && **(**int32)(__ccgo_up(libc.X__errno_location(tls))) == int32(EINTR) {
		rc = int32((*(*func(*libc.TLS, int32, uintptr, Tsize_t, Toff_t) Tssize_t)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(12)].FpCurrent})))(tls, fd, pBuf, libc.Uint64FromInt32(nBuf), iOff))
	}
	if rc < 0 {
		**(**int32)(__ccgo_up(piErrno)) = **(**int32)(__ccgo_up(libc.X__errno_location(tls)))
	}
	return rc
}

func _sqlite3MutexInit(tls *libc.TLS) (r int32) {
	mu.Lock()
	defer mu.Unlock()
	var pFrom, pTo uintptr
	var rc int32
	_, _, _ = pFrom, pTo, rc
	rc = SQLITE_OK
	if !(_sqlite3Config.Fmutex.FxMutexAlloc != 0) {
		pTo = uintptr(unsafe.Pointer(&_sqlite3Config)) + 96
		if _sqlite3Config.FbCoreMutex != 0 {
			pFrom = _sqlite3DefaultMutex(tls)
		} else {
			pFrom = _sqlite3NoopMutex(tls)
		}
		(*Tsqlite3_mutex_methods)(unsafe.Pointer(pTo)).FxMutexInit = (*Tsqlite3_mutex_methods)(unsafe.Pointer(pFrom)).FxMutexInit
		(*Tsqlite3_mutex_methods)(unsafe.Pointer(pTo)).FxMutexEnd = (*Tsqlite3_mutex_methods)(unsafe.Pointer(pFrom)).FxMutexEnd
		(*Tsqlite3_mutex_methods)(unsafe.Pointer(pTo)).FxMutexFree = (*Tsqlite3_mutex_methods)(unsafe.Pointer(pFrom)).FxMutexFree
		(*Tsqlite3_mutex_methods)(unsafe.Pointer(pTo)).FxMutexEnter = (*Tsqlite3_mutex_methods)(unsafe.Pointer(pFrom)).FxMutexEnter
		(*Tsqlite3_mutex_methods)(unsafe.Pointer(pTo)).FxMutexTry = (*Tsqlite3_mutex_methods)(unsafe.Pointer(pFrom)).FxMutexTry
		(*Tsqlite3_mutex_methods)(unsafe.Pointer(pTo)).FxMutexLeave = (*Tsqlite3_mutex_methods)(unsafe.Pointer(pFrom)).FxMutexLeave
		(*Tsqlite3_mutex_methods)(unsafe.Pointer(pTo)).FxMutexHeld = (*Tsqlite3_mutex_methods)(unsafe.Pointer(pFrom)).FxMutexHeld
		(*Tsqlite3_mutex_methods)(unsafe.Pointer(pTo)).FxMutexNotheld = (*Tsqlite3_mutex_methods)(unsafe.Pointer(pFrom)).FxMutexNotheld
		_sqlite3MemoryBarrier(tls)
		(*Tsqlite3_mutex_methods)(unsafe.Pointer(pTo)).FxMutexAlloc = (*Tsqlite3_mutex_methods)(unsafe.Pointer(pFrom)).FxMutexAlloc
	}
	rc = (*(*func(*libc.TLS) int32)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fmutex.FxMutexInit})))(tls)
	_sqlite3MemoryBarrier(tls)
	return rc
}

// C documentation
//
//	/* Create a new thread */
func _sqlite3ThreadCreate(tls *libc.TLS, ppThread uintptr, __ccgo_fp_xTask uintptr, pIn uintptr) (r int32) {
	var p uintptr
	var rc int32
	_, _ = p, rc
	/* This routine is never used in single-threaded mode */
	**(**uintptr)(__ccgo_up(ppThread)) = uintptr(0)
	p = _sqlite3Malloc(tls, uint64(40))
	if p == uintptr(0) {
		return int32(SQLITE_NOMEM)
	}
	libc.Xmemset(tls, p, 0, uint64(40))
	(*TSQLiteThread)(unsafe.Pointer(p)).FxTask = __ccgo_fp_xTask
	(*TSQLiteThread)(unsafe.Pointer(p)).FpIn = pIn
	/* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
	 ** function that returns SQLITE_ERROR when passed the argument 200, that
	 ** forces worker threads to run sequentially and deterministically
	 ** for testing purposes. */
	if _sqlite3FaultSim(tls, int32(200)) != 0 {
		rc = int32(1)
	} else {
		rc = libc.Xpthread_create(tls, p, uintptr(0), __ccgo_fp_xTask, pIn)
	}
	if rc != 0 {
		(*TSQLiteThread)(unsafe.Pointer(p)).Fdone = int32(1)
		(*TSQLiteThread)(unsafe.Pointer(p)).FpOut = (*(*func(*libc.TLS, uintptr) uintptr)(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xTask})))(tls, pIn)
	}
	**(**uintptr)(__ccgo_up(ppThread)) = p
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Use F_GETLK to check whether or not there are any readers with open
//	** wal-mode transactions in other processes on database file pFile. If
//	** no error occurs, return SQLITE_OK and set (*piOut) to 1 if there are
//	** such transactions, or 0 otherwise. If an error occurs, return an
//	** SQLite error code. The final value of *piOut is undefined in this
//	** case.
//	*/
func _unixFcntlExternalReader(tls *libc.TLS, pFile uintptr, piOut uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var pShmNode uintptr
	var rc int32
	var _ /* f at bp+0 */ Tflock
	_, _ = pShmNode, rc
	rc = SQLITE_OK
	**(**int32)(__ccgo_up(piOut)) = 0
	if (*TunixFile)(unsafe.Pointer(pFile)).FpShm != 0 {
		pShmNode = (*TunixShm)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(pFile)).FpShm)).FpShmNode
		libc.Xmemset(tls, bp, 0, uint64(32))
		(**(**Tflock)(__ccgo_up(bp))).Fl_type = int16(F_WRLCK)
		(**(**Tflock)(__ccgo_up(bp))).Fl_whence = 0
		(**(**Tflock)(__ccgo_up(bp))).Fl_start = int64((libc.Int32FromInt32(22)+libc.Int32FromInt32(SQLITE_SHM_NLOCK))*libc.Int32FromInt32(4) + libc.Int32FromInt32(3))
		(**(**Tflock)(__ccgo_up(bp))).Fl_len = int64(libc.Int32FromInt32(SQLITE_SHM_NLOCK) - libc.Int32FromInt32(3))
		Xsqlite3_mutex_enter(tls, (*TunixShmNode)(unsafe.Pointer(pShmNode)).FpShmMutex)
		if (*(*func(*libc.TLS, int32, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(7)].FpCurrent})))(tls, (*TunixShmNode)(unsafe.Pointer(pShmNode)).FhShm, int32(F_GETLK), libc.VaList(bp+40, bp)) < 0 {
			rc = libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(15)<<libc.Int32FromInt32(8)
		} else {
			**(**int32)(__ccgo_up(piOut)) = libc.BoolInt32(int32((**(**Tflock)(__ccgo_up(bp))).Fl_type) != int32(F_UNLCK))
		}
		Xsqlite3_mutex_leave(tls, (*TunixShmNode)(unsafe.Pointer(pShmNode)).FpShmMutex)
	}
	return rc
}

// C documentation
//
//	/*
//	** If pFile has a -shm file open and it is sharing that file with some
//	** other connection, either in the same process or in a separate process,
//	** then return true.  Return false if either pFile does not have a -shm
//	** file open or if it is the only connection to that -shm file across the
//	** entire system.
//	**
//	** This routine is not required for correct operation.  It can always return
//	** false and SQLite will continue to operate according to spec.  However,
//	** when this routine does its job, it adds extra robustness in cases
//	** where database file locks have been erroneously deleted in a WAL-mode
//	** database by doing close(open(DATABASE_PATHNAME)) or similar.
//	**
//	** With false negatives, SQLite still operates to spec, though with less
//	** robustness.  With false positives, the last database connection on a
//	** WAL-mode database will fail to unlink the -wal and -shm files, which
//	** is annoying but harmless.  False positives will also prevent a database
//	** connection from running "PRAGMA journal_mode=DELETE" in order to take
//	** the database out of WAL mode, which is perhaps more serious, but is
//	** still not a disaster.
//	*/
func _unixIsSharingShmNode(tls *libc.TLS, pFile uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var pShmNode uintptr
	var _ /* lock at bp+0 */ Tflock
	_ = pShmNode
	if (*TunixFile)(unsafe.Pointer(pFile)).FpShm == uintptr(0) {
		return 0
	}
	if libc.Int32FromUint16((*TunixFile)(unsafe.Pointer(pFile)).FctrlFlags)&int32(UNIXFILE_EXCL) != 0 {
		return 0
	}
	pShmNode = (*TunixShm)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(pFile)).FpShm)).FpShmNode
	libc.Xmemset(tls, bp, 0, uint64(32))
	(**(**Tflock)(__ccgo_up(bp))).Fl_whence = 0
	(**(**Tflock)(__ccgo_up(bp))).Fl_start = int64((libc.Int32FromInt32(22)+libc.Int32FromInt32(SQLITE_SHM_NLOCK))*libc.Int32FromInt32(4) + libc.Int32FromInt32(SQLITE_SHM_NLOCK))
	(**(**Tflock)(__ccgo_up(bp))).Fl_len = int64(1)
	(**(**Tflock)(__ccgo_up(bp))).Fl_type = int16(F_WRLCK)
	(*(*func(*libc.TLS, int32, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(7)].FpCurrent})))(tls, (*TunixShmNode)(unsafe.Pointer(pShmNode)).FhShm, int32(F_GETLK), libc.VaList(bp+40, bp))
	return libc.BoolInt32(int32((**(**Tflock)(__ccgo_up(bp))).Fl_type) != int32(F_UNLCK))
}

// C documentation
//
//	/*
//	** Change the lock state for a shared-memory segment.
//	**
//	** Note that the relationship between SHARED and EXCLUSIVE locks is a little
//	** different here than in posix.  In xShmLock(), one can go from unlocked
//	** to shared and back or from unlocked to exclusive and back.  But one may
//	** not go from shared to exclusive or from exclusive to shared.
//	*/
func _unixShmLock(tls *libc.TLS, fd uintptr, ofst int32, n int32, flags int32) (r int32) {
	var aLock, p, pDbFd, pShmNode, v1 uintptr
	var bUnlock, ii, rc int32
	var mask Tu16
	_, _, _, _, _, _, _, _, _ = aLock, bUnlock, ii, mask, p, pDbFd, pShmNode, rc, v1
	pDbFd = fd     /* The underlying file iNode */
	rc = SQLITE_OK /* Result code */
	mask = libc.Uint16FromInt32(int32(1)<<(ofst+n) - int32(1)<<ofst)
	p = (*TunixFile)(unsafe.Pointer(pDbFd)).FpShm
	if p == uintptr(0) {
		return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(20)<<libc.Int32FromInt32(8)
	}
	pShmNode = (*TunixShm)(unsafe.Pointer(p)).FpShmNode
	if pShmNode == uintptr(0) {
		return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(20)<<libc.Int32FromInt32(8)
	}
	aLock = pShmNode + 64
	/* Check that, if this to be a blocking lock, no locks that occur later
	 ** in the following list than the lock being obtained are already held:
	 **
	 **   1. Recovery lock (ofst==2).
	 **   2. Checkpointer lock (ofst==1).
	 **   3. Write lock (ofst==0).
	 **   4. Read locks (ofst>=3 && ofst<SQLITE_SHM_NLOCK).
	 **
	 ** In other words, if this is a blocking lock, none of the locks that
	 ** occur later in the above list than the lock being obtained may be
	 ** held.
	 */
	/* Check if there is any work to do. There are three cases:
	 **
	 **    a) An unlock operation where there are locks to unlock,
	 **    b) An shared lock where the requested lock is not already held
	 **    c) An exclusive lock where the requested lock is not already held
	 **
	 ** The SQLite core never requests an exclusive lock that it already holds.
	 ** This is assert()ed below.
	 */
	if flags&int32(SQLITE_SHM_UNLOCK) != 0 && (libc.Int32FromUint16((*TunixShm)(unsafe.Pointer(p)).FexclMask)|libc.Int32FromUint16((*TunixShm)(unsafe.Pointer(p)).FsharedMask))&libc.Int32FromUint16(mask) != 0 || flags == libc.Int32FromInt32(SQLITE_SHM_SHARED)|libc.Int32FromInt32(SQLITE_SHM_LOCK) && 0 == libc.Int32FromUint16((*TunixShm)(unsafe.Pointer(p)).FsharedMask)&libc.Int32FromUint16(mask) || flags == libc.Int32FromInt32(SQLITE_SHM_EXCLUSIVE)|libc.Int32FromInt32(SQLITE_SHM_LOCK) {
		/* Take the required mutexes. In SETLK_TIMEOUT mode (blocking locks), if
		 ** this is an attempt on an exclusive lock use sqlite3_mutex_try(). If any
		 ** other thread is holding this mutex, then it is either holding or about
		 ** to hold a lock exclusive to the one being requested, and we may
		 ** therefore return SQLITE_BUSY to the caller.
		 **
		 ** Doing this prevents some deadlock scenarios. For example, thread 1 may
		 ** be a checkpointer blocked waiting on the WRITER lock. And thread 2
		 ** may be a normal SQL client upgrading to a write transaction. In this
		 ** case thread 2 does a non-blocking request for the WRITER lock. But -
		 ** if it were to use sqlite3_mutex_enter() then it would effectively
		 ** become a (doomed) blocking request, as thread 2 would block until thread
		 ** 1 obtained WRITER and released the mutex. Since thread 2 already holds
		 ** a lock on a read-locking slot at this point, this breaks the
		 ** anti-deadlock rules (see above).  */
		Xsqlite3_mutex_enter(tls, (*TunixShmNode)(unsafe.Pointer(pShmNode)).FpShmMutex)
		if rc == SQLITE_OK {
			if flags&int32(SQLITE_SHM_UNLOCK) != 0 {
				/* Case (a) - unlock.  */
				bUnlock = int32(1)
				/* If this is a SHARED lock being unlocked, it is possible that other
				 ** clients within this process are holding the same SHARED lock. In
				 ** this case, set bUnlock to 0 so that the posix lock is not removed
				 ** from the file-descriptor below.  */
				if flags&int32(SQLITE_SHM_SHARED) != 0 {
					if **(**int32)(__ccgo_up(aLock + uintptr(ofst)*4)) > int32(1) {
						bUnlock = 0
						**(**int32)(__ccgo_up(aLock + uintptr(ofst)*4)) = **(**int32)(__ccgo_up(aLock + uintptr(ofst)*4)) - 1
						v1 = p + 18
						*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^libc.Int32FromUint16(mask))
					}
				}
				if bUnlock != 0 {
					rc = _unixShmSystemLock(tls, pDbFd, int32(F_UNLCK), ofst+(libc.Int32FromInt32(22)+libc.Int32FromInt32(SQLITE_SHM_NLOCK))*libc.Int32FromInt32(4), n)
					if rc == SQLITE_OK {
						libc.Xmemset(tls, aLock+uintptr(ofst)*4, 0, uint64(4)*libc.Uint64FromInt32(n))
						v1 = p + 18
						*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^libc.Int32FromUint16(mask))
						v1 = p + 20
						*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^libc.Int32FromUint16(mask))
					}
				}
			} else {
				if flags&int32(SQLITE_SHM_SHARED) != 0 {
					/* Case (b) - a shared lock.  */
					if **(**int32)(__ccgo_up(aLock + uintptr(ofst)*4)) < 0 {
						/* An exclusive lock is held by some other connection. BUSY. */
						rc = int32(SQLITE_BUSY)
					} else {
						if **(**int32)(__ccgo_up(aLock + uintptr(ofst)*4)) == 0 {
							rc = _unixShmSystemLock(tls, pDbFd, F_RDLCK, ofst+(libc.Int32FromInt32(22)+libc.Int32FromInt32(SQLITE_SHM_NLOCK))*libc.Int32FromInt32(4), n)
						}
					}
					/* Get the local shared locks */
					if rc == SQLITE_OK {
						v1 = p + 18
						*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromUint16(mask))
						**(**int32)(__ccgo_up(aLock + uintptr(ofst)*4)) = **(**int32)(__ccgo_up(aLock + uintptr(ofst)*4)) + 1
					}
				} else {
					/* Make sure no sibling connections hold locks that will block this
					 ** lock.  If any do, return SQLITE_BUSY right away.  */
					ii = ofst
					for {
						if !(ii < ofst+n) {
							break
						}
						if **(**int32)(__ccgo_up(aLock + uintptr(ii)*4)) != 0 {
							rc = int32(SQLITE_BUSY)
							break
						}
						goto _5
					_5:
						;
						ii = ii + 1
					}
					/* Get the exclusive locks at the system level. Then if successful
					 ** also update the in-memory values. */
					if rc == SQLITE_OK {
						rc = _unixShmSystemLock(tls, pDbFd, int32(F_WRLCK), ofst+(libc.Int32FromInt32(22)+libc.Int32FromInt32(SQLITE_SHM_NLOCK))*libc.Int32FromInt32(4), n)
						if rc == SQLITE_OK {
							v1 = p + 20
							*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromUint16(mask))
							ii = ofst
							for {
								if !(ii < ofst+n) {
									break
								}
								**(**int32)(__ccgo_up(aLock + uintptr(ii)*4)) = -int32(1)
								goto _7
							_7:
								;
								ii = ii + 1
							}
						}
					}
				}
			}
		}
		/* Drop the mutexes acquired above. */
		Xsqlite3_mutex_leave(tls, (*TunixShmNode)(unsafe.Pointer(pShmNode)).FpShmMutex)
	}
	return rc
}

func init() {
	p := unsafe.Pointer(&_aSyscall)
	*(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_posixOpen)
	*(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(libc.Xclose)
	*(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(libc.Xaccess)
	*(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(libc.Xgetcwd)
	*(*uintptr)(unsafe.Add(p, 104)) = __ccgo_fp(libc.Xstat)
	*(*uintptr)(unsafe.Add(p, 128)) = __ccgo_fp(libc.Xfstat)
	*(*uintptr)(unsafe.Add(p, 152)) = __ccgo_fp(libc.Xftruncate)
	*(*uintptr)(unsafe.Add(p, 176)) = __ccgo_fp(libc.Xfcntl)
	*(*uintptr)(unsafe.Add(p, 200)) = __ccgo_fp(libc.Xread)
	*(*uintptr)(unsafe.Add(p, 224)) = __ccgo_fp(libc.Xpread)
	*(*uintptr)(unsafe.Add(p, 272)) = __ccgo_fp(libc.Xwrite)
	*(*uintptr)(unsafe.Add(p, 296)) = __ccgo_fp(libc.Xpwrite)
	*(*uintptr)(unsafe.Add(p, 344)) = __ccgo_fp(libc.Xfchmod)
	*(*uintptr)(unsafe.Add(p, 392)) = __ccgo_fp(libc.Xunlink)
	*(*uintptr)(unsafe.Add(p, 416)) = __ccgo_fp(_openDirectory)
	*(*uintptr)(unsafe.Add(p, 440)) = __ccgo_fp(libc.Xmkdir)
	*(*uintptr)(unsafe.Add(p, 464)) = __ccgo_fp(libc.Xrmdir)
	*(*uintptr)(unsafe.Add(p, 488)) = __ccgo_fp(libc.Xfchown)
	*(*uintptr)(unsafe.Add(p, 512)) = __ccgo_fp(libc.Xgeteuid)
	*(*uintptr)(unsafe.Add(p, 536)) = __ccgo_fp(libc.Xmmap)
	*(*uintptr)(unsafe.Add(p, 560)) = __ccgo_fp(libc.Xmunmap)
	*(*uintptr)(unsafe.Add(p, 584)) = __ccgo_fp(libc.Xmremap)
	*(*uintptr)(unsafe.Add(p, 608)) = __ccgo_fp(_unixGetpagesize)
	*(*uintptr)(unsafe.Add(p, 632)) = __ccgo_fp(libc.Xreadlink)
	*(*uintptr)(unsafe.Add(p, 656)) = __ccgo_fp(libc.Xlstat)
}

/* End of the overrideable system calls */

func init() {
	p := unsafe.Pointer(&_sMutex1)
	*(*uintptr)(unsafe.Add(p, 0)) = __ccgo_fp(_pthreadMutexInit)
	*(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_pthreadMutexEnd)
	*(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_pthreadMutexAlloc)
	*(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_pthreadMutexFree)
	*(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_pthreadMutexEnter)
	*(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_pthreadMutexTry)
	*(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_pthreadMutexLeave)
}

/************** End of mutex_unix.c ******************************************/
/************** Begin file mutex_w32.c ***************************************/
/*
** 2007 August 14
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains the C functions that implement mutexes for Win32.
 */
/* #include "sqliteInt.h" */

/*
** The code in this file is only used if we are compiling multithreaded
** on a Win32 system.
 */

/************** End of mutex_w32.c *******************************************/
/************** Begin file malloc.c ******************************************/
/*
** 2001 September 15
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
**
** Memory allocation functions used throughout sqlite.
 */
/* #include "sqliteInt.h" */
/* #include <stdarg.h> */
