1 /****************************************************************************
2 * Copyright 2018-2020,2022 Thomas E. Dickey *
3 * Copyright 1998-2014,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 * $Id: rain.c,v 1.57 2022/12/04 00:40:11 tom Exp $
32 #include <test.priv.h>
33 #include <popup_msg.h>
35 /* rain 11/3/1980 EPS/CITHEP */
43 #define MAX_THREADS 10
48 typedef void (*DrawPart) (struct DATA *);
59 pthread_cond_t cond_next_drop;
60 pthread_mutex_t mutex_drop_data;
61 pthread_mutex_t mutex_next_drop;
62 static int used_threads;
69 static STATS drop_threads[MAX_THREADS];
74 safe_wgetch(WINDOW *w, void *data GCC_UNUSED)
81 onsig(int n GCC_UNUSED)
84 ExitProgram(EXIT_FAILURE);
90 long r = (rand() & 077777);
91 return ((double) r / 32768.);
97 return (int) (((double) (COLS - 4) * ranf()) + 2);
103 return (int) (((double) (LINES - 4) * ranf()) + 2);
114 int z = (int) (3 * ranf());
115 (void) attrset(AttrArg(COLOR_PAIR(z), (z ? A_BOLD : A_NORMAL)));
123 MvAddCh(drop->y, drop->x, '.');
129 MvAddCh(drop->y, drop->x, 'o');
135 MvAddCh(drop->y, drop->x, 'O');
141 MvAddCh(drop->y - 1, drop->x, '-');
142 MvAddStr(drop->y, drop->x - 1, "|.|");
143 MvAddCh(drop->y + 1, drop->x, '-');
149 MvAddCh(drop->y - 2, drop->x, '-');
150 MvAddStr(drop->y - 1, drop->x - 1, "/ \\");
151 MvAddStr(drop->y, drop->x - 2, "| O |");
152 MvAddStr(drop->y + 1, drop->x - 1, "\\ /");
153 MvAddCh(drop->y + 2, drop->x, '-');
159 MvAddCh(drop->y - 2, drop->x, ' ');
160 MvAddStr(drop->y - 1, drop->x - 1, " ");
161 MvAddStr(drop->y, drop->x - 2, " ");
162 MvAddStr(drop->y + 1, drop->x - 1, " ");
163 MvAddCh(drop->y + 2, drop->x, ' ');
174 * This runs inside the use_window() mutex.
177 really_draw(WINDOW *win, void *arg)
179 DATA *data = (DATA *) arg;
189 draw_part(void (*func) (DATA *), int state, DATA * data)
193 use_window(stdscr, really_draw, (void *) data);
198 * Tell the threads that one of them can start work on a new raindrop.
199 * They may all be busy if we're sending requests too rapidly.
204 pthread_cond_broadcast(&cond_next_drop);
205 pthread_mutex_unlock(&mutex_next_drop);
211 * Wait until we're assigned the task of drawing a new raindrop.
216 pthread_mutex_lock(&mutex_next_drop);
217 pthread_cond_wait(&cond_next_drop, &mutex_next_drop);
229 * Find myself in the list of threads so we can count the number of loops.
231 for (mystats = 0; mystats < MAX_THREADS; ++mystats) {
232 #if defined(_NC_WINDOWS) && !defined(__WINPTHREADS_VERSION)
233 if (drop_threads[mystats].myself.p == pthread_self().p)
235 if (drop_threads[mystats].myself == pthread_self())
241 if (mystats < MAX_THREADS)
242 drop_threads[mystats].counter++;
245 * Make a copy of caller's data. We're cheating for the cases after
246 * the first loop since we still have a pointer into the main thread
247 * to the data which it uses for setting up this thread (but it has
248 * been modified to use different coordinates).
250 pthread_mutex_lock(&mutex_drop_data);
251 mydata = *(DATA *) arg;
252 pthread_mutex_unlock(&mutex_drop_data);
254 draw_part(part1, 0, &mydata);
255 draw_part(part2, 1, &mydata);
256 draw_part(part3, 2, &mydata);
257 draw_part(part4, 3, &mydata);
258 draw_part(part5, 4, &mydata);
259 draw_part(part6, 0, &mydata);
261 } while (get_next_drop());
267 * The description of pthread_create() is misleading, since it implies that
268 * threads will exit cleanly after their function returns.
270 * Since they do not (and the number of threads is limited by system
271 * resources), make a limited number of threads, and signal any that are
272 * waiting when we want a thread past that limit.
275 start_drop(DATA * data)
280 /* mutex and condition for signalling thread */
281 pthread_mutex_init(&mutex_next_drop, NULL);
282 pthread_cond_init(&cond_next_drop, NULL);
285 if (used_threads < MAX_THREADS) {
286 rc = pthread_create(&(drop_threads[used_threads].myself),
292 rc = put_next_drop();
301 return USING_WINDOW1(stdscr, wgetch, safe_wgetch);
307 static const char *msg[] =
309 "Usage: rain [options]"
313 #if HAVE_USE_DEFAULT_COLORS
314 ," -d invoke use_default_colors"
319 for (n = 0; n < SIZEOF(msg); n++)
320 fprintf(stderr, "%s\n", msg[n]);
322 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
329 main(int argc, char *argv[])
331 static const char *help[] =
334 " q/Q exit the program",
336 " <space> undo single-step",
348 #if HAVE_USE_DEFAULT_COLORS
349 bool d_option = FALSE;
352 while ((ch = getopt(argc, argv, OPTS_COMMON "d")) != -1) {
354 #if HAVE_USE_DEFAULT_COLORS
361 ExitProgram(EXIT_SUCCESS);
363 usage(ch == OPTS_USAGE);
370 setlocale(LC_ALL, "");
372 InitAndCatch(initscr(), onsig);
374 int bg = COLOR_BLACK;
376 #if HAVE_USE_DEFAULT_COLORS
377 if (d_option && (use_default_colors() == OK))
380 init_pair(1, COLOR_BLUE, (short) bg);
381 init_pair(2, COLOR_CYAN, (short) bg);
389 pthread_mutex_init(&mutex_drop_data, NULL);
390 #else /* !USE_PTHREADS */
391 for (j = MAX_DROP; --j >= 0;) {
392 last[j].x = random_x();
393 last[j].y = random_y();
400 pthread_mutex_lock(&mutex_drop_data);
405 if (start_drop(&drop) != 0) {
409 pthread_mutex_unlock(&mutex_drop_data);
415 * The non-threaded code draws parts of each drop on each loop.
436 switch (get_input()) {
442 nodelay(stdscr, FALSE);
445 nodelay(stdscr, TRUE);
452 popup_msg(stdscr, help);
463 printf("Counts per thread:\n");
464 for (j = 0; j < MAX_THREADS; ++j)
465 printf(" %d:%ld\n", j, drop_threads[j].counter);
467 ExitProgram(EXIT_SUCCESS);