#ifndef READER_CACHE_API_HPP #define READER_CACHE_API_HPP #include #include #include #include namespace readercache { struct Event { int kind; int object; int argument; }; struct Resource { bool allocated = false; bool initialized = false; int readers = 0; bool writer = false; }; class Pool { public: int allocate(); bool initialize(int object); bool enter_reader(int object); bool leave_reader(int object); bool enter_writer(int object); bool leave_writer(int object); bool destroy(int object); bool release(int object); void fail_next_initialization(); void clear_events(); const std::vector& events() const; std::vector counters() const; private: Resource* find(int object); void record(int kind, int object, int argument); bool reject(int kind, int object, int argument); std::map resources_; std::vector events_; int next_ = 1; int allocated_ = 0; int released_ = 0; int initialized_ = 0; int destroyed_ = 0; int violations_ = 0; bool fail_ = false; }; struct View { int handle; int phase; int resource; long long value; long long version; int readers; int writer; }; struct Ticket { int ticket; int handle; long long value; long long version; }; class Cache { public: explicit Cache(Pool& pool); ~Cache(); int create(int handle, long long initial); int write(int handle, long long value); int begin(int handle, int ticket, long long& value, long long& version); int end(int ticket); int close(int handle); int start_write(int handle); int commit(int handle, long long value); int abort(int handle); std::vector views() const; std::vector tickets() const; private: struct Impl; std::unique_ptr impl_; }; } #endif