lean2/src/kernel/threadsafe_environment.h
Leonardo de Moura f97c260b0b refactor(kernel/environment): add ro_environment
The environment object is a "smart-pointer".
Before this commit, the use of "const &" for environment objects was broken.
For example, suppose we have a function f that should not modify the input environment.
Before this commit, its signature would be
       void f(environment const & env)
This is broken, f's implementation can easilty convert it to a read-write pointer by using
the copy constructor.
       environment rw_env(env);
Now, f can use rw_env to update env.

To fix this issue, we now have ro_environment. It is a shared *const* pointer.
We can convert an environment into a ro_environment, but not the other way around.

ro_environment can also be seen as a form of documentation.
For example, now it is clear that type_inferer is not updating the environment, since its constructor takes a ro_environment.

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
2013-12-12 16:48:34 -08:00

45 lines
1.5 KiB
C++

/*
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#pragma once
#include "util/shared_mutex.h"
#include "kernel/environment.h"
namespace lean {
/**
\brief The environment object is not thread safe.
The helper classes \c read_only_shared_environment and \c read_write_shared_environment
provides thread safe access to the environment object.
\remark We do not use these classes internally.
They are only used for implementing external APIs.
*/
class read_only_shared_environment {
ro_environment m_env;
shared_lock m_lock;
public:
read_only_shared_environment(ro_environment const & env);
~read_only_shared_environment();
operator ro_environment const &() const { return m_env; }
environment_cell const * operator->() const { return m_env.m_ptr.get(); }
environment_cell const & operator*() const { return *(m_env.m_ptr.get()); }
};
/**
\brief See \c read_only_shared_environment
*/
class read_write_shared_environment {
environment m_env;
exclusive_lock m_lock;
public:
read_write_shared_environment(environment const & env);
~read_write_shared_environment();
operator environment const &() const { return m_env; }
operator ro_environment() const { return ro_environment(m_env); }
environment_cell * operator->() const { return m_env.m_ptr.get(); }
environment_cell & operator*() const { return *(m_env.m_ptr.get()); }
};
}