Add a mutex to mem6.c to make it threadsafe. (CVS 5468)

FossilOrigin-Name: 9b97ce60c63c8b8afa43496a97917afd5fc26c71
This commit is contained in:
danielk1977 2008-07-24 10:11:27 +00:00
parent 2d34081de9
commit 8969718379
4 changed files with 89 additions and 58 deletions

View File

@ -1,5 +1,5 @@
C Add\smem6.c,\sa\snew\sallocator.\sMore\sto\scome.\s(CVS\s5467) C Add\sa\smutex\sto\smem6.c\sto\smake\sit\sthreadsafe.\s(CVS\s5468)
D 2008-07-24T08:20:40 D 2008-07-24T10:11:28
F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
F Makefile.in 77ff156061bb870aa0a8b3d545c670d08070f7e6 F Makefile.in 77ff156061bb870aa0a8b3d545c670d08070f7e6
F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@ -123,7 +123,7 @@ F src/mem2.c 1e14a86d12dff279427cc52bb41ce5d80f138137
F src/mem3.c c73e935d0b900abc51d5fa45f880937b062f4a9f F src/mem3.c c73e935d0b900abc51d5fa45f880937b062f4a9f
F src/mem4.c 6703adb1717b26d9d70a1c2586b4b7b7ffee7909 F src/mem4.c 6703adb1717b26d9d70a1c2586b4b7b7ffee7909
F src/mem5.c 0b0ba1c2a02d86eb812dea6debacee841e3856f7 F src/mem5.c 0b0ba1c2a02d86eb812dea6debacee841e3856f7
F src/mem6.c c80dcbf6dbfe225282abcd9de2387dfca2c8515e F src/mem6.c af63943a8311ad87d12d9b4d4908f87b3f956773
F src/mutex.c a485a0eac8ee2cd95f66e565b4c6696c18db968f F src/mutex.c a485a0eac8ee2cd95f66e565b4c6696c18db968f
F src/mutex.h e52ffa1dfc6a6077e8b1823d2c2b7dfcbcf85594 F src/mutex.h e52ffa1dfc6a6077e8b1823d2c2b7dfcbcf85594
F src/mutex_os2.c 9c5637aa4c307c552566d0f0b3bd206245b54a97 F src/mutex_os2.c 9c5637aa4c307c552566d0f0b3bd206245b54a97
@ -436,7 +436,7 @@ F test/pager2.test c025f91b75fe65e85febda64d9416428b8a5cab5
F test/pager3.test 2323bf27fd5bd887b580247e5bce500ceee994b4 F test/pager3.test 2323bf27fd5bd887b580247e5bce500ceee994b4
F test/pageropt.test 6df72c441db0a037b7ec6990d16311c24fbea77b F test/pageropt.test 6df72c441db0a037b7ec6990d16311c24fbea77b
F test/pagesize.test e0a8b3fe80f8b8e808d94a00734c7a18c76c407e F test/pagesize.test e0a8b3fe80f8b8e808d94a00734c7a18c76c407e
F test/permutations.test 3ed44783c79005f82585e68955b393521cd12d9e F test/permutations.test d90e744f64f05e9cc99ebbb4ae588301f1aeaafc
F test/pragma.test 2c675ed9a288094ed62bf55b35fbc749e25670fb F test/pragma.test 2c675ed9a288094ed62bf55b35fbc749e25670fb
F test/pragma2.test 5364893491b9231dd170e3459bfc2e2342658b47 F test/pragma2.test 5364893491b9231dd170e3459bfc2e2342658b47
F test/printf.test 262a5acd3158f788e9bdf7f18d718f3af32ff6ef F test/printf.test 262a5acd3158f788e9bdf7f18d718f3af32ff6ef
@ -612,7 +612,7 @@ F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81
F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
F tool/speedtest8.c 1dbced29de5f59ba2ebf877edcadf171540374d1 F tool/speedtest8.c 1dbced29de5f59ba2ebf877edcadf171540374d1
F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
P 4a9dd5e782a363e93d4705fa3671bc6cf0bb5a33 P 192bc192185a7b475ef9331e2a4a0dc68083ec03
R 8b283cbfe876ed053eb7874d7f342505 R 5534db543abd237cf60349e1495c018f
U danielk1977 U danielk1977
Z 8afc40bb2e219ef5496de2d6671efc02 Z 781d9ebe3b9b2c802e3c883b54338895

