rain.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /****************************************************************************
  2. * Copyright (c) 1998-2006,2008 Free Software Foundation, Inc. *
  3. * *
  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: *
  11. * *
  12. * The above copyright notice and this permission notice shall be included *
  13. * in all copies or substantial portions of the Software. *
  14. * *
  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. *
  22. * *
  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 *
  26. * authorization. *
  27. ****************************************************************************/
  28. /*
  29. * $Id: rain.c,v 1.34 2008/05/24 23:34:34 tom Exp $
  30. */
  31. #include <test.priv.h>
  32. /* rain 11/3/1980 EPS/CITHEP */
  33. #ifdef USE_PTHREADS
  34. #include <pthread.h>
  35. #endif
  36. WANT_USE_WINDOW();
  37. #define MAX_THREADS 10
  38. #define MAX_DROP 5
  39. struct DATA;
  40. typedef void (*DrawPart) (struct DATA *);
  41. typedef struct DATA {
  42. int y, x;
  43. #ifdef USE_PTHREADS
  44. DrawPart func;
  45. int state;
  46. #endif
  47. } DATA;
  48. #ifdef USE_PTHREADS
  49. pthread_cond_t cond_next_drop;
  50. pthread_mutex_t mutex_next_drop;
  51. static int used_threads;
  52. typedef struct {
  53. pthread_t myself;
  54. long counter;
  55. } STATS;
  56. static STATS drop_threads[MAX_THREADS];
  57. #endif
  58. static void
  59. onsig(int n GCC_UNUSED)
  60. {
  61. curs_set(1);
  62. endwin();
  63. ExitProgram(EXIT_FAILURE);
  64. }
  65. static float
  66. ranf(void)
  67. {
  68. long r = (rand() & 077777);
  69. return ((float) r / 32768.);
  70. }
  71. static int
  72. random_x(void)
  73. {
  74. return (((float) (COLS - 4) * ranf()) + 2);
  75. }
  76. static int
  77. random_y(void)
  78. {
  79. return (((float) (LINES - 4) * ranf()) + 2);
  80. }
  81. static int
  82. next_j(int j)
  83. {
  84. if (j == 0)
  85. j = MAX_DROP - 1;
  86. else
  87. --j;
  88. if (has_colors()) {
  89. int z = (int) (3 * ranf());
  90. chtype color = COLOR_PAIR(z);
  91. if (z)
  92. color |= A_BOLD;
  93. attrset(color);
  94. }
  95. return j;
  96. }
  97. static void
  98. part1(DATA * drop)
  99. {
  100. mvaddch(drop->y, drop->x, '.');
  101. }
  102. static void
  103. part2(DATA * drop)
  104. {
  105. mvaddch(drop->y, drop->x, 'o');
  106. }
  107. static void
  108. part3(DATA * drop)
  109. {
  110. mvaddch(drop->y, drop->x, 'O');
  111. }
  112. static void
  113. part4(DATA * drop)
  114. {
  115. mvaddch(drop->y - 1, drop->x, '-');
  116. mvaddstr(drop->y, drop->x - 1, "|.|");
  117. mvaddch(drop->y + 1, drop->x, '-');
  118. }
  119. static void
  120. part5(DATA * drop)
  121. {
  122. mvaddch(drop->y - 2, drop->x, '-');
  123. mvaddstr(drop->y - 1, drop->x - 1, "/ \\");
  124. mvaddstr(drop->y, drop->x - 2, "| O |");
  125. mvaddstr(drop->y + 1, drop->x - 1, "\\ /");
  126. mvaddch(drop->y + 2, drop->x, '-');
  127. }
  128. static void
  129. part6(DATA * drop)
  130. {
  131. mvaddch(drop->y - 2, drop->x, ' ');
  132. mvaddstr(drop->y - 1, drop->x - 1, " ");
  133. mvaddstr(drop->y, drop->x - 2, " ");
  134. mvaddstr(drop->y + 1, drop->x - 1, " ");
  135. mvaddch(drop->y + 2, drop->x, ' ');
  136. }
  137. #ifdef USE_PTHREADS
  138. static void
  139. napsome(void)
  140. {
  141. napms(60);
  142. }
  143. /*
  144. * This runs inside the use_window() mutex.
  145. */
  146. static int
  147. really_draw(WINDOW *win, void *arg)
  148. {
  149. DATA *data = (DATA *) arg;
  150. (void) win;
  151. next_j(data->state);
  152. data->func(data);
  153. refresh();
  154. return OK;
  155. }
  156. static void
  157. draw_part(void (*func) (DATA *), int state, DATA * data)
  158. {
  159. data->func = func;
  160. data->state = state;
  161. use_window(stdscr, really_draw, (void *) data);
  162. napsome();
  163. }
  164. /*
  165. * Tell the threads that one of them can start work on a new raindrop.
  166. * They may all be busy if we're sending requests too rapidly.
  167. */
  168. static int
  169. put_next_drop(void)
  170. {
  171. pthread_cond_signal(&cond_next_drop);
  172. pthread_mutex_unlock(&mutex_next_drop);
  173. return 0;
  174. }
  175. /*
  176. * Wait until we're assigned the task of drawing a new raindrop.
  177. */
  178. static int
  179. get_next_drop(void)
  180. {
  181. pthread_mutex_lock(&mutex_next_drop);
  182. pthread_cond_wait(&cond_next_drop, &mutex_next_drop);
  183. return TRUE;
  184. }
  185. static void *
  186. draw_drop(void *arg)
  187. {
  188. DATA mydata;
  189. int mystats;
  190. /*
  191. * Find myself in the list of threads so we can count the number of loops.
  192. */
  193. for (mystats = 0; mystats < MAX_THREADS; ++mystats) {
  194. if (drop_threads[mystats].myself == pthread_self())
  195. break;
  196. }
  197. do {
  198. if (mystats < MAX_THREADS)
  199. drop_threads[mystats].counter++;
  200. /*
  201. * Make a copy of caller's data. We're cheating for the cases after
  202. * the first loop since we still have a pointer into the main thread
  203. * to the data which it uses for setting up this thread (but it has
  204. * been modified to use different coordinates).
  205. */
  206. mydata = *(DATA *) arg;
  207. draw_part(part1, 0, &mydata);
  208. draw_part(part2, 1, &mydata);
  209. draw_part(part3, 2, &mydata);
  210. draw_part(part4, 3, &mydata);
  211. draw_part(part5, 4, &mydata);
  212. draw_part(part6, 0, &mydata);
  213. } while (get_next_drop());
  214. return NULL;
  215. }
  216. /*
  217. * The description of pthread_create() is misleading, since it implies that
  218. * threads will exit cleanly after their function returns.
  219. *
  220. * Since they do not (and the number of threads is limited by system
  221. * resources), make a limited number of threads, and signal any that are
  222. * waiting when we want a thread past that limit.
  223. */
  224. static int
  225. start_drop(DATA * data)
  226. {
  227. int rc;
  228. if (!used_threads) {
  229. /* mutex and condition for signalling thread */
  230. pthread_mutex_init(&mutex_next_drop, NULL);
  231. pthread_cond_init(&cond_next_drop, NULL);
  232. }
  233. if (used_threads < MAX_THREADS) {
  234. rc = pthread_create(&(drop_threads[used_threads].myself),
  235. NULL,
  236. draw_drop,
  237. data);
  238. ++used_threads;
  239. } else {
  240. rc = put_next_drop();
  241. }
  242. return rc;
  243. }
  244. #endif
  245. static int
  246. get_input(void)
  247. {
  248. return USING_WINDOW(stdscr, wgetch);
  249. }
  250. int
  251. main(int argc GCC_UNUSED,
  252. char *argv[]GCC_UNUSED)
  253. {
  254. bool done = FALSE;
  255. DATA drop;
  256. #ifndef USE_PTHREADS
  257. DATA last[MAX_DROP];
  258. #endif
  259. int j = 0;
  260. setlocale(LC_ALL, "");
  261. CATCHALL(onsig);
  262. initscr();
  263. if (has_colors()) {
  264. int bg = COLOR_BLACK;
  265. start_color();
  266. #if HAVE_USE_DEFAULT_COLORS
  267. if (use_default_colors() == OK)
  268. bg = -1;
  269. #endif
  270. init_pair(1, COLOR_BLUE, bg);
  271. init_pair(2, COLOR_CYAN, bg);
  272. }
  273. nl();
  274. noecho();
  275. curs_set(0);
  276. timeout(0);
  277. #ifndef USE_PTHREADS
  278. for (j = MAX_DROP; --j >= 0;) {
  279. last[j].x = random_x();
  280. last[j].y = random_y();
  281. }
  282. j = 0;
  283. #endif
  284. while (!done) {
  285. drop.x = random_x();
  286. drop.y = random_y();
  287. #ifdef USE_PTHREADS
  288. if (start_drop(&drop) != 0) {
  289. beep();
  290. }
  291. #else
  292. /*
  293. * The non-threaded code draws parts of each drop on each loop.
  294. */
  295. part1(&drop);
  296. part2(&last[j]);
  297. j = next_j(j);
  298. part3(&last[j]);
  299. j = next_j(j);
  300. part4(&last[j]);
  301. j = next_j(j);
  302. part5(&last[j]);
  303. j = next_j(j);
  304. part6(&last[j]);
  305. last[j] = drop;
  306. #endif
  307. switch (get_input()) {
  308. case ('q'):
  309. case ('Q'):
  310. done = TRUE;
  311. break;
  312. case 's':
  313. nodelay(stdscr, FALSE);
  314. break;
  315. case ' ':
  316. nodelay(stdscr, TRUE);
  317. break;
  318. #ifdef KEY_RESIZE
  319. case (KEY_RESIZE):
  320. break;
  321. #endif
  322. }
  323. napms(50);
  324. }
  325. curs_set(1);
  326. endwin();
  327. #ifdef USE_PTHREADS
  328. printf("Counts per thread:\n");
  329. for (j = 0; j < MAX_THREADS; ++j)
  330. printf(" %d:%ld\n", j, drop_threads[j].counter);
  331. #endif
  332. ExitProgram(EXIT_SUCCESS);
  333. }