mirror of
https://github.com/Z3Prover/z3
synced 2025-06-06 14:13:23 +00:00
fix crash on Mac due to different destruction order of globals
the mutex in memory_manager has to be destroyed after all mem deallocations happen
This commit is contained in:
parent
2bee9a062f
commit
cf3e649462
12 changed files with 80 additions and 120 deletions
|
@ -16,9 +16,8 @@ Author:
|
||||||
#include "util/rlimit.h"
|
#include "util/rlimit.h"
|
||||||
#include "util/gparams.h"
|
#include "util/gparams.h"
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <mutex>
|
|
||||||
|
|
||||||
extern std::mutex* g_stat_mux;
|
static std::mutex display_stats_mux;
|
||||||
|
|
||||||
static lp::lp_solver<double, double>* g_solver = nullptr;
|
static lp::lp_solver<double, double>* g_solver = nullptr;
|
||||||
|
|
||||||
|
@ -31,14 +30,14 @@ static void display_statistics() {
|
||||||
static void STD_CALL on_ctrl_c(int) {
|
static void STD_CALL on_ctrl_c(int) {
|
||||||
signal (SIGINT, SIG_DFL);
|
signal (SIGINT, SIG_DFL);
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(*g_stat_mux);
|
std::lock_guard<std::mutex> lock(display_stats_mux);
|
||||||
display_statistics();
|
display_statistics();
|
||||||
}
|
}
|
||||||
raise(SIGINT);
|
raise(SIGINT);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void on_timeout() {
|
static void on_timeout() {
|
||||||
std::lock_guard<std::mutex> lock(*g_stat_mux);
|
std::lock_guard<std::mutex> lock(display_stats_mux);
|
||||||
display_statistics();
|
display_statistics();
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,6 @@ Revision History:
|
||||||
|
|
||||||
--*/
|
--*/
|
||||||
#include<iostream>
|
#include<iostream>
|
||||||
#include<mutex>
|
|
||||||
#include "util/memory_manager.h"
|
#include "util/memory_manager.h"
|
||||||
#include "util/trace.h"
|
#include "util/trace.h"
|
||||||
#include "util/debug.h"
|
#include "util/debug.h"
|
||||||
|
@ -51,7 +50,6 @@ static char const * g_input_file = nullptr;
|
||||||
static bool g_standard_input = false;
|
static bool g_standard_input = false;
|
||||||
static input_kind g_input_kind = IN_UNSPECIFIED;
|
static input_kind g_input_kind = IN_UNSPECIFIED;
|
||||||
bool g_display_statistics = false;
|
bool g_display_statistics = false;
|
||||||
std::mutex* g_stat_mux = nullptr;
|
|
||||||
static bool g_display_istatistics = false;
|
static bool g_display_istatistics = false;
|
||||||
|
|
||||||
static void error(const char * msg) {
|
static void error(const char * msg) {
|
||||||
|
@ -313,7 +311,6 @@ int STD_CALL main(int argc, char ** argv) {
|
||||||
unsigned return_value = 0;
|
unsigned return_value = 0;
|
||||||
memory::initialize(0);
|
memory::initialize(0);
|
||||||
memory::exit_when_out_of_memory(true, "ERROR: out of memory");
|
memory::exit_when_out_of_memory(true, "ERROR: out of memory");
|
||||||
g_stat_mux = alloc(std::mutex);
|
|
||||||
parse_cmd_line_args(argc, argv);
|
parse_cmd_line_args(argc, argv);
|
||||||
env_params::updt_params();
|
env_params::updt_params();
|
||||||
|
|
||||||
|
@ -384,7 +381,6 @@ int STD_CALL main(int argc, char ** argv) {
|
||||||
default:
|
default:
|
||||||
UNREACHABLE();
|
UNREACHABLE();
|
||||||
}
|
}
|
||||||
dealloc(g_stat_mux);
|
|
||||||
memory::finalize();
|
memory::finalize();
|
||||||
#ifdef _WINDOWS
|
#ifdef _WINDOWS
|
||||||
_CrtDumpMemoryLeaks();
|
_CrtDumpMemoryLeaks();
|
||||||
|
|
|
@ -7,7 +7,6 @@ Copyright (c) 2015 Microsoft Corporation
|
||||||
#include<fstream>
|
#include<fstream>
|
||||||
#include<signal.h>
|
#include<signal.h>
|
||||||
#include<time.h>
|
#include<time.h>
|
||||||
#include<mutex>
|
|
||||||
#include "util/gparams.h"
|
#include "util/gparams.h"
|
||||||
#include "util/timeout.h"
|
#include "util/timeout.h"
|
||||||
#include "util/cancel_eh.h"
|
#include "util/cancel_eh.h"
|
||||||
|
@ -22,11 +21,11 @@ Copyright (c) 2015 Microsoft Corporation
|
||||||
#include "opt/opt_parse.h"
|
#include "opt/opt_parse.h"
|
||||||
|
|
||||||
extern bool g_display_statistics;
|
extern bool g_display_statistics;
|
||||||
extern std::mutex* g_stat_mux;
|
|
||||||
static bool g_first_interrupt = true;
|
static bool g_first_interrupt = true;
|
||||||
static opt::context* g_opt = nullptr;
|
static opt::context* g_opt = nullptr;
|
||||||
static double g_start_time = 0;
|
static double g_start_time = 0;
|
||||||
static unsigned_vector g_handles;
|
static unsigned_vector g_handles;
|
||||||
|
static std::mutex display_stats_mux;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -71,7 +70,7 @@ static void STD_CALL on_ctrl_c(int) {
|
||||||
else {
|
else {
|
||||||
signal (SIGINT, SIG_DFL);
|
signal (SIGINT, SIG_DFL);
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(*g_stat_mux);
|
std::lock_guard<std::mutex> lock(display_stats_mux);
|
||||||
display_statistics();
|
display_statistics();
|
||||||
}
|
}
|
||||||
raise(SIGINT);
|
raise(SIGINT);
|
||||||
|
@ -80,7 +79,7 @@ static void STD_CALL on_ctrl_c(int) {
|
||||||
|
|
||||||
static void on_timeout() {
|
static void on_timeout() {
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(*g_stat_mux);
|
std::lock_guard<std::mutex> lock(display_stats_mux);
|
||||||
display_statistics();
|
display_statistics();
|
||||||
}
|
}
|
||||||
exit(0);
|
exit(0);
|
||||||
|
@ -134,7 +133,7 @@ static unsigned parse_opt(std::istream& in, opt_format f) {
|
||||||
std::cerr << ex.msg() << "\n";
|
std::cerr << ex.msg() << "\n";
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(*g_stat_mux);
|
std::lock_guard<std::mutex> lock(display_stats_mux);
|
||||||
display_statistics();
|
display_statistics();
|
||||||
register_on_timeout_proc(nullptr);
|
register_on_timeout_proc(nullptr);
|
||||||
g_opt = nullptr;
|
g_opt = nullptr;
|
||||||
|
|
|
@ -21,8 +21,6 @@ Revision History:
|
||||||
#include<iostream>
|
#include<iostream>
|
||||||
#include<time.h>
|
#include<time.h>
|
||||||
#include<signal.h>
|
#include<signal.h>
|
||||||
#include<mutex>
|
|
||||||
|
|
||||||
#include "util/timeout.h"
|
#include "util/timeout.h"
|
||||||
#include "parsers/smt2/smt2parser.h"
|
#include "parsers/smt2/smt2parser.h"
|
||||||
#include "muz/fp/dl_cmds.h"
|
#include "muz/fp/dl_cmds.h"
|
||||||
|
@ -34,7 +32,8 @@ Revision History:
|
||||||
#include "tactic/portfolio/smt_strategic_solver.h"
|
#include "tactic/portfolio/smt_strategic_solver.h"
|
||||||
#include "smt/smt_solver.h"
|
#include "smt/smt_solver.h"
|
||||||
|
|
||||||
extern std::mutex* g_stat_mux;
|
static std::mutex display_stats_mux;
|
||||||
|
|
||||||
extern bool g_display_statistics;
|
extern bool g_display_statistics;
|
||||||
static clock_t g_start_time;
|
static clock_t g_start_time;
|
||||||
static cmd_context * g_cmd_context = nullptr;
|
static cmd_context * g_cmd_context = nullptr;
|
||||||
|
@ -52,7 +51,7 @@ static void display_statistics() {
|
||||||
}
|
}
|
||||||
|
|
||||||
static void on_timeout() {
|
static void on_timeout() {
|
||||||
std::lock_guard<std::mutex> lock(*g_stat_mux);
|
std::lock_guard<std::mutex> lock(display_stats_mux);
|
||||||
display_statistics();
|
display_statistics();
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
@ -60,7 +59,7 @@ static void on_timeout() {
|
||||||
static void STD_CALL on_ctrl_c(int) {
|
static void STD_CALL on_ctrl_c(int) {
|
||||||
signal (SIGINT, SIG_DFL);
|
signal (SIGINT, SIG_DFL);
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(*g_stat_mux);
|
std::lock_guard<std::mutex> lock(display_stats_mux);
|
||||||
display_statistics();
|
display_statistics();
|
||||||
}
|
}
|
||||||
raise(SIGINT);
|
raise(SIGINT);
|
||||||
|
@ -100,7 +99,7 @@ unsigned read_smtlib2_commands(char const * file_name) {
|
||||||
|
|
||||||
|
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(*g_stat_mux);
|
std::lock_guard<std::mutex> lock(display_stats_mux);
|
||||||
display_statistics();
|
display_statistics();
|
||||||
g_cmd_context = nullptr;
|
g_cmd_context = nullptr;
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ Notes:
|
||||||
#include "util/trace.h"
|
#include "util/trace.h"
|
||||||
#include "util/mutex.h"
|
#include "util/mutex.h"
|
||||||
|
|
||||||
static mutex* s_mux = nullptr;
|
static mutex gparams_mux;
|
||||||
|
|
||||||
extern void gparams_register_modules();
|
extern void gparams_register_modules();
|
||||||
|
|
||||||
|
@ -113,7 +113,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
void reset() {
|
void reset() {
|
||||||
lock_guard lock(*s_mux);
|
lock_guard lock(gparams_mux);
|
||||||
m_params.reset();
|
m_params.reset();
|
||||||
for (auto & kv : m_module_params) {
|
for (auto & kv : m_module_params) {
|
||||||
dealloc(kv.m_value);
|
dealloc(kv.m_value);
|
||||||
|
@ -329,7 +329,7 @@ public:
|
||||||
bool error = false;
|
bool error = false;
|
||||||
std::string error_msg;
|
std::string error_msg;
|
||||||
{
|
{
|
||||||
lock_guard lock(*s_mux);
|
lock_guard lock(gparams_mux);
|
||||||
try {
|
try {
|
||||||
symbol m, p;
|
symbol m, p;
|
||||||
normalize(name, m, p);
|
normalize(name, m, p);
|
||||||
|
@ -381,7 +381,7 @@ public:
|
||||||
bool error = false;
|
bool error = false;
|
||||||
std::string error_msg;
|
std::string error_msg;
|
||||||
{
|
{
|
||||||
lock_guard lock(*s_mux);
|
lock_guard lock(gparams_mux);
|
||||||
try {
|
try {
|
||||||
symbol m, p;
|
symbol m, p;
|
||||||
normalize(name, m, p);
|
normalize(name, m, p);
|
||||||
|
@ -428,7 +428,7 @@ public:
|
||||||
params_ref result;
|
params_ref result;
|
||||||
params_ref * ps = nullptr;
|
params_ref * ps = nullptr;
|
||||||
{
|
{
|
||||||
lock_guard lock(*s_mux);
|
lock_guard lock(gparams_mux);
|
||||||
if (m_module_params.find(module_name, ps)) {
|
if (m_module_params.find(module_name, ps)) {
|
||||||
result.copy(*ps);
|
result.copy(*ps);
|
||||||
}
|
}
|
||||||
|
@ -448,7 +448,7 @@ public:
|
||||||
|
|
||||||
void display(std::ostream & out, unsigned indent, bool smt2_style, bool include_descr) {
|
void display(std::ostream & out, unsigned indent, bool smt2_style, bool include_descr) {
|
||||||
{
|
{
|
||||||
lock_guard lock(*s_mux);
|
lock_guard lock(gparams_mux);
|
||||||
out << "Global parameters\n";
|
out << "Global parameters\n";
|
||||||
get_param_descrs().display(out, indent + 4, smt2_style, include_descr);
|
get_param_descrs().display(out, indent + 4, smt2_style, include_descr);
|
||||||
out << "\n";
|
out << "\n";
|
||||||
|
@ -470,7 +470,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
void display_modules(std::ostream & out) {
|
void display_modules(std::ostream & out) {
|
||||||
lock_guard lock(*s_mux);
|
lock_guard lock(gparams_mux);
|
||||||
for (auto & kv : get_module_param_descrs()) {
|
for (auto & kv : get_module_param_descrs()) {
|
||||||
out << "[module] " << kv.m_key;
|
out << "[module] " << kv.m_key;
|
||||||
char const * descr = nullptr;
|
char const * descr = nullptr;
|
||||||
|
@ -484,7 +484,7 @@ public:
|
||||||
void display_module(std::ostream & out, symbol const & module_name) {
|
void display_module(std::ostream & out, symbol const & module_name) {
|
||||||
bool error = false;
|
bool error = false;
|
||||||
std::string error_msg;
|
std::string error_msg;
|
||||||
lock_guard lock(*s_mux);
|
lock_guard lock(gparams_mux);
|
||||||
try {
|
try {
|
||||||
param_descrs * d = nullptr;
|
param_descrs * d = nullptr;
|
||||||
if (!get_module_param_descrs().find(module_name, d)) {
|
if (!get_module_param_descrs().find(module_name, d)) {
|
||||||
|
@ -513,7 +513,7 @@ public:
|
||||||
bool error = false;
|
bool error = false;
|
||||||
std::string error_msg;
|
std::string error_msg;
|
||||||
{
|
{
|
||||||
lock_guard lock(*s_mux);
|
lock_guard lock(gparams_mux);
|
||||||
try {
|
try {
|
||||||
symbol m, p;
|
symbol m, p;
|
||||||
normalize(name, m, p);
|
normalize(name, m, p);
|
||||||
|
@ -631,16 +631,15 @@ void gparams::display_parameter(std::ostream & out, char const * name) {
|
||||||
|
|
||||||
void gparams::init() {
|
void gparams::init() {
|
||||||
TRACE("gparams", tout << "gparams::init()\n";);
|
TRACE("gparams", tout << "gparams::init()\n";);
|
||||||
s_mux = alloc(mutex);
|
|
||||||
g_imp = alloc(imp);
|
g_imp = alloc(imp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void gparams::finalize() {
|
void gparams::finalize() {
|
||||||
TRACE("gparams", tout << "gparams::finalize()\n";);
|
TRACE("gparams", tout << "gparams::finalize()\n";);
|
||||||
dealloc(g_imp);
|
if (g_imp != nullptr) {
|
||||||
g_imp = nullptr;
|
dealloc(g_imp);
|
||||||
dealloc(s_mux);
|
g_imp = nullptr;
|
||||||
s_mux = nullptr;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,7 @@ out_of_memory_error::out_of_memory_error():z3_error(ERR_MEMOUT) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static mutex g_memory_mux;
|
static mutex *g_memory_mux;
|
||||||
static atomic<bool> g_memory_out_of_memory(false);
|
static atomic<bool> g_memory_out_of_memory(false);
|
||||||
static bool g_memory_initialized = false;
|
static bool g_memory_initialized = false;
|
||||||
static long long g_memory_alloc_size = 0;
|
static long long g_memory_alloc_size = 0;
|
||||||
|
@ -46,7 +46,6 @@ static long long g_memory_alloc_count = 0;
|
||||||
static long long g_memory_max_alloc_count = 0;
|
static long long g_memory_max_alloc_count = 0;
|
||||||
static bool g_exit_when_out_of_memory = false;
|
static bool g_exit_when_out_of_memory = false;
|
||||||
static char const * g_out_of_memory_msg = "ERROR: out of memory";
|
static char const * g_out_of_memory_msg = "ERROR: out of memory";
|
||||||
static volatile bool g_memory_fully_initialized = false;
|
|
||||||
|
|
||||||
void memory::exit_when_out_of_memory(bool flag, char const * msg) {
|
void memory::exit_when_out_of_memory(bool flag, char const * msg) {
|
||||||
g_exit_when_out_of_memory = flag;
|
g_exit_when_out_of_memory = flag;
|
||||||
|
@ -87,29 +86,22 @@ mem_usage_report g_info;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void memory::initialize(size_t max_size) {
|
void memory::initialize(size_t max_size) {
|
||||||
bool initialize = false;
|
static mutex init_mux;
|
||||||
{
|
lock_guard lock(init_mux);
|
||||||
lock_guard lock(g_memory_mux);
|
|
||||||
// only update the maximum size if max_size != UINT_MAX
|
// only update the maximum size if max_size != UINT_MAX
|
||||||
if (max_size != UINT_MAX)
|
if (max_size != UINT_MAX)
|
||||||
g_memory_max_size = max_size;
|
g_memory_max_size = max_size;
|
||||||
if (!g_memory_initialized) {
|
|
||||||
g_memory_initialized = true;
|
if (g_memory_initialized)
|
||||||
initialize = true;
|
return;
|
||||||
}
|
|
||||||
}
|
#ifndef SINGLE_THREAD
|
||||||
if (initialize) {
|
g_memory_mux = new mutex;
|
||||||
g_memory_out_of_memory = false;
|
#endif
|
||||||
mem_initialize();
|
g_memory_out_of_memory = false;
|
||||||
g_memory_fully_initialized = true;
|
mem_initialize();
|
||||||
}
|
g_memory_initialized = true;
|
||||||
else {
|
|
||||||
// Delay the current thread until the DLL is fully initialized
|
|
||||||
// Without this, multiple threads can start to call API functions
|
|
||||||
// before memory::initialize(...) finishes.
|
|
||||||
while (!g_memory_fully_initialized)
|
|
||||||
/* wait */ ;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool memory::is_out_of_memory() {
|
bool memory::is_out_of_memory() {
|
||||||
|
@ -124,7 +116,7 @@ void memory::set_high_watermark(size_t watermark) {
|
||||||
bool memory::above_high_watermark() {
|
bool memory::above_high_watermark() {
|
||||||
if (g_memory_watermark == 0)
|
if (g_memory_watermark == 0)
|
||||||
return false;
|
return false;
|
||||||
lock_guard lock(g_memory_mux);
|
lock_guard lock(*g_memory_mux);
|
||||||
return g_memory_watermark < g_memory_alloc_size;
|
return g_memory_watermark < g_memory_alloc_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,6 +137,9 @@ void memory::finalize() {
|
||||||
if (g_memory_initialized) {
|
if (g_memory_initialized) {
|
||||||
g_finalizing = true;
|
g_finalizing = true;
|
||||||
mem_finalize();
|
mem_finalize();
|
||||||
|
#ifndef SINGLE_THREAD
|
||||||
|
delete g_memory_mux;
|
||||||
|
#endif
|
||||||
g_memory_initialized = false;
|
g_memory_initialized = false;
|
||||||
g_finalizing = false;
|
g_finalizing = false;
|
||||||
}
|
}
|
||||||
|
@ -153,7 +148,7 @@ void memory::finalize() {
|
||||||
unsigned long long memory::get_allocation_size() {
|
unsigned long long memory::get_allocation_size() {
|
||||||
long long r;
|
long long r;
|
||||||
{
|
{
|
||||||
lock_guard lock(g_memory_mux);
|
lock_guard lock(*g_memory_mux);
|
||||||
r = g_memory_alloc_size;
|
r = g_memory_alloc_size;
|
||||||
}
|
}
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
|
@ -164,7 +159,7 @@ unsigned long long memory::get_allocation_size() {
|
||||||
unsigned long long memory::get_max_used_memory() {
|
unsigned long long memory::get_max_used_memory() {
|
||||||
unsigned long long r;
|
unsigned long long r;
|
||||||
{
|
{
|
||||||
lock_guard lock(g_memory_mux);
|
lock_guard lock(*g_memory_mux);
|
||||||
r = g_memory_max_used_size;
|
r = g_memory_max_used_size;
|
||||||
}
|
}
|
||||||
return r;
|
return r;
|
||||||
|
@ -248,7 +243,7 @@ static void synchronize_counters(bool allocating) {
|
||||||
bool out_of_mem = false;
|
bool out_of_mem = false;
|
||||||
bool counts_exceeded = false;
|
bool counts_exceeded = false;
|
||||||
{
|
{
|
||||||
lock_guard lock(g_memory_mux);
|
lock_guard lock(*g_memory_mux);
|
||||||
g_memory_alloc_size += g_memory_thread_alloc_size;
|
g_memory_alloc_size += g_memory_thread_alloc_size;
|
||||||
g_memory_alloc_count += g_memory_thread_alloc_count;
|
g_memory_alloc_count += g_memory_thread_alloc_count;
|
||||||
if (g_memory_alloc_size > g_memory_max_used_size)
|
if (g_memory_alloc_size > g_memory_max_used_size)
|
||||||
|
@ -329,7 +324,7 @@ void memory::deallocate(void * p) {
|
||||||
size_t sz = *sz_p;
|
size_t sz = *sz_p;
|
||||||
void * real_p = reinterpret_cast<void*>(sz_p);
|
void * real_p = reinterpret_cast<void*>(sz_p);
|
||||||
{
|
{
|
||||||
lock_guard lock(g_memory_mux);
|
lock_guard lock(*g_memory_mux);
|
||||||
g_memory_alloc_size -= sz;
|
g_memory_alloc_size -= sz;
|
||||||
}
|
}
|
||||||
free(real_p);
|
free(real_p);
|
||||||
|
@ -339,7 +334,7 @@ void * memory::allocate(size_t s) {
|
||||||
s = s + sizeof(size_t); // we allocate an extra field!
|
s = s + sizeof(size_t); // we allocate an extra field!
|
||||||
bool out_of_mem = false, counts_exceeded = false;
|
bool out_of_mem = false, counts_exceeded = false;
|
||||||
{
|
{
|
||||||
lock_guard lock(g_memory_mux);
|
lock_guard lock(*g_memory_mux);
|
||||||
g_memory_alloc_size += s;
|
g_memory_alloc_size += s;
|
||||||
g_memory_alloc_count += 1;
|
g_memory_alloc_count += 1;
|
||||||
if (g_memory_alloc_size > g_memory_max_used_size)
|
if (g_memory_alloc_size > g_memory_max_used_size)
|
||||||
|
@ -369,7 +364,7 @@ void* memory::reallocate(void *p, size_t s) {
|
||||||
s = s + sizeof(size_t); // we allocate an extra field!
|
s = s + sizeof(size_t); // we allocate an extra field!
|
||||||
bool out_of_mem = false, counts_exceeded = false;
|
bool out_of_mem = false, counts_exceeded = false;
|
||||||
{
|
{
|
||||||
lock_guard lock(g_memory_mux);
|
lock_guard lock(*g_memory_mux);
|
||||||
g_memory_alloc_size += s - sz;
|
g_memory_alloc_size += s - sz;
|
||||||
g_memory_alloc_count += 1;
|
g_memory_alloc_count += 1;
|
||||||
if (g_memory_alloc_size > g_memory_max_used_size)
|
if (g_memory_alloc_size > g_memory_max_used_size)
|
||||||
|
|
|
@ -17,21 +17,10 @@ Notes:
|
||||||
|
|
||||||
--*/
|
--*/
|
||||||
#include "util/prime_generator.h"
|
#include "util/prime_generator.h"
|
||||||
|
#include "util/mutex.h"
|
||||||
|
|
||||||
#define PRIME_LIST_MAX_SIZE 1<<20
|
#define PRIME_LIST_MAX_SIZE 1<<20
|
||||||
|
|
||||||
prime_generator::prime_generator() {
|
|
||||||
m_primes.push_back(2);
|
|
||||||
m_primes.push_back(3);
|
|
||||||
m_mux = alloc(mutex);
|
|
||||||
process_next_k_numbers(128);
|
|
||||||
}
|
|
||||||
|
|
||||||
prime_generator::~prime_generator() {
|
|
||||||
dealloc(m_mux);
|
|
||||||
m_mux = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void prime_generator::process_next_k_numbers(uint64_t k) {
|
void prime_generator::process_next_k_numbers(uint64_t k) {
|
||||||
svector<uint64_t> todo;
|
svector<uint64_t> todo;
|
||||||
uint64_t begin = m_primes.back() + 2;
|
uint64_t begin = m_primes.back() + 2;
|
||||||
|
@ -85,10 +74,14 @@ void prime_generator::process_next_k_numbers(uint64_t k) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void prime_generator::initialize() {
|
||||||
|
m_primes.push_back(2);
|
||||||
|
m_primes.push_back(3);
|
||||||
|
process_next_k_numbers(128);
|
||||||
|
}
|
||||||
|
|
||||||
void prime_generator::finalize() {
|
void prime_generator::finalize() {
|
||||||
m_primes.finalize();
|
m_primes.finalize();
|
||||||
dealloc(m_mux);
|
|
||||||
m_mux = nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t prime_generator::operator()(unsigned idx) {
|
uint64_t prime_generator::operator()(unsigned idx) {
|
||||||
|
@ -117,6 +110,8 @@ prime_iterator::prime_iterator(prime_generator * g):m_idx(0) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static mutex g_prime_iterator;
|
||||||
|
|
||||||
uint64_t prime_iterator::next() {
|
uint64_t prime_iterator::next() {
|
||||||
unsigned idx = m_idx;
|
unsigned idx = m_idx;
|
||||||
m_idx++;
|
m_idx++;
|
||||||
|
@ -125,7 +120,7 @@ uint64_t prime_iterator::next() {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
uint64_t r;
|
uint64_t r;
|
||||||
lock_guard lock(*m_generator->m_mux);
|
lock_guard lock(g_prime_iterator);
|
||||||
{
|
{
|
||||||
r = (*m_generator)(idx);
|
r = (*m_generator)(idx);
|
||||||
}
|
}
|
||||||
|
@ -133,6 +128,10 @@ uint64_t prime_iterator::next() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void prime_iterator::initialize() {
|
||||||
|
g_prime_generator.initialize();
|
||||||
|
}
|
||||||
|
|
||||||
void prime_iterator::finalize() {
|
void prime_iterator::finalize() {
|
||||||
g_prime_generator.finalize();
|
g_prime_generator.finalize();
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,6 @@ Notes:
|
||||||
#include "util/vector.h"
|
#include "util/vector.h"
|
||||||
#include "util/z3_exception.h"
|
#include "util/z3_exception.h"
|
||||||
#include "util/util.h"
|
#include "util/util.h"
|
||||||
#include "util/mutex.h"
|
|
||||||
|
|
||||||
class prime_generator_exception : public default_exception {
|
class prime_generator_exception : public default_exception {
|
||||||
public:
|
public:
|
||||||
|
@ -36,10 +35,8 @@ class prime_generator {
|
||||||
svector<uint64_t> m_primes;
|
svector<uint64_t> m_primes;
|
||||||
void process_next_k_numbers(uint64_t k);
|
void process_next_k_numbers(uint64_t k);
|
||||||
public:
|
public:
|
||||||
mutex *m_mux;
|
|
||||||
prime_generator();
|
|
||||||
~prime_generator();
|
|
||||||
uint64_t operator()(unsigned idx);
|
uint64_t operator()(unsigned idx);
|
||||||
|
void initialize();
|
||||||
void finalize();
|
void finalize();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -50,8 +47,10 @@ class prime_iterator {
|
||||||
public:
|
public:
|
||||||
prime_iterator(prime_generator * g = nullptr);
|
prime_iterator(prime_generator * g = nullptr);
|
||||||
uint64_t next();
|
uint64_t next();
|
||||||
|
static void initialize();
|
||||||
static void finalize();
|
static void finalize();
|
||||||
/*
|
/*
|
||||||
|
ADD_INITIALIZER('prime_iterator::initialize();')
|
||||||
ADD_FINALIZER('prime_iterator::finalize();')
|
ADD_FINALIZER('prime_iterator::finalize();')
|
||||||
*/
|
*/
|
||||||
};
|
};
|
||||||
|
|
|
@ -26,7 +26,6 @@ rational rational::m_zero;
|
||||||
rational rational::m_one;
|
rational rational::m_one;
|
||||||
rational rational::m_minus_one;
|
rational rational::m_minus_one;
|
||||||
vector<rational> rational::m_powers_of_two;
|
vector<rational> rational::m_powers_of_two;
|
||||||
static mutex* s_mux = nullptr;
|
|
||||||
|
|
||||||
static void mk_power_up_to(vector<rational> & pws, unsigned n) {
|
static void mk_power_up_to(vector<rational> & pws, unsigned n) {
|
||||||
if (pws.empty()) {
|
if (pws.empty()) {
|
||||||
|
@ -41,10 +40,11 @@ static void mk_power_up_to(vector<rational> & pws, unsigned n) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static mutex g_powers_of_two;
|
||||||
|
|
||||||
rational rational::power_of_two(unsigned k) {
|
rational rational::power_of_two(unsigned k) {
|
||||||
rational result;
|
rational result;
|
||||||
lock_guard lock(*s_mux);
|
lock_guard lock(g_powers_of_two);
|
||||||
{
|
{
|
||||||
if (k >= m_powers_of_two.size())
|
if (k >= m_powers_of_two.size())
|
||||||
mk_power_up_to(m_powers_of_two, k+1);
|
mk_power_up_to(m_powers_of_two, k+1);
|
||||||
|
@ -64,7 +64,6 @@ void finalize_inf_int_rational();
|
||||||
void rational::initialize() {
|
void rational::initialize() {
|
||||||
if (!g_mpq_manager) {
|
if (!g_mpq_manager) {
|
||||||
g_mpq_manager = alloc(synch_mpq_manager);
|
g_mpq_manager = alloc(synch_mpq_manager);
|
||||||
s_mux = alloc(mutex);
|
|
||||||
m().set(m_zero.m_val, 0);
|
m().set(m_zero.m_val, 0);
|
||||||
m().set(m_one.m_val, 1);
|
m().set(m_one.m_val, 1);
|
||||||
m().set(m_minus_one.m_val, -1);
|
m().set(m_minus_one.m_val, -1);
|
||||||
|
@ -82,7 +81,5 @@ void rational::finalize() {
|
||||||
m_minus_one.~rational();
|
m_minus_one.~rational();
|
||||||
dealloc(g_mpq_manager);
|
dealloc(g_mpq_manager);
|
||||||
g_mpq_manager = nullptr;
|
g_mpq_manager = nullptr;
|
||||||
dealloc(s_mux);
|
|
||||||
s_mux = nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,16 +21,7 @@ Revision History:
|
||||||
#include "util/mutex.h"
|
#include "util/mutex.h"
|
||||||
|
|
||||||
|
|
||||||
static mutex* s_mux = nullptr;
|
static mutex g_rlimit_mux;
|
||||||
|
|
||||||
void initialize_rlimit() {
|
|
||||||
s_mux = new mutex;
|
|
||||||
}
|
|
||||||
void finalize_rlimit() {
|
|
||||||
delete s_mux;
|
|
||||||
s_mux = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
reslimit::reslimit():
|
reslimit::reslimit():
|
||||||
m_cancel(0),
|
m_cancel(0),
|
||||||
|
@ -82,32 +73,32 @@ char const* reslimit::get_cancel_msg() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
void reslimit::push_child(reslimit* r) {
|
void reslimit::push_child(reslimit* r) {
|
||||||
lock_guard lock(*s_mux);
|
lock_guard lock(g_rlimit_mux);
|
||||||
m_children.push_back(r);
|
m_children.push_back(r);
|
||||||
}
|
}
|
||||||
|
|
||||||
void reslimit::pop_child() {
|
void reslimit::pop_child() {
|
||||||
lock_guard lock(*s_mux);
|
lock_guard lock(g_rlimit_mux);
|
||||||
m_children.pop_back();
|
m_children.pop_back();
|
||||||
}
|
}
|
||||||
|
|
||||||
void reslimit::cancel() {
|
void reslimit::cancel() {
|
||||||
lock_guard lock(*s_mux);
|
lock_guard lock(g_rlimit_mux);
|
||||||
set_cancel(m_cancel+1);
|
set_cancel(m_cancel+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void reslimit::reset_cancel() {
|
void reslimit::reset_cancel() {
|
||||||
lock_guard lock(*s_mux);
|
lock_guard lock(g_rlimit_mux);
|
||||||
set_cancel(0);
|
set_cancel(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void reslimit::inc_cancel() {
|
void reslimit::inc_cancel() {
|
||||||
lock_guard lock(*s_mux);
|
lock_guard lock(g_rlimit_mux);
|
||||||
set_cancel(m_cancel+1);
|
set_cancel(m_cancel+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void reslimit::dec_cancel() {
|
void reslimit::dec_cancel() {
|
||||||
lock_guard lock(*s_mux);
|
lock_guard lock(g_rlimit_mux);
|
||||||
if (m_cancel > 0) {
|
if (m_cancel > 0) {
|
||||||
set_cancel(m_cancel-1);
|
set_cancel(m_cancel-1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,14 +20,6 @@ Revision History:
|
||||||
|
|
||||||
#include "util/vector.h"
|
#include "util/vector.h"
|
||||||
|
|
||||||
|
|
||||||
void initialize_rlimit();
|
|
||||||
void finalize_rlimit();
|
|
||||||
/*
|
|
||||||
ADD_INITIALIZER('initialize_rlimit();')
|
|
||||||
ADD_FINALIZER('finalize_rlimit();')
|
|
||||||
*/
|
|
||||||
|
|
||||||
class reslimit {
|
class reslimit {
|
||||||
volatile unsigned m_cancel;
|
volatile unsigned m_cancel;
|
||||||
bool m_suspend;
|
bool m_suspend;
|
||||||
|
|
|
@ -23,7 +23,7 @@ Revision History:
|
||||||
#include "util/string_buffer.h"
|
#include "util/string_buffer.h"
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
static mutex* s_mux = nullptr;
|
static mutex g_symbol_lock;
|
||||||
|
|
||||||
symbol symbol::m_dummy(TAG(void*, nullptr, 2));
|
symbol symbol::m_dummy(TAG(void*, nullptr, 2));
|
||||||
const symbol symbol::null;
|
const symbol symbol::null;
|
||||||
|
@ -38,7 +38,7 @@ public:
|
||||||
|
|
||||||
char const * get_str(char const * d) {
|
char const * get_str(char const * d) {
|
||||||
const char * result;
|
const char * result;
|
||||||
lock_guard lock(*s_mux);
|
lock_guard lock(g_symbol_lock);
|
||||||
str_hashtable::entry * e;
|
str_hashtable::entry * e;
|
||||||
if (m_table.insert_if_not_there_core(d, e)) {
|
if (m_table.insert_if_not_there_core(d, e)) {
|
||||||
// new entry
|
// new entry
|
||||||
|
@ -66,16 +66,11 @@ void initialize_symbols() {
|
||||||
if (!g_symbol_table) {
|
if (!g_symbol_table) {
|
||||||
g_symbol_table = alloc(internal_symbol_table);
|
g_symbol_table = alloc(internal_symbol_table);
|
||||||
}
|
}
|
||||||
if (!s_mux) {
|
|
||||||
s_mux = alloc(mutex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void finalize_symbols() {
|
void finalize_symbols() {
|
||||||
dealloc(g_symbol_table);
|
dealloc(g_symbol_table);
|
||||||
dealloc(s_mux);
|
|
||||||
g_symbol_table = nullptr;
|
g_symbol_table = nullptr;
|
||||||
s_mux = nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
symbol::symbol(char const * d) {
|
symbol::symbol(char const * d) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue