resource.h 1.8 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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
X
xulongteng 已提交
16
#include <memory>
W
wangguibao 已提交
17
#include <string>
X
xulongteng 已提交
18
#include "cube/cube-api/include/cube_api.h"
19
#include "kvdb/paddle_rocksdb.h"
W
wangguibao 已提交
20 21
#include "predictor/common/inner_common.h"
#include "predictor/framework/memory.h"
W
wangguibao 已提交
22 23 24 25 26 27 28

namespace baidu {
namespace paddle_serving {
namespace predictor {

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

  ~DynamicResource();

  int initialize();

  int clear();
W
wangguibao 已提交
36 37 38
};

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

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

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

W
wangguibao 已提交
49
  int initialize(const std::string& path, const std::string& file);
X
xulongteng 已提交
50
  int cube_initialize(const std::string& path, const std::string& file);
W
wangguibao 已提交
51
  int thread_initialize();
W
wangguibao 已提交
52

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

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

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

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

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

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

W
wangguibao 已提交
70
  THREAD_KEY_T _tls_bspec_key;
W
wangguibao 已提交
71 72
};

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