Updated thread_test
This change updates thread test to accept an optional argument of the
number of threads to test and also now calculates the expected sum of
the thread chain based on number of threads and loop count.
@@ -8,6 +8,11 @@ typedef unsigned long long word;
#define true 1
#define false 0
+#define NNUMS 2048
+#define NTHREADS 32
+
+static int nthreads = NTHREADS;
+
static word pipe_exit = (word)-1;
typedef int boolean;
@@ -79,6 +84,12 @@ void halt(char *message)
exit(-1);
}
+word expected_sum(word n, word nt) {
+ word tsum = (nt) * (0 + (nt - 1)) / 2;
+ n -= 1;
+ return n * (n + 1) * (2 * n + 1) / 6 + tsum * n;
+}
+
void *terminus(void *k)
{
word x = 0, v;
@@ -86,8 +97,7 @@ void *terminus(void *k)
while ((v = pipelock_read(p)) != pipe_exit) {
x += v;
}
- // make a derivable function
- if (x == 2862230032) {
+ if (x == expected_sum(NNUMS, nthreads)) {
printf("passed\n");
exit(0);
}
@@ -100,7 +110,9 @@ void *terminus(void *k)
// reader and shutdown
int main(int argc, char **argv)
{
- int nthreads = 32;
+ if (argc >= 2 && atoi(argv[1]) > 0)
+ nthreads = atoi(argv[1]);
+ printf("nthreads=%d\n", nthreads);
int npipes = nthreads+1 ;
pipelock *locks = malloc(sizeof(pipelock *)*npipes);
pthread_t *threads = malloc(sizeof(pthread_t)*nthreads);
@@ -119,7 +131,7 @@ int main(int argc, char **argv)
}
}
- for (int i = 1; i < 2048; i ++)
+ for (int i = 1; i < NNUMS; i ++)
pipelock_write(locks[0], i*i);
pipelock_write(locks[0], pipe_exit);
