aboutsummaryrefslogtreecommitdiffstats
path: root/main.c
blob: 2fe9a0f9919fb6ef6a030cc345b2dad7e52830da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <stdbool.h>
#include <string.h>
#define NUM_THREADS	31

enum thead_id_status {
	THREAD_AT_HOME = 0,
	THREAD_AT_PARTY = 1,
	THREAD_DONE = 2,
};

struct bin {
	bool ready;
	int food[NUM_THREADS+1];
	int trash[NUM_THREADS+1];
	int tid_status[NUM_THREADS+1];
	pthread_mutex_t mutex;
};

struct party_ticket {
	long threadid;
	struct bin *party;
};

struct bin *party;

static void sleep_random(void)
{
	unsigned int val;

	val = 10000LL * rand() / RAND_MAX;
	srand(getpid());
	usleep(val);
}

void eat(long tid)
{
	bool eat_in_order = true;
	long t;

	pthread_mutex_lock(&party->mutex);
	/* If anyone ate out of place lets take notice of that... */
	for (t=0; t < NUM_THREADS; t++) {
		if (t == tid)
			continue;
		if (party->tid_status[t] >= THREAD_AT_PARTY &&
		    party->food[t])
			eat_in_order = false;
	}

	sleep_random();

	if (party->food[tid])
		if (eat_in_order)
			party->food[tid] = 0;
	pthread_mutex_unlock(&party->mutex);
}

void cleanup(long tid)
{
	bool clean_follow = true;
	long t;

	for (t=0; t < NUM_THREADS; t++) {
		if (t == tid)
			continue;
		if (party->tid_status[t] >= THREAD_DONE &&
		    !party->trash[t])
			clean_follow = false;
	}

	if (clean_follow)
		party->trash[tid] = 1;
}

void *thread_party(void *t)
{
	long tid = (long)t;

	party->tid_status[tid] = THREAD_AT_PARTY;

	eat(tid);

	if (tid % 2 == 1)
		sleep_random();

	cleanup(tid);

	party->tid_status[tid] = THREAD_DONE;

	pthread_exit(NULL);
}

void show_party(struct bin *party)
{
	long t;

	printf("FOOD:  [ ");

	for (t=0; t < NUM_THREADS; t++) {
		if (party->food[t])
			printf("*");
		else
			printf(" ");
	}
	printf(" ]\n");

	printf("TRASH: [ ");
	for (t=0; t < NUM_THREADS; t++) {
		if (party->trash[t])
			printf("*");
		else
			printf(" ");
	}
	printf(" ]\n");
}

#define pthread_mutex_protects_3(mutex, var1, var2, var3)

int main(int argc, char *argv[])
{
	pthread_t threads[NUM_THREADS];
	int ret;
	long t;
	pthread_attr_t attr;
	void *status;

	party = malloc(sizeof(struct bin));
	if (!party)
		return -ENOMEM;
	memset(party, 0, sizeof(struct bin));
	for (t=0; t <= NUM_THREADS; t++) {
		party->food[t] = 1;
		party->trash[t] = 0;
		party->tid_status[t] = THREAD_AT_HOME;
	}
	party->ready = true;

	printf("Before party:\n");
	show_party(party);

	pthread_mutex_init(&party->mutex, NULL);
	pthread_mutex_protects_3(&party->mutex,
				 party->food,
				 party->trash,
				 party->tid_status);

	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

	for (t=0; t < NUM_THREADS; t++) {
		ret = pthread_create(&threads[t], &attr, thread_party, (void *)t);
		if (ret){
			printf("ERROR; return code from pthread_create() is %d\n", ret);
			exit(-1);
		}
	}

	pthread_attr_destroy(&attr);
	for (t=0; t < NUM_THREADS; t++)
		pthread_join(threads[t], &status);

	printf("\nAfter party:\n");
	show_party(party);

	pthread_mutex_destroy(&party->mutex);
	pthread_exit(NULL);

	free(party);
}