atomic_ops.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2006 iptelorg GmbH
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. /*
  19. * atomic operations init
  20. */
  21. /*
  22. * History:
  23. * --------
  24. * 2006-03-08 created by andrei
  25. * 2007-05-11 added lock_set support (andrei)
  26. */
  27. #include "atomic_ops_init.h"
  28. #include "atomic_ops.h"
  29. #if defined ATOMIC_OPS_USE_LOCK || defined ATOMIC_OPS_USE_LOCK_SET || \
  30. defined MEMBAR_USES_LOCK
  31. #include "locking.h"
  32. #endif
  33. #ifdef MEMBAR_USES_LOCK
  34. gen_lock_t* __membar_lock=0; /* init in atomic_ops.c */
  35. #endif
  36. #ifdef ATOMIC_OPS_USE_LOCK_SET
  37. gen_lock_set_t* _atomic_lock_set=0;
  38. #elif defined ATOMIC_OPS_USE_LOCK
  39. gen_lock_t* _atomic_lock=0;
  40. #endif
  41. /* returns 0 on success, -1 on error */
  42. int init_atomic_ops()
  43. {
  44. #ifdef MEMBAR_USES_LOCK
  45. if ((__membar_lock=lock_alloc())==0){
  46. goto error;
  47. }
  48. if (lock_init(__membar_lock)==0){
  49. lock_dealloc(__membar_lock);
  50. __membar_lock=0;
  51. goto error;
  52. }
  53. _membar_lock; /* start with the lock "taken" so that we can safely use
  54. unlock/lock sequences on it later */
  55. #endif
  56. #ifdef ATOMIC_OPS_USE_LOCK_SET
  57. if ((_atomic_lock_set=lock_set_alloc(_ATOMIC_LS_SIZE))==0){
  58. goto error;
  59. }
  60. if (lock_set_init(_atomic_lock_set)==0){
  61. lock_set_dealloc(_atomic_lock_set);
  62. _atomic_lock_set=0;
  63. goto error;
  64. }
  65. #elif defined ATOMIC_OPS_USE_LOCK
  66. if ((_atomic_lock=lock_alloc())==0){
  67. goto error;
  68. }
  69. if (lock_init(_atomic_lock)==0){
  70. lock_dealloc(_atomic_lock);
  71. _atomic_lock=0;
  72. goto error;
  73. }
  74. #endif /* ATOMIC_OPS_USE_LOCK_SET/ATOMIC_OPS_USE_LOCK */
  75. return 0;
  76. #if defined MEMBAR_USES_LOCK || defined ATOMIC_OPS_USE_LOCK || \
  77. defined ATOMIC_OPS_USE_LOCK_SET
  78. error:
  79. destroy_atomic_ops();
  80. return -1;
  81. #endif
  82. }
  83. void destroy_atomic_ops()
  84. {
  85. #ifdef MEMBAR_USES_LOCK
  86. if (__membar_lock!=0){
  87. lock_destroy(__membar_lock);
  88. lock_dealloc(__membar_lock);
  89. __membar_lock=0;
  90. }
  91. #endif
  92. #ifdef ATOMIC_OPS_USE_LOCK_SET
  93. if (_atomic_lock_set!=0){
  94. lock_set_destroy(_atomic_lock_set);
  95. lock_set_dealloc(_atomic_lock_set);
  96. _atomic_lock_set=0;
  97. }
  98. #elif defined ATOMIC_OPS_USE_LOCK
  99. if (_atomic_lock!=0){
  100. lock_destroy(_atomic_lock);
  101. lock_dealloc(_atomic_lock);
  102. _atomic_lock=0;
  103. }
  104. #endif /* ATOMIC_OPS_USE_LOCK_SET / ATOMIC_OPS_USE_LOCK*/
  105. }