Skip to content

Commit

Permalink
Removed most mutexes from gfsm wrapper (see #1).
Browse files Browse the repository at this point in the history
As it turns out, the only lock that is needed is one for gfsmxlCascadeLookup. All other mutexes have been removed. This also means we no longer need a builder for the automatons.
  • Loading branch information
fpetran committed May 15, 2015
1 parent abdbb45 commit 21076d5
Show file tree
Hide file tree
Showing 22 changed files with 67 additions and 266 deletions.
4 changes: 2 additions & 2 deletions src/gfsm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
add_sources(alphabet.cpp labelvector.cpp implode_explode.cpp automaton.cpp
acceptor.cpp string_acceptor.cpp transducer.cpp
string_transducer.cpp cascade.cpp string_cascade.cpp builder.cpp)
install_headers(acceptor.h alphabet.h automaton.h builder.h cascade.h
string_transducer.cpp cascade.cpp string_cascade.cpp)
install_headers(acceptor.h alphabet.h automaton.h cascade.h
implode_explode.h labelvector.h path.h semiring.h
string_acceptor.h string_cascade.h string_transducer.h
transducer.h)
Expand Down
3 changes: 0 additions & 3 deletions src/gfsm/acceptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* with Norma. If not, see <http://www.gnu.org/licenses/>.
*/
#include"acceptor.h"
#include<mutex>
#include<set>
#include"gfsmlibs.h"
#include"labelvector.h"
Expand All @@ -30,7 +29,6 @@ Acceptor& Acceptor::operator=(Acceptor a) {
}

bool Acceptor::accepts(const LabelVector& vec) const {
std::lock_guard<std::mutex> guard(*gfsm_mutex);
gfsmAutomaton* result = gfsm_automaton_new();
gfsm_automaton_lookup(_fsm, vec._vec, result);
bool is_accepted = (gfsm_automaton_n_final_states(result) > 0);
Expand All @@ -48,7 +46,6 @@ std::set<LabelVector> Acceptor::accepted() const {
}

void Acceptor::add_path(const LabelVector& vec, bool set_all_final) {
std::lock_guard<std::mutex> guard(*gfsm_mutex);
gfsmStateId from = root();
gfsmWeight one = _fsm->sr->one;
for (gfsmLabelVal value : vec) {
Expand Down
11 changes: 3 additions & 8 deletions src/gfsm/acceptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@
#ifndef GFSM_ACCEPTOR_H_
#define GFSM_ACCEPTOR_H_
#include<utility>
#include<mutex>
#include<set>
#include"automaton.h"

namespace Gfsm {
class LabelVector;
class AutomatonBuilder;

/// A finite-state acceptor.
/** Implements functions specific to a finite-state acceptor, i.e., an
Automaton where input labels always equal output labels.
*/
class Acceptor : public Automaton {
friend class AutomatonBuilder;
public:
Acceptor() : Automaton() {
_fsm->flags.is_transducer = FALSE;
}
Acceptor(const Acceptor& a) : Automaton(a) {}
Acceptor(Acceptor&& a) : Automaton(std::move(a)) {}
Acceptor& operator=(Acceptor a);
Expand All @@ -56,11 +56,6 @@ class Acceptor : public Automaton {
the automaton will also accept {1} and {1, 3}).
*/
void add_path(const LabelVector& vec, bool set_all_final = false);

protected:
Acceptor() = delete;
explicit Acceptor(std::mutex* m) : Automaton(m)
{ _fsm->flags.is_transducer = FALSE; }
};

} // namespace Gfsm
Expand Down
21 changes: 2 additions & 19 deletions src/gfsm/automaton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,36 @@
#include<string>
#include<sstream>
#include<algorithm>
#include<mutex>
#include<set>
#include"gfsmlibs.h"
#include"labelvector.h"
#include"semiring.h"
#include"path.h"

namespace Gfsm {
Automaton::Automaton(std::mutex* m, SemiringType sr) {
gfsm_mutex = m;
std::lock_guard<std::mutex> guard(*gfsm_mutex);
Automaton::Automaton(SemiringType sr) {
_fsm = gfsm_automaton_new();
gfsm_automaton_set_semiring_type(_fsm, static_cast<gfsmSRType>(sr));
}

Automaton::Automaton(const Automaton& a) {
_fsm = gfsm_automaton_clone(a._fsm);
this->gfsm_mutex = a.gfsm_mutex;
}

Automaton::Automaton(Automaton&& a) : Automaton(gfsm_mutex) {
Automaton::Automaton(Automaton&& a) {
std::swap(_fsm, a._fsm);
std::swap(gfsm_mutex, a.gfsm_mutex);
}

Automaton& Automaton::operator=(Automaton a) {
std::swap(_fsm, a._fsm);
std::swap(gfsm_mutex, a.gfsm_mutex);
return *this;
}

Automaton::~Automaton() throw() {
std::lock_guard<std::mutex> guard(*gfsm_mutex);
gfsm_automaton_free(_fsm);
}

void Automaton::load_binfile(const std::string& filename) {
std::lock_guard<std::mutex> guard(*gfsm_mutex);
gfsmError* err = NULL;
// _fsm is implicitly cleared, no need to reset manually
gfsm_automaton_load_bin_filename(_fsm, filename.c_str(), &err);
Expand All @@ -72,7 +64,6 @@ void Automaton::load_binfile(const std::string& filename) {
}

void Automaton::save_binfile(const std::string& filename) {
std::lock_guard<std::mutex> guard(*gfsm_mutex);
gfsmError* err = NULL;
gfsm_automaton_save_bin_filename(_fsm, filename.c_str(), -1, &err);
if (err != NULL) {
Expand All @@ -83,7 +74,6 @@ void Automaton::save_binfile(const std::string& filename) {
}

void Automaton::set_semiring_type(SemiringType sr) {
std::lock_guard<std::mutex> guard(*gfsm_mutex);
gfsm_automaton_set_semiring_type(_fsm, static_cast<gfsmSRType>(sr));
}

Expand All @@ -103,34 +93,28 @@ gfsmStateId Automaton::root() {
}

void Automaton::arcsort() {
std::lock_guard<std::mutex> guard(*gfsm_mutex);
gfsm_automaton_arcsort(_fsm, gfsmASMLower);
}

void Automaton::arcuniq() {
std::lock_guard<std::mutex> guard(*gfsm_mutex);
gfsm_automaton_arcuniq(_fsm);
}

void Automaton::arith_sr_zero_to_zero() {
std::lock_guard<std::mutex> guard(*gfsm_mutex);
gfsm_automaton_arith_state(_fsm, gfsmNoState, gfsmAOMult, 0,
gfsmNoLabel, gfsmNoLabel,
FALSE, FALSE, TRUE);
}

void Automaton::determinize() {
std::lock_guard<std::mutex> guard(*gfsm_mutex);
gfsm_automaton_determinize(_fsm);
}

void Automaton::minimize(bool remove_eps) {
std::lock_guard<std::mutex> guard(*gfsm_mutex);
gfsm_automaton_minimize_full(_fsm, static_cast<gboolean>(remove_eps));
}

std::set<Path> Automaton::accepted_paths() const {
std::lock_guard<std::mutex> guard(*gfsm_mutex);
return find_accepted_paths();
}

Expand Down Expand Up @@ -165,7 +149,6 @@ std::set<Path> Automaton::find_accepted_paths() const {
}

void Automaton::set_gfsm_automaton(gfsmAutomaton* fsm) {
std::lock_guard<std::mutex> guard(*gfsm_mutex);
gfsm_automaton_free(_fsm);
_fsm = fsm;
}
Expand Down
8 changes: 2 additions & 6 deletions src/gfsm/automaton.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@
#ifndef GFSM_AUTOMATON_H_
#define GFSM_AUTOMATON_H_
#include<string>
#include<mutex>
#include<set>
#include"gfsmlibs.h"
#include"semiring.h"
#include"path.h"

namespace Gfsm {
class LabelVector;
class AutomatonBuilder;
class Cascade;

/// A finite-state automaton.
Expand All @@ -35,9 +33,10 @@ class Cascade;
use the specialized Acceptor or Transducer classes.
*/
class Automaton {
friend class AutomatonBuilder;
friend class Cascade;
public:
Automaton() : Automaton(SemiringType::TROPICAL) {}
Automaton(SemiringType sr);
Automaton(const Automaton& a);
Automaton(Automaton&& a);
Automaton& operator=(Automaton a);
Expand Down Expand Up @@ -75,11 +74,8 @@ class Automaton {
void ensure_root() { root(); }

protected:
Automaton() = delete;
Automaton(std::mutex* m, SemiringType sr = SemiringType::TROPICAL);
gfsmAutomaton* _fsm; /**< Pointer to automaton object. */
gfsmStateId _root = gfsmNoState; /**< ID of the root state. */
std::mutex* gfsm_mutex; /**< Mutex for C functions. */

void set_gfsm_automaton(gfsmAutomaton* fsm);

Expand Down
59 changes: 0 additions & 59 deletions src/gfsm/builder.cpp

This file was deleted.

74 changes: 0 additions & 74 deletions src/gfsm/builder.h

This file was deleted.

Loading

0 comments on commit 21076d5

Please sign in to comment.