summaryrefslogtreecommitdiff
path: root/cpp/src/IceGrid/Allocatable.cpp
blob: df21a3fbfdbe1d0503df5f6141eacf9b40f8082c (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// **********************************************************************
//
// 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.
//
// **********************************************************************

#include <IceGrid/Allocatable.h>
#include <IceGrid/SessionI.h>

using namespace std;
using namespace IceGrid;

AllocationRequest::~AllocationRequest()
{
}

bool
AllocationRequest::pending()
{
    Lock sync(*this);
    assert(_state == Initial);
    
    if(_timeout == 0)
    {
	_state = Canceled;
	canceled(AllocationTimeoutException());
	return false;
    }
    else if(!_session->addAllocationRequest(this))
    {
	_state = Canceled;
	canceled(AllocationException("session destroyed"));
	return false;
    }

    if(_timeout > 0)
    {
	_session->getWaitQueue()->add(this, IceUtil::Time::milliSeconds(_timeout));
    }
    _state = Pending;
    return true;
}

bool
AllocationRequest::allocate(const AllocatablePtr& allocatable, const SessionIPtr& session)
{
    Lock sync(*this);
    switch(_state)
    {
    case Initial:
	break;
    case Canceled:
	return false;
    case Pending:
	if(_timeout > 0)
	{
	    _session->getWaitQueue()->remove(this);
	}
	_session->removeAllocationRequest(this);
	break;
    case Allocated:
	assert(false);
	break;
    }

    //
    // Check if the allocatable is already allocated by the session.
    //
    if(_session == session)
    {
	_state = Canceled;
	canceled(AllocationException("already allocated by the session"));
	return false;
    }
    else
    {
	_state = Allocated;
	return true;
    }
}

void 
AllocationRequest::cancel(const AllocationException& ex) 
{
    Lock sync(*this);
    switch(_state)
    {
    case Initial:
	break;
    case Canceled:
    case Allocated:
	return;
    case Pending:
	if(_timeout > 0)
	{
	    _session->getWaitQueue()->remove(this);
	}
	_session->removeAllocationRequest(this);
	break;
    }    

    _state = Canceled;
    canceled(ex);
}

void
AllocationRequest::expired(bool destroyed)
{
    Lock sync(*this);
    switch(_state)
    {
    case Initial:
	assert(false);
    case Canceled:
    case Allocated:
	return;
    case Pending:
	_session->removeAllocationRequest(this);
	break;
    }
    
    _state = Canceled;
    canceled(AllocationTimeoutException());
}

bool
AllocationRequest::isCanceled() const
{
    Lock sync(*this);
    return _state == Canceled;
}

bool
AllocationRequest::operator<(const AllocationRequest& r) const
{
    return this < &r;
}

AllocationRequest::AllocationRequest(const SessionIPtr& session) :
    _session(session),
    _timeout(_session->getAllocationTimeout()), // The session timeout can be updated so we need to cache it here.
    _state(Initial)
{
}

Allocatable::Allocatable(bool allocatable, const AllocatablePtr& parent) : 
    _allocatable(allocatable || parent && parent->isAllocatable()),
    _count(0),
    _releasing(false)
{
    //
    // COMPILERFIX: the constructor initializaton:
    //
    //     _parent((parent && parent->isAllocatable()) ? parent : AllocatablePtr())
    //
    // doesn't work on HP-UX (aCC: HP ANSI C++ B3910B A.03.56). It
    // results in a SEGFAULT at runtime.
    //
    if(parent && parent->isAllocatable())
    {
        const_cast<AllocatablePtr&>(_parent) = parent;
    }
    assert(!_parent || _parent->isAllocatable()); // Parent is only set if it's allocatable.
}

Allocatable::~Allocatable()
{
}

void
Allocatable::checkAllocatable()
{
    if(!isAllocatable())
    {
	throw AllocationException("not allocatable");
    }
}

bool
Allocatable::allocate(const AllocationRequestPtr& request, bool fromRelease)
{
    try
    {
	return allocate(request, false, fromRelease);
    }
    catch(const SessionDestroyedException&)
    {
	return false; // The session was destroyed
    }
}

bool
Allocatable::tryAllocate(const AllocationRequestPtr& request, bool fromRelease)
{
    try
    {
	return allocate(request, true, fromRelease);
    }
    catch(const AllocationException&)
    {
	return false; // Not allocatable
    }
}

bool
Allocatable::release(const SessionIPtr& session, bool fromRelease)
{
    bool isReleased = false;
    bool hasRequests = false;
    {
	Lock sync(*this);
	if(!fromRelease)
	{
	    while(_releasing)
	    {
		wait();
	    }
	}

	if(!_session || _session != session)
	{
	    throw AllocationException("can't release object which is not allocated");
	}
	
	if(--_count == 0)
	{
	    _session = 0;

	    released(session);

	    if(!_releasing)
	    {
		if(!_requests.empty())
		{
		    assert(!_parent);
		    _releasing = true; // Prevent new allocations.
		    hasRequests = true;
		}
		
		isReleased = true;
	    }
	}
    }

    if(_parent)
    {
	return _parent->release(session, fromRelease);
    }

    if(hasRequests)
    {
	while(true)
	{
	    AllocationRequestPtr request;
	    AllocatablePtr allocatable = dequeueAllocationAttempt(request);
	    if(!allocatable)
	    {
		Lock sync(*this);
		assert(_count == 0 && _requests.empty());
		_releasing = false;
		notifyAll();
		return true;
	    }
	    
	    //
	    // Try to allocate the allocatable with the request or if
	    // there's no request, just notify the allocatable that it can
	    // be allocated again.
	    //
	    if(request && allocatable->allocate(request, true) || !request && allocatable->canTryAllocate())
	    {
		while(true)
		{
		    {
			Lock sync(*this);
			assert(_count);
			
			allocatable = 0;
			request = 0;

			//
			// Check if there's other requests from the session
			// waiting to allocate this allocatable.
			//
			list<pair<AllocatablePtr, AllocationRequestPtr> >::iterator p = _requests.begin();
			while(p != _requests.end())
			{
			    if(p->second && p->second->getSession() == _session)
			    {
				allocatable = p->first;
				request = p->second;
				_requests.erase(p);
				break;
			    }
			    ++p;
			}			
			if(!allocatable)
			{
			    _releasing = false;
			    notifyAll();
			    return true; // We're done, the allocatable was released (but is allocated again)!
			}
		    }

		    assert(allocatable && request);
		    allocatable->allocate(request, true);
		}
	    }
	}
    }
    else if(isReleased)
    {
	canTryAllocate(); // Notify that this allocatable can be allocated.
	return true; // The allocatable was released.
    }
    return false;
}

SessionIPtr
Allocatable::getSession() const
{
    Lock sync(*this);
    return _session;
}

bool
Allocatable::operator<(const Allocatable& r) const
{
    return this < &r;
}

void
Allocatable::queueAllocationAttempt(const AllocatablePtr& allocatable, 
				    const AllocationRequestPtr& request, 
				    bool tryAllocate)
{
    assert(!_parent);
    if(!tryAllocate)
    {
	if(request->pending())
	{
	    _requests.push_back(make_pair(allocatable, request));
	}
    }
    else
    {
	if(_attempts.insert(allocatable).second)
	{
	    _requests.push_back(make_pair(allocatable, AllocationRequestPtr()));
	}
    }    
}

AllocatablePtr
Allocatable::dequeueAllocationAttempt(AllocationRequestPtr& request)
{
    Lock sync(*this);
    if(_requests.empty())
    {
	return 0;
    }

    pair<AllocatablePtr, AllocationRequestPtr> alloc = _requests.front();
    _requests.pop_front();
    if(alloc.second)
    {
	request = alloc.second;
    }
    else
    {
	_attempts.erase(alloc.first);
    }
    return alloc.first;
}

bool
Allocatable::allocate(const AllocationRequestPtr& request, bool tryAllocate, bool fromRelease)
{
    if(_parent && !_parent->allocateFromChild(request, this, tryAllocate, fromRelease))
    {
	return false;
    }

    try
    {
	Lock sync(*this);
	checkAllocatable();

	if(!_session && (fromRelease || !_releasing))
	{
	    if(request->allocate(this, _session))
	    {
		try
		{
		    allocated(request->getSession()); // This might throw SessionDestroyedException
		    request->allocated(this, request->getSession());
		}
		catch(const SessionDestroyedException&)
		{
		    request->canceled(AllocationException("session destroyed"));
		    throw;
		}
		assert(_count == 0);
		_session = request->getSession();
		++_count;
		return true; // Allocated
	    }
	}
	else if(_session == request->getSession())
	{
	    if(!tryAllocate && request->allocate(this, _session))
	    {
		assert(_count > 0);
		++_count;
		request->allocated(this, _session);
		return true; // Allocated
	    }
	}
	else
	{
	    queueAllocationAttempt(this, request, tryAllocate);
	}
    }
    catch(const SessionDestroyedException& ex)
    {
	if(_parent)
	{
	    _parent->release(request->getSession(), fromRelease);
	}
	throw ex;
    }
    catch(const AllocationException& ex)
    {
	if(_parent)
	{
	    _parent->release(request->getSession(), fromRelease);
	}
	throw ex;
    }
    
    if(_parent)
    {
	_parent->release(request->getSession(), fromRelease);
    }
    return false;
}

bool
Allocatable::allocateFromChild(const AllocationRequestPtr& request, 
			       const AllocatablePtr& child, 
			       bool tryAllocate,
			       bool fromRelease)
{
    assert(_allocatable);

    if(_parent && !_parent->allocateFromChild(request, child, tryAllocate, fromRelease))
    {
	return false;
    }

    Lock sync(*this);
    if((!_session || _session == request->getSession()) && (fromRelease || !_releasing))
    {
	if(!_session)
	{
	    try
	    {
		allocated(request->getSession());
	    }
	    catch(const SessionDestroyedException&)
	    {
		// Ignore
	    }
	}
	_session = request->getSession();
	++_count;
	return true; // Allocated	    
    }
    else
    {
	queueAllocationAttempt(child, request, tryAllocate);
    }

    return false;
}