You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.2 KiB

  1. /* -*- mode: C; c-basic-offset: 4 -*- */
  2. #ifndef TOKU_NBMUTEX_H
  3. #define TOKU_NBMUTEX_H
  4. #ident "$Id$"
  5. #ident "Copyright (c) 2007-2010 Tokutek Inc. All rights reserved."
  6. #ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it."
  7. #include <toku_assert.h>
  8. #if defined(__cplusplus) || defined(__cilkplusplus)
  9. extern "C" {
  10. #endif
  11. #include "rwlock.h"
  12. //Use case:
  13. // General purpose non blocking mutex with properties:
  14. // 1. one writer at a time
  15. // An external mutex must be locked when using these functions. An alternate
  16. // design would bury a mutex into the nb_mutex itself. While this may
  17. // increase parallelism at the expense of single thread performance, we
  18. // are experimenting with a single higher level lock.
  19. typedef struct nb_mutex *NB_MUTEX;
  20. struct nb_mutex {
  21. struct rwlock lock;
  22. };
  23. // initialize an nb mutex
  24. static __attribute__((__unused__))
  25. void
  26. nb_mutex_init(NB_MUTEX nb_mutex) {
  27. rwlock_init(&nb_mutex->lock);
  28. }
  29. // destroy a read write lock
  30. static __attribute__((__unused__))
  31. void
  32. nb_mutex_destroy(NB_MUTEX nb_mutex) {
  33. rwlock_destroy(&nb_mutex->lock);
  34. }
  35. // obtain a write lock
  36. // expects: mutex is locked
  37. static inline void nb_mutex_lock(NB_MUTEX nb_mutex,
  38. toku_pthread_mutex_t *mutex) {
  39. rwlock_write_lock(&nb_mutex->lock, mutex);
  40. }
  41. // release a write lock
  42. // expects: mutex is locked
  43. static inline void nb_mutex_write_unlock(NB_MUTEX nb_mutex) {
  44. rwlock_write_unlock(&nb_mutex->lock);
  45. }
  46. // returns: the number of writers who are waiting for the lock
  47. static inline int nb_mutex_blocked_writers(NB_MUTEX nb_mutex) {
  48. return rwlock_blocked_writers(&nb_mutex->lock);
  49. }
  50. // returns: the number of writers
  51. static inline int nb_mutex_writers(NB_MUTEX nb_mutex) {
  52. return rwlock_writers(&nb_mutex->lock);
  53. }
  54. // returns: the sum of the number of readers, pending readers,
  55. // writers, and pending writers
  56. static inline int nb_mutex_users(NB_MUTEX nb_mutex) {
  57. return rwlock_users(&nb_mutex->lock);
  58. }
  59. #if defined(__cplusplus) || defined(__cilkplusplus)
  60. };
  61. #endif
  62. #endif