/External/Mysql-5.0/include/atomic/rwlock.h

http://awoe.googlecode.com/ · C++ Header · 57 lines · 28 code · 4 blank · 25 comment · 3 complexity · 78f69a499291ff47fdacc6eda0f7a5e0 MD5 · raw file

  1. /* Copyright (C) 2006 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. typedef struct {pthread_mutex_t rw;} my_atomic_rwlock_t;
  13. #define MY_ATOMIC_MODE_RWLOCKS 1
  14. #ifdef MY_ATOMIC_MODE_DUMMY
  15. /*
  16. the following can never be enabled by ./configure, one need to put #define in
  17. a source to trigger the following warning. The resulting code will be broken,
  18. it only makes sense to do it to see now test_atomic detects broken
  19. implementations (another way is to run a UP build on an SMP box).
  20. */
  21. #warning MY_ATOMIC_MODE_DUMMY and MY_ATOMIC_MODE_RWLOCKS are incompatible
  22. #define my_atomic_rwlock_destroy(name)
  23. #define my_atomic_rwlock_init(name)
  24. #define my_atomic_rwlock_rdlock(name)
  25. #define my_atomic_rwlock_wrlock(name)
  26. #define my_atomic_rwlock_rdunlock(name)
  27. #define my_atomic_rwlock_wrunlock(name)
  28. #define MY_ATOMIC_MODE "dummy (non-atomic)"
  29. #else
  30. /*
  31. we're using read-write lock macros but map them to mutex locks, and they're
  32. faster. Still, having semantically rich API we can change the
  33. underlying implementation, if necessary.
  34. */
  35. #define my_atomic_rwlock_destroy(name) pthread_mutex_destroy(& (name)->rw)
  36. #define my_atomic_rwlock_init(name) pthread_mutex_init(& (name)->rw, 0)
  37. #define my_atomic_rwlock_rdlock(name) pthread_mutex_lock(& (name)->rw)
  38. #define my_atomic_rwlock_wrlock(name) pthread_mutex_lock(& (name)->rw)
  39. #define my_atomic_rwlock_rdunlock(name) pthread_mutex_unlock(& (name)->rw)
  40. #define my_atomic_rwlock_wrunlock(name) pthread_mutex_unlock(& (name)->rw)
  41. #define MY_ATOMIC_MODE "mutex"
  42. #ifndef MY_ATOMIC_MODE_RWLOCKS
  43. #define MY_ATOMIC_MODE_RWLOCKS 1
  44. #endif
  45. #endif
  46. #define make_atomic_add_body(S) int ## S sav; sav= *a; *a+= v; v=sav;
  47. #define make_atomic_fas_body(S) int ## S sav; sav= *a; *a= v; v=sav;
  48. #define make_atomic_cas_body(S) if ((ret= (*a == *cmp))) *a= set; else *cmp=*a;
  49. #define make_atomic_load_body(S) ret= *a;
  50. #define make_atomic_store_body(S) *a= v;