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>
W
wangguibao 已提交
17 18
#include "predictor/common/inner_common.h"
#include "predictor/framework/memory.h"
W
wangjiawei04 已提交
19
#include "kvdb/paddle_rocksdb.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
wangjiawei04 已提交
34

W
wangguibao 已提交
35 36 37
};

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

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

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

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

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

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

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

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

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

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

W
wangguibao 已提交
65 66
 private:
  int thread_finalize() { return 0; }
W
wangjiawei04 已提交
67 68
  std::shared_ptr<RocksDBWrapper> db;
  
W
wangguibao 已提交
69
  THREAD_KEY_T _tls_bspec_key;
W
wangguibao 已提交
70 71
};

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