// // Copyright (c) ZeroC, Inc. All rights reserved. // #ifndef ICE_GRID_ALLOCATABLE_H #define ICE_GRID_ALLOCATABLE_H #include #include #include #include namespace IceGrid { class Allocatable; class SessionI; class AllocationRequest : public IceUtil::TimerTask, public std::enable_shared_from_this { public: virtual ~AllocationRequest() = default; virtual void allocated(const std::shared_ptr&, const std::shared_ptr&) = 0; virtual void canceled(std::exception_ptr) = 0; bool pending(); bool allocate(const std::shared_ptr&, const std::shared_ptr&); void cancel(std::exception_ptr); void runTimerTask() override; int getTimeout() const { return _timeout; } const std::shared_ptr& getSession() const { return _session; } bool isCanceled() const; bool operator<(const AllocationRequest&) const; protected: AllocationRequest(const std::shared_ptr&); private: enum State { Initial, Pending, Canceled, Allocated }; const std::shared_ptr _session; const int _timeout; State _state; mutable std::mutex _mutex; }; class Allocatable: public virtual std::enable_shared_from_this { public: Allocatable(bool, const std::shared_ptr&); virtual ~Allocatable() = default; virtual void checkAllocatable(); virtual bool allocate(const std::shared_ptr&, bool = false); virtual bool tryAllocate(const std::shared_ptr&, bool = false); virtual void release(const std::shared_ptr&, bool = false); bool isAllocatable() const { return _allocatable; } std::shared_ptr getSession() const; virtual bool isEnabled() const = 0; virtual void allocated(const std::shared_ptr&) = 0; virtual void released(const std::shared_ptr&) = 0; virtual bool canTryAllocate() { return false; } virtual void allocatedNoSync(const std::shared_ptr&) {} virtual void releasedNoSync(const std::shared_ptr&) {} bool operator<(const Allocatable&) const; protected: bool allocate(const std::shared_ptr&, bool, bool); void queueAllocationAttemptFromChild(const std::shared_ptr&); bool allocateFromChild(const std::shared_ptr&, const std::shared_ptr&, bool, bool); void queueAllocationAttempt(const std::shared_ptr&, const std::shared_ptr&, bool); std::shared_ptr dequeueAllocationAttempt(std::shared_ptr&); bool _allocatable; const std::shared_ptr _parent; std::list, std::shared_ptr>> _requests; std::shared_ptr _session; int _count; bool _releasing; mutable std::mutex _mutex; std::condition_variable _condVar; }; }; #endif