#define NR_READ 10
#define NR_WRITE 9
+pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+void rcu_copy_mutex_lock(void)
+{
+ int ret;
+ ret = pthread_mutex_lock(&rcu_copy_mutex);
+ if (ret) {
+ perror("Error in pthread mutex lock");
+ exit(-1);
+ }
+}
+
+void rcu_copy_mutex_unlock(void)
+{
+ int ret;
+
+ ret = pthread_mutex_unlock(&rcu_copy_mutex);
+ if (ret) {
+ perror("Error in pthread mutex unlock");
+ exit(-1);
+ }
+}
void *thr_reader(void *arg)
{
for (i = 0; i < 100000; i++) {
for (j = 0; j < 100000000; j++) {
- qparity = rcu_read_lock();
+ rcu_read_lock(&qparity);
local_ptr = rcu_dereference(test_rcu_pointer);
if (local_ptr) {
assert(local_ptr->a == 8);
assert(local_ptr->b == 12);
assert(local_ptr->c[55] == 2);
}
- rcu_read_unlock(qparity);
+ rcu_read_unlock(&qparity);
}
}
for (i = 0; i < 10000000; i++) {
new = malloc(sizeof(struct test_array));
- rcu_write_lock();
+ rcu_copy_mutex_lock();
old = test_rcu_pointer;
if (old) {
assert(old->a == 8);
new->b = 12;
new->a = 8;
old = urcu_publish_content((void **)&test_rcu_pointer, new);
- rcu_write_unlock();
+ rcu_copy_mutex_unlock();
/* can be done after unlock */
if (old) {
old->a = 0;
#include "urcu.h"
+pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+void rcu_copy_mutex_lock(void)
+{
+ int ret;
+ ret = pthread_mutex_lock(&rcu_copy_mutex);
+ if (ret) {
+ perror("Error in pthread mutex lock");
+ exit(-1);
+ }
+}
+
+void rcu_copy_mutex_unlock(void)
+{
+ int ret;
+
+ ret = pthread_mutex_unlock(&rcu_copy_mutex);
+ if (ret) {
+ perror("Error in pthread mutex unlock");
+ exit(-1);
+ }
+}
+
struct test_array {
int a;
};
time1 = get_cycles();
for (i = 0; i < OUTER_READ_LOOP; i++) {
for (j = 0; j < INNER_READ_LOOP; j++) {
- qparity = rcu_read_lock();
+ rcu_read_lock(&qparity);
local_ptr = rcu_dereference(test_rcu_pointer);
if (local_ptr) {
assert(local_ptr->a == 8);
}
- rcu_read_unlock(qparity);
+ rcu_read_unlock(&qparity);
}
}
time2 = get_cycles();
for (i = 0; i < WRITE_LOOP; i++) {
new = malloc(sizeof(struct test_array));
- rcu_write_lock();
+ rcu_copy_mutex_lock();
old = test_rcu_pointer;
if (old) {
assert(old->a == 8);
}
new->a = 8;
old = urcu_publish_content((void **)&test_rcu_pointer, new);
- rcu_write_unlock();
+ rcu_copy_mutex_unlock();
/* can be done after unlock */
if (old) {
old->a = 0;
static int num_readers, alloc_readers;
static int sig_done;
-void rcu_write_lock(void)
+void internal_urcu_lock(void)
{
int ret;
ret = pthread_mutex_lock(&urcu_mutex);
}
}
-void rcu_write_unlock(void)
+void internal_urcu_unlock(void)
{
int ret;
void synchronize_rcu(void)
{
- rcu_write_lock();
+ internal_urcu_lock();
switch_qparity();
switch_qparity();
- rcu_write_unlock();
+ internal_urcu_lock();
}
/*
{
void *oldptr;
+ internal_urcu_lock();
/*
* We can publish the new pointer before we change the current qparity.
* Readers seeing the new pointer while being in the previous qparity
switch_qparity();
switch_qparity();
+ internal_urcu_unlock();
return oldptr;
}
void urcu_register_thread(void)
{
- rcu_write_lock();
+ internal_urcu_lock();
urcu_add_reader(pthread_self());
- rcu_write_unlock();
+ internal_urcu_unlock();
}
void urcu_unregister_thread(void)
{
- rcu_write_lock();
+ internal_urcu_lock();
urcu_remove_reader(pthread_self());
- rcu_write_unlock();
+ internal_urcu_unlock();
}
void sigurcu_handler(int signo, siginfo_t *siginfo, void *context)
}
/*
- * returns urcu_parity.
+ * urcu_parity should be declared on the caller's stack.
*/
-static inline int rcu_read_lock(void)
+static inline void rcu_read_lock(int *urcu_parity)
{
- int urcu_parity = get_urcu_qparity();
- urcu_active_readers[urcu_parity]++;
+ *urcu_parity = get_urcu_qparity();
+ urcu_active_readers[*urcu_parity]++;
/*
* Increment active readers count before accessing the pointer.
* See force_mb_all_threads().
*/
barrier();
- return urcu_parity;
}
-static inline void rcu_read_unlock(int urcu_parity)
+static inline void rcu_read_unlock(int *urcu_parity)
{
barrier();
/*
* Finish using rcu before decrementing the pointer.
* See force_mb_all_threads().
*/
- urcu_active_readers[urcu_parity]--;
+ urcu_active_readers[*urcu_parity]--;
}
-extern void rcu_write_lock(void);
-extern void rcu_write_unlock(void);
-
extern void *urcu_publish_content(void **ptr, void *new);
/*