summaryrefslogtreecommitdiff
path: root/cpp/src/IceGrid/Allocatable.h
blob: 0e31503622367b25623a9916c0dbcc85fa14278c (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
// **********************************************************************
//
// 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/Mutex.h>
#include <IceUtil/Shared.h>
#include <IceUtil/Time.h>
#include <list>

namespace IceGrid
{

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

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

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

    virtual ~AllocationRequest();
    
    virtual void timeout() = 0;
    virtual void allocated(const AllocatablePtr&) = 0;
    virtual void canceled() = 0;

    bool setAllocatable(const AllocatablePtr&);
    bool checkTimeout(const IceUtil::Time&);
    void cancel();
    void allocate();
    void release(const SessionIPtr&);

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

    bool operator<(const AllocationRequest&) const;

protected:

    AllocationRequest(const SessionIPtr&);

private:

    const SessionIPtr _session;
    const int _timeout;
    const IceUtil::Time _expiration;
    bool _canceled;
    AllocatablePtr _allocatable;
};
typedef IceUtil::Handle<AllocationRequest> AllocationRequestPtr;

class Allocatable : public IceUtil::Shared
{
public:

    Allocatable(bool);
    virtual ~Allocatable();

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

    bool allocatable() const { return _allocatable; }

protected:

    bool _allocatable;
    IceUtil::Mutex _allocateMutex;
    std::list<AllocationRequestPtr> _requests;
    AllocationRequestPtr _allocated;
};

};

#endif