cnstream_rwlock.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*************************************************************************
  2. * Copyright (C) [2019] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #ifndef CNSTREAM_RWLOCK_H_
  21. #define CNSTREAM_RWLOCK_H_
  22. #include <pthread.h> // for pthread_rwlock_t
  23. #include <memory>
  24. #include <utility>
  25. namespace cnstream {
  26. // FIXME
  27. class RwLock {
  28. public:
  29. RwLock() { pthread_rwlock_init(&rwlock, NULL); }
  30. ~RwLock() { pthread_rwlock_destroy(&rwlock); }
  31. void wrlock() { pthread_rwlock_wrlock(&rwlock); }
  32. void rdlock() { pthread_rwlock_rdlock(&rwlock); }
  33. void unlock() { pthread_rwlock_unlock(&rwlock); }
  34. private:
  35. pthread_rwlock_t rwlock;
  36. };
  37. class RwLockWriteGuard {
  38. public:
  39. explicit RwLockWriteGuard(RwLock& lock) : lock_(lock) { lock_.wrlock(); }
  40. ~RwLockWriteGuard() { lock_.unlock(); }
  41. private:
  42. RwLock& lock_;
  43. };
  44. class RwLockReadGuard {
  45. public:
  46. explicit RwLockReadGuard(RwLock& lock) : lock_(lock) { lock_.rdlock(); }
  47. ~RwLockReadGuard() { lock_.unlock(); }
  48. private:
  49. RwLock& lock_;
  50. };
  51. } /* namespace cnstream */
  52. #endif /* CNSTREAM_RWLOCK_H_ */