Plan 9 from Bell Labs’s /usr/web/sources/contrib/mospak/libc-canlock-wakeup/libc-canlock-wakeup.diff

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


--- sys/src/libc/port/lock.c
+++ sys/src/libc/port/lock.c
@@ -25,9 +25,12 @@
 {
 	if(ainc(&l->key) == 1)
 		return 1;	/* changed from 0 -> 1: success */
-	/* Undo increment (but don't miss wakeup) */
-	if(adec(&l->key) == 0)
-		return 0;	/* changed from 1 -> 0: no contention */
-	semrelease(&l->sem, 1);
+	/* Undo increment */
+	if(adec(&l->key) != 0)
+		return 0;	/* still held */
+	/* unlocked meanwhile: absorb the wakeup our increment caused */
+	while(semacquire(&l->sem, 1) < 0){
+		/* interrupted; try again */
+	}
 	return 0;
 }
--- sys/src/ape/lib/ap/386/lock.c
+++ sys/src/ape/lib/ap/386/lock.c
@@ -28,9 +28,12 @@
 {
 	if(ainc(&l->key) == 1)
 		return 1;	/* changed from 0 -> 1: success */
-	/* Undo increment (but don't miss wakeup) */
-	if(adec(&l->key) == 0)
-		return 0;	/* changed from 1 -> 0: no contention */
-	_SEMRELEASE(&l->sem, 1);
+	/* Undo increment */
+	if(adec(&l->key) != 0)
+		return 0;	/* still held */
+	/* unlocked meanwhile: absorb the wakeup our increment caused */
+	while(_SEMACQUIRE(&l->sem, 1) < 0){
+		/* interrupted; try again */
+	}
 	return 0;
 }
--- sys/src/ape/lib/ap/amd64/lock.c
+++ sys/src/ape/lib/ap/amd64/lock.c
@@ -28,9 +28,12 @@
 {
 	if(ainc(&l->key) == 1)
 		return 1;	/* changed from 0 -> 1: success */
-	/* Undo increment (but don't miss wakeup) */
-	if(adec(&l->key) == 0)
-		return 0;	/* changed from 1 -> 0: no contention */
-	_SEMRELEASE(&l->sem, 1);
+	/* Undo increment */
+	if(adec(&l->key) != 0)
+		return 0;	/* still held */
+	/* unlocked meanwhile: absorb the wakeup our increment caused */
+	while(_SEMACQUIRE(&l->sem, 1) < 0){
+		/* interrupted; try again */
+	}
 	return 0;
 }
--- sys/src/ape/lib/ap/arm/lock.c
+++ sys/src/ape/lib/ap/arm/lock.c
@@ -28,9 +28,12 @@
 {
 	if(ainc(&l->key) == 1)
 		return 1;	/* changed from 0 -> 1: success */
-	/* Undo increment (but don't miss wakeup) */
-	if(adec(&l->key) == 0)
-		return 0;	/* changed from 1 -> 0: no contention */
-	_SEMRELEASE(&l->sem, 1);
+	/* Undo increment */
+	if(adec(&l->key) != 0)
+		return 0;	/* still held */
+	/* unlocked meanwhile: absorb the wakeup our increment caused */
+	while(_SEMACQUIRE(&l->sem, 1) < 0){
+		/* interrupted; try again */
+	}
 	return 0;
 }
--- sys/src/ape/lib/ap/arm64/lock.c
+++ sys/src/ape/lib/ap/arm64/lock.c
@@ -28,9 +28,12 @@
 {
 	if(ainc(&l->key) == 1)
 		return 1;	/* changed from 0 -> 1: success */
-	/* Undo increment (but don't miss wakeup) */
-	if(adec(&l->key) == 0)
-		return 0;	/* changed from 1 -> 0: no contention */
-	_SEMRELEASE(&l->sem, 1);
+	/* Undo increment */
+	if(adec(&l->key) != 0)
+		return 0;	/* still held */
+	/* unlocked meanwhile: absorb the wakeup our increment caused */
+	while(_SEMACQUIRE(&l->sem, 1) < 0){
+		/* interrupted; try again */
+	}
 	return 0;
 }
--- sys/src/ape/lib/ap/mips/lock.c
+++ sys/src/ape/lib/ap/mips/lock.c
@@ -28,9 +28,12 @@
 {
 	if(ainc(&l->key) == 1)
 		return 1;	/* changed from 0 -> 1: success */
-	/* Undo increment (but don't miss wakeup) */
-	if(adec(&l->key) == 0)
-		return 0;	/* changed from 1 -> 0: no contention */
-	_SEMRELEASE(&l->sem, 1);
+	/* Undo increment */
+	if(adec(&l->key) != 0)
+		return 0;	/* still held */
+	/* unlocked meanwhile: absorb the wakeup our increment caused */
+	while(_SEMACQUIRE(&l->sem, 1) < 0){
+		/* interrupted; try again */
+	}
 	return 0;
 }
--- sys/src/ape/lib/ap/mips64/lock.c
+++ sys/src/ape/lib/ap/mips64/lock.c
@@ -28,9 +28,12 @@
 {
 	if(ainc(&l->key) == 1)
 		return 1;	/* changed from 0 -> 1: success */
-	/* Undo increment (but don't miss wakeup) */
-	if(adec(&l->key) == 0)
-		return 0;	/* changed from 1 -> 0: no contention */
-	_SEMRELEASE(&l->sem, 1);
+	/* Undo increment */
+	if(adec(&l->key) != 0)
+		return 0;	/* still held */
+	/* unlocked meanwhile: absorb the wakeup our increment caused */
+	while(_SEMACQUIRE(&l->sem, 1) < 0){
+		/* interrupted; try again */
+	}
 	return 0;
 }
--- sys/src/ape/lib/ap/power/lock.c
+++ sys/src/ape/lib/ap/power/lock.c
@@ -28,9 +28,12 @@
 {
 	if(ainc(&l->key) == 1)
 		return 1;	/* changed from 0 -> 1: success */
-	/* Undo increment (but don't miss wakeup) */
-	if(adec(&l->key) == 0)
-		return 0;	/* changed from 1 -> 0: no contention */
-	_SEMRELEASE(&l->sem, 1);
+	/* Undo increment */
+	if(adec(&l->key) != 0)
+		return 0;	/* still held */
+	/* unlocked meanwhile: absorb the wakeup our increment caused */
+	while(_SEMACQUIRE(&l->sem, 1) < 0){
+		/* interrupted; try again */
+	}
 	return 0;
 }
--- sys/src/ape/lib/ap/riscv/lock.c
+++ sys/src/ape/lib/ap/riscv/lock.c
@@ -28,9 +28,12 @@
 {
 	if(ainc(&l->key) == 1)
 		return 1;	/* changed from 0 -> 1: success */
-	/* Undo increment (but don't miss wakeup) */
-	if(adec(&l->key) == 0)
-		return 0;	/* changed from 1 -> 0: no contention */
-	_SEMRELEASE(&l->sem, 1);
+	/* Undo increment */
+	if(adec(&l->key) != 0)
+		return 0;	/* still held */
+	/* unlocked meanwhile: absorb the wakeup our increment caused */
+	while(_SEMACQUIRE(&l->sem, 1) < 0){
+		/* interrupted; try again */
+	}
 	return 0;
 }
--- sys/src/ape/lib/ap/riscv64/lock.c
+++ sys/src/ape/lib/ap/riscv64/lock.c
@@ -28,9 +28,12 @@
 {
 	if(ainc(&l->key) == 1)
 		return 1;	/* changed from 0 -> 1: success */
-	/* Undo increment (but don't miss wakeup) */
-	if(adec(&l->key) == 0)
-		return 0;	/* changed from 1 -> 0: no contention */
-	_SEMRELEASE(&l->sem, 1);
+	/* Undo increment */
+	if(adec(&l->key) != 0)
+		return 0;	/* still held */
+	/* unlocked meanwhile: absorb the wakeup our increment caused */
+	while(_SEMACQUIRE(&l->sem, 1) < 0){
+		/* interrupted; try again */
+	}
 	return 0;
 }
--- sys/src/ape/lib/ap/sparc/lock.c
+++ sys/src/ape/lib/ap/sparc/lock.c
@@ -28,9 +28,12 @@
 {
 	if(ainc(&l->key) == 1)
 		return 1;	/* changed from 0 -> 1: success */
-	/* Undo increment (but don't miss wakeup) */
-	if(adec(&l->key) == 0)
-		return 0;	/* changed from 1 -> 0: no contention */
-	_SEMRELEASE(&l->sem, 1);
+	/* Undo increment */
+	if(adec(&l->key) != 0)
+		return 0;	/* still held */
+	/* unlocked meanwhile: absorb the wakeup our increment caused */
+	while(_SEMACQUIRE(&l->sem, 1) < 0){
+		/* interrupted; try again */
+	}
 	return 0;
 }
--- sys/src/ape/lib/ap/spim/lock.c
+++ sys/src/ape/lib/ap/spim/lock.c
@@ -28,9 +28,12 @@
 {
 	if(ainc(&l->key) == 1)
 		return 1;	/* changed from 0 -> 1: success */
-	/* Undo increment (but don't miss wakeup) */
-	if(adec(&l->key) == 0)
-		return 0;	/* changed from 1 -> 0: no contention */
-	_SEMRELEASE(&l->sem, 1);
+	/* Undo increment */
+	if(adec(&l->key) != 0)
+		return 0;	/* still held */
+	/* unlocked meanwhile: absorb the wakeup our increment caused */
+	while(_SEMACQUIRE(&l->sem, 1) < 0){
+		/* interrupted; try again */
+	}
 	return 0;
 }
--- sys/src/ape/lib/ap/spim64/lock.c
+++ sys/src/ape/lib/ap/spim64/lock.c
@@ -28,9 +28,12 @@
 {
 	if(ainc(&l->key) == 1)
 		return 1;	/* changed from 0 -> 1: success */
-	/* Undo increment (but don't miss wakeup) */
-	if(adec(&l->key) == 0)
-		return 0;	/* changed from 1 -> 0: no contention */
-	_SEMRELEASE(&l->sem, 1);
+	/* Undo increment */
+	if(adec(&l->key) != 0)
+		return 0;	/* still held */
+	/* unlocked meanwhile: absorb the wakeup our increment caused */
+	while(_SEMACQUIRE(&l->sem, 1) < 0){
+		/* interrupted; try again */
+	}
 	return 0;
 }

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.