Fix concurrency bugs in BCC JIT cache.

ContextManager could potentially be accessed by multiple threads
so we now use a Mutex.

Fixed a bug in ContextManager where it could mark a slot as having
been assigned even though the subsequent call to mmap failed.

Added code to delete the cache file before recreating it.  The existing
file may still be mmapped and executing somewhere else so we don't want
to modify its contents.

Bug: 3345334
Change-Id: I9c8382d9695741b92a4f66c4529ebb5d9a95f1d9
diff --git a/lib/bcc/Script.cpp b/lib/bcc/Script.cpp
index ac74118..74b4793 100644
--- a/lib/bcc/Script.cpp
+++ b/lib/bcc/Script.cpp
@@ -254,6 +254,12 @@
   if (mCachePath && !getBooleanProp("debug.bcc.nocache")) {
     FileHandle file;
 
+    // Remove the file if it already exists before writing the new file.
+    // The old file may still be mapped elsewhere in memory and we do not want
+    // to modify its contents.  (The same script may be running concurrently in
+    // the same process or a different process!)
+    ::unlink(mCachePath);
+
     if (file.open(mCachePath, OpenMode::Write) >= 0) {
       CacheWriter writer;