View File

@ -1 +1 @@
192bc192185a7b475ef9331e2a4a0dc68083ec03 9b97ce60c63c8b8afa43496a97917afd5fc26c71

View File

@ -10,7 +10,7 @@
** **
************************************************************************* *************************************************************************
** **
** $Id: mem6.c,v 1.1 2008/07/24 08:20:40 danielk1977 Exp $ ** $Id: mem6.c,v 1.2 2008/07/24 10:11:28 danielk1977 Exp $
*/ */
#ifdef SQLITE_ENABLE_MEMSYS6 #ifdef SQLITE_ENABLE_MEMSYS6
@ -281,10 +281,21 @@ struct Mem6Global {
int nChunkSize; /* Size of each chunk, in bytes. */ int nChunkSize; /* Size of each chunk, in bytes. */
int nMinAlloc; /* Minimum allowed allocation size */ int nMinAlloc; /* Minimum allowed allocation size */
sqlite3_mutex *mutex;
/* This data structure will be fixed... */ /* This data structure will be fixed... */
Mem6Chunk *pChunk; /* Singly linked list of all memory chunks */ Mem6Chunk *pChunk; /* Singly linked list of all memory chunks */
} mem6; } mem6;
static void mem6Enter(void){
sqlite3_mutex_enter(mem6.mutex);
}
static void mem6Leave(void){
sqlite3_mutex_leave(mem6.mutex);
}
/* /*
** The argument is a pointer that may or may not have been allocated from ** The argument is a pointer that may or may not have been allocated from
** one of the Mem6Chunk objects managed within mem6. If it is, return ** one of the Mem6Chunk objects managed within mem6. If it is, return
@ -309,36 +320,47 @@ static void freeChunk(Mem6Chunk *pChunk){
static void *memsys6Malloc(int nByte){ static void *memsys6Malloc(int nByte){
Mem6Chunk *pChunk; Mem6Chunk *pChunk;
void *p; void *p = 0;
mem6Enter();
if( nByte>=mem6.nChunkSize/3 ){ if( nByte>=mem6.nChunkSize/3 ){
return mem6.parent.xMalloc(nByte); p = mem6.parent.xMalloc(nByte);
} }else{
for(pChunk=mem6.pChunk; pChunk; pChunk=pChunk->pNext){ for(pChunk=mem6.pChunk; !p && pChunk; pChunk=pChunk->pNext){
p = chunkMalloc(pChunk, nByte); p = chunkMalloc(pChunk, nByte);
if( p ){ }
return p;
if( !p ){
p = mem6.parent.xMalloc(mem6.nChunkSize);
if( p ){
pChunk = chunkInit((u8 *)p, mem6.nChunkSize, mem6.nMinAlloc);
pChunk->pNext = mem6.pChunk;
mem6.pChunk = pChunk;
p = chunkMalloc(pChunk, nByte);
assert(p);
}
} }
} }
mem6Leave();
p = mem6.parent.xMalloc(mem6.nChunkSize);
if( p ){
pChunk = chunkInit((u8 *)p, mem6.nChunkSize, mem6.nMinAlloc);
pChunk->pNext = mem6.pChunk;
mem6.pChunk = pChunk;
p = chunkMalloc(pChunk, nByte);
assert(p);
}
return p; return p;
} }
static int memsys6Size(void *p){ static int memsys6Size(void *p){
Mem6Chunk *pChunk = findChunk(p); Mem6Chunk *pChunk;
return (pChunk ? chunkSize(pChunk, p) : mem6.parent.xSize(p)); int iSize;
mem6Enter();
pChunk = findChunk(p);
iSize = (pChunk ? chunkSize(pChunk, p) : mem6.parent.xSize(p));
mem6Leave();
return iSize;
} }
static void memsys6Free(void *p){ static void memsys6Free(void *p){
Mem6Chunk *pChunk = findChunk(p); Mem6Chunk *pChunk;
mem6Enter();
pChunk = findChunk(p);
if( pChunk ){ if( pChunk ){
chunkFree(pChunk, p); chunkFree(pChunk, p);
if( chunkIsEmpty(pChunk) ){ if( chunkIsEmpty(pChunk) ){
@ -347,22 +369,22 @@ static void memsys6Free(void *p){
}else{ }else{
mem6.parent.xFree(p); mem6.parent.xFree(p);
} }
mem6Leave();
} }
static void *memsys6Realloc(void *p, int nByte){ static void *memsys6Realloc(void *p, int nByte){
Mem6Chunk *pChunk = findChunk(p);
void *p2; void *p2;
if( !pChunk ){ if( p && nByte<=memsys6Size(p) ){
return mem6.parent.xRealloc(p, nByte); p2 = p;
}else{
p2 = memsys6Malloc(nByte);
if( p && p2 ){
memcpy(p2, p, memsys6Size(p));
memsys6Free(p);
}
} }
p2 = memsys6Malloc(nByte);
if( p2 ){
assert( memsys6Size(p)<nByte );
memcpy(p2, p, memsys6Size(p));
memsys6Free(p);
}
return p2; return p2;
} }
@ -374,13 +396,24 @@ static int memsys6Roundup(int n){
} }
static int memsys6Init(void *pCtx){ static int memsys6Init(void *pCtx){
u8 bMemstat = sqlite3Config.bMemstat;
mem6.parent = *sqlite3MemGetDefault(); mem6.parent = *sqlite3MemGetDefault();
mem6.nChunkSize = (1<<16); mem6.nChunkSize = (1<<16);
mem6.nMinAlloc = 16; mem6.nMinAlloc = 16;
mem6.pChunk = 0; mem6.pChunk = 0;
if( !bMemstat ){
mem6.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
}
/* Initialize the parent allocator. */ /* Initialize the parent allocator. */
#ifdef SQLITE_MEMDEBUG
sqlite3Config.bMemstat = 1;
#endif
mem6.parent.xInit(mem6.parent.pAppData); mem6.parent.xInit(mem6.parent.pAppData);
#ifdef SQLITE_MEMDEBUG
sqlite3Config.bMemstat = bMemstat;
#endif
return SQLITE_OK; return SQLITE_OK;
} }

View File

@ -9,7 +9,7 @@
# #
#*********************************************************************** #***********************************************************************
# #
# $Id: permutations.test,v 1.14 2008/07/24 08:20:40 danielk1977 Exp $ # $Id: permutations.test,v 1.15 2008/07/24 10:11:29 danielk1977 Exp $
set testdir [file dirname $argv0] set testdir [file dirname $argv0]
source $testdir/tester.tcl source $testdir/tester.tcl
@ -439,26 +439,24 @@ ifcapable threadsafe {
} }
} }
run_tests "memsys6" -description { run_tests "memsys6" -description {
Run tests using the allocator in mem6.c. Run tests using the allocator in mem6.c.
} -include { } -initialize {
select1.test catch {db close}
} -initialize { sqlite3_reset_auto_extension
catch {db close} sqlite3_shutdown
sqlite3_reset_auto_extension sqlite3_config_chunkalloc
sqlite3_shutdown install_malloc_faultsim 1
sqlite3_config_chunkalloc sqlite3_initialize
install_malloc_faultsim 1 autoinstall_test_functions
sqlite3_initialize } -shutdown {
autoinstall_test_functions catch {db close}
} -shutdown { sqlite3_reset_auto_extension
catch {db close} sqlite3_shutdown
sqlite3_reset_auto_extension sqlite3_config_heap 0 0
sqlite3_shutdown install_malloc_faultsim 1
sqlite3_config_heap 0 0 sqlite3_initialize
install_malloc_faultsim 1 }
sqlite3_initialize
}
# run_tests "crash_safe_append" -description { # run_tests "crash_safe_append" -description {
# Run crash.test with persistent journals on a SAFE_APPEND file-system. # Run crash.test with persistent journals on a SAFE_APPEND file-system.