resource.h 1.6 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once
#include <string>
17
#include "kvdb/paddle_rocksdb.h"
W
wangguibao 已提交
18 19
#include "predictor/common/inner_common.h"
#include "predictor/framework/memory.h"
W
wangguibao 已提交
20 21 22 23 24 25 26

namespace baidu {
namespace paddle_serving {
namespace predictor {

class BaseRdDict;
struct DynamicResource {
W
wangguibao 已提交
27 28 29 30 31 32 33
  DynamicResource();

  ~DynamicResource();

  int initialize();

  int clear();
W
wangguibao 已提交
34 35 36
};

class Resource {
W
wangguibao 已提交
37 38
 public:
  Resource() {}
W
wangguibao 已提交
39

W
wangguibao 已提交
40
  ~Resource() { finalize(); }
W
wangguibao 已提交
41

W
wangguibao 已提交
42 43 44 45
  static Resource& instance() {
    static Resource ins;
    return ins;
  }
W
wangguibao 已提交
46

W
wangguibao 已提交
47
  int initialize(const std::string& path, const std::string& file);
W
wangguibao 已提交
48

W
wangguibao 已提交
49
  int thread_initialize();
W
wangguibao 已提交
50

W
wangguibao 已提交
51
  int thread_clear();
W
wangguibao 已提交
52

W
wangguibao 已提交
53
  int reload();
W
wangguibao 已提交
54

W
wangguibao 已提交
55
  int finalize();
W
wangguibao 已提交
56

W
wangjiawei04 已提交
57 58
  std::shared_ptr<RocksDBWrapper> getDB();

W
wangguibao 已提交
59 60 61 62
  DynamicResource* get_dynamic_resource() {
    return reinterpret_cast<DynamicResource*>(
        THREAD_GETSPECIFIC(_tls_bspec_key));
  }
W
wangguibao 已提交
63

W
wangguibao 已提交
64 65
 private:
  int thread_finalize() { return 0; }
W
wangjiawei04 已提交
66
  std::shared_ptr<RocksDBWrapper> db;
67

W
wangguibao 已提交
68
  THREAD_KEY_T _tls_bspec_key;
W
wangguibao 已提交
69 70
};

W
wangguibao 已提交
71 72 73
}  // namespace predictor
}  // namespace paddle_serving
}  // namespace baidu