Skip to content

Commit

Permalink
switched to heap allocation of selected structures instead
Browse files Browse the repository at this point in the history
  • Loading branch information
finnschiermer committed Oct 25, 2016
1 parent 84fb390 commit 13a4fe7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 40 deletions.
6 changes: 4 additions & 2 deletions src/realm/alloc_slab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,10 @@ ref_type SlabAlloc::get_top_ref(const char* buffer, size_t len)

namespace {

std::map<std::string, std::weak_ptr<SlabAlloc::MappedFile>> all_files;
util::Mutex all_files_mutex;
// prevent destruction at exit (which can lead to races if other threads are still running)
std::map<std::string, std::weak_ptr<SlabAlloc::MappedFile>>& all_files =
*new std::map<std::string, std::weak_ptr<SlabAlloc::MappedFile>>;
util::Mutex& all_files_mutex = *new util::Mutex;
}


Expand Down
20 changes: 5 additions & 15 deletions src/realm/util/file_mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,11 @@ struct mapping_and_addr {
size_t size;
};

util::Mutex mapping_mutex(Mutex::no_destruction_tag());
std::vector<mapping_and_addr> mappings_by_addr;
std::vector<mappings_for_file> mappings_by_file;

// If there's any active mappings when the program exits, deliberately leak them
// to avoid flushing things that were in the middle of being modified on a different thrad
struct AtExit {
~AtExit()
{
if (!mappings_by_addr.empty())
(new std::vector<mapping_and_addr>)->swap(mappings_by_addr);
if (!mappings_by_file.empty())
(new std::vector<mappings_for_file>)->swap(mappings_by_file);
}
} at_exit;
// prevent destruction at exit (which can lead to races if other threads are still running)
util::Mutex& mapping_mutex = *new Mutex;
std::vector<mapping_and_addr>& mappings_by_addr = *new std::vector<mapping_and_addr>;
std::vector<mappings_for_file>& mappings_by_file = *new std::vector<mappings_for_file>;


mapping_and_addr* find_mapping_for_addr(void* addr, size_t size)
{
Expand Down
23 changes: 0 additions & 23 deletions src/realm/util/thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,6 @@ class Mutex {
Mutex();
~Mutex() noexcept;

struct no_destruction_tag {
};

// Configure this mutex to ignore destruction. This should be used
// ONLY for a mutex placed in a global variable. Global variables
// are destroyed on exit of the main thread, but some use cases
// indicate that background threads may still be alive and accessing
// the mutex after destruction. This leads to apps failing.
// For global variables, it is ok not to destroy the mutex properly.
// When the process terminates, the kernel will release the resources
// in any case. This facility should *never* be used for process
// shared mutexes.
Mutex(no_destruction_tag);

struct process_shared_tag {
};
/// Initialize this mutex for use across multiple processes. When
Expand All @@ -134,7 +120,6 @@ class Mutex {

protected:
pthread_mutex_t m_impl = PTHREAD_MUTEX_INITIALIZER;
bool do_not_destroy = false;

struct no_init_tag {
};
Expand Down Expand Up @@ -403,16 +388,8 @@ inline Mutex::Mutex(process_shared_tag)
init_as_process_shared(robust_if_available);
}

inline Mutex::Mutex(no_destruction_tag)
{
do_not_destroy = true;
}

inline Mutex::~Mutex() noexcept
{
if (do_not_destroy)
return;

int r = pthread_mutex_destroy(&m_impl);
if (REALM_UNLIKELY(r != 0))
destroy_failed(r);
Expand Down

0 comments on commit 13a4fe7

Please sign in to comment.