1 /* Copyright (c) 2015, 2021, Oracle and/or its affiliates. 2 3 This program is free software; you can redistribute it and/or modify 4 it under the terms of the GNU General Public License, version 2.0, 5 as published by the Free Software Foundation. 6 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 14 Without limiting anything contained in the foregoing, this file, 15 which is part of C Driver for MySQL (Connector/C), is also subject to the 16 Universal FOSS Exception, version 1.0, a copy of which can be found at 17 http://oss.oracle.com/licenses/universal-foss-exception. 18 19 This program is distributed in the hope that it will be useful, 20 but WITHOUT ANY WARRANTY; without even the implied warranty of 21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 GNU General Public License, version 2.0, for more details. 23 24 You should have received a copy of the GNU General Public License 25 along with this program; if not, write to the Free Software 26 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 27 02110-1301 USA */ 28 29 30 #ifndef ATOMIC_CLASS_H_INCLUDED 31 #define ATOMIC_CLASS_H_INCLUDED 32 33 #include "my_atomic.h" 34 35 /** 36 Wrapper class to use C++ syntax to access atomic integers. 37 */ 38 #define DEFINE_ATOMIC_CLASS(NAME, SUFFIX, TYPE) \ 39 class Atomic_##NAME \ 40 { \ 41 public: \ 42 /* Create a new Atomic_* object. */ \ 43 Atomic_##NAME(TYPE n= 0) \ 44 { \ 45 my_atomic_store##SUFFIX(&value, n); \ 46 } \ 47 /* Atomically read the value. */ \ 48 TYPE atomic_get() \ 49 { \ 50 return my_atomic_load##SUFFIX(&value); \ 51 } \ 52 /* Atomically set the value. */ \ 53 void atomic_set(TYPE n) \ 54 { \ 55 my_atomic_store##SUFFIX(&value, n); \ 56 } \ 57 /* Atomically add to the value, and return the old value. */ \ 58 TYPE atomic_add(TYPE n) \ 59 { \ 60 return my_atomic_add##SUFFIX(&value, n); \ 61 } \ 62 /* Atomically set the value and return the old value. */ \ 63 TYPE atomic_get_and_set(TYPE n) \ 64 { \ 65 return my_atomic_fas##SUFFIX(&value, n); \ 66 } \ 67 /* If the old value is equal to *old, set the value to new and */ \ 68 /* return true. Otherwise, store the old value in '*old' and */ \ 69 /* return false. */ \ 70 bool atomic_compare_and_swap(TYPE *old, TYPE n) \ 71 { \ 72 return my_atomic_cas##SUFFIX(&value, old, n); \ 73 } \ 74 /* Read the value *non-atomically*. */ \ 75 TYPE non_atomic_get() \ 76 { \ 77 return value; \ 78 } \ 79 /* Set the value *non-atomically*. */ \ 80 void non_atomic_set(TYPE n) \ 81 { \ 82 value= n; \ 83 } \ 84 /* Add to the value *non-atomically*. */ \ 85 TYPE non_atomic_add(TYPE n) \ 86 { \ 87 TYPE ret= value; \ 88 value+= n; \ 89 return ret; \ 90 } \ 91 /* Set the value to the greatest of (old, n). */ \ 92 \ 93 /* The function will internally requires multiple atomic */ \ 94 /* operations. If the old value is known (or guessed), and less */ \ 95 /* than n, it requires one atomic operation less. Therefore, */ \ 96 /* the caller should set *guess to whatever is the likely value */ \ 97 /* that the variable currently has, if such a guess is known. */ \ 98 \ 99 /* @return If the new value is changed to n, *guess is set to */ \ 100 /* the old value and the the function returns true. Otherwise, */ \ 101 /* *guess is set to the current value (which is greater than or */ \ 102 /* equal to n), and the function returns false. */ \ 103 bool atomic_set_to_max(TYPE n, TYPE *guess= NULL) \ 104 { \ 105 TYPE _guess; \ 106 if (guess == NULL) \ 107 { \ 108 _guess= n - 1; \ 109 guess= &_guess; \ 110 } \ 111 else \ 112 assert(*guess < n); \ 113 bool ret; \ 114 do { \ 115 ret= atomic_compare_and_swap(guess, n); \ 116 } while (!ret && *guess < n); \ 117 return ret; \ 118 } \ 119 \ 120 private: \ 121 TYPE value; \ 122 } 123 124 DEFINE_ATOMIC_CLASS(int32, 32, int32); 125 DEFINE_ATOMIC_CLASS(int64, 64, int64); 126 //DEFINE_ATOMIC_CLASS(pointer, ptr, void *); 127 128 #endif //ifndef ATOMIC_CLASS_H_INCLUDED 129