Browse Source

Add a tsemacquire test

It seems that we may be having long timer times.

Change-Id: I69bece2f414566d4261ff60f5649cb27fadbc0de
Signed-off-by: Ronald G. Minnich <rminnich@gmail.com>
Ronald G. Minnich 9 years ago
parent
commit
0299c94c2d
2 changed files with 116 additions and 0 deletions
  1. 1 0
      sys/src/regress/regress.json
  2. 115 0
      sys/src/regress/tsemacquire.c

+ 1 - 0
sys/src/regress/regress.json

@@ -24,6 +24,7 @@
 		"sysfatal.c",
 		"sysstatread.c",
 		"thread.c",
+		"tsemacquire.c",
 		"va_copy.c",
 		"tls.c"
 	]

+ 115 - 0
sys/src/regress/tsemacquire.c

@@ -0,0 +1,115 @@
+#include <u.h>
+#include <libc.h>
+
+int32_t nprocs = 32;
+int32_t nloops = 100000;
+int32_t x = 1;
+int32_t incr;
+
+void
+tsemloop(void)
+{
+	int i;
+	i = 0;
+	while(i < nloops){
+		if(tsemacquire(&x, 10)){
+			if((i % 1000) == 0)
+				sleep(10);
+			incr++;
+			i++;
+			semrelease(&x, 1);
+		} else {
+			//print("pid %d timeout\n", getpid());
+		}
+	}
+	exits(nil);
+}
+
+void
+semloop(void)
+{
+	int i;
+	i = 0;
+	while(i < nloops){
+		if(semacquire(&x, 1)){
+			incr++;
+			i++;
+			semrelease(&x, 1);
+		} else {
+			sysfatal("semacquire failed");
+		}
+	}
+}
+
+
+void
+main(void)
+{
+	int pid, i;
+
+	incr = 0;
+	for(i = 0; i < nprocs; i++){
+		switch(rfork(RFMEM|RFPROC)){
+		case -1:
+			sysfatal("rfork");
+		case 0:
+			tsemloop();
+			exits(nil);
+		default:
+			break;
+		}
+	}
+	for(i = 0; i < nprocs; i++)
+		waitpid();
+	print("tsemloop incr %d\n", incr);
+	if(incr != nprocs*nloops){
+		print("FAIL\n");
+		exits("FAIL");
+	}
+
+	incr = 0;
+	for(i = 0; i < nprocs; i++){
+		switch(rfork(RFMEM|RFPROC)){
+		case -1:
+			sysfatal("rfork");
+		case 0:
+			semloop();
+			exits(nil);
+		default:
+			break;
+		}
+	}
+	for(i = 0; i < nprocs; i++)
+		waitpid();
+	print("semloop incr %d\n", incr);
+	if(incr != nprocs*nloops){
+		print("FAIL\n");
+		exits("FAIL");
+	}
+
+	incr = 0;
+	for(i = 0; i < nprocs; i++){
+		switch(rfork(RFMEM|RFPROC)){
+		case -1:
+			sysfatal("rfork");
+		case 0:
+			if((i&1) == 0)
+				semloop();
+			else
+				tsemloop();
+			exits(nil);
+		default:
+			break;
+		}
+	}
+	for(i = 0; i < nprocs; i++)
+		waitpid();
+	print("mixed incr %d\n", incr);
+	if(incr != nprocs*nloops){
+		print("FAIL\n");
+		exits("FAIL");
+	}
+
+	print("PASS\n");
+	exits(nil);
+}