summaryrefslogtreecommitdiffstats
path: root/examples/prod-cons-threads.c
blob: 8f0ecd06c33a7dcb9bdaa3acf93354ca041af9e0 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* ==========================================================================
 *  Licensed under BSD 2clause license See LICENSE file for more information
 *  Author: Michał Łyszczek <michal.lyszczek@bofc.pl>
 * ==========================================================================
 *       ------------------------------------------------------------
 *      / This example uses threads. It is classic consumer-producer \
 *      | problem that thanks to rb internal thread awareness is     |
 *      | reduced to minimum. Note that there is no mutex in this    |
 *      | file, all synchronization is performed by rb. Program      |
 *      | prints blocks representing count elements in rb to         |
 *      | visualize how elements are popped and pushed. This example |
 *      \ also shows how growable buffer works.                      /
 *       ------------------------------------------------------------
 *           \
 *            \
 *                oO)-.                       .-(Oo
 *               /__  _\                     /_  __\
 *               \  \(  |     ()~()         |  )/  /
 *                \__|\ |    (-___-)        | /|__/
 *                '  '--'    ==`-'==        '--'  '
 * ==========================================================================
 *                       ░▀█▀░█▀█░█▀▀░█░░░█░█░█▀▄░█▀▀░█▀▀
 *                       ░░█░░█░█░█░░░█░░░█░█░█░█░█▀▀░▀▀█
 *                       ░▀▀▀░▀░▀░▀▀▀░▀▀▀░▀▀▀░▀▀░░▀▀▀░▀▀▀
 * ========================================================================== */
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "rb.h"
#include "common.h"

/* ==========================================================================
 *               ░█▀▄░█▀▀░█▀▀░█░░░█▀█░█▀▄░█▀█░▀█▀░▀█▀░█▀█░█▀█░█▀▀
 *               ░█░█░█▀▀░█░░░█░░░█▀█░█▀▄░█▀█░░█░░░█░░█░█░█░█░▀▀█
 *               ░▀▀░░▀▀▀░▀▀▀░▀▀▀░▀░▀░▀░▀░▀░▀░░▀░░▀▀▀░▀▀▀░▀░▀░▀▀▀
 * ========================================================================== */
static struct rb  *rb;  /* pointer to malloc()ed rb object */
static const char *bar =
"=============================================================================";
static const char *sbar =
"                                                                             ";

/* ==========================================================================
 *                     ░█▀▀░█░█░█▀█░█▀▀░▀█▀░▀█▀░█▀█░█▀█░█▀▀
 *                     ░█▀▀░█░█░█░█░█░░░░█░░░█░░█░█░█░█░▀▀█
 *                     ░▀░░░▀▀▀░▀░▀░▀▀▀░░▀░░▀▀▀░▀▀▀░▀░▀░▀▀▀
 * ========================================================================== */

/** =========================================================================
 * Consumer thread.
 *
 * @param arg small integer, to control how fast data is read from buffer
 * ========================================================================== */
static void *consumer(void *arg)
{
	struct timespec  req;
	int              sleep_ms;
	int              i;
	int              data;
	int              run_time;
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

	sleep_ms = *(int *)arg;
	req.tv_sec = sleep_ms / 1000;
	req.tv_nsec = 1000000L * (sleep_ms % 1000);

	run_time = sleep_ms ? 15 : 300;

	for (i = 0; i != run_time; ++i) {
		if (rb_read(rb, &data, 1) != 1)
			pdie("rb_read()");

		/* use random time */
		if (sleep_ms == 0) {
			req.tv_sec = 0;
			req.tv_nsec = 1000000L * (rand() % 50 + 20);
		}
		nanosleep(&req, NULL);
	}

	return NULL;
}

/** =========================================================================
 * Producer thread.
 *
 * @param arg small integer, to control how fast data is read from buffer
 * ========================================================================== */
static void *producer(void *arg)
{
	struct timespec  req;
	int              sleep_ms;
	int              i;
	int              run_time;
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

	sleep_ms = *(int *)arg;
	req.tv_sec = sleep_ms / 1000;
	req.tv_nsec = 1000000L * (sleep_ms % 1000);

	run_time = sleep_ms ? 15 : 300;

	for (i = 0; i != run_time; ++i) {
		if (rb_write(rb, &i, 1) != 1)
			pdie("rb_write()");

		/* use random time */
		if (sleep_ms == 0) {
			req.tv_sec = 0;
			req.tv_nsec = 1000000L * (rand() % 50 + 1);
		}
		nanosleep(&req, NULL);
	}

	return NULL;
}

/** =========================================================================
 * Thread prints current ring buffer status with "graphical" representation
 * of fullness.
 *
 * @param rb ring buffer object
 * ========================================================================== */
static void *print_rb_status(void *rb)
{
	char dummy;
	struct timespec  req;

	req.tv_sec = 0;
	req.tv_nsec = 1000 * 1000L;

	for (;;) {
		int count = rb_count(rb);
		int space = rb_space(rb);
		int size = count + space;

		fprintf(stderr, "%3d/%d [%.*s%.*s]\r",
			count, size, count, bar, space, sbar);

		/* do this until we can no longer access rb */
		if (rb_recv(rb, &dummy, 1, rb_peek) == -1 && errno == ECANCELED) {
			fprintf(stderr, "\n");
			return NULL;
		}

		nanosleep(&req, NULL);
	}
}

/** =========================================================================
 * Run example. Start consumer and producer threads with configured action
 * speed.
 *
 * @param cons_sleep sleep between consuming action, 0 random
 * @param prod_sleep sleep between production action, 0 random
 * ========================================================================== */
static void run(int cons_sleep, int prod_sleep)
{
	pthread_t cons_t, prod_t, print_t;
	int flags;
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

	flags = rb_multithread;
	if (!cons_sleep && !prod_sleep)
		flags |= rb_growable;

	if ((rb = rb_new(8, sizeof(int), flags)) == NULL)
		pdie("rb_new()");

	if (!cons_sleep && !prod_sleep) {
		rb_set_hard_max_count(rb, 64);
		printf("=====] growable, hard limit 64, production > consumption [=====\n");
		printf("=====] consumer delay: [25-75]ms, producer delay: [50-100]ms [=====\n");
	} else
		printf("=====] consumer delay: %dms, producer delay: %dms [=====\n",
			cons_sleep, prod_sleep);

	pthread_create(&print_t, NULL, print_rb_status, rb);
	printf("-----| spawning producer |-----\n");
	pthread_create(&prod_t, NULL, producer, &prod_sleep);
	if (prod_sleep > cons_sleep)
		sleep(5);
	printf("-----| spawning consumer |-----\n");
	pthread_create(&cons_t, NULL, consumer, &cons_sleep);

	pthread_join(cons_t, NULL);
	pthread_join(prod_t, NULL);
	rb_stop(rb);
	pthread_join(print_t, NULL);
	rb_destroy(rb);
}

/* ==========================================================================
 *                               ░█▄█░█▀█░▀█▀░█▀█
 *                               ░█░█░█▀█░░█░░█░█
 *                               ░▀░▀░▀░▀░▀▀▀░▀░▀
 * ========================================================================== */
int main(void)
{
	run(100, 500);
	run(500, 100);
	run(0, 0);

	return 0;
}