aboutsummaryrefslogtreecommitdiffstats
path: root/staging_vespalib/src/vespa/vespalib/util/librarypool.cpp
blob: 2a3ca21c369161c04c748a0577bcbac1c58d32d8 (plain) (blame)
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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/util/librarypool.h>
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vespalib/util/stringfmt.h>

namespace vespalib {

LibraryPool::LibraryPool() :
    _libraries(),
    _lock()
{
}

LibraryPool::~LibraryPool()
{
    LockGuard guard(_lock);
    _libraries.clear();
}

void
LibraryPool::loadLibrary(stringref libName)
{
    LockGuard guard(_lock);
    if (_libraries.find(libName) == _libraries.end()) {
        DynamicLibrarySP lib(new FastOS_DynamicLibrary);
        string file(libName);
        if (!lib->Open(file.c_str())) {
            string error = lib->GetLastErrorString();
            throw IllegalArgumentException(make_string("Failed loading dynamic library '%s' due to '%s'.",
                      file.c_str(), error.c_str()));
        } else {
            _libraries[libName] = lib;
        }
    }
}

FastOS_DynamicLibrary *
LibraryPool::get(stringref name)
{
    LockGuard guard(_lock);
    LibraryMap::const_iterator found(_libraries.find(name));
    return (found != _libraries.end())
               ? found->second.get()
               : NULL;
}

const FastOS_DynamicLibrary *
LibraryPool::get(stringref name) const
{
    LockGuard guard(_lock);
    LibraryMap::const_iterator found(_libraries.find(name));
    return (found != _libraries.end())
               ? found->second.get()
               : NULL;
}

}