locks.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "hdfspp/locks.h"
  19. #include <mutex>
  20. namespace hdfs {
  21. LockGuard::LockGuard(Mutex *m) : _mtx(m) {
  22. if(!m) {
  23. throw LockFailure("LockGuard passed invalid (null) Mutex pointer");
  24. }
  25. _mtx->lock();
  26. }
  27. LockGuard::~LockGuard() {
  28. if(_mtx) {
  29. _mtx->unlock();
  30. }
  31. }
  32. // Basic mutexes to use as default. Just a wrapper around C++11 std::mutex.
  33. class DefaultMutex : public Mutex {
  34. public:
  35. DefaultMutex() {}
  36. void lock() override {
  37. // Could throw in here if the implementation couldn't lock for some reason.
  38. _mtx.lock();
  39. }
  40. void unlock() override {
  41. _mtx.unlock();
  42. }
  43. std::string str() override {
  44. return "DefaultMutex";
  45. }
  46. private:
  47. std::mutex _mtx;
  48. };
  49. DefaultMutex defaultTestMutex;
  50. DefaultMutex defaultGssapiMutex;
  51. // LockManager static var instantiation
  52. Mutex *LockManager::TEST_default_mutex = &defaultTestMutex;
  53. Mutex *LockManager::gssapiMtx = &defaultGssapiMutex;
  54. std::mutex LockManager::_state_lock;
  55. bool LockManager::_finalized = false;
  56. bool LockManager::InitLocks(Mutex *gssapi) {
  57. std::lock_guard<std::mutex> guard(_state_lock);
  58. // You get once shot to set this - swapping the locks
  59. // out while in use gets risky. It can still be done by
  60. // using the Mutex as a proxy object if one understands
  61. // the implied risk of doing so.
  62. if(_finalized)
  63. return false;
  64. gssapiMtx = gssapi;
  65. _finalized = true;
  66. return true;
  67. }
  68. Mutex *LockManager::getGssapiMutex() {
  69. std::lock_guard<std::mutex> guard(_state_lock);
  70. return gssapiMtx;
  71. }
  72. Mutex *LockManager::TEST_get_default_mutex() {
  73. return TEST_default_mutex;
  74. }
  75. void LockManager::TEST_reset_manager() {
  76. _finalized = false;
  77. // user still responsible for cleanup
  78. gssapiMtx = &defaultGssapiMutex;
  79. }
  80. } // end namepace hdfs