my_atomic.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #ifndef MY_ATOMIC_INCLUDED
  2. #define MY_ATOMIC_INCLUDED
  3. /* Copyright (c) 2006, 2017, Oracle and/or its affiliates. All rights reserved.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License, version 2.0,
  6. as published by the Free Software Foundation.
  7. This program is also distributed with certain software (including
  8. but not limited to OpenSSL) that is licensed under separate terms,
  9. as designated in a particular file or component or in included license
  10. documentation. The authors of MySQL hereby grant you an additional
  11. permission to link the program and your derivative works with the
  12. separately licensed software that they have included with MySQL.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License, version 2.0, for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  20. /**
  21. @file include/my_atomic.h
  22. */
  23. #if defined(_MSC_VER)
  24. #include <windows.h>
  25. /*
  26. my_yield_processor (equivalent of x86 PAUSE instruction) should be used
  27. to improve performance on hyperthreaded CPUs. Intel recommends to use it in
  28. spin loops also on non-HT machines to reduce power consumption (see e.g
  29. http://softwarecommunity.intel.com/articles/eng/2004.htm)
  30. Running benchmarks for spinlocks implemented with InterlockedCompareExchange
  31. and YieldProcessor shows that much better performance is achieved by calling
  32. YieldProcessor in a loop - that is, yielding longer. On Intel boxes setting
  33. loop count in the range 200-300 brought best results.
  34. */
  35. #define YIELD_LOOPS 200
  36. static inline int my_yield_processor() {
  37. int i;
  38. for (i = 0; i < YIELD_LOOPS; i++) {
  39. YieldProcessor();
  40. }
  41. return 1;
  42. }
  43. #define LF_BACKOFF my_yield_processor()
  44. #else // !defined(_MSC_VER)
  45. #define LF_BACKOFF (1)
  46. #endif
  47. #endif /* MY_ATOMIC_INCLUDED */