summaryrefslogtreecommitdiff
path: root/cpp/src/IceGrid/Allocatable.h
blob: 008001eb92ad6e19b8013ba924077ae787449749 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// **********************************************************************
//
// Copyright (c) 2003-2006 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#ifndef ICE_GRID_ALLOCATABLE_H
#define ICE_GRID_ALLOCATABLE_H

#include <IceUtil/Handle.h>
#include <IceUtil/RecMutex.h>
#include <IceUtil/Shared.h>
#include <IceUtil/Time.h>

#include <IceGrid/WaitQueue.h>
#include <IceGrid/Session.h>

#include <list>
#include <set>

namespace IceGrid
{

class SessionI;
typedef IceUtil::Handle<SessionI> SessionIPtr;

class Allocatable;
typedef IceUtil::Handle<Allocatable> AllocatablePtr;

class AllocationRequest : public IceUtil::Mutex, public WaitItem
{
public:

    virtual ~AllocationRequest();
    
    virtual void allocated(const AllocatablePtr&) = 0;
    virtual void canceled(const AllocationException&) = 0;
    
    virtual bool allocateOnce() { return false; }

    bool pending();
    bool finish(const AllocatablePtr&);
    void cancel(const AllocationException&);
    void expired(bool);

    int getTimeout() const { return _timeout; }
    const SessionIPtr& getSession() const { return _session; }

    bool operator<(const AllocationRequest&) const;

protected:

    AllocationRequest(const SessionIPtr&);

private:

    enum State
    {
	Initial,
	Pending,
	Canceled,
	Allocated
    };

    const SessionIPtr _session;
    const int _timeout;
    State _state;
};
typedef IceUtil::Handle<AllocationRequest> AllocationRequestPtr;

class ParentAllocationRequest : public AllocationRequest
{
public:

    ParentAllocationRequest(const AllocationRequestPtr&, const AllocatablePtr&);
    
    virtual void allocated(const AllocatablePtr&);
    virtual void canceled(const AllocationException&);

private:
    
    const AllocationRequestPtr _request;
    const AllocatablePtr _allocatable;
};

class Allocatable : public IceUtil::Shared
{
public:

    Allocatable();
    virtual ~Allocatable();

    virtual void allocate(const AllocationRequestPtr&, bool = true);
    virtual bool tryAllocate(const AllocationRequestPtr&);
    virtual bool release(const SessionIPtr&);

    bool allocatable() const { return _allocatable; }
    bool isAllocated() const;
    SessionIPtr getSession() const;

    virtual void allocated(const SessionIPtr&) {  }
    virtual void released(const SessionIPtr&) {  }

    bool operator<(const Allocatable&) const;

protected:

    bool tryAllocateWithSession(const SessionIPtr&, const AllocatablePtr&);
    bool release(const SessionIPtr&, bool, std::set<AllocatablePtr>&);

    bool _allocatable;
    AllocatablePtr _parent;
    
    IceUtil::RecMutex _allocateMutex;
    std::list<AllocationRequestPtr> _requests;
    SessionIPtr _session;
    int _count;
    std::set<AllocatablePtr> _attempts;
};

};

#endif