Bug 1313451 - [1.1] Make Zip thread-safe. r=glandium
authorEugen Sawin <esawin@mozilla.com>
Thu, 10 Nov 2016 19:02:56 +0100
changeset 352194 bfd5335fa635ec73c18dde685db8385f1ef113c3
parent 352193 385cf64debf70955bec05f8729f42ed8477f1d81
child 352195 3f1dcd04bc47823809e7776ca852977c3ed2881d
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-esr52@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersglandium
bugs1313451
milestone52.0a1
Bug 1313451 - [1.1] Make Zip thread-safe. r=glandium
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: