|
| 1 | +/* |
| 2 | + * Copyright (c) 2009-2022 The University of Tennessee and The University |
| 3 | + * of Tennessee Research Foundation. All rights |
| 4 | + * reserved. |
| 5 | + */ |
| 6 | + |
| 7 | +#include "parsec/class/parsec_biased_rwlock.h" |
| 8 | + |
| 9 | +#include <assert.h> |
| 10 | + |
| 11 | +#include "parsec/runtime.h" |
| 12 | +#include "parsec/constants.h" |
| 13 | +#include "parsec/execution_stream.h" |
| 14 | +#include "parsec/sys/atomic.h" |
| 15 | +#include "parsec/class/parsec_rwlock.h" |
| 16 | + |
| 17 | +/** |
| 18 | + * An implementation of the BRAVO biased reader/writer lock wrapper. |
| 19 | + * The goal of the BRAVO lock wrapper is to avoid contending the atomic |
| 20 | + * rwlock with reader locks, instead having threads mark their read status |
| 21 | + * is an array. A writer will first take the rwlock, signal that a writer |
| 22 | + * is active, and then wait for all readers to complete. New readers will |
| 23 | + * see that a writer is active and wait for the reader lock to become available. |
| 24 | + * |
| 25 | + * This is clearly biased towards readers so this implementation is meant for |
| 26 | + * cases where the majority of accesses is reading and only occasional writes occur. |
| 27 | + * |
| 28 | + * The paper presenting this technique is available at: |
| 29 | + * https://arxiv.org/abs/1810.01553 |
| 30 | + * |
| 31 | + * While the original implementation uses a global hash table, we use a smaller table |
| 32 | + * per lock. In PaRSEC, we know the number of threads we control up front. |
| 33 | + * We simply pad for a cache line. If an unknown thread tries to take the lock against |
| 34 | + * all odds, it falls back to taking the reader lock. |
| 35 | + */ |
| 36 | + |
| 37 | +struct parsec_biased_rwlock_t { |
| 38 | + parsec_atomic_rwlock_t rw_lock; /**< underlying reader-writer lock */ |
| 39 | + int32_t reader_bias; /**< whether locking is biased towards readers, will change if a writer occurs */ |
| 40 | + uint32_t num_reader; /**< size of the reader_active field */ |
| 41 | + uint8_t reader_active[]; /**< array with flags signalling reading threads */ |
| 42 | +}; |
| 43 | + |
| 44 | +#define DEFAULT_CACHE_SIZE 64 |
| 45 | + |
| 46 | +int parsec_biased_rwlock_init(parsec_biased_rwlock_t **lock) { |
| 47 | + parsec_biased_rwlock_t *res; |
| 48 | + parsec_execution_stream_t *es = parsec_my_execution_stream(); |
| 49 | + if (NULL == es) { |
| 50 | + /* should be called from a parsec thread */ |
| 51 | + res = (parsec_biased_rwlock_t *)malloc(sizeof(parsec_biased_rwlock_t)); |
| 52 | + res->num_reader = 0; |
| 53 | + res->reader_bias = 0; // disable reader biasing |
| 54 | + } else { |
| 55 | + uint32_t num_threads = es->virtual_process->nb_cores; |
| 56 | + /* one cache line per reader */ |
| 57 | + uint32_t num_reader = num_threads*DEFAULT_CACHE_SIZE; |
| 58 | + res = (parsec_biased_rwlock_t *)malloc(sizeof(parsec_biased_rwlock_t) + num_reader*sizeof(uint8_t)); |
| 59 | + parsec_atomic_rwlock_init(&res->rw_lock); |
| 60 | + res->reader_bias = 1; |
| 61 | + res->num_reader = num_reader; |
| 62 | + memset(res->reader_active, 0, num_reader); |
| 63 | + } |
| 64 | + *lock = res; |
| 65 | + |
| 66 | + return PARSEC_SUCCESS; |
| 67 | +} |
| 68 | + |
| 69 | +void parsec_biased_rwlock_rdlock(parsec_biased_rwlock_t *lock) |
| 70 | +{ |
| 71 | + parsec_execution_stream_t *es = parsec_my_execution_stream(); |
| 72 | + if (PARSEC_UNLIKELY(NULL == es || lock->num_reader == 0)) { |
| 73 | + /* fall back to the underlying rwlock */ |
| 74 | + parsec_atomic_rwlock_rdlock(&lock->rw_lock); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + if (PARSEC_UNLIKELY(!lock->reader_bias)) { |
| 79 | + /* a writer is active, wait for the rwlock to become available */ |
| 80 | + parsec_atomic_rwlock_rdlock(&lock->rw_lock); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + /* fast-path: no writer, simply mark as active reader and make sure there is no race */ |
| 85 | + size_t reader_entry = es->th_id*DEFAULT_CACHE_SIZE; |
| 86 | + assert(reader_entry >= 0 && reader_entry < lock->num_reader); |
| 87 | + assert(lock->reader_active[reader_entry] == 0); |
| 88 | + |
| 89 | + lock->reader_active[reader_entry] = 1; |
| 90 | + /* make sure the writer check is not moved to before setting the flag */ |
| 91 | + parsec_atomic_rmb(); |
| 92 | + /* double check that no writer came in between */ |
| 93 | + if (PARSEC_UNLIKELY(!lock->reader_bias)) { |
| 94 | + /* a writer has become active, fallback to the rwlock */ |
| 95 | + lock->reader_active[reader_entry] = 0; |
| 96 | + parsec_atomic_rwlock_rdlock(&lock->rw_lock); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +void parsec_biased_rwlock_rdunlock(parsec_biased_rwlock_t *lock) |
| 101 | +{ |
| 102 | + parsec_execution_stream_t *es = parsec_my_execution_stream(); |
| 103 | + |
| 104 | + if (PARSEC_UNLIKELY(NULL == es || lock->num_reader == 0)) { |
| 105 | + /* fall back to the underlying rwlock */ |
| 106 | + parsec_atomic_rwlock_rdunlock(&lock->rw_lock); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + size_t reader_entry = es->th_id*DEFAULT_CACHE_SIZE; |
| 111 | + assert(reader_entry >= 0 && reader_entry < lock->num_reader); |
| 112 | + |
| 113 | + if (PARSEC_UNLIKELY(lock->reader_active[reader_entry] == 0)) { |
| 114 | + /* we had to take a lock, give it back */ |
| 115 | + parsec_atomic_rwlock_rdunlock(&lock->rw_lock); |
| 116 | + } else { |
| 117 | + lock->reader_active[reader_entry] = 0; |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +void parsec_biased_rwlock_wrlock(parsec_biased_rwlock_t *lock) |
| 122 | +{ |
| 123 | + /* acquire the writer lock first */ |
| 124 | + parsec_atomic_rwlock_wrlock(&lock->rw_lock); |
| 125 | + |
| 126 | + lock->reader_bias = 0; |
| 127 | + |
| 128 | + /* make sure the reads below are not moved before the write */ |
| 129 | + parsec_atomic_wmb(); |
| 130 | + |
| 131 | + /* wait for all current reader to complete */ |
| 132 | + for (uint32_t i = 0; i < lock->num_reader; ++i) { |
| 133 | + while (lock->reader_active[i] != 0) { |
| 134 | + static struct timespec ts = { .tv_sec = 0, .tv_nsec = 100 }; |
| 135 | + nanosleep(&ts, NULL); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +void parsec_biased_rwlock_wrunlock(parsec_biased_rwlock_t *lock) |
| 141 | +{ |
| 142 | + assert(lock->reader_bias == 0); |
| 143 | + if (lock->num_reader > 0) { |
| 144 | + /* re-enable reader bias, if we support it */ |
| 145 | + lock->reader_bias = 1; |
| 146 | + } |
| 147 | + parsec_atomic_rwlock_wrunlock(&lock->rw_lock); |
| 148 | +} |
0 commit comments