Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
243 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (C) 2018 Yasuhiro Matsumoto <mattn.jp@gmail.com>. | ||
// | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY | ||
#include <sqlite3-binding.h> | ||
|
||
extern int unlock_notify_wait(sqlite3 *db); | ||
|
||
void _unlock_notify_callback(void *arg, int argc) | ||
{ | ||
extern void unlock_notify_callback(void *, int); | ||
unlock_notify_callback(arg, argc); | ||
} | ||
|
||
int | ||
sqlite3_step_blocking(sqlite3_stmt *stmt) | ||
{ | ||
int rv; | ||
sqlite3* db = sqlite3_db_handle(stmt); | ||
|
||
for (;;) { | ||
rv = sqlite3_step(stmt); | ||
if (rv != SQLITE_LOCKED) { | ||
break; | ||
} | ||
if (sqlite3_extended_errcode(db) != SQLITE_LOCKED_SHAREDCACHE) { | ||
break; | ||
} | ||
rv = unlock_notify_wait(db); | ||
if (rv != SQLITE_OK) { | ||
break; | ||
} | ||
sqlite3_reset(stmt); | ||
} | ||
|
||
return rv; | ||
} | ||
|
||
int | ||
_sqlite3_step_blocking(sqlite3_stmt* stmt, long long* rowid, long long* changes) | ||
{ | ||
int rv; | ||
sqlite3* db; | ||
|
||
for (;;) { | ||
rv = sqlite3_step(stmt); | ||
if (rv!=SQLITE_LOCKED) { | ||
break; | ||
} | ||
if (sqlite3_extended_errcode(db) != SQLITE_LOCKED_SHAREDCACHE) { | ||
break; | ||
} | ||
rv = unlock_notify_wait(db); | ||
if (rv != SQLITE_OK) { | ||
break; | ||
} | ||
sqlite3_reset(stmt); | ||
} | ||
|
||
db = sqlite3_db_handle(stmt); | ||
*rowid = (long long) sqlite3_last_insert_rowid(db); | ||
*changes = (long long) sqlite3_changes(db); | ||
return rv; | ||
} | ||
|
||
int | ||
sqlite3_prepare_v2_blocking(sqlite3 *db, const char *zSql, int nBytes, sqlite3_stmt **ppStmt, const char **pzTail) | ||
{ | ||
int rv; | ||
|
||
for (;;) { | ||
rv = sqlite3_prepare_v2(db, zSql, nBytes, ppStmt, pzTail); | ||
if (rv!=SQLITE_LOCKED) { | ||
break; | ||
} | ||
if (sqlite3_extended_errcode(db) != SQLITE_LOCKED_SHAREDCACHE) { | ||
break; | ||
} | ||
rv = unlock_notify_wait(db); | ||
if (rv != SQLITE_OK) { | ||
break; | ||
} | ||
} | ||
|
||
return rv; | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (C) 2018 Yasuhiro Matsumoto <mattn.jp@gmail.com>. | ||
// | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build cgo | ||
// +build sqlite_unlock_notify | ||
|
||
package sqlite3 | ||
|
||
/* | ||
#cgo CFLAGS: -DSQLITE_ENABLE_UNLOCK_NOTIFY | ||
#include <sqlite3-binding.h> | ||
extern void _unlock_notify_callback(void *arg, int argc); | ||
*/ | ||
import "C" | ||
import ( | ||
"sync" | ||
"unsafe" | ||
) | ||
|
||
type unlockNotification struct { | ||
notify chan struct{} | ||
lock sync.Mutex | ||
} | ||
|
||
//export unlock_notify_callback | ||
func unlock_notify_callback(pargv unsafe.Pointer, argc C.int) { | ||
argv := *(*uintptr)(pargv) | ||
v := (*[1 << 30]uintptr)(unsafe.Pointer(argv)) | ||
for i := 0; i < int(argc); i++ { | ||
un := lookupHandle(v[i]).(unlockNotification) | ||
un.notify <- struct{}{} | ||
} | ||
} | ||
|
||
var notifyMutex sync.Mutex | ||
|
||
//export unlock_notify_wait | ||
func unlock_notify_wait(db *C.sqlite3) C.int { | ||
var un unlockNotification | ||
un.notify = make(chan struct{}) | ||
defer close(un.notify) | ||
|
||
argv := [1]uintptr{newHandle(nil, un)} | ||
if rv := C.sqlite3_unlock_notify(db, (*[0]byte)(C._unlock_notify_callback), unsafe.Pointer(&argv)); rv != C.SQLITE_OK { | ||
return rv | ||
} | ||
<-un.notify | ||
return C.SQLITE_OK | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters