Make extra calls to sqlite3_shutdown() be harmless no-ops. (CVS 6520)

FossilOrigin-Name: d80822953c2d2f2fd7f6acdd3caa403c0decacc4
diff --git a/src/main.c b/src/main.c
index 6edaeee..a6cdefb 100644
--- a/src/main.c
+++ b/src/main.c
@@ -14,7 +14,7 @@
 ** other files are for internal use by SQLite and should not be
 ** accessed by users of the library.
 **
-** $Id: main.c,v 1.537 2009/04/17 16:54:23 drh Exp $
+** $Id: main.c,v 1.538 2009/04/19 12:23:58 drh Exp $
 */
 #include "sqliteInt.h"
 
@@ -213,18 +213,22 @@
 ** Undo the effects of sqlite3_initialize().  Must not be called while
 ** there are outstanding database connections or memory allocations or
 ** while any part of SQLite is otherwise in use in any thread.  This
-** routine is not threadsafe.  Not by a long shot.
+** routine is not threadsafe.  But it is safe to invoke this routine
+** on when SQLite is already shut down.  If SQLite is already shut down
+** when this routine is invoked, then this routine is a harmless no-op.
 */
 int sqlite3_shutdown(void){
-  sqlite3GlobalConfig.isMallocInit = 0;
-  sqlite3PcacheShutdown();
   if( sqlite3GlobalConfig.isInit ){
-    sqlite3_os_end();
+    sqlite3GlobalConfig.isMallocInit = 0;
+    sqlite3PcacheShutdown();
+    if( sqlite3GlobalConfig.isInit ){
+      sqlite3_os_end();
+    }
+    sqlite3_reset_auto_extension();
+    sqlite3MallocEnd();
+    sqlite3MutexEnd();
+    sqlite3GlobalConfig.isInit = 0;
   }
-  sqlite3_reset_auto_extension();
-  sqlite3MallocEnd();
-  sqlite3MutexEnd();
-  sqlite3GlobalConfig.isInit = 0;
   return SQLITE_OK;
 }
 
diff --git a/src/sqlite.h.in b/src/sqlite.h.in
index 9b5184c..440bc54 100644
--- a/src/sqlite.h.in
+++ b/src/sqlite.h.in
@@ -30,7 +30,7 @@
 ** the version number) and changes its name to "sqlite3.h" as
 ** part of the build process.
 **
-** @(#) $Id: sqlite.h.in,v 1.441 2009/04/13 14:43:41 drh Exp $
+** @(#) $Id: sqlite.h.in,v 1.442 2009/04/19 12:23:58 drh Exp $
 */
 #ifndef _SQLITE3_H_
 #define _SQLITE3_H_
@@ -791,6 +791,11 @@
 ** of sqlite3_initialize() does any initialization.  All other calls
 ** are harmless no-ops.
 **
+** A call to sqlite3_shutdown() is an "effective" call if it is the first
+** call to sqlite3_shutdown() since the last sqlite3_initialize().  Only
+** an effective call to sqlite3_shutdown() does any deinitialization.
+** All other calls to sqlite3_shutdown() are harmless no-ops.
+**
 ** Among other things, sqlite3_initialize() shall invoke
 ** sqlite3_os_init().  Similarly, sqlite3_shutdown()
 ** shall invoke sqlite3_os_end().