i80321_mutex.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* $OpenBSD: i80321_mutex.c,v 1.3 2009/08/13 19:39:46 deraadt 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. /*
  34. * Single processor systems don't need any mutexes, but they need the spl
  35. * raising semantics of the mutexes.
  36. */
  37. void
  38. mtx_init(struct mutex *mtx, int wantipl)
  39. {
  40. mtx->mtx_oldipl = 0;
  41. mtx->mtx_wantipl = wantipl;
  42. mtx->mtx_lock = 0;
  43. }
  44. void
  45. mtx_enter(struct mutex *mtx)
  46. {
  47. if (mtx->mtx_wantipl != IPL_NONE)
  48. mtx->mtx_oldipl = _splraise(mtx->mtx_wantipl);
  49. MUTEX_ASSERT_UNLOCKED(mtx);
  50. mtx->mtx_lock = 1;
  51. }
  52. int
  53. mtx_enter_try(struct mutex *mtx)
  54. {
  55. if (mtx->mtx_wantipl != IPL_NONE)
  56. mtx->mtx_oldipl = _splraise(mtx->mtx_wantipl);
  57. MUTEX_ASSERT_UNLOCKED(mtx);
  58. mtx->mtx_lock = 1;
  59. return 1;
  60. }
  61. void
  62. mtx_leave(struct mutex *mtx)
  63. {
  64. MUTEX_ASSERT_LOCKED(mtx);
  65. mtx->mtx_lock = 0;
  66. if (mtx->mtx_wantipl != IPL_NONE)
  67. splx(mtx->mtx_oldipl);
  68. }