mutex.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Pull in the generic implementation for the mutex fastpath.
  3. *
  4. * TODO: implement optimized primitives instead, or leave the generic
  5. * implementation in place, or pick the atomic_xchg() based generic
  6. * implementation. (see asm-generic/mutex-xchg.h for details)
  7. *
  8. * Copyright 2006-2009 Analog Devices Inc.
  9. *
  10. * Licensed under the GPL-2 or later.
  11. */
  12. #ifndef _ASM_MUTEX_H
  13. #define _ASM_MUTEX_H
  14. #ifndef CONFIG_SMP
  15. #include <asm-generic/mutex.h>
  16. #else
  17. static inline void
  18. __mutex_fastpath_lock(atomic_t *count, void (*fail_fn)(atomic_t *))
  19. {
  20. if (unlikely(atomic_dec_return(count) < 0))
  21. fail_fn(count);
  22. else
  23. smp_mb();
  24. }
  25. static inline int
  26. __mutex_fastpath_lock_retval(atomic_t *count, int (*fail_fn)(atomic_t *))
  27. {
  28. if (unlikely(atomic_dec_return(count) < 0))
  29. return fail_fn(count);
  30. else {
  31. smp_mb();
  32. return 0;
  33. }
  34. }
  35. static inline void
  36. __mutex_fastpath_unlock(atomic_t *count, void (*fail_fn)(atomic_t *))
  37. {
  38. smp_mb();
  39. if (unlikely(atomic_inc_return(count) <= 0))
  40. fail_fn(count);
  41. }
  42. #define __mutex_slowpath_needs_to_unlock() 1
  43. static inline int
  44. __mutex_fastpath_trylock(atomic_t *count, int (*fail_fn)(atomic_t *))
  45. {
  46. /*
  47. * We have two variants here. The cmpxchg based one is the best one
  48. * because it never induce a false contention state. It is included
  49. * here because architectures using the inc/dec algorithms over the
  50. * xchg ones are much more likely to support cmpxchg natively.
  51. *
  52. * If not we fall back to the spinlock based variant - that is
  53. * just as efficient (and simpler) as a 'destructive' probing of
  54. * the mutex state would be.
  55. */
  56. #ifdef __HAVE_ARCH_CMPXCHG
  57. if (likely(atomic_cmpxchg(count, 1, 0) == 1)) {
  58. smp_mb();
  59. return 1;
  60. }
  61. return 0;
  62. #else
  63. return fail_fn(count);
  64. #endif
  65. }
  66. #endif
  67. #endif