shared_store.cc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* Copyright 2016 Google Inc. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #include "syntaxnet/shared_store.h"
  13. #include <unordered_map>
  14. #include "tensorflow/core/lib/strings/stringprintf.h"
  15. namespace syntaxnet {
  16. SharedStore::SharedObjectMap *SharedStore::shared_object_map_ =
  17. new SharedObjectMap;
  18. mutex SharedStore::shared_object_map_mutex_(tensorflow::LINKER_INITIALIZED);
  19. SharedStore::SharedObjectMap *SharedStore::shared_object_map() {
  20. return shared_object_map_;
  21. }
  22. bool SharedStore::Release(const void *object) {
  23. if (object == nullptr) {
  24. return true;
  25. }
  26. mutex_lock l(shared_object_map_mutex_);
  27. for (SharedObjectMap::iterator it = shared_object_map()->begin();
  28. it != shared_object_map()->end(); ++it) {
  29. if (it->second.object == object) {
  30. // Check the invariant that reference counts are positive. A violation
  31. // likely implies memory corruption.
  32. CHECK_GE(it->second.refcount, 1);
  33. it->second.refcount--;
  34. if (it->second.refcount == 0) {
  35. it->second.delete_callback();
  36. shared_object_map()->erase(it);
  37. }
  38. return true;
  39. }
  40. }
  41. return false;
  42. }
  43. void SharedStore::Clear() {
  44. mutex_lock l(shared_object_map_mutex_);
  45. for (SharedObjectMap::iterator it = shared_object_map()->begin();
  46. it != shared_object_map()->end(); ++it) {
  47. it->second.delete_callback();
  48. }
  49. shared_object_map()->clear();
  50. }
  51. string SharedStoreUtils::CreateDefaultName() { return string(); }
  52. string SharedStoreUtils::ToString(const string &input) {
  53. return ToString(tensorflow::StringPiece(input));
  54. }
  55. string SharedStoreUtils::ToString(const char *input) {
  56. return ToString(tensorflow::StringPiece(input));
  57. }
  58. string SharedStoreUtils::ToString(tensorflow::StringPiece input) {
  59. return tensorflow::strings::StrCat("\"", utils::CEscape(input.ToString()),
  60. "\"");
  61. }
  62. string SharedStoreUtils::ToString(bool input) {
  63. return input ? "true" : "false";
  64. }
  65. string SharedStoreUtils::ToString(float input) {
  66. return tensorflow::strings::Printf("%af", input);
  67. }
  68. string SharedStoreUtils::ToString(double input) {
  69. return tensorflow::strings::Printf("%a", input);
  70. }
  71. } // namespace syntaxnet