forked from nanvix/nanvix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbthd.c
177 lines (154 loc) · 4.07 KB
/
bthd.c
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
175
176
#include <stdio.h>
#include <sys/types.h>
#include <bthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
/*----------------------------------------------------------------------------*
* PRODUCER-CONSUMER EXAMPLE *
*----------------------------------------------------------------------------*/
#define PROD_CONS_DURATION 0x10
#define BUFFER_SIZE 10
int buffer[BUFFER_SIZE] = { 0 };
int buffer_index = 0;
struct bt_mutex *lock;
void *producer()
{
int i = 0;
while (i++ < PROD_CONS_DURATION)
{
if (buffer_index < BUFFER_SIZE)
{
bthread_mutex_lock(lock);
if (buffer_index < BUFFER_SIZE)
{
int r = rand() % 100 + 1;
buffer[buffer_index++] = r;
printf("Produced: %d\n", r);
}
bthread_mutex_unlock(lock);
}
if (rand() % 2)
bthread_yield();
}
return NULL;
}
void *consumer()
{
int i = 0;
while (i++ < PROD_CONS_DURATION)
{
if (buffer_index > 0)
{
bthread_mutex_lock(lock);
if (buffer_index > 0)
{
int r = buffer[buffer_index - 1];
buffer[--buffer_index] = 0;
printf("Consumed: %d\n", r);
}
bthread_mutex_unlock(lock);
}
if (rand() % 2)
bthread_yield();
}
return NULL;
}
void producer_consumer_test(void)
{
lock = bthread_mutex_init();
bthread_t t1, t2;
bthread_create(&t1, producer, NULL);
bthread_create(&t2, consumer, NULL);
bthread_detach(t1);
bthread_join(t2, NULL);
bthread_mutex_destroy(lock);
// Print the buffer
printf("Buffer: ");
for (int i = 0; i < BUFFER_SIZE; i++)
{
printf("%d ", buffer[i]);
}
printf("\n");
}
/*----------------------------------------------------------------------------*
* SIMPLE SCHEDULER EXAMPLE *
*----------------------------------------------------------------------------*/
#define N 3
int arr[N] = { 0 };
void *thread(int *arg)
{
// WARNING - A larger number can cause a stack overflow
#define THREAD_DURATION 5
for (int i = 0; i < THREAD_DURATION; i++)
{
*arg += 1;
printf("array: ");
for (int i = 0; i < N; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
return NULL;
}
void simple_sched(void)
{
bthread_t t[N];
for (int i = 0; i < N; i++)
{
bthread_create(&t[i], thread, &arr[i]);
}
for (int i = 0; i < N; i++)
{
bthread_join(t[i], NULL);
}
}
/*----------------------------------------------------------------------------*
* MAIN & OPTIONS *
*----------------------------------------------------------------------------*/
/*
* Prints program version and exits.
*/
static void version(void)
{
printf("bthd (Bthread Example)\n");
printf("Copyright(C) 2023 Bthread maintainers\n");
printf("This is free software under the ");
printf("GNU General Public License Version 3.\n");
printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
exit(EXIT_SUCCESS);
}
/*
* Prints program usage and exits.
*/
static void usage(void)
{
printf("Usage: bthd [options]\n\n");
printf("Brief: Bthread library examples.\n\n");
printf("Options:\n");
printf(" sched Executes simple scheduling example\n");
printf(" prodcons Executes producer-consumer example\n");
printf(" --help Display this information and exit\n");
printf(" --version Display program version and exit\n");
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[])
{
if (argc != 2)
usage();
if (strcmp(argv[1], "sched") == 0)
{
simple_sched();
}
else if (strcmp(argv[1], "prodcons") == 0)
{
producer_consumer_test();
}
else if (strcmp(argv[1], "--version") == 0) {
version();
} else {
usage();
}
return (EXIT_SUCCESS);
}