#ifndef _MUTEXTREE_
#define _MUTEXTREE_

#include "ThreadsAndMutexes/mutexnode/mutexnode"
#include "config/config"

// This adds verbosity to the below class functions
// #define DEBUG

class MutexTree {
public:
    MutexTree();
    MutexTree(MutexTree const &other);
    ~MutexTree();

    MutexTree const &operator=(MutexTree const &other);

    void lock(void *o);
    void unlock(void *o);

    int weight() const;
    int balance() const;

private:
    void locktree();
    void unlocktree();
    MutexNode *nodelock(void *o, MutexNode *start);
    void nodeunlock(void *o, MutexNode *start);

    void copy(MutexTree const &other);
    void destroy();

    MutexNode *_root;
    Mutex _treelock;
};

#endif
