static int procs = 2;
static int iterations = 1;
-private_t private;
static queue_t *queue;
+static thrd_t *threads;
+static int num_threads;
+
+int get_thread_num()
+{
+ thrd_t curr = thrd_current();
+ int i;
+ for (i = 0; i < num_threads; i++)
+ if (curr.priv == threads[i].priv)
+ return i;
+ assert(0);
+ return -1;
+}
static void parse_args(int argc, char **argv)
{
unsigned val;
int pid = *((int *)param);
- init_private(pid);
for (i = 0; i < iterations; i++) {
val = 1 + pid * iterations + i;
enqueue(queue, val);
int user_main(int argc, char **argv)
{
- int i, num_threads;
- thrd_t *t;
+ int i;
int *param;
parse_args(argc, argv);
assert(queue);
num_threads = procs;
- t = malloc(num_threads * sizeof(thrd_t));
+ threads = malloc(num_threads * sizeof(thrd_t));
param = malloc(num_threads * sizeof(*param));
- init_queue(queue);
+ init_queue(queue, num_threads);
for (i = 0; i < num_threads; i++) {
param[i] = i;
- thrd_create(&t[i], main_task, ¶m[i]);
+ thrd_create(&threads[i], main_task, ¶m[i]);
}
for (i = 0; i < num_threads; i++)
- thrd_join(t[i]);
+ thrd_join(threads[i]);
free(param);
- free(t);
+ free(threads);
free(queue);
return 0;
#include <threads.h>
+#include <stdlib.h>
#include "my_queue.h"
-extern private_t private;
-
-void init_private(int pid)
-{
- private.node = 2 + pid;
-}
+static unsigned int *node_nums;
static unsigned int new_node()
{
- return private.node;
+ return node_nums[get_thread_num()];
}
static void reclaim(unsigned int node)
{
- private.node = node;
+ node_nums[get_thread_num()] = node;
}
-void init_queue(queue_t *q)
+void init_queue(queue_t *q, int num_threads)
{
unsigned int i;
pointer head;
pointer tail;
pointer next;
+ node_nums = malloc(num_threads * sizeof(*node_nums));
+ for (i = 0; i < num_threads; i++)
+ node_nums[i] = 2 + i;
+
/* initialize queue */
head = MAKE_POINTER(1, 0);
tail = MAKE_POINTER(1, 0);
pointer_t next;
} node_t;
-typedef struct private {
- unsigned int node;
-} private_t;
-
-typedef struct shared_mem {
+typedef struct {
pointer_t head;
pointer_t tail;
node_t nodes[MAX_NODES + 1];
} queue_t;
-void init_private(int pid);
-void init_queue(queue_t *q);
+void init_queue(queue_t *q, int num_threads);
void enqueue(queue_t *q, unsigned int val);
unsigned int dequeue(queue_t *q);
+int get_thread_num();