armv7_mutex.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* $OpenBSD: armv7_mutex.c,v 1.2 2013/05/09 14:27:17 patrick Exp $ */
  2. /*
  3. * Copyright (c) 2004 Artur Grabowski <art@openbsd.org>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. The name of the author may not be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  16. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  18. * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  21. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  22. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  23. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  24. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <sys/param.h>
  27. #include <sys/mutex.h>
  28. #include <sys/systm.h>
  29. #include <machine/intr.h>
  30. #ifdef MULTIPROCESSOR
  31. #error This code needs work
  32. #endif
  33. /* rewrite with the proper lock mechanism */
  34. /*
  35. * Single processor systems don't need any mutexes, but they need the spl
  36. * raising semantics of the mutexes.
  37. */
  38. void
  39. mtx_init(struct mutex *mtx, int wantipl)
  40. {
  41. mtx->mtx_oldipl = 0;
  42. mtx->mtx_wantipl = wantipl;
  43. mtx->mtx_lock = 0;
  44. }
  45. void
  46. mtx_enter(struct mutex *mtx)
  47. {
  48. if (mtx->mtx_wantipl != IPL_NONE)
  49. mtx->mtx_oldipl = _splraise(mtx->mtx_wantipl);
  50. MUTEX_ASSERT_UNLOCKED(mtx);
  51. mtx->mtx_lock = 1;
  52. #ifdef DIAGNOSTIC
  53. curcpu()->ci_mutex_level++;
  54. #endif
  55. }
  56. int
  57. mtx_enter_try(struct mutex *mtx)
  58. {
  59. if (mtx->mtx_wantipl != IPL_NONE)
  60. mtx->mtx_oldipl = _splraise(mtx->mtx_wantipl);
  61. if (mtx->mtx_lock) {
  62. splx(mtx->mtx_oldipl);
  63. return 0;
  64. }
  65. mtx->mtx_lock = 1;
  66. #ifdef DIAGNOSTIC
  67. curcpu()->ci_mutex_level++;
  68. #endif
  69. return 1;
  70. }
  71. void
  72. mtx_leave(struct mutex *mtx)
  73. {
  74. MUTEX_ASSERT_LOCKED(mtx);
  75. mtx->mtx_lock = 0;
  76. #ifdef DIAGNOSTIC
  77. curcpu()->ci_mutex_level--;
  78. #endif
  79. if (mtx->mtx_wantipl != IPL_NONE)
  80. splx(mtx->mtx_oldipl);
  81. }