00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 #ifndef _CM_H
00055 #define _CM_H
00056
00057 #define CM_RESTART 1
00058 #define CM_RESTART_NO_LOAD 2
00059 #define CM_RESTART_LOCKED 3
00060
00061 static inline
00062 int
00063 cm_conflict(mtm_tx_t *tx, volatile mtm_word_t *lock, mtm_word_t *l)
00064 {
00065 mode_data_t *modedata = (mode_data_t *) tx->modedata[tx->mode];
00066 w_entry_t *w;
00067
00068 #if CM == CM_PRIORITY
00069 if (tx->retries >= cm_threshold) {
00070 if (LOCK_GET_PRIORITY(*l) < tx->priority ||
00071 (LOCK_GET_PRIORITY(*l) == tx->priority &&
00072 *l < (mtm_word_t)modedata->w_set.entries
00073 && !LOCK_GET_WAIT(*l)))
00074 {
00075
00076 if (ATOMIC_CAS_FULL(lock, *l, LOCK_SET_PRIORITY_WAIT(*l, tx->priority)) == 0) {
00077 return CM_RESTART;
00078 }
00079 *l = LOCK_SET_PRIORITY_WAIT(*l, tx->priority);
00080 }
00081
00082 while (1) {
00083 int nb;
00084 mtm_word_t lw;
00085
00086 w = modedata->w_set.entries;
00087 for (nb = modedata->w_set.nb_entries; nb > 0; nb--, w++) {
00088 lw = ATOMIC_LOAD(w->lock);
00089 if (LOCK_GET_WAIT(lw)) {
00090
00091 goto give_up;
00092 }
00093 }
00094
00095 lw = ATOMIC_LOAD(lock);
00096 if (*l != lw) {
00097 *l = lw;
00098 return CM_RESTART_NO_LOAD;
00099 }
00100 }
00101 give_up:
00102 if (tx->priority < PRIORITY_MAX) {
00103 tx->priority++;
00104 } else {
00105 PRINT_DEBUG("Reached maximum priority\n");
00106 }
00107 tx->c_lock = lock;
00108 }
00109 #elif CM == CM_DELAY
00110 tx->c_lock = lock;
00111 #endif
00112 return CM_RESTART_LOCKED;
00113 }
00114
00115
00116 static inline
00117 int
00118 cm_upgrade_lock(mtm_tx_t *tx)
00119 {
00120 #if CM == CM_PRIORITY
00121 if (tx->visible_reads >= vr_threshold && vr_threshold >= 0) {
00122 return 1;
00123 } else {
00124 return 0;
00125 }
00126 #endif
00127 return 0;
00128 }
00129
00130
00131 static inline
00132 void
00133 cm_delay(mtm_tx_t *tx)
00134 {
00135 #if CM == CM_BACKOFF
00136 unsigned long wait;
00137 volatile int j;
00138
00139
00140 tx->seed ^= (tx->seed << 17);
00141 tx->seed ^= (tx->seed >> 13);
00142 tx->seed ^= (tx->seed << 5);
00143 wait = tx->seed % tx->backoff;
00144 for (j = 0; j < wait; j++) {
00145
00146 }
00147 if (tx->backoff < MAX_BACKOFF) {
00148 tx->backoff <<= 1;
00149 }
00150 #endif
00151
00152 #if CM == CM_DELAY || CM == CM_PRIORITY
00153
00154 if (tx->c_lock != NULL) {
00155
00156 while (LOCK_GET_OWNED(ATOMIC_LOAD(tx->c_lock))) {
00157 # ifdef WAIT_YIELD
00158 sched_yield();
00159 # endif
00160 }
00161 tx->c_lock = NULL;
00162 }
00163 #endif
00164 }
00165
00166
00167 static inline
00168 void
00169 cm_visible_read(mtm_tx_t *tx)
00170 {
00171 #if CM == CM_PRIORITY
00172 tx->visible_reads++;
00173 #endif
00174 }
00175
00176
00177 static inline
00178 void
00179 cm_reset(mtm_tx_t *tx)
00180 {
00181 #if CM == CM_PRIORITY || defined(INTERNAL_STATS)
00182 tx->retries = 0;
00183 #endif
00184
00185 #if CM == CM_BACKOFF
00186
00187 tx->backoff = MIN_BACKOFF;
00188 #endif
00189
00190 #if CM == CM_PRIORITY
00191
00192 tx->priority = 0;
00193 tx->visible_reads = 0;
00194 #endif
00195 }
00196
00197
00198 #endif