Bug 1313451 - [1.1] Make Zip thread-safe. r=glandium, a=gchang
authorEugen Sawin <esawin@mozilla.com>
Thu, 10 Nov 2016 19:02:56 +0100
changeset 356687 115cc4b13d7ebb463c973091cf3e34fcb037d415
parent 356686 c8e0d652196fc582a73830d6db7dfa9eb027261e
child 356688 1fb19c64c1cc26cafa1814d13e143217cb27f404
push id6597
push userryanvm@gmail.com
push dateMon, 21 Nov 2016 16:17:59 +0000
treeherdermozilla-beta@e0bec38b612a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersglandium, gchang
bugs1313451
milestone51.0
Bug 1313451 - [1.1] Make Zip thread-safe. r=glandium, a=gchang
mozglue/linker/Zip.cpp
mozglue/linker/Zip.h
--- a/mozglue/linker/Zip.cpp
+++ b/mozglue/linker/Zip.cpp
@@ -60,35 +60,39 @@ Zip::Create(const char *filename, void *
 Zip::Zip(const char *filename, void *mapped, size_t size)
 : name(filename ? strdup(filename) : nullptr)
 , mapped(mapped)
 , size(size)
 , nextFile(LocalFile::validate(mapped)) // first Local File entry
 , nextDir(nullptr)
 , entries(nullptr)
 {
+  pthread_mutex_init(&mutex, nullptr);
   // If the first local file entry couldn't be found (which can happen
   // with optimized jars), check the first central directory entry.
   if (!nextFile)
     GetFirstEntry();
 }
 
 Zip::~Zip()
 {
   ZipCollection::Forget(this);
   if (name) {
     munmap(mapped, size);
     DEBUG_LOG("Unmapped %s @%p", name, mapped);
     free(name);
   }
+  pthread_mutex_destroy(&mutex);
 }
 
 bool
 Zip::GetStream(const char *path, Zip::Stream *out) const
 {
+  AutoLock lock(&mutex);
+
   DEBUG_LOG("%s - GetFile %s", name, path);
   /* Fast path: if the Local File header on store matches, we can return the
    * corresponding stream right away.
    * However, the Local File header may not contain enough information, in
    * which case the 3rd bit on the generalFlag is set. Unfortunately, this
    * bit is also set in some archives even when we do have the data (most
    * notably the android packages as built by the Mozilla build system).
    * So instead of testing the generalFlag bit, only use the fast path when
--- a/mozglue/linker/Zip.h
+++ b/mozglue/linker/Zip.h
@@ -4,16 +4,17 @@
 
 #ifndef Zip_h
 #define Zip_h
 
 #include <cstring>
 #include <stdint.h>
 #include <vector>
 #include <zlib.h>
+#include <pthread.h>
 #include "Utils.h"
 #include "mozilla/Assertions.h"
 #include "mozilla/RefCounted.h"
 #include "mozilla/RefPtr.h"
 
 /**
  * Helper class wrapping z_stream to avoid malloc() calls during
  * inflate. Do not use for deflate.
@@ -454,16 +455,18 @@ private:
    * requested entry is that one. */
   mutable const LocalFile *nextFile;
 
   /* Likewise for the next Directory entry */
   mutable const DirectoryEntry *nextDir;
 
   /* Pointer to the Directory entries */
   mutable const DirectoryEntry *entries;
+
+  mutable pthread_mutex_t mutex;
 };
 
 /**
  * Class for bookkeeping Zip instances
  */
 class ZipCollection
 {
 public: