1 /****************************************************************************
2 * Copyright 2018-2019,2020 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.51 2020/02/02 23:34:34 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_next_drop;
61 static int used_threads;
68 static STATS drop_threads[MAX_THREADS];
73 safe_wgetch(WINDOW *w, void *data GCC_UNUSED)
80 onsig(int n GCC_UNUSED)
83 ExitProgram(EXIT_FAILURE);
89 long r = (rand() & 077777);
90 return ((double) r / 32768.);
96 return (int) (((double) (COLS - 4) * ranf()) + 2);
102 return (int) (((double) (LINES - 4) * ranf()) + 2);
113 int z = (int) (3 * ranf());
114 (void) attrset(AttrArg(COLOR_PAIR(z), (z ? A_BOLD : A_NORMAL)));
122 MvAddCh(drop->y, drop->x, '.');
128 MvAddCh(drop->y, drop->x, 'o');
134 MvAddCh(drop->y, drop->x, 'O');
140 MvAddCh(drop->y - 1, drop->x, '-');
141 MvAddStr(drop->y, drop->x - 1, "|.|");
142 MvAddCh(drop->y + 1, drop->x, '-');
148 MvAddCh(drop->y - 2, drop->x, '-');
149 MvAddStr(drop->y - 1, drop->x - 1, "/ \\");
150 MvAddStr(drop->y, drop->x - 2, "| O |");
151 MvAddStr(drop->y + 1, drop->x - 1, "\\ /");
152 MvAddCh(drop->y + 2, drop->x, '-');
158 MvAddCh(drop->y - 2, drop->x, ' ');
159 MvAddStr(drop->y - 1, drop->x - 1, " ");
160 MvAddStr(drop->y, drop->x - 2, " ");
161 MvAddStr(drop->y + 1, drop->x - 1, " ");
162 MvAddCh(drop->y + 2, drop->x, ' ');
173 * This runs inside the use_window() mutex.
176 really_draw(WINDOW *win, void *arg)
178 DATA *data = (DATA *) arg;
188 draw_part(void (*func) (DATA *), int state, DATA * data)
192 use_window(stdscr, really_draw, (void *) data);
197 * Tell the threads that one of them can start work on a new raindrop.
198 * They may all be busy if we're sending requests too rapidly.
203 pthread_cond_signal(&cond_next_drop);
204 pthread_mutex_unlock(&mutex_next_drop);
210 * Wait until we're assigned the task of drawing a new raindrop.
215 pthread_mutex_lock(&mutex_next_drop);
216 pthread_cond_wait(&cond_next_drop, &mutex_next_drop);
228 * Find myself in the list of threads so we can count the number of loops.
230 for (mystats = 0; mystats < MAX_THREADS; ++mystats) {
231 #if defined(_WIN32) && !defined(__WINPTHREADS_VERSION)
232 if (drop_threads[mystats].myself.p == pthread_self().p)
234 if (drop_threads[mystats].myself == pthread_self())
240 if (mystats < MAX_THREADS)
241 drop_threads[mystats].counter++;
244 * Make a copy of caller's data. We're cheating for the cases after
245 * the first loop since we still have a pointer into the main thread
246 * to the data which it uses for setting up this thread (but it has
247 * been modified to use different coordinates).
249 mydata = *(DATA *) arg;
251 draw_part(part1, 0, &mydata);
252 draw_part(part2, 1, &mydata);
253 draw_part(part3, 2, &mydata);
254 draw_part(part4, 3, &mydata);
255 draw_part(part5, 4, &mydata);
256 draw_part(part6, 0, &mydata);
257 } while (get_next_drop());
263 * The description of pthread_create() is misleading, since it implies that
264 * threads will exit cleanly after their function returns.
266 * Since they do not (and the number of threads is limited by system
267 * resources), make a limited number of threads, and signal any that are
268 * waiting when we want a thread past that limit.
271 start_drop(DATA * data)
276 /* mutex and condition for signalling thread */
277 pthread_mutex_init(&mutex_next_drop, NULL);
278 pthread_cond_init(&cond_next_drop, NULL);
281 if (used_threads < MAX_THREADS) {
282 rc = pthread_create(&(drop_threads[used_threads].myself),
288 rc = put_next_drop();
297 return USING_WINDOW1(stdscr, wgetch, safe_wgetch);
303 static const char *msg[] =
305 "Usage: rain [options]"
308 #if HAVE_USE_DEFAULT_COLORS
309 ," -d invoke use_default_colors"
314 for (n = 0; n < SIZEOF(msg); n++)
315 fprintf(stderr, "%s\n", msg[n]);
317 ExitProgram(EXIT_FAILURE);
321 main(int argc, char *argv[])
323 static const char *help[] =
326 " q/Q exit the program",
328 " <space> undo single-step",
340 #if HAVE_USE_DEFAULT_COLORS
341 bool d_option = FALSE;
344 while ((ch = getopt(argc, argv, "d")) != -1) {
346 #if HAVE_USE_DEFAULT_COLORS
359 setlocale(LC_ALL, "");
361 InitAndCatch(initscr(), onsig);
363 int bg = COLOR_BLACK;
365 #if HAVE_USE_DEFAULT_COLORS
366 if (d_option && (use_default_colors() == OK))
369 init_pair(1, COLOR_BLUE, (short) bg);
370 init_pair(2, COLOR_CYAN, (short) bg);
378 for (j = MAX_DROP; --j >= 0;) {
379 last[j].x = random_x();
380 last[j].y = random_y();
390 if (start_drop(&drop) != 0) {
395 * The non-threaded code draws parts of each drop on each loop.
416 switch (get_input()) {
422 nodelay(stdscr, FALSE);
425 nodelay(stdscr, TRUE);
432 popup_msg(stdscr, help);
443 printf("Counts per thread:\n");
444 for (j = 0; j < MAX_THREADS; ++j)
445 printf(" %d:%ld\n", j, drop_threads[j].counter);
447 ExitProgram(EXIT_SUCCESS);