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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
|
// **********************************************************************
//
// Copyright (c) 2003-2017 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 <Ice/ImplicitContextI.h>
#include <Ice/OutputStream.h>
#include <Ice/Object.h>
#include <IceUtil/Mutex.h>
#include <IceUtil/MutexPtrLock.h>
using namespace std;
using namespace Ice;
namespace
{
class SharedImplicitContext : public ImplicitContextI
{
public:
virtual Context getContext() const;
virtual void setContext(const Context&);
virtual bool containsKey(const string&) const;
virtual string get(const string&) const;
virtual string put(const string&, const string&);
virtual string remove(const string&);
virtual void write(const Context&, ::Ice::OutputStream*) const;
virtual void combine(const Context&, Context&) const;
private:
Context _context;
IceUtil::Mutex _mutex;
};
class PerThreadImplicitContext : public ImplicitContextI
{
public:
PerThreadImplicitContext();
virtual ~PerThreadImplicitContext();
virtual Context getContext() const;
virtual void setContext(const Context&);
virtual bool containsKey(const string&) const;
virtual string get(const string&) const;
virtual string put(const string&, const string&);
virtual string remove(const string&);
virtual void write(const Context&, ::Ice::OutputStream*) const;
virtual void combine(const Context&, Context&) const;
struct Slot
{
Slot() :
context(0),
owner(-1) // just to avoid UMR; a random value would work as well
{
}
Context* context;
long owner;
};
//
// Each thread maintains a SlotVector. Each PerThreadImplicitContext instance
// is assigned a slot in this vector.
//
typedef std::vector<Slot> SlotVector;
//
// We remember which slot-indices are in use (to be able to reuse indices)
//
typedef std::vector<bool> IndexInUse;
static IndexInUse* _indexInUse;
static IceUtil::Mutex* _mutex;
static long _nextId;
static long _destroyedIds;
static size_t _slotVectors;
#ifdef _WIN32
static DWORD _key;
#else
static pthread_key_t _key;
#endif
static void tryCleanupKey(); // must be called with _mutex locked
private:
Context* getThreadContext(bool) const;
void clearThreadContext() const;
size_t _index; // index in all SlotVector
long _id; // corresponds to owner in the Slot
};
}
extern "C" void iceImplicitContextThreadDestructor(void*);
ImplicitContextIPtr
ImplicitContextI::create(const std::string& kind)
{
if(kind == "None" || kind == "")
{
return 0;
}
else if(kind == "Shared")
{
return ICE_MAKE_SHARED(SharedImplicitContext);
}
else if(kind == "PerThread")
{
return ICE_MAKE_SHARED(PerThreadImplicitContext);
}
else
{
throw Ice::InitializationException(
__FILE__, __LINE__,
"'" + kind + "' is not a valid value for Ice.ImplicitContext");
return 0; // Keep the compiler happy.
}
}
#if defined(_WIN32)
void
ImplicitContextI::cleanupThread()
{
if(PerThreadImplicitContext::_nextId > 0)
{
iceImplicitContextThreadDestructor(TlsGetValue(PerThreadImplicitContext::_key));
}
}
#endif
//
// SharedImplicitContext implementation
//
Context
SharedImplicitContext::getContext() const
{
IceUtil::Mutex::Lock lock(_mutex);
return _context;
}
void
SharedImplicitContext::setContext(const Context& newContext)
{
IceUtil::Mutex::Lock lock(_mutex);
_context = newContext;
}
bool
SharedImplicitContext::containsKey(const string& k) const
{
IceUtil::Mutex::Lock lock(_mutex);
Context::const_iterator p = _context.find(k);
return p != _context.end();
}
string
SharedImplicitContext::get(const string& k) const
{
IceUtil::Mutex::Lock lock(_mutex);
Context::const_iterator p = _context.find(k);
if(p == _context.end())
{
return "";
}
return p->second;
}
string
SharedImplicitContext::put(const string& k, const string& v)
{
IceUtil::Mutex::Lock lock(_mutex);
string& val = _context[k];
string oldVal = val;
val = v;
return oldVal;
}
string
SharedImplicitContext::remove(const string& k)
{
IceUtil::Mutex::Lock lock(_mutex);
Context::iterator p = _context.find(k);
if(p == _context.end())
{
return "";
}
else
{
string oldVal = p->second;
_context.erase(p);
return oldVal;
}
}
void
SharedImplicitContext::write(const Context& proxyCtx, ::Ice::OutputStream* s) const
{
IceUtil::Mutex::Lock lock(_mutex);
if(proxyCtx.size() == 0)
{
s->write(_context);
}
else if(_context.size() == 0)
{
lock.release();
s->write(proxyCtx);
}
else
{
Context combined = proxyCtx;
combined.insert(_context.begin(), _context.end());
lock.release();
s->write(combined);
}
}
void
SharedImplicitContext::combine(const Context& proxyCtx, Context& ctx) const
{
IceUtil::Mutex::Lock lock(_mutex);
if(proxyCtx.size() == 0)
{
ctx = _context;
}
else if(_context.size() == 0)
{
ctx = proxyCtx;
}
else
{
ctx = proxyCtx;
ctx.insert(_context.begin(), _context.end());
}
}
//
// PerThreadImplicitContext implementation
//
long PerThreadImplicitContext::_nextId;
long PerThreadImplicitContext::_destroyedIds;
size_t PerThreadImplicitContext::_slotVectors;
PerThreadImplicitContext::IndexInUse* PerThreadImplicitContext::_indexInUse;
IceUtil::Mutex* PerThreadImplicitContext::_mutex = 0;
namespace
{
class Init
{
public:
Init()
{
PerThreadImplicitContext::_mutex = new IceUtil::Mutex;
}
~Init()
{
delete PerThreadImplicitContext::_mutex;
PerThreadImplicitContext::_mutex = 0;
}
};
Init init;
}
# ifdef _WIN32
DWORD PerThreadImplicitContext::_key;
# else
pthread_key_t PerThreadImplicitContext::_key;
# endif
PerThreadImplicitContext::PerThreadImplicitContext()
{
IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(_mutex);
_id = _nextId++;
if(_id == 0)
{
# ifdef _WIN32
_key = TlsAlloc();
if(_key == TLS_OUT_OF_INDEXES)
{
throw IceUtil::ThreadSyscallException(__FILE__, __LINE__, GetLastError());
}
# else
int err = pthread_key_create(&_key, &iceImplicitContextThreadDestructor);
if(err != 0)
{
throw IceUtil::ThreadSyscallException(__FILE__, __LINE__, err);
}
# endif
}
//
// Now grab an index
//
if(_indexInUse == 0)
{
_indexInUse = new IndexInUse(1);
}
size_t i = 0;
while(i < _indexInUse->size() && (*_indexInUse)[i])
{
i++;
}
if(i == _indexInUse->size())
{
_indexInUse->resize(i + 1);
}
(*_indexInUse)[i] = true;
_index = i;
}
PerThreadImplicitContext::~PerThreadImplicitContext()
{
IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(_mutex);
(*_indexInUse)[_index] = false;
if(find(_indexInUse->begin(), _indexInUse->end(), true) == _indexInUse->end())
{
delete _indexInUse;
_indexInUse = 0;
}
_destroyedIds++;
tryCleanupKey();
}
void
PerThreadImplicitContext::tryCleanupKey()
{
if(_destroyedIds == _nextId && _slotVectors == 0)
{
//
// We can do a full reset
//
_nextId = 0;
_destroyedIds = 0;
# ifdef _WIN32
TlsFree(_key);
# else
pthread_key_delete(_key);
# endif
}
}
Context*
PerThreadImplicitContext::getThreadContext(bool allocate) const
{
# ifdef _WIN32
SlotVector* sv = static_cast<SlotVector*>(TlsGetValue(_key));
# else
SlotVector* sv = static_cast<SlotVector*>(pthread_getspecific(_key));
# endif
if(sv == 0)
{
if(!allocate)
{
return 0;
}
{
IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(_mutex);
sv = new SlotVector(_index + 1);
_slotVectors++;
}
# ifdef _WIN32
if(TlsSetValue(_key, sv) == 0)
{
throw IceUtil::ThreadSyscallException(__FILE__, __LINE__, GetLastError());
}
# else
if(int err = pthread_setspecific(_key, sv))
{
throw IceUtil::ThreadSyscallException(__FILE__, __LINE__, err);
}
# endif
}
else
{
if(sv->size() <= _index)
{
if(!allocate)
{
return 0;
}
else
{
sv->resize(_index + 1);
}
}
}
Slot& slot = (*sv)[_index];
if(slot.context != 0)
{
if(slot.owner != _id)
{
//
// Reuse the slot from another (dead) communicator
//
slot.context->clear();
slot.owner = _id;
}
//
// else keep this slot.context
//
}
else
{
if(allocate)
{
slot.context = new Context;
slot.owner = _id;
}
//
// else keep null slot.context
//
}
return slot.context;
}
void
PerThreadImplicitContext::clearThreadContext() const
{
# ifdef _WIN32
SlotVector* sv = static_cast<SlotVector*>(TlsGetValue(_key));
# else
SlotVector* sv = static_cast<SlotVector*>(pthread_getspecific(_key));
# endif
if(sv != 0 && _index < sv->size())
{
delete (*sv)[_index].context;
(*sv)[_index].context = 0;
//
// Trim tailing empty contexts.
//
size_t i = sv->size();
bool clear = true;
while(i != 0)
{
i--;
if((*sv)[i].context != 0)
{
clear = false;
break;
}
}
//
// If we did not find any contexts, delete the SlotVector.
//
if(clear)
{
delete sv;
# ifdef _WIN32
if(TlsSetValue(_key, 0) == 0)
{
IceUtil::ThreadSyscallException(__FILE__, __LINE__, GetLastError());
}
# else
if(int err = pthread_setspecific(_key, 0))
{
throw IceUtil::ThreadSyscallException(__FILE__, __LINE__, err);
}
{
IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(_mutex);
_slotVectors--;
}
# endif
}
else
{
sv->resize(i + 1);
}
}
}
Context
PerThreadImplicitContext::getContext() const
{
Context* ctx = getThreadContext(false);
if(ctx == 0)
{
return Context();
}
else
{
return *ctx;
}
}
void
PerThreadImplicitContext::setContext(const Context& newContext)
{
if(newContext.size() == 0)
{
clearThreadContext();
}
else
{
Context* ctx = getThreadContext(true);
assert(ctx != 0);
*ctx = newContext;
}
}
bool
PerThreadImplicitContext::containsKey(const string& k) const
{
const Context* ctx = getThreadContext(false);
if(ctx == 0)
{
return false;
}
Context::const_iterator p = ctx->find(k);
return p != ctx->end();
}
string
PerThreadImplicitContext::get(const string& k) const
{
const Context* ctx = getThreadContext(false);
if(ctx == 0)
{
return "";
}
Context::const_iterator p = ctx->find(k);
if(p == ctx->end())
{
return "";
}
return p->second;
}
string
PerThreadImplicitContext::put(const string& k, const string& v)
{
Context* ctx = getThreadContext(true);
string& val = (*ctx)[k];
string oldVal = val;
val = v;
return oldVal;
}
string
PerThreadImplicitContext::remove(const string& k)
{
Context* ctx = getThreadContext(false);
if(ctx == 0)
{
return "";
}
Context::iterator p = ctx->find(k);
if(p == ctx->end())
{
return "";
}
else
{
string oldVal = p->second;
ctx->erase(p);
if(ctx->size() == 0)
{
clearThreadContext();
}
return oldVal;
}
}
void
PerThreadImplicitContext::write(const Context& proxyCtx, ::Ice::OutputStream* s) const
{
const Context* threadCtx = getThreadContext(false);
if(threadCtx == 0 || threadCtx->size() == 0)
{
s->write(proxyCtx);
}
else if(proxyCtx.size() == 0)
{
s->write(*threadCtx);
}
else
{
Context combined = proxyCtx;
combined.insert(threadCtx->begin(), threadCtx->end());
s->write(combined);
}
}
void
PerThreadImplicitContext::combine(const Context& proxyCtx, Context& ctx) const
{
const Context* threadCtx = getThreadContext(false);
if(threadCtx == 0 || threadCtx->size() == 0)
{
ctx = proxyCtx;
}
else if(proxyCtx.size() == 0)
{
ctx = *threadCtx;
}
else
{
ctx = proxyCtx;
ctx.insert(threadCtx->begin(), threadCtx->end());
}
}
extern "C" void iceImplicitContextThreadDestructor(void* v)
{
PerThreadImplicitContext::SlotVector* sv = static_cast<PerThreadImplicitContext::SlotVector*>(v);
if(sv != 0)
{
//
// Cleanup each slot
//
for(PerThreadImplicitContext::SlotVector::iterator p = sv->begin(); p != sv->end(); ++p)
{
delete p->context;
}
//
// Then the vector
//
delete sv;
{
IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(PerThreadImplicitContext::_mutex);
PerThreadImplicitContext::_slotVectors--;
PerThreadImplicitContext::tryCleanupKey();
}
}
}
|