Skip to contentSkip to conversation

Fix: pthread_rwlock initialization on Cygwin

PATCH New Michael Jeanson 2 participants 6 messages Nov 23, 2018 4 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 2/4 Fix: pthread_rwlock initialization on Cygwin

On Cygwin the PTHREAD_RWLOCK_INITIALIZER macro is not sufficient to get a properly initialized pthread_rwlock_t struct. Use the pthread_rwlock_init function instead which should work on all platforms.

---
 tests/benchmark/test_rwlock.c        | 34 +++++++++++++++++++++++-----
 tests/benchmark/test_rwlock_timing.c | 33 +++++++++++++++++++++------
 2 files changed, 54 insertions(+), 13 deletions(-)
tests/benchmark/test_rwlock.c +28 -6
index 4448597..c23e782 100644
@@ -48,7 +48,7 @@ struct test_array {
 	int a;
 };
 
-pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;
+pthread_rwlock_t lock;
 
 static volatile int test_go, test_stop;
 
@@ -173,14 +173,21 @@ void *thr_reader(void *_count)
 	}
 
 	for (;;) {
-		int a;
+		int a, ret;
+
+		ret = pthread_rwlock_rdlock(&lock);
+		if (ret)
+			fprintf(stderr, "reader pthread_rwlock_rdlock: %s\n", strerror(ret));
 
-		pthread_rwlock_rdlock(&lock);
 		a = test_array.a;
 		assert(a == 8);
 		if (caa_unlikely(rduration))
 			loop_sleep(rduration);
-		pthread_rwlock_unlock(&lock);
+
+		ret = pthread_rwlock_unlock(&lock);
+		if (ret)
+			fprintf(stderr, "reader pthread_rwlock_unlock: %s\n", strerror(ret));
+
 		URCU_TLS(nr_reads)++;
 		if (caa_unlikely(!test_duration_read()))
 			break;
@@ -208,12 +215,21 @@ void *thr_writer(void *_count)
 	cmm_smp_mb();
 
 	for (;;) {
-		pthread_rwlock_wrlock(&lock);
+		int ret;
+
+		ret = pthread_rwlock_wrlock(&lock);
+		if (ret)
+			fprintf(stderr, "writer pthread_rwlock_wrlock: %s\n", strerror(ret));
+
 		test_array.a = 0;
 		test_array.a = 8;
 		if (caa_unlikely(wduration))
 			loop_sleep(wduration);
-		pthread_rwlock_unlock(&lock);
+
+		ret = pthread_rwlock_unlock(&lock);
+		if (ret)
+			fprintf(stderr, "writer pthread_rwlock_unlock: %s\n", strerror(ret));
+
 		URCU_TLS(nr_writes)++;
 		if (caa_unlikely(!test_duration_write()))
 			break;
@@ -321,6 +337,12 @@ int main(int argc, char **argv)
 	printf_verbose("thread %-6s, tid %lu\n",
 			"main", urcu_get_thread_id());
 
+	err = pthread_rwlock_init(&lock, NULL);
+	if (err != 0) {
+		fprintf(stderr, "pthread_rwlock_init: (%d) %s\n", err, strerror(err));
+		exit(1);
+	}
+
 	tid_reader = calloc(nr_readers, sizeof(*tid_reader));
 	tid_writer = calloc(nr_writers, sizeof(*tid_writer));
 	count_reader = calloc(nr_readers, sizeof(*count_reader));
tests/benchmark/test_rwlock_timing.c +26 -7
index a52da38..1e7863a 100644
@@ -42,7 +42,7 @@ struct test_array {
 	int a;
 };
 
-pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;
+pthread_rwlock_t lock;
 
 static struct test_array test_array = { 8 };
 
@@ -65,7 +65,7 @@ static caa_cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *writer_time;
 
 void *thr_reader(void *arg)
 {
-	int i, j;
+	int i, j, ret;
 	caa_cycles_t time1, time2;
 
 	printf("thread_begin %s, tid %lu\n",
@@ -75,9 +75,15 @@ void *thr_reader(void *arg)
 	time1 = caa_get_cycles();
 	for (i = 0; i < OUTER_READ_LOOP; i++) {
 		for (j = 0; j < INNER_READ_LOOP; j++) {
-			pthread_rwlock_rdlock(&lock);
+			ret = pthread_rwlock_rdlock(&lock);
+			if (ret)
+				fprintf(stderr, "reader pthread_rwlock_rdlock: %s\n", strerror(ret));
+
 			assert(test_array.a == 8);
-			pthread_rwlock_unlock(&lock);
+
+			ret = pthread_rwlock_unlock(&lock);
+			if (ret)
+				fprintf(stderr, "reader pthread_rwlock_unlock: %s\n", strerror(ret));
 		}
 	}
 	time2 = caa_get_cycles();
@@ -93,7 +99,7 @@ void *thr_reader(void *arg)
 
 void *thr_writer(void *arg)
 {
-	int i, j;
+	int i, j, ret;
 	caa_cycles_t time1, time2;
 
 	printf("thread_begin %s, tid %lu\n",
@@ -103,9 +109,16 @@ void *thr_writer(void *arg)
 	for (i = 0; i < OUTER_WRITE_LOOP; i++) {
 		for (j = 0; j < INNER_WRITE_LOOP; j++) {
 			time1 = caa_get_cycles();
-			pthread_rwlock_wrlock(&lock);
+			ret = pthread_rwlock_wrlock(&lock);
+			if (ret)
+				fprintf(stderr, "writer pthread_rwlock_wrlock: %s\n", strerror(ret));
+
 			test_array.a = 8;
-			pthread_rwlock_unlock(&lock);
+
+			ret = pthread_rwlock_unlock(&lock);
+			if (ret)
+				fprintf(stderr, "writer pthread_rwlock_unlock: %s\n", strerror(ret));
+
 			time2 = caa_get_cycles();
 			writer_time[(unsigned long)arg] += time2 - time1;
 			usleep(1);
@@ -133,6 +146,12 @@ int main(int argc, char **argv)
 	num_read = atoi(argv[1]);
 	num_write = atoi(argv[2]);
 
+	err = pthread_rwlock_init(&lock, NULL);
+	if (err != 0) {
+		fprintf(stderr, "pthread_rwlock_init: (%d) %s\n", err, strerror(err));
+		exit(1);
+	}
+
 	reader_time = calloc(num_read, sizeof(*reader_time));
 	writer_time = calloc(num_write, sizeof(*writer_time));
 	tid_reader = calloc(num_read, sizeof(*tid_reader));
Signature · 1 line
2.17.1
#3 lore raw

PATCH 4/4 test_rwlock: Add per-thread count to verbose output

In reply to a message not in the archive
---
 tests/benchmark/test_rwlock.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
tests/benchmark/test_rwlock.c +7 -5
index c23e782..02073f1 100644
@@ -194,8 +194,9 @@ void *thr_reader(void *_count)
 	}
 
 	*count = URCU_TLS(nr_reads);
-	printf_verbose("thread_end %s, tid %lu\n",
-			"reader", urcu_get_thread_id());
+
+	printf_verbose("thread_end %s, tid %lu, count %llu\n",
+			"reader", urcu_get_thread_id(), *count);
 	return ((void*)1);
 
 }
@@ -236,10 +237,10 @@ void *thr_writer(void *_count)
 		if (caa_unlikely(wdelay))
 			loop_sleep(wdelay);
 	}
-
-	printf_verbose("thread_end %s, tid %lu\n",
-			"writer", urcu_get_thread_id());
 	*count = URCU_TLS(nr_writes);
+
+	printf_verbose("thread_end %s, tid %lu, count %llu\n",
+			"writer", urcu_get_thread_id(), *count);
 	return ((void*)2);
 }
 
@@ -333,6 +334,7 @@ int main(int argc, char **argv)
 	printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
 		duration, nr_readers, nr_writers);
 	printf_verbose("Writer delay : %lu loops.\n", wdelay);
+	printf_verbose("Writer duration : %lu loops.\n", wduration);
 	printf_verbose("Reader duration : %lu loops.\n", rduration);
 	printf_verbose("thread %-6s, tid %lu\n",
 			"main", urcu_get_thread_id());
Signature · 1 line
2.17.1

A message that is not in the archive (sent off-list or not yet received): [email protected]

#5 lore raw
Quoted 25 lines · ----- On Nov 23, 2018, at 3:27 PM, Michael Jeanson [email protected] wrote:

On Cygwin the PTHREAD_RWLOCK_INITIALIZER macro is not sufficient to get a properly initialized pthread_rwlock_t struct. Use the pthread_rwlock_init function instead which should work on all platforms.

---
tests/benchmark/test_rwlock.c | 34 +++++++++++++++++++++++-----
tests/benchmark/test_rwlock_timing.c | 33 +++++++++++++++++++++------

2 files changed, 54 insertions(+), 13 deletions(-)
tests/benchmark/test_rwlock.c +1 -1
index 4448597..c23e782 100644
@@ -48,7 +48,7 @@ struct test_array {
 	int a;
 };
 
-pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;
+pthread_rwlock_t lock;
 
 static volatile int test_go, test_stop;
 

tests/benchmark/test_rwlock.c void *thr_reader(void *_count) · line 180

Quoted 9 lines
tests/benchmark/test_rwlock.c +5 -1
@@ -173,14 +173,21 @@ void *thr_reader(void *_count)
 	}
 
 	for (;;) {
-		int a;
+		int a, ret;
+
+		ret = pthread_rwlock_rdlock(&lock);
+		if (ret)
+			fprintf(stderr, "reader pthread_rwlock_rdlock: %s\n", strerror(ret));

In each of those cases, shouldn't we also "abort()" ?

Quoted 123 lines
tests/benchmark/test_rwlock.c +22 -4
-		pthread_rwlock_rdlock(&lock);
 		a = test_array.a;
 		assert(a == 8);
 		if (caa_unlikely(rduration))
 			loop_sleep(rduration);
-		pthread_rwlock_unlock(&lock);
+
+		ret = pthread_rwlock_unlock(&lock);
+		if (ret)
+			fprintf(stderr, "reader pthread_rwlock_unlock: %s\n", strerror(ret));
+
 		URCU_TLS(nr_reads)++;
 		if (caa_unlikely(!test_duration_read()))
 			break;
@@ -208,12 +215,21 @@ void *thr_writer(void *_count)
 	cmm_smp_mb();
 
 	for (;;) {
-		pthread_rwlock_wrlock(&lock);
+		int ret;
+
+		ret = pthread_rwlock_wrlock(&lock);
+		if (ret)
+			fprintf(stderr, "writer pthread_rwlock_wrlock: %s\n", strerror(ret));
+
 		test_array.a = 0;
 		test_array.a = 8;
 		if (caa_unlikely(wduration))
 			loop_sleep(wduration);
-		pthread_rwlock_unlock(&lock);
+
+		ret = pthread_rwlock_unlock(&lock);
+		if (ret)
+			fprintf(stderr, "writer pthread_rwlock_unlock: %s\n", strerror(ret));
+
 		URCU_TLS(nr_writes)++;
 		if (caa_unlikely(!test_duration_write()))
 			break;
@@ -321,6 +337,12 @@ int main(int argc, char **argv)
 	printf_verbose("thread %-6s, tid %lu\n",
 			"main", urcu_get_thread_id());
 
+	err = pthread_rwlock_init(&lock, NULL);
+	if (err != 0) {
+		fprintf(stderr, "pthread_rwlock_init: (%d) %s\n", err, strerror(err));
+		exit(1);
+	}
+
 	tid_reader = calloc(nr_readers, sizeof(*tid_reader));
 	tid_writer = calloc(nr_writers, sizeof(*tid_writer));
 	count_reader = calloc(nr_readers, sizeof(*count_reader));
tests/benchmark/test_rwlock_timing.c +2 -2
index a52da38..1e7863a 100644
@@ -42,7 +42,7 @@ struct test_array {
 	int a;
 };
 
-pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;
+pthread_rwlock_t lock;
 
 static struct test_array test_array = { 8 };
 
@@ -65,7 +65,7 @@ static caa_cycles_t
 __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *writer_time;
 
 void *thr_reader(void *arg)
 {
-	int i, j;
+	int i, j, ret;
 	caa_cycles_t time1, time2;
 

printf("thread_begin %s, tid %lu\n",

tests/benchmark/test_rwlock_timing.c +18 -5
@@ -75,9 +75,15 @@ void *thr_reader(void *arg)
 	time1 = caa_get_cycles();
 	for (i = 0; i < OUTER_READ_LOOP; i++) {
 		for (j = 0; j < INNER_READ_LOOP; j++) {
-			pthread_rwlock_rdlock(&lock);
+			ret = pthread_rwlock_rdlock(&lock);
+			if (ret)
+				fprintf(stderr, "reader pthread_rwlock_rdlock: %s\n", strerror(ret));
+
 			assert(test_array.a == 8);
-			pthread_rwlock_unlock(&lock);
+
+			ret = pthread_rwlock_unlock(&lock);
+			if (ret)
+				fprintf(stderr, "reader pthread_rwlock_unlock: %s\n", strerror(ret));
 		}
 	}
 	time2 = caa_get_cycles();
@@ -93,7 +99,7 @@ void *thr_reader(void *arg)
 
 void *thr_writer(void *arg)
 {
-	int i, j;
+	int i, j, ret;
 	caa_cycles_t time1, time2;
 
 	printf("thread_begin %s, tid %lu\n",
@@ -103,9 +109,16 @@ void *thr_writer(void *arg)
 	for (i = 0; i < OUTER_WRITE_LOOP; i++) {
 		for (j = 0; j < INNER_WRITE_LOOP; j++) {
 			time1 = caa_get_cycles();
-			pthread_rwlock_wrlock(&lock);
+			ret = pthread_rwlock_wrlock(&lock);
+			if (ret)
+				fprintf(stderr, "writer pthread_rwlock_wrlock: %s\n", strerror(ret));
+
 			test_array.a = 8;
-			pthread_rwlock_unlock(&lock);
+
+			ret = pthread_rwlock_unlock(&lock);
+			if (ret)
+				fprintf(stderr, "writer pthread_rwlock_unlock: %s\n", strerror(ret));
+
 			time2 = caa_get_cycles();
 			writer_time[(unsigned long)arg] += time2 - time1;
 			usleep(1);

tests/benchmark/test_rwlock_timing.c int main(int argc, char **argv) · line 152

Quoted 7 lines
tests/benchmark/test_rwlock_timing.c +4 -0
@@ -133,6 +146,12 @@ int main(int argc, char **argv)
 	num_read = atoi(argv[1]);
 	num_write = atoi(argv[2]);
 
+	err = pthread_rwlock_init(&lock, NULL);
+	if (err != 0) {
+		fprintf(stderr, "pthread_rwlock_init: (%d) %s\n", err, strerror(err));
+		exit(1);

I typically use "abort()" instead.

Thanks,

Mathieu

Quoted 7 lines
tests/benchmark/test_rwlock_timing.c +2 -1
+	}
+
 	reader_time = calloc(num_read, sizeof(*reader_time));
 	writer_time = calloc(num_write, sizeof(*writer_time));
 	tid_reader = calloc(num_read, sizeof(*tid_reader));
--

2.17.1

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com

A message that is not in the archive (sent off-list or not yet received): [email protected]

#7 lore raw
Quoted 29 lines · Mathieu Desnoyers
Quoted 27 lines · ----- On Nov 23, 2018, at 3:27 PM, Michael Jeanson [email protected] wrote:

On Cygwin the PTHREAD_RWLOCK_INITIALIZER macro is not sufficient to get a properly initialized pthread_rwlock_t struct. Use the pthread_rwlock_init function instead which should work on all platforms.

---
tests/benchmark/test_rwlock.c | 34 +++++++++++++++++++++++-----
tests/benchmark/test_rwlock_timing.c | 33 +++++++++++++++++++++------

2 files changed, 54 insertions(+), 13 deletions(-)
tests/benchmark/test_rwlock.c +1 -1
index 4448597..c23e782 100644
@@ -48,7 +48,7 @@ struct test_array {
 	int a;
 };
 
-pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;
+pthread_rwlock_t lock;
 
 static volatile int test_go, test_stop;
 
@@ -173,14 +173,21 @@ void *thr_reader(void *_count)
 	}
 
Quoted 9 lines

tests/benchmark/test_rwlock.c void *thr_reader(void *_count) · line 180

Quoted 7 lines
tests/benchmark/test_rwlock.c +5 -1
 	for (;;) {
-		int a;
+		int a, ret;
+
+		ret = pthread_rwlock_rdlock(&lock);
+		if (ret)
+			fprintf(stderr, "reader pthread_rwlock_rdlock: %s\n", strerror(ret));

In each of those cases, shouldn't we also "abort()" ?

Yes, that would make sense.

Quoted 127 lines
Quoted 127 lines
tests/benchmark/test_rwlock.c +22 -4
-		pthread_rwlock_rdlock(&lock);
 		a = test_array.a;
 		assert(a == 8);
 		if (caa_unlikely(rduration))
 			loop_sleep(rduration);
-		pthread_rwlock_unlock(&lock);
+
+		ret = pthread_rwlock_unlock(&lock);
+		if (ret)
+			fprintf(stderr, "reader pthread_rwlock_unlock: %s\n", strerror(ret));
+
 		URCU_TLS(nr_reads)++;
 		if (caa_unlikely(!test_duration_read()))
 			break;
@@ -208,12 +215,21 @@ void *thr_writer(void *_count)
 	cmm_smp_mb();
 
 	for (;;) {
-		pthread_rwlock_wrlock(&lock);
+		int ret;
+
+		ret = pthread_rwlock_wrlock(&lock);
+		if (ret)
+			fprintf(stderr, "writer pthread_rwlock_wrlock: %s\n", strerror(ret));
+
 		test_array.a = 0;
 		test_array.a = 8;
 		if (caa_unlikely(wduration))
 			loop_sleep(wduration);
-		pthread_rwlock_unlock(&lock);
+
+		ret = pthread_rwlock_unlock(&lock);
+		if (ret)
+			fprintf(stderr, "writer pthread_rwlock_unlock: %s\n", strerror(ret));
+
 		URCU_TLS(nr_writes)++;
 		if (caa_unlikely(!test_duration_write()))
 			break;
@@ -321,6 +337,12 @@ int main(int argc, char **argv)
 	printf_verbose("thread %-6s, tid %lu\n",
 			"main", urcu_get_thread_id());
 
+	err = pthread_rwlock_init(&lock, NULL);
+	if (err != 0) {
+		fprintf(stderr, "pthread_rwlock_init: (%d) %s\n", err, strerror(err));
+		exit(1);
+	}
+
 	tid_reader = calloc(nr_readers, sizeof(*tid_reader));
 	tid_writer = calloc(nr_writers, sizeof(*tid_writer));
 	count_reader = calloc(nr_readers, sizeof(*count_reader));
tests/benchmark/test_rwlock_timing.c +2 -2
index a52da38..1e7863a 100644
@@ -42,7 +42,7 @@ struct test_array {
 	int a;
 };
 
-pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;
+pthread_rwlock_t lock;
 
 static struct test_array test_array = { 8 };
 
@@ -65,7 +65,7 @@ static caa_cycles_t
 __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *writer_time;
 
 void *thr_reader(void *arg)
 {
-	int i, j;
+	int i, j, ret;
 	caa_cycles_t time1, time2;
 

printf("thread_begin %s, tid %lu\n",

tests/benchmark/test_rwlock_timing.c +18 -5
@@ -75,9 +75,15 @@ void *thr_reader(void *arg)
 	time1 = caa_get_cycles();
 	for (i = 0; i < OUTER_READ_LOOP; i++) {
 		for (j = 0; j < INNER_READ_LOOP; j++) {
-			pthread_rwlock_rdlock(&lock);
+			ret = pthread_rwlock_rdlock(&lock);
+			if (ret)
+				fprintf(stderr, "reader pthread_rwlock_rdlock: %s\n", strerror(ret));
+
 			assert(test_array.a == 8);
-			pthread_rwlock_unlock(&lock);
+
+			ret = pthread_rwlock_unlock(&lock);
+			if (ret)
+				fprintf(stderr, "reader pthread_rwlock_unlock: %s\n", strerror(ret));
 		}
 	}
 	time2 = caa_get_cycles();
@@ -93,7 +99,7 @@ void *thr_reader(void *arg)
 
 void *thr_writer(void *arg)
 {
-	int i, j;
+	int i, j, ret;
 	caa_cycles_t time1, time2;
 
 	printf("thread_begin %s, tid %lu\n",
@@ -103,9 +109,16 @@ void *thr_writer(void *arg)
 	for (i = 0; i < OUTER_WRITE_LOOP; i++) {
 		for (j = 0; j < INNER_WRITE_LOOP; j++) {
 			time1 = caa_get_cycles();
-			pthread_rwlock_wrlock(&lock);
+			ret = pthread_rwlock_wrlock(&lock);
+			if (ret)
+				fprintf(stderr, "writer pthread_rwlock_wrlock: %s\n", strerror(ret));
+
 			test_array.a = 8;
-			pthread_rwlock_unlock(&lock);
+
+			ret = pthread_rwlock_unlock(&lock);
+			if (ret)
+				fprintf(stderr, "writer pthread_rwlock_unlock: %s\n", strerror(ret));
+
 			time2 = caa_get_cycles();
 			writer_time[(unsigned long)arg] += time2 - time1;
 			usleep(1);
@@ -133,6 +146,12 @@ int main(int argc, char **argv)
 	num_read = atoi(argv[1]);
 	num_write = atoi(argv[2]);
 
Quoted 6 lines

tests/benchmark/test_rwlock_timing.c int main(int argc, char **argv) · line 152

Quoted 4 lines
tests/benchmark/test_rwlock_timing.c +4 -0
+	err = pthread_rwlock_init(&lock, NULL);
+	if (err != 0) {
+		fprintf(stderr, "pthread_rwlock_init: (%d) %s\n", err, strerror(err));
+		exit(1);

I typically use "abort()" instead.

exit() is used everywhere in this file to abort, I went with the "local" style.

Quoted 14 lines

Thanks,

Mathieu

Quoted 7 lines
tests/benchmark/test_rwlock_timing.c +2 -1
+	}
+
 	reader_time = calloc(num_read, sizeof(*reader_time));
 	writer_time = calloc(num_write, sizeof(*writer_time));
 	tid_reader = calloc(num_read, sizeof(*tid_reader));
--

2.17.1

A message that is not in the archive (sent off-list or not yet received): [email protected]

#9 lore raw
Quoted 1 line · ----- On Nov 23, 2018, at 3:54 PM, Michael Jeanson [email protected] wrote:

On 2018-11-23 3:46 p.m., Mathieu Desnoyers wrote:

[...]

Quoted 4 lines
Quoted 1 line

I typically use "abort()" instead.

exit() is used everywhere in this file to abort, I went with the "local" style.

Works for me!

Thanks,

Mathieu

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com