#include <stdlib.h>
-#include <assert.h>
#include <stdio.h>
#include <threads.h>
#include "my_queue.h"
+#include "model-assert.h"
static int procs = 2;
-static int iterations = 1;
static queue_t *queue;
static thrd_t *threads;
+static unsigned int *input;
+static unsigned int *output;
static int num_threads;
int get_thread_num()
for (i = 0; i < num_threads; i++)
if (curr.priv == threads[i].priv)
return i;
- assert(0);
+ MODEL_ASSERT(0);
return -1;
}
-static void parse_args(int argc, char **argv)
-{
- extern char *optarg;
- int c;
-
- while ((c = getopt(argc, argv, "i:p:")) != EOF) {
- switch (c) {
- case 'i':
- iterations = atoi(optarg);
- break;
- case 'p':
- procs = atoi(optarg);
- break;
- default:
- assert(0);
- }
- }
-}
-
static void main_task(void *param)
{
- unsigned int i, j;
unsigned int val;
int pid = *((int *)param);
- for (i = 0; i < iterations; i++) {
- val = 1 + pid * iterations + i;
- printf("worker %d, enqueueing: %u\n", pid, val);
- enqueue(queue, val);
-
- val = dequeue(queue);
- printf("worker %d, dequeued: %u\n", pid, val);
+ if (!pid) {
+ input[0] = 17;
+ enqueue(queue, input[0]);
+ output[0] = dequeue(queue);
+ } else {
+ input[1] = 37;
+ enqueue(queue, input[1]);
+ output[1] = dequeue(queue);
}
}
{
int i;
int *param;
-
- parse_args(argc, argv);
+ unsigned int in_sum = 0, out_sum = 0;
+ int zero = 0;
queue = calloc(1, sizeof(*queue));
- assert(queue);
+ MODEL_ASSERT(queue);
num_threads = procs;
threads = malloc(num_threads * sizeof(thrd_t));
param = malloc(num_threads * sizeof(*param));
+ input = calloc(num_threads, sizeof(*input));
+ output = calloc(num_threads, sizeof(*output));
init_queue(queue, num_threads);
for (i = 0; i < num_threads; i++) {
for (i = 0; i < num_threads; i++)
thrd_join(threads[i]);
+ for (i = 0; i < num_threads; i++) {
+ in_sum += input[i];
+ out_sum += output[i];
+ }
+ for (i = 0; i < num_threads; i++)
+ printf("input[%d] = %u\n", i, input[i]);
+ for (i = 0; i < num_threads; i++) {
+ if (output[i] == 0)
+ zero = 1; /* A zero result means queue was empty */
+ printf("output[%d] = %u\n", i, output[i]);
+ }
+ MODEL_ASSERT(in_sum == out_sum || zero);
+
free(param);
free(threads);
free(queue);