1 /****************************************************************************
2 * Copyright (c) 1998-2011,2012 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.40 2012/01/21 23:54:47 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 chtype color = (chtype) COLOR_PAIR(z);
108 (void) attrset(color);
116 MvAddCh(drop->y, drop->x, '.');
122 MvAddCh(drop->y, drop->x, 'o');
128 MvAddCh(drop->y, drop->x, 'O');
134 MvAddCh(drop->y - 1, drop->x, '-');
135 MvAddStr(drop->y, drop->x - 1, "|.|");
136 MvAddCh(drop->y + 1, drop->x, '-');
142 MvAddCh(drop->y - 2, drop->x, '-');
143 MvAddStr(drop->y - 1, drop->x - 1, "/ \\");
144 MvAddStr(drop->y, drop->x - 2, "| O |");
145 MvAddStr(drop->y + 1, drop->x - 1, "\\ /");
146 MvAddCh(drop->y + 2, drop->x, '-');
152 MvAddCh(drop->y - 2, drop->x, ' ');
153 MvAddStr(drop->y - 1, drop->x - 1, " ");
154 MvAddStr(drop->y, drop->x - 2, " ");
155 MvAddStr(drop->y + 1, drop->x - 1, " ");
156 MvAddCh(drop->y + 2, drop->x, ' ');
167 * This runs inside the use_window() mutex.
170 really_draw(WINDOW *win, void *arg)
172 DATA *data = (DATA *) arg;
182 draw_part(void (*func) (DATA *), int state, DATA * data)
186 use_window(stdscr, really_draw, (void *) data);
191 * Tell the threads that one of them can start work on a new raindrop.
192 * They may all be busy if we're sending requests too rapidly.
197 pthread_cond_signal(&cond_next_drop);
198 pthread_mutex_unlock(&mutex_next_drop);
204 * Wait until we're assigned the task of drawing a new raindrop.
209 pthread_mutex_lock(&mutex_next_drop);
210 pthread_cond_wait(&cond_next_drop, &mutex_next_drop);
222 * Find myself in the list of threads so we can count the number of loops.
224 for (mystats = 0; mystats < MAX_THREADS; ++mystats) {
225 #if defined(__MINGW32__) && !defined(__WINPTHREADS_VERSION)
226 if (drop_threads[mystats].myself.p == pthread_self().p)
228 if (drop_threads[mystats].myself == pthread_self())
234 if (mystats < MAX_THREADS)
235 drop_threads[mystats].counter++;
238 * Make a copy of caller's data. We're cheating for the cases after
239 * the first loop since we still have a pointer into the main thread
240 * to the data which it uses for setting up this thread (but it has
241 * been modified to use different coordinates).
243 mydata = *(DATA *) arg;
245 draw_part(part1, 0, &mydata);
246 draw_part(part2, 1, &mydata);
247 draw_part(part3, 2, &mydata);
248 draw_part(part4, 3, &mydata);
249 draw_part(part5, 4, &mydata);
250 draw_part(part6, 0, &mydata);
251 } while (get_next_drop());
257 * The description of pthread_create() is misleading, since it implies that
258 * threads will exit cleanly after their function returns.
260 * Since they do not (and the number of threads is limited by system
261 * resources), make a limited number of threads, and signal any that are
262 * waiting when we want a thread past that limit.
265 start_drop(DATA * data)
270 /* mutex and condition for signalling thread */
271 pthread_mutex_init(&mutex_next_drop, NULL);
272 pthread_cond_init(&cond_next_drop, NULL);
275 if (used_threads < MAX_THREADS) {
276 rc = pthread_create(&(drop_threads[used_threads].myself),
282 rc = put_next_drop();
291 return USING_WINDOW(stdscr, wgetch);
295 main(int argc GCC_UNUSED,
296 char *argv[]GCC_UNUSED)
305 setlocale(LC_ALL, "");
311 int bg = COLOR_BLACK;
313 #if HAVE_USE_DEFAULT_COLORS
314 if (use_default_colors() == OK)
317 init_pair(1, COLOR_BLUE, (short) bg);
318 init_pair(2, COLOR_CYAN, (short) bg);
326 for (j = MAX_DROP; --j >= 0;) {
327 last[j].x = random_x();
328 last[j].y = random_y();
338 if (start_drop(&drop) != 0) {
343 * The non-threaded code draws parts of each drop on each loop.
364 switch (get_input()) {
370 nodelay(stdscr, FALSE);
373 nodelay(stdscr, TRUE);
385 printf("Counts per thread:\n");
386 for (j = 0; j < MAX_THREADS; ++j)
387 printf(" %d:%ld\n", j, drop_threads[j].counter);
389 ExitProgram(EXIT_SUCCESS);