1 /****************************************************************************
2 * Copyright (c) 1998-2012,2014 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.41 2014/08/02 17:24:07 tom Exp $
31 #include <test.priv.h>
33 /* rain 11/3/1980 EPS/CITHEP */
41 #define MAX_THREADS 10
46 typedef void (*DrawPart) (struct DATA *);
57 pthread_cond_t cond_next_drop;
58 pthread_mutex_t mutex_next_drop;
59 static int used_threads;
66 static STATS drop_threads[MAX_THREADS];
70 onsig(int n GCC_UNUSED)
74 ExitProgram(EXIT_FAILURE);
80 long r = (rand() & 077777);
81 return ((double) r / 32768.);
87 return (int) (((double) (COLS - 4) * ranf()) + 2);
93 return (int) (((double) (LINES - 4) * ranf()) + 2);
104 int z = (int) (3 * ranf());
105 (void) attrset(AttrArg(COLOR_PAIR(z), (z ? A_BOLD : A_NORMAL)));
113 MvAddCh(drop->y, drop->x, '.');
119 MvAddCh(drop->y, drop->x, 'o');
125 MvAddCh(drop->y, drop->x, 'O');
131 MvAddCh(drop->y - 1, drop->x, '-');
132 MvAddStr(drop->y, drop->x - 1, "|.|");
133 MvAddCh(drop->y + 1, drop->x, '-');
139 MvAddCh(drop->y - 2, drop->x, '-');
140 MvAddStr(drop->y - 1, drop->x - 1, "/ \\");
141 MvAddStr(drop->y, drop->x - 2, "| O |");
142 MvAddStr(drop->y + 1, drop->x - 1, "\\ /");
143 MvAddCh(drop->y + 2, drop->x, '-');
149 MvAddCh(drop->y - 2, drop->x, ' ');
150 MvAddStr(drop->y - 1, drop->x - 1, " ");
151 MvAddStr(drop->y, drop->x - 2, " ");
152 MvAddStr(drop->y + 1, drop->x - 1, " ");
153 MvAddCh(drop->y + 2, drop->x, ' ');
164 * This runs inside the use_window() mutex.
167 really_draw(WINDOW *win, void *arg)
169 DATA *data = (DATA *) arg;
179 draw_part(void (*func) (DATA *), int state, DATA * data)
183 use_window(stdscr, really_draw, (void *) data);
188 * Tell the threads that one of them can start work on a new raindrop.
189 * They may all be busy if we're sending requests too rapidly.
194 pthread_cond_signal(&cond_next_drop);
195 pthread_mutex_unlock(&mutex_next_drop);
201 * Wait until we're assigned the task of drawing a new raindrop.
206 pthread_mutex_lock(&mutex_next_drop);
207 pthread_cond_wait(&cond_next_drop, &mutex_next_drop);
219 * Find myself in the list of threads so we can count the number of loops.
221 for (mystats = 0; mystats < MAX_THREADS; ++mystats) {
222 #if defined(__MINGW32__) && !defined(__WINPTHREADS_VERSION)
223 if (drop_threads[mystats].myself.p == pthread_self().p)
225 if (drop_threads[mystats].myself == pthread_self())
231 if (mystats < MAX_THREADS)
232 drop_threads[mystats].counter++;
235 * Make a copy of caller's data. We're cheating for the cases after
236 * the first loop since we still have a pointer into the main thread
237 * to the data which it uses for setting up this thread (but it has
238 * been modified to use different coordinates).
240 mydata = *(DATA *) arg;
242 draw_part(part1, 0, &mydata);
243 draw_part(part2, 1, &mydata);
244 draw_part(part3, 2, &mydata);
245 draw_part(part4, 3, &mydata);
246 draw_part(part5, 4, &mydata);
247 draw_part(part6, 0, &mydata);
248 } while (get_next_drop());
254 * The description of pthread_create() is misleading, since it implies that
255 * threads will exit cleanly after their function returns.
257 * Since they do not (and the number of threads is limited by system
258 * resources), make a limited number of threads, and signal any that are
259 * waiting when we want a thread past that limit.
262 start_drop(DATA * data)
267 /* mutex and condition for signalling thread */
268 pthread_mutex_init(&mutex_next_drop, NULL);
269 pthread_cond_init(&cond_next_drop, NULL);
272 if (used_threads < MAX_THREADS) {
273 rc = pthread_create(&(drop_threads[used_threads].myself),
279 rc = put_next_drop();
288 return USING_WINDOW(stdscr, wgetch);
292 main(int argc GCC_UNUSED,
293 char *argv[]GCC_UNUSED)
302 setlocale(LC_ALL, "");
308 int bg = COLOR_BLACK;
310 #if HAVE_USE_DEFAULT_COLORS
311 if (use_default_colors() == OK)
314 init_pair(1, COLOR_BLUE, (short) bg);
315 init_pair(2, COLOR_CYAN, (short) bg);
323 for (j = MAX_DROP; --j >= 0;) {
324 last[j].x = random_x();
325 last[j].y = random_y();
335 if (start_drop(&drop) != 0) {
340 * The non-threaded code draws parts of each drop on each loop.
361 switch (get_input()) {
367 nodelay(stdscr, FALSE);
370 nodelay(stdscr, TRUE);
382 printf("Counts per thread:\n");
383 for (j = 0; j < MAX_THREADS; ++j)
384 printf(" %d:%ld\n", j, drop_threads[j].counter);
386 ExitProgram(EXIT_SUCCESS);