locks.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright Institute of Communication and Computer Systems (ICCS)
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. /**
  18. * @file locks.h
  19. * @author Ioannis Koutras (joko@microlab.ntua.gr)
  20. * @date September 2012
  21. * @brief Locking functions
  22. */
  23. #include <dmmlib/config.h>
  24. #ifndef LOCKS_H
  25. #define LOCKS_H
  26. #ifdef HAVE_LOCKS
  27. /** Enumeration of lock states */
  28. typedef enum locktype_en {STATE_LOCKED, STATE_UNLOCKED} locktype_t;
  29. /** Initializes a locktype_t variable */
  30. #define INIT_DEALY_LOCK(lock) lock = STATE_UNLOCKED
  31. /** Locks a locktype_t variable */
  32. #define DEALY_LOCK(lock) \
  33. while(__sync_bool_compare_and_swap(&lock, \
  34. STATE_UNLOCKED, STATE_LOCKED) != STATE_UNLOCKED){}
  35. /** Tries to lock a locktype_t variable */
  36. #define DEALY_TRYLOCK(lock) \
  37. ((__sync_bool_compare_and_swap(&lock, STATE_UNLOCKED, STATE_LOCKED) != \
  38. STATE_UNLOCKED))
  39. /** Unlocks a locktype_t variable */
  40. #define DEALY_UNLOCK(lock) lock = STATE_UNLOCKED
  41. /** Destroys a locktype_t */
  42. #define DEALY_DESTROY_LOCK(lock) lock = STATE_UNLOCKED
  43. #else /* HAVE_LOCKS */
  44. /** Does nothing. */
  45. #define INIT_DEALY_LOCK()
  46. /** Does nothing. */
  47. #define DEALY_LOCK()
  48. /** Does nothing. */
  49. #define DEALY_TRYLOCK(lock)
  50. /** Does nothing. */
  51. #define DEALY_UNLOCK(lock)
  52. /** Does nothing. */
  53. #define DEALY_DESTROY_LOCK(lock)
  54. #endif /* HAVE_LOCKS */
  55. #endif /* LOCKS_H */