| 1 | #include <stdio.h> |
| 2 | #include <pthread.h> |
| 3 | #include <signal.h> |
| 4 | #include <assert.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | |
| 8 | #include "urcu.h" |
| 9 | |
| 10 | pthread_mutex_t urcu_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 11 | |
| 12 | /* Global quiescent period parity */ |
| 13 | int urcu_qparity; |
| 14 | |
| 15 | int __thread urcu_active_readers[2]; |
| 16 | |
| 17 | /* Thread IDs of registered readers */ |
| 18 | #define INIT_NUM_THREADS 4 |
| 19 | |
| 20 | struct reader_data { |
| 21 | pthread_t tid; |
| 22 | int *urcu_active_readers; |
| 23 | }; |
| 24 | |
| 25 | static struct reader_data *reader_data; |
| 26 | static int num_readers, alloc_readers; |
| 27 | static int sig_done; |
| 28 | |
| 29 | void rcu_write_lock(void) |
| 30 | { |
| 31 | int ret; |
| 32 | ret = pthread_mutex_lock(&urcu_mutex); |
| 33 | if (ret) { |
| 34 | perror("Error in pthread mutex lock"); |
| 35 | exit(-1); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | void rcu_write_unlock(void) |
| 40 | { |
| 41 | int ret; |
| 42 | |
| 43 | ret = pthread_mutex_unlock(&urcu_mutex); |
| 44 | if (ret) { |
| 45 | perror("Error in pthread mutex unlock"); |
| 46 | exit(-1); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * called with urcu_mutex held. |
| 52 | */ |
| 53 | static int switch_next_urcu_qparity(void) |
| 54 | { |
| 55 | int old_parity = urcu_qparity; |
| 56 | urcu_qparity = 1 - old_parity; |
| 57 | return old_parity; |
| 58 | } |
| 59 | |
| 60 | static void force_mb_all_threads(void) |
| 61 | { |
| 62 | struct reader_data *index; |
| 63 | /* |
| 64 | * Ask for each threads to execute a mb() so we can consider the |
| 65 | * compiler barriers around rcu read lock as real memory barriers. |
| 66 | */ |
| 67 | if (!reader_data) |
| 68 | return; |
| 69 | sig_done = 0; |
| 70 | mb(); /* write sig_done before sending the signals */ |
| 71 | for (index = reader_data; index < reader_data + num_readers; index++) |
| 72 | pthread_kill(index->tid, SIGURCU); |
| 73 | /* |
| 74 | * Wait for sighandler (and thus mb()) to execute on every thread. |
| 75 | * BUSY-LOOP. |
| 76 | */ |
| 77 | while (sig_done < num_readers) |
| 78 | barrier(); |
| 79 | mb(); /* read sig_done before ending the barrier */ |
| 80 | } |
| 81 | |
| 82 | void wait_for_quiescent_state(int parity) |
| 83 | { |
| 84 | struct reader_data *index; |
| 85 | |
| 86 | if (!reader_data) |
| 87 | return; |
| 88 | /* Wait for each thread urcu_active_readers count to become 0. |
| 89 | */ |
| 90 | for (index = reader_data; index < reader_data + num_readers; index++) { |
| 91 | /* |
| 92 | * BUSY-LOOP. |
| 93 | */ |
| 94 | while (*index->urcu_active_readers != 0) |
| 95 | barrier(); |
| 96 | } |
| 97 | /* |
| 98 | * Locally : read *index->urcu_active_readers before freeing old |
| 99 | * pointer. |
| 100 | * Remote (reader threads) : Order urcu_qparity update and other |
| 101 | * thread's quiescent state counter read. |
| 102 | */ |
| 103 | force_mb_all_threads(); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Return old pointer, OK to free, no more reference exist. |
| 108 | * Called under rcu_write_lock. |
| 109 | */ |
| 110 | void *urcu_publish_content(void **ptr, void *new) |
| 111 | { |
| 112 | int ret, prev_parity; |
| 113 | void *oldptr; |
| 114 | |
| 115 | /* |
| 116 | * We can publish the new pointer before we change the current qparity. |
| 117 | * Readers seeing the new pointer while being in the previous qparity |
| 118 | * window will make us wait until the end of the quiescent state before |
| 119 | * we release the unrelated memory area. However, given we hold the |
| 120 | * urcu_mutex, we are making sure that no further garbage collection can |
| 121 | * occur until we release the mutex, therefore we guarantee that this |
| 122 | * given reader will have completed its execution using the new pointer |
| 123 | * when the next quiescent state window will be over. |
| 124 | */ |
| 125 | oldptr = *ptr; |
| 126 | *ptr = new; |
| 127 | wmb(); /* Write ptr before changing the qparity */ |
| 128 | /* All threads should read qparity before ptr */ |
| 129 | force_mb_all_threads(); |
| 130 | prev_parity = switch_next_urcu_qparity(); |
| 131 | |
| 132 | /* |
| 133 | * Wait for previous parity to be empty of readers. |
| 134 | */ |
| 135 | wait_for_quiescent_state(prev_parity); |
| 136 | /* |
| 137 | * Deleting old data is ok ! |
| 138 | */ |
| 139 | |
| 140 | return oldptr; |
| 141 | } |
| 142 | |
| 143 | void urcu_add_reader(pthread_t id) |
| 144 | { |
| 145 | struct reader_data *oldarray; |
| 146 | |
| 147 | if (!reader_data) { |
| 148 | alloc_readers = INIT_NUM_THREADS; |
| 149 | num_readers = 0; |
| 150 | reader_data = |
| 151 | malloc(sizeof(struct reader_data) * alloc_readers); |
| 152 | } |
| 153 | if (alloc_readers < num_readers + 1) { |
| 154 | oldarray = reader_data; |
| 155 | reader_data = malloc(sizeof(struct reader_data) |
| 156 | * (alloc_readers << 1)); |
| 157 | memcpy(reader_data, oldarray, |
| 158 | sizeof(struct reader_data) * alloc_readers); |
| 159 | alloc_readers <<= 1; |
| 160 | free(oldarray); |
| 161 | } |
| 162 | reader_data[num_readers].tid = id; |
| 163 | /* reference to the TLS of _this_ reader thread. */ |
| 164 | reader_data[num_readers].urcu_active_readers = urcu_active_readers; |
| 165 | num_readers++; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Never shrink (implementation limitation). |
| 170 | * This is O(nb threads). Eventually use a hash table. |
| 171 | */ |
| 172 | void urcu_remove_reader(pthread_t id) |
| 173 | { |
| 174 | struct reader_data *index; |
| 175 | |
| 176 | assert(reader_data != NULL); |
| 177 | for (index = reader_data; index < reader_data + num_readers; index++) { |
| 178 | if (index->tid == id) { |
| 179 | memcpy(index, &reader_data[num_readers - 1], |
| 180 | sizeof(struct reader_data)); |
| 181 | reader_data[num_readers - 1].tid = 0; |
| 182 | reader_data[num_readers - 1].urcu_active_readers = NULL; |
| 183 | num_readers--; |
| 184 | return; |
| 185 | } |
| 186 | } |
| 187 | /* Hrm not found, forgot to register ? */ |
| 188 | assert(0); |
| 189 | } |
| 190 | |
| 191 | void urcu_register_thread(void) |
| 192 | { |
| 193 | rcu_write_lock(); |
| 194 | urcu_add_reader(pthread_self()); |
| 195 | rcu_write_unlock(); |
| 196 | } |
| 197 | |
| 198 | void urcu_unregister_thread(void) |
| 199 | { |
| 200 | pthread_t self = pthread_self(); |
| 201 | rcu_write_lock(); |
| 202 | urcu_remove_reader(pthread_self()); |
| 203 | rcu_write_unlock(); |
| 204 | } |
| 205 | |
| 206 | void sigurcu_handler(int signo, siginfo_t *siginfo, void *context) |
| 207 | { |
| 208 | mb(); |
| 209 | atomic_inc(&sig_done); |
| 210 | } |
| 211 | |
| 212 | void __attribute__((constructor)) urcu_init(void) |
| 213 | { |
| 214 | struct sigaction act; |
| 215 | int ret; |
| 216 | |
| 217 | act.sa_sigaction = sigurcu_handler; |
| 218 | ret = sigaction(SIGURCU, &act, NULL); |
| 219 | if (ret) { |
| 220 | perror("Error in sigaction"); |
| 221 | exit(-1); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | void __attribute__((destructor)) urcu_exit(void) |
| 226 | { |
| 227 | struct sigaction act; |
| 228 | int ret; |
| 229 | |
| 230 | ret = sigaction(SIGURCU, NULL, &act); |
| 231 | if (ret) { |
| 232 | perror("Error in sigaction"); |
| 233 | exit(-1); |
| 234 | } |
| 235 | assert(act.sa_sigaction == sigurcu_handler); |
| 236 | free(reader_data); |
| 237 | } |