1 /****************************************************************************
2 * Copyright (c) 1998-2017,2018 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 * $Id: rain.c,v 1.49 2018/06/23 21:35:06 tom Exp $
31 #include <test.priv.h>
32 #include <popup_msg.h>
34 /* rain 11/3/1980 EPS/CITHEP */
42 #define MAX_THREADS 10
47 typedef void (*DrawPart) (struct DATA *);
58 pthread_cond_t cond_next_drop;
59 pthread_mutex_t mutex_next_drop;
60 static int used_threads;
67 static STATS drop_threads[MAX_THREADS];
72 safe_wgetch(WINDOW *w, void *data GCC_UNUSED)
79 onsig(int n GCC_UNUSED)
82 ExitProgram(EXIT_FAILURE);
88 long r = (rand() & 077777);
89 return ((double) r / 32768.);
95 return (int) (((double) (COLS - 4) * ranf()) + 2);
101 return (int) (((double) (LINES - 4) * ranf()) + 2);
112 int z = (int) (3 * ranf());
113 (void) attrset(AttrArg(COLOR_PAIR(z), (z ? A_BOLD : A_NORMAL)));
121 MvAddCh(drop->y, drop->x, '.');
127 MvAddCh(drop->y, drop->x, 'o');
133 MvAddCh(drop->y, drop->x, 'O');
139 MvAddCh(drop->y - 1, drop->x, '-');
140 MvAddStr(drop->y, drop->x - 1, "|.|");
141 MvAddCh(drop->y + 1, drop->x, '-');
147 MvAddCh(drop->y - 2, drop->x, '-');
148 MvAddStr(drop->y - 1, drop->x - 1, "/ \\");
149 MvAddStr(drop->y, drop->x - 2, "| O |");
150 MvAddStr(drop->y + 1, drop->x - 1, "\\ /");
151 MvAddCh(drop->y + 2, drop->x, '-');
157 MvAddCh(drop->y - 2, drop->x, ' ');
158 MvAddStr(drop->y - 1, drop->x - 1, " ");
159 MvAddStr(drop->y, drop->x - 2, " ");
160 MvAddStr(drop->y + 1, drop->x - 1, " ");
161 MvAddCh(drop->y + 2, drop->x, ' ');
172 * This runs inside the use_window() mutex.
175 really_draw(WINDOW *win, void *arg)
177 DATA *data = (DATA *) arg;
187 draw_part(void (*func) (DATA *), int state, DATA * data)
191 use_window(stdscr, really_draw, (void *) data);
196 * Tell the threads that one of them can start work on a new raindrop.
197 * They may all be busy if we're sending requests too rapidly.
202 pthread_cond_signal(&cond_next_drop);
203 pthread_mutex_unlock(&mutex_next_drop);
209 * Wait until we're assigned the task of drawing a new raindrop.
214 pthread_mutex_lock(&mutex_next_drop);
215 pthread_cond_wait(&cond_next_drop, &mutex_next_drop);
227 * Find myself in the list of threads so we can count the number of loops.
229 for (mystats = 0; mystats < MAX_THREADS; ++mystats) {
230 #if defined(_WIN32) && !defined(__WINPTHREADS_VERSION)
231 if (drop_threads[mystats].myself.p == pthread_self().p)
233 if (drop_threads[mystats].myself == pthread_self())
239 if (mystats < MAX_THREADS)
240 drop_threads[mystats].counter++;
243 * Make a copy of caller's data. We're cheating for the cases after
244 * the first loop since we still have a pointer into the main thread
245 * to the data which it uses for setting up this thread (but it has
246 * been modified to use different coordinates).
248 mydata = *(DATA *) arg;
250 draw_part(part1, 0, &mydata);
251 draw_part(part2, 1, &mydata);
252 draw_part(part3, 2, &mydata);
253 draw_part(part4, 3, &mydata);
254 draw_part(part5, 4, &mydata);
255 draw_part(part6, 0, &mydata);
256 } while (get_next_drop());
262 * The description of pthread_create() is misleading, since it implies that
263 * threads will exit cleanly after their function returns.
265 * Since they do not (and the number of threads is limited by system
266 * resources), make a limited number of threads, and signal any that are
267 * waiting when we want a thread past that limit.
270 start_drop(DATA * data)
275 /* mutex and condition for signalling thread */
276 pthread_mutex_init(&mutex_next_drop, NULL);
277 pthread_cond_init(&cond_next_drop, NULL);
280 if (used_threads < MAX_THREADS) {
281 rc = pthread_create(&(drop_threads[used_threads].myself),
287 rc = put_next_drop();
296 return USING_WINDOW1(stdscr, wgetch, safe_wgetch);
302 static const char *msg[] =
304 "Usage: rain [options]"
307 #if HAVE_USE_DEFAULT_COLORS
308 ," -d invoke use_default_colors"
313 for (n = 0; n < SIZEOF(msg); n++)
314 fprintf(stderr, "%s\n", msg[n]);
316 ExitProgram(EXIT_FAILURE);
320 main(int argc, char *argv[])
322 static const char *help[] =
325 " q/Q exit the program",
327 " <space> undo single-step",
339 #if HAVE_USE_DEFAULT_COLORS
340 bool d_option = FALSE;
343 while ((ch = getopt(argc, argv, "d")) != -1) {
345 #if HAVE_USE_DEFAULT_COLORS
358 setlocale(LC_ALL, "");
360 InitAndCatch(initscr(), onsig);
362 int bg = COLOR_BLACK;
364 #if HAVE_USE_DEFAULT_COLORS
365 if (d_option && (use_default_colors() == OK))
368 init_pair(1, COLOR_BLUE, (short) bg);
369 init_pair(2, COLOR_CYAN, (short) bg);
377 for (j = MAX_DROP; --j >= 0;) {
378 last[j].x = random_x();
379 last[j].y = random_y();
389 if (start_drop(&drop) != 0) {
394 * The non-threaded code draws parts of each drop on each loop.
415 switch (get_input()) {
421 nodelay(stdscr, FALSE);
424 nodelay(stdscr, TRUE);
431 popup_msg(stdscr, help);
442 printf("Counts per thread:\n");
443 for (j = 0; j < MAX_THREADS; ++j)
444 printf(" %d:%ld\n", j, drop_threads[j].counter);
446 ExitProgram(EXIT_SUCCESS);