summaryrefslogtreecommitdiff
path: root/cpp/demo/Freeze/customEvictor/CurrentDatabase.cpp
blob: f4b93be87ea210ca7cbd7ae54e243d139d513177 (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
// **********************************************************************
//
// Copyright (c) 2003-2007 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 <Freeze/Freeze.h>
#include <CurrentDatabase.h>

using namespace std;
using namespace IceUtil;

//
// This implementation is very simple but not restartable, i.e., you
// can only create and destroy one CurrentDatabase per process run.
//

#if defined(_MSC_VER) || defined(__BCPLUSPLUS__)
   #define __thread __declspec(thread)
#endif

namespace
{

#ifdef __HP_aCC
//
// Strange HP aCC (or linker) bug, so we use directly the pthread calls
//
pthread_key_t dbKey;
#else
__thread Database* db = 0;
#endif

}

CurrentDatabase::CurrentDatabase(const Ice::CommunicatorPtr& comm, const string& envName, const string& dbName) :
    _communicator(comm),
    _envName(envName),
    _dbName(dbName)
{
#ifdef __HP_aCC
    int rs = pthread_key_create(&dbKey, 0);
    assert(rs == 0);
#endif
}

CurrentDatabase::~CurrentDatabase()
{
    for(list<Database*>::iterator p = _dbList.begin(); p != _dbList.end(); ++p)
    {
        delete *p;
    }
}

Database&
CurrentDatabase::get()
{
#ifdef __HP_aCC
    Database* db = static_cast<Database*>(pthread_getspecific(dbKey));
#endif

    if(db == 0)
    {
        Freeze::ConnectionPtr connection = Freeze::createConnection(_communicator, _envName);
        db = new Database(connection, _dbName);

        Mutex::Lock sync(_dbListMutex);
        _dbList.push_back(db);
#ifdef __HP_aCC
	int rs = pthread_setspecific(dbKey, db);
	assert(rs == 0);
#endif
    }
    return *db;
}