1 /****************************************************************************
2 * Copyright (c) 1998-2014,2017 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.43 2017/04/30 01:08:14 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];
71 onsig(int n GCC_UNUSED)
75 ExitProgram(EXIT_FAILURE);
81 long r = (rand() & 077777);
82 return ((double) r / 32768.);
88 return (int) (((double) (COLS - 4) * ranf()) + 2);
94 return (int) (((double) (LINES - 4) * ranf()) + 2);
105 int z = (int) (3 * ranf());
106 (void) attrset(AttrArg(COLOR_PAIR(z), (z ? A_BOLD : A_NORMAL)));
114 MvAddCh(drop->y, drop->x, '.');
120 MvAddCh(drop->y, drop->x, 'o');
126 MvAddCh(drop->y, drop->x, 'O');
132 MvAddCh(drop->y - 1, drop->x, '-');
133 MvAddStr(drop->y, drop->x - 1, "|.|");
134 MvAddCh(drop->y + 1, drop->x, '-');
140 MvAddCh(drop->y - 2, drop->x, '-');
141 MvAddStr(drop->y - 1, drop->x - 1, "/ \\");
142 MvAddStr(drop->y, drop->x - 2, "| O |");
143 MvAddStr(drop->y + 1, drop->x - 1, "\\ /");
144 MvAddCh(drop->y + 2, drop->x, '-');
150 MvAddCh(drop->y - 2, drop->x, ' ');
151 MvAddStr(drop->y - 1, drop->x - 1, " ");
152 MvAddStr(drop->y, drop->x - 2, " ");
153 MvAddStr(drop->y + 1, drop->x - 1, " ");
154 MvAddCh(drop->y + 2, drop->x, ' ');
165 * This runs inside the use_window() mutex.
168 really_draw(WINDOW *win, void *arg)
170 DATA *data = (DATA *) arg;
180 draw_part(void (*func) (DATA *), int state, DATA * data)
184 use_window(stdscr, really_draw, (void *) data);
189 * Tell the threads that one of them can start work on a new raindrop.
190 * They may all be busy if we're sending requests too rapidly.
195 pthread_cond_signal(&cond_next_drop);
196 pthread_mutex_unlock(&mutex_next_drop);
202 * Wait until we're assigned the task of drawing a new raindrop.
207 pthread_mutex_lock(&mutex_next_drop);
208 pthread_cond_wait(&cond_next_drop, &mutex_next_drop);
220 * Find myself in the list of threads so we can count the number of loops.
222 for (mystats = 0; mystats < MAX_THREADS; ++mystats) {
223 #if defined(__MINGW32__) && !defined(__WINPTHREADS_VERSION)
224 if (drop_threads[mystats].myself.p == pthread_self().p)
226 if (drop_threads[mystats].myself == pthread_self())
232 if (mystats < MAX_THREADS)
233 drop_threads[mystats].counter++;
236 * Make a copy of caller's data. We're cheating for the cases after
237 * the first loop since we still have a pointer into the main thread
238 * to the data which it uses for setting up this thread (but it has
239 * been modified to use different coordinates).
241 mydata = *(DATA *) arg;
243 draw_part(part1, 0, &mydata);
244 draw_part(part2, 1, &mydata);
245 draw_part(part3, 2, &mydata);
246 draw_part(part4, 3, &mydata);
247 draw_part(part5, 4, &mydata);
248 draw_part(part6, 0, &mydata);
249 } while (get_next_drop());
255 * The description of pthread_create() is misleading, since it implies that
256 * threads will exit cleanly after their function returns.
258 * Since they do not (and the number of threads is limited by system
259 * resources), make a limited number of threads, and signal any that are
260 * waiting when we want a thread past that limit.
263 start_drop(DATA * data)
268 /* mutex and condition for signalling thread */
269 pthread_mutex_init(&mutex_next_drop, NULL);
270 pthread_cond_init(&cond_next_drop, NULL);
273 if (used_threads < MAX_THREADS) {
274 rc = pthread_create(&(drop_threads[used_threads].myself),
280 rc = put_next_drop();
289 return USING_WINDOW(stdscr, wgetch);
293 main(int argc GCC_UNUSED,
294 char *argv[]GCC_UNUSED)
296 static const char *help[] =
299 " q/Q exit the program",
301 " <space> undo single-step",
313 setlocale(LC_ALL, "");
319 int bg = COLOR_BLACK;
321 #if HAVE_USE_DEFAULT_COLORS
322 if (use_default_colors() == OK)
325 init_pair(1, COLOR_BLUE, (short) bg);
326 init_pair(2, COLOR_CYAN, (short) bg);
334 for (j = MAX_DROP; --j >= 0;) {
335 last[j].x = random_x();
336 last[j].y = random_y();
346 if (start_drop(&drop) != 0) {
351 * The non-threaded code draws parts of each drop on each loop.
372 switch (get_input()) {
378 nodelay(stdscr, FALSE);
381 nodelay(stdscr, TRUE);
388 popup_msg(stdscr, help);
400 printf("Counts per thread:\n");
401 for (j = 0; j < MAX_THREADS; ++j)
402 printf(" %d:%ld\n", j, drop_threads[j].counter);
404 ExitProgram(EXIT_SUCCESS);