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
00066 #ifndef _PWB_COMMON_BARRIER_BITS_JKI671_H
00067 #define _PWB_COMMON_BARRIER_BITS_JKI671_H
00068
00069
00070
00071
00072
00073 static inline
00074 int
00075 pwb_extend (mtm_tx_t *tx, mode_data_t *modedata)
00076 {
00077 mtm_word_t now;
00078
00079 PRINT_DEBUG("==> pwb_extend(%p[%lu-%lu])\n", tx,
00080 (unsigned long)modedata->start,
00081 (unsigned long)modedata->end);
00082
00083
00084 assert(tx->status == TX_ACTIVE);
00085
00086
00087 now = GET_CLOCK;
00088 #ifdef ROLLOVER_CLOCK
00089 if (now >= VERSION_MAX) {
00090
00091 return 0;
00092 }
00093 #endif
00094
00095 if (mtm_validate(tx, modedata)) {
00096
00097 modedata->end = now;
00098 return 1;
00099 }
00100 return 0;
00101 }
00102
00103
00127 static
00128 w_entry_t* initialize_write_set_entry(w_entry_t* entry,
00129 volatile mtm_word_t *address,
00130 mtm_word_t value,
00131 mtm_word_t mask,
00132 volatile mtm_word_t version,
00133 volatile mtm_word_t* lock,
00134 int is_nonvolatile)
00135 {
00136
00137 entry->addr = address;
00138 entry->lock = lock;
00139 entry->mask = 0x0;
00140 mask_new_value(entry, address, value, mask);
00141 entry->version = version;
00142 entry->next = NULL;
00143 entry->next_cache_neighbor = NULL;
00144 entry->is_nonvolatile = is_nonvolatile;
00145
00146 return entry;
00147 }
00148
00149
00164 static
00165 void insert_write_set_entry_after(w_entry_t* new_entry,
00166 w_entry_t* tail,
00167 mtm_tx_t* transaction,
00168 w_entry_t* cache_neighbor)
00169 {
00170
00171 if (tail != NULL) {
00172 new_entry->next = tail->next;
00173 tail->next = new_entry;
00174 } else {
00175 new_entry->next = NULL;
00176 }
00177
00178
00179 if (cache_neighbor != NULL) {
00180 new_entry->next_cache_neighbor = cache_neighbor->next_cache_neighbor;
00181 cache_neighbor->next_cache_neighbor = new_entry;
00182 } else {
00183 new_entry->next_cache_neighbor = NULL;
00184 }
00185
00186
00187 mode_data_t* modedata = (mode_data_t *) transaction->modedata[transaction->mode];
00188 modedata->w_set.nb_entries++;
00189
00190
00191 if (new_entry->is_nonvolatile) {
00192 M_TMLOG_WRITE(transaction->pcm_storeset, modedata->ptmlog, (uintptr_t) new_entry->addr, new_entry->value, new_entry->mask);
00193 }
00194 }
00195
00196
00214 static inline
00215 w_entry_t *
00216 matching_write_set_entry(w_entry_t* const list_head,
00217 volatile mtm_word_t* address,
00218 w_entry_t** list_tail,
00219 w_entry_t** last_cache_neighbor)
00220 {
00221 w_entry_t* this_entry = list_head;
00222 while (true) {
00223 if (last_cache_neighbor != NULL && BLOCK_ADDR(address) == BLOCK_ADDR(this_entry->addr))
00224 *last_cache_neighbor = this_entry;
00225
00226 if (address == this_entry->addr) {
00227
00228 return this_entry;
00229 }
00230 else if (this_entry->next == NULL) {
00231
00232 if (list_tail != NULL) {
00233 *list_tail = this_entry;
00234 }
00235 return NULL;
00236 }
00237 else {
00238 this_entry = this_entry->next;
00239 }
00240 }
00241 }
00242
00243
00270 static inline
00271 w_entry_t *
00272 pwb_write_internal(mtm_tx_t *tx,
00273 volatile mtm_word_t *addr,
00274 mtm_word_t value,
00275 mtm_word_t mask,
00276 int enable_isolation)
00277 {
00278 assert(tx->mode == MTM_MODE_pwbnl || tx->mode == MTM_MODE_pwbetl);
00279 mode_data_t *modedata = (mode_data_t *) tx->modedata[tx->mode];
00280 volatile mtm_word_t *lock;
00281 mtm_word_t l;
00282 mtm_word_t version;
00283 w_entry_t *w;
00284 w_entry_t *write_set_tail = NULL;
00285 int ret;
00286 int access_is_nonvolatile;
00287
00288 MTM_DEBUG_PRINT("==> pwb_write(t=%p[%lu-%lu],a=%p,d=%llX-%llu,m=0x%llX)\n", tx,
00289 (unsigned long)modedata->start,
00290 (unsigned long)modedata->end,
00291 addr,
00292 (unsigned long long)value,
00293 (unsigned long long)value,
00294 (unsigned long long)mask);
00295
00296
00297 if (tx->status != TX_ACTIVE) {
00298 assert(0);
00299 }
00300
00301 #if 0
00302
00303 {
00304 mtm_word_t prev_value;
00305 prev_value = ATOMIC_LOAD(addr);
00306 if (mask == 0) {
00307 return NULL;
00308 }
00309 if (mask != ~(mtm_word_t)0) {
00310 value = (prev_value & ~mask) | (value & mask);
00311 }
00312
00313
00314
00315
00316
00317
00318
00319
00320 ATOMIC_STORE(addr, value);
00321 return NULL;
00322 }
00323 #endif
00324
00325
00326
00327 if (((uintptr_t) addr >= PSEGMENT_RESERVED_REGION_START &&
00328 (uintptr_t) addr < (PSEGMENT_RESERVED_REGION_START + PSEGMENT_RESERVED_REGION_SIZE)))
00329 {
00330 access_is_nonvolatile = 1;
00331 } else {
00332 access_is_nonvolatile = 0;
00333
00334
00335 if ((uintptr_t) addr <= tx->stack_base &&
00336 (uintptr_t) addr > tx->stack_base - tx->stack_size)
00337 {
00338 mtm_word_t prev_value;
00339 prev_value = ATOMIC_LOAD(addr);
00340 if (mask == 0) {
00341 return NULL;
00342 }
00343 if (mask != ~(mtm_word_t)0) {
00344 value = (prev_value & ~mask) | (value & mask);
00345 }
00346
00347
00348
00349
00350
00351
00352
00353
00354 if (enable_isolation) {
00355 mtm_local_LB(tx, (void *) addr, sizeof(mtm_word_t));
00356 } else {
00357 #ifdef ALLOW_ABORTS
00358 mtm_local_LB(tx, (void *) addr, sizeof(mtm_word_t));
00359 #endif
00360 }
00361 ATOMIC_STORE(addr, value);
00362 return NULL;
00363 }
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375 if (!enable_isolation) {
00376 #if !defined(ALLOW_ABORTS)
00377 PCM_WB_STORE_MASKED(tx->pcm_storeset, addr, value, mask);
00378 return NULL;
00379 #endif
00380 }
00381 }
00382
00383
00384 #ifdef _M_STATS_BUILD
00385 m_stats_statset_increment(mtm_statsmgr, tx->statset, XACT, writes, 1);
00386 if (access_is_nonvolatile) {
00387 m_stats_statset_increment(mtm_statsmgr, tx->statset, XACT, nvwrites, 1);
00388 } else {
00389 m_stats_statset_increment(mtm_statsmgr, tx->statset, XACT, vwrites, 1);
00390 }
00391 #endif
00392
00393
00394 if (enable_isolation) {
00395 lock = GET_LOCK(addr);
00396 } else {
00397
00398
00399
00400
00401
00402
00403
00404 lock = PRIVATE_GET_LOCK(tx, addr);
00405 }
00406
00407
00408 restart:
00409 l = ATOMIC_LOAD_ACQ(lock);
00410 restart_no_load:
00411 if (LOCK_GET_OWNED(l)) {
00412
00413
00414 w_entry_t *write_set_head;
00415 write_set_head = (w_entry_t *)LOCK_GET_ADDR(l);
00416
00417
00418 if (modedata->w_set.entries <= write_set_head &&
00419 write_set_head < modedata->w_set.entries + modedata->w_set.nb_entries)
00420 {
00421
00422
00423 w_entry_t* write_set_tail = NULL;
00424 w_entry_t* last_entry_in_same_cache_block = NULL;
00425 w_entry_t* matching_entry = matching_write_set_entry(write_set_head, addr, &write_set_tail, &last_entry_in_same_cache_block);
00426 if (matching_entry != NULL) {
00427 if (matching_entry->mask != 0) {
00428 mask_new_value(matching_entry, addr, value, mask);
00429
00430 if (access_is_nonvolatile) {
00431 M_TMLOG_WRITE(tx->pcm_storeset, modedata->ptmlog, (uintptr_t) matching_entry->addr, matching_entry->value, matching_entry->mask);
00432 }
00433 }
00434 return matching_entry;
00435 } else {
00436 if (modedata->w_set.nb_entries == modedata->w_set.size) {
00437
00438 modedata->w_set.size *= 2;
00439 modedata->w_set.reallocate = 1;
00440 #ifdef _M_STATS_BUILD
00441 m_stats_statset_increment(mtm_statsmgr, tx->statset, XACT, aborts, 1);
00442 #endif
00443 #ifdef INTERNAL_STATS
00444 tx->aborts_reallocate++;
00445 #endif
00446
00447 mtm_pwb_restart_transaction (tx, RESTART_REALLOCATE);
00448 } else {
00449 #ifdef _M_STATS_BUILD
00450 m_stats_statset_increment(mtm_statsmgr, tx->statset, XACT, writes_distinct, 1);
00451 if (access_is_nonvolatile) {
00452 m_stats_statset_increment(mtm_statsmgr, tx->statset, XACT, nvwrites_distinct, 1);
00453 } else {
00454 m_stats_statset_increment(mtm_statsmgr, tx->statset, XACT, vwrites_distinct, 1);
00455 }
00456 #endif
00457
00458 w = &modedata->w_set.entries[modedata->w_set.nb_entries];
00459 version = write_set_tail->version;
00460
00461 w_entry_t* initialized_entry = initialize_write_set_entry(w, addr, value, mask, version, lock, access_is_nonvolatile);
00462
00463
00464
00465 insert_write_set_entry_after(initialized_entry, write_set_tail, tx, last_entry_in_same_cache_block);
00466 return initialized_entry;
00467 }
00468 }
00469 }
00470
00471
00472 assert(enable_isolation);
00473
00474
00475 ret = cm_conflict(tx, lock, &l);
00476 switch (ret) {
00477 case CM_RESTART:
00478 goto restart;
00479 case CM_RESTART_NO_LOAD:
00480 goto restart_no_load;
00481 case CM_RESTART_LOCKED:
00482
00483 #ifdef _M_STATS_BUILD
00484 m_stats_statset_increment(mtm_statsmgr, tx->statset, XACT, aborts, 1);
00485 #endif
00486 #ifdef INTERNAL_STATS
00487 tx->aborts_locked_write++;
00488 #endif
00489 mtm_pwb_restart_transaction(tx, RESTART_LOCKED_WRITE);
00490 }
00491
00492 assert(0);
00493 } else {
00494
00495 if (enable_isolation) {
00496
00497 version = LOCK_GET_TIMESTAMP(l);
00498
00499 if (version > modedata->end) {
00500
00501 if (!tx->can_extend || mtm_has_read(tx, modedata, lock) != NULL) {
00502
00503
00504
00505 cm_visible_read(tx);
00506 #ifdef _M_STATS_BUILD
00507 m_stats_statset_increment(mtm_statsmgr, tx->statset, XACT, aborts, 1);
00508 #endif
00509 #ifdef INTERNAL_STATS
00510 tx->aborts_validate_write++;
00511 #endif
00512 mtm_pwb_restart_transaction(tx, RESTART_VALIDATE_WRITE);
00513 }
00514 }
00515 }
00516
00517
00518 if (modedata->w_set.nb_entries == modedata->w_set.size) {
00519
00520 modedata->w_set.size *= 2;
00521 modedata->w_set.reallocate = 1;
00522 #ifdef _M_STATS_BUILD
00523 m_stats_statset_increment(mtm_statsmgr, tx->statset, XACT, aborts, 1);
00524 #endif
00525 # ifdef INTERNAL_STATS
00526 tx->aborts_reallocate++;
00527 # endif
00528 mtm_pwb_restart_transaction (tx, RESTART_REALLOCATE);
00529 }
00530 w = &modedata->w_set.entries[modedata->w_set.nb_entries];
00531 if (enable_isolation) {
00532 # ifdef READ_LOCKED_DATA
00533 w->version = version;
00534 # endif
00535 # if CM == CM_PRIORITY
00536 if (ATOMIC_CAS_FULL(lock, l, LOCK_SET_ADDR((mtm_word_t)w, tx->priority)) == 0) {
00537 goto restart;
00538 }
00539 # else
00540 if (ATOMIC_CAS_FULL(lock, l, LOCK_SET_ADDR((mtm_word_t)w)) == 0) {
00541 goto restart;
00542 }
00543 # endif
00544 } else {
00545
00546
00547
00548 *lock = LOCK_SET_ADDR((mtm_word_t)w);
00549 }
00550
00551 w_entry_t* initialized_entry = initialize_write_set_entry(w, addr, value, mask, version, lock, access_is_nonvolatile);
00552 insert_write_set_entry_after(initialized_entry, write_set_tail, tx, NULL);
00553 #ifdef _M_STATS_BUILD
00554 m_stats_statset_increment(mtm_statsmgr, tx->statset, XACT, writes_distinct, 1);
00555 if (access_is_nonvolatile) {
00556 m_stats_statset_increment(mtm_statsmgr, tx->statset, XACT, nvwrites_distinct, 1);
00557 } else {
00558 m_stats_statset_increment(mtm_statsmgr, tx->statset, XACT, vwrites_distinct, 1);
00559 }
00560 #endif
00561 return w;
00562 }
00563 }
00564
00565
00566 static inline
00567 mtm_word_t
00568 pwb_load_internal(mtm_tx_t *tx, volatile mtm_word_t *addr, int enable_isolation)
00569 {
00570 assert(tx->mode == MTM_MODE_pwbnl || tx->mode == MTM_MODE_pwbetl);
00571 mode_data_t *modedata = (mode_data_t *) tx->modedata[tx->mode];
00572 volatile mtm_word_t *lock;
00573 mtm_word_t l;
00574 mtm_word_t l2;
00575 mtm_word_t value;
00576 mtm_word_t version;
00577 r_entry_t *r;
00578 w_entry_t *w;
00579 int ret;
00580
00581 MTM_DEBUG_PRINT("==> mtm_pwb_load(t=%p[%lu-%lu],a=%p)\n", tx,
00582 (unsigned long)modedata->start,
00583 (unsigned long)modedata->end,
00584 addr);
00585
00586
00587 if (tx->status != TX_ACTIVE) {
00588 printf("%p, tx->status = %d\n", tx, tx->status);
00589 }
00590 assert(tx->status == TX_ACTIVE);
00591
00592 #if 0
00593
00594
00595 value = ATOMIC_LOAD(addr);
00596 return value;
00597 #endif
00598
00599
00600 if (((uintptr_t) addr >= PSEGMENT_RESERVED_REGION_START &&
00601 (uintptr_t) addr < (PSEGMENT_RESERVED_REGION_START + PSEGMENT_RESERVED_REGION_SIZE)))
00602 {
00603
00604
00605 } else {
00606
00607 if ((uintptr_t) addr <= tx->stack_base &&
00608 (uintptr_t) addr > tx->stack_base - tx->stack_size)
00609 {
00610 value = ATOMIC_LOAD(addr);
00611 return value;
00612 }
00613
00614
00615
00616
00617
00618
00619
00620
00621 if (!enable_isolation) {
00622 #if !defined(ALLOW_ABORTS)
00623 value = ATOMIC_LOAD(addr);
00624 return value;
00625 #endif
00626 }
00627 }
00628
00629
00630 if (enable_isolation) {
00631
00632 if (cm_upgrade_lock(tx)) {
00633 w = pwb_write_internal(tx, addr, 0, 0, enable_isolation);
00634
00635 if(tx->status != TX_ACTIVE) {
00636 return 0;
00637 }
00638 assert(w != NULL);
00639
00640 return w->mask == 0 ? ATOMIC_LOAD(addr) : w->value;
00641 }
00642 }
00643
00644
00645 if (enable_isolation) {
00646 lock = GET_LOCK(addr);
00647 } else {
00648
00649 lock = PRIVATE_GET_LOCK(tx, addr);
00650 }
00651
00652
00653
00654
00655
00656 restart:
00657 l = ATOMIC_LOAD_ACQ(lock);
00658 restart_no_load:
00659 if (LOCK_GET_OWNED(l)) {
00660
00661
00662 w = (w_entry_t *)LOCK_GET_ADDR(l);
00663
00664 if (modedata->w_set.entries <= w &&
00665 w < modedata->w_set.entries + modedata->w_set.nb_entries)
00666 {
00667
00668 while (1) {
00669 if (addr == w->addr) {
00670
00671 value = (w->mask == 0 ? ATOMIC_LOAD(addr) : w->value);
00672 MTM_DEBUG_PRINT("==> mtm_load[OWN LOCK|READ FROM WSET]");
00673 break;
00674 }
00675 if (w->next == NULL) {
00676
00677 value = ATOMIC_LOAD(addr);
00678 MTM_DEBUG_PRINT("==> mtm_load[OWN LOCK|READ FROM MEMORY]");
00679 break;
00680 }
00681 w = w->next;
00682 }
00683
00684 MTM_DEBUG_PRINT("(t=%p[%lu-%lu],a=%p,l=%p,*l=%lu,d=%p-%lu)\n",
00685 tx, (unsigned long)modedata->start,
00686 (unsigned long)modedata->end,
00687 addr,
00688 lock,
00689 (unsigned long)l,
00690 (void *)value,
00691 (unsigned long)value);
00692 return value;
00693 }
00694
00695
00696 assert(enable_isolation);
00697
00698
00699
00700 ret = cm_conflict(tx, lock, &l);
00701 switch (ret) {
00702 case CM_RESTART:
00703 goto restart;
00704 case CM_RESTART_NO_LOAD:
00705 goto restart_no_load;
00706 case CM_RESTART_LOCKED:
00707
00708 #ifdef _M_STATS_BUILD
00709 m_stats_statset_increment(mtm_statsmgr, tx->statset, XACT, aborts, 1);
00710 #endif
00711 #ifdef INTERNAL_STATS
00712 tx->aborts_locked_write++;
00713 #endif
00714 mtm_pwb_restart_transaction(tx, RESTART_LOCKED_READ);
00715 }
00716
00717 assert(0);
00718 } else {
00719
00720 value = ATOMIC_LOAD_ACQ(addr);
00721 if (enable_isolation) {
00722 l2 = ATOMIC_LOAD_ACQ(lock);
00723 if (l != l2) {
00724 l = l2;
00725 goto restart_no_load;
00726 }
00727
00728 version = LOCK_GET_TIMESTAMP(l);
00729
00730 if (version > modedata->end) {
00731
00732 if (!tx->can_extend || !pwb_extend(tx, modedata)) {
00733
00734
00735 cm_visible_read(tx);
00736 #ifdef _M_STATS_BUILD
00737 m_stats_statset_increment(mtm_statsmgr, tx->statset, XACT, aborts, 1);
00738 #endif
00739 #ifdef INTERNAL_STATS
00740 tx->aborts_validate_read++;
00741 #endif
00742 mtm_pwb_restart_transaction (tx, RESTART_VALIDATE_READ);
00743 }
00744
00745
00746
00747 l = ATOMIC_LOAD_ACQ(lock);
00748 if (l != l2) {
00749 l = l2;
00750 goto restart_no_load;
00751 }
00752
00753 }
00754 }
00755 }
00756
00757
00758 if (enable_isolation) {
00759
00760 if (modedata->r_set.nb_entries == modedata->r_set.size) {
00761 mtm_allocate_rs_entries(tx, modedata, 1);
00762 }
00763 r = &modedata->r_set.entries[modedata->r_set.nb_entries++];
00764 r->version = version;
00765 r->lock = lock;
00766 }
00767
00768 MTM_DEBUG_PRINT("==> mtm_pwb_load(t=%p[%lu-%lu],a=%p,l=%p,*l=%lu,d=%p-%lu,v=%lu)\n",
00769 tx, (unsigned long)modedata->start,
00770 (unsigned long)modedata->end,
00771 addr,
00772 lock,
00773 (unsigned long)l,
00774 (void *)value,
00775 (unsigned long)value,
00776 (unsigned long)version);
00777
00778 return value;
00779 }
00780
00781
00782 #endif