3 /* remove the next line to enable asserts */
18 #define NCYCLES 1000000
20 #define SORTER_MOVE_TRIES 100
21 #define FREE_PER_ITEM 50
24 typedef enum {A
=0, EMPTY
, SORTER
} item_t
;
31 pthread_mutex_t
**lock
;
34 typedef struct board_s board_t
;
40 typedef struct sorter_s sorter_t
;
42 void count_item (board_t
*, item_t
*, unsigned int *);
43 void drop_item (unsigned int, unsigned int, board_t
*, item_t
*);
44 void erase_board (board_t
*);
45 void fetch_item(unsigned int, unsigned int, item_t
*);
46 void finish_board(board_t
*);
47 void move_sorter (board_t
*, sorter_t
*);
48 void peek_item (unsigned int, unsigned int, board_t
*, item_t
*);
49 void pick_item (unsigned int, unsigned int, board_t
*, item_t
*);
50 void poke_item (unsigned int, unsigned int, board_t
*, item_t
);
51 void print_board (board_t
*);
52 void print_item (unsigned int, unsigned int, item_t
);
53 void *launch_sorter (void *);
54 void setup_board(board_t
*);
56 /* Erase the output medium. */
57 void erase_board (board_t
*board
)
61 conglMoveCursor(1, 1);
64 void move_sorter(board_t
*board
, sorter_t
*sorter
)
66 /* sorter current position was locked before calling this function */
68 assert(sorter
!= NULL
);
69 assert(sorter
->column
< WIDTH
);
70 assert(sorter
->line
< HEIGHT
);
73 unsigned int newline
, newcolumn
;
75 item_t item
, sorter_item
;
79 /* Select random displacement. */
83 newline
= (sorter
->line
+ (h
+HEIGHT
)) % HEIGHT
;
84 newcolumn
= (sorter
->column
+ (v
+WIDTH
)) % WIDTH
;
86 /* lock position to where the sorter may move */
87 pthread_mutex_lock((board
->lock
)[newline
]+newcolumn
);
88 peek_item(newline
, newcolumn
, board
, &item
);
90 /* unlock if position if not available */
91 pthread_mutex_unlock((board
->lock
)[newline
]+newcolumn
);
94 } while ((item
!= EMPTY
) && (ncycles
< SORTER_MOVE_TRIES
));
96 /* If sorter failed to move, respawn in random position */
97 while (item
!= EMPTY
) {
98 newline
= random()%HEIGHT
;
99 newcolumn
= random()%WIDTH
;
101 /* lock position to where the sorter may move */
102 pthread_mutex_lock((board
->lock
)[newline
]+newcolumn
);
103 peek_item(newline
, newcolumn
, board
, &item
);
105 /* unlock if position if not available */
106 pthread_mutex_unlock((board
->lock
)[newline
]+newcolumn
);
112 /* Don't reset the item if sorter just droped one. */
113 /* If an item was droped, it will be different from SORTER. */
114 peek_item(sorter
->line
, sorter
->column
, board
, &sorter_item
);
115 if (sorter_item
== SORTER
) {
116 poke_item (sorter
->line
, sorter
->column
, board
, EMPTY
);
118 poke_item(newline
, newcolumn
, board
, SORTER
);
119 sorter
->line
= newline
;
120 sorter
->column
= newcolumn
;
122 pthread_mutex_unlock((board
->lock
)[newline
]+newcolumn
);
124 assert(sorter
->column
< WIDTH
);
125 assert(sorter
->line
< HEIGHT
);
128 void *launch_sorter (void *board
)
136 /* Place sorter in some free position. */
138 line
= random()%HEIGHT
;
139 column
= random()%WIDTH
;
140 peek_item(line
, column
, board
, &item
);
141 } while (item
!= EMPTY
);
142 poke_item(line
, column
, board
, SORTER
);
144 sorter
.column
= column
;
146 for (itera
= 0; itera
<= NCYCLES
; ++itera
) {
147 //for (itera = 0; 0==0; ++itera) {
149 move_sorter(board
, &sorter
);
150 pick_item(sorter
.line
, sorter
.column
, board
, &item
);
153 } while (item
== EMPTY
);
155 /* because the sorter allways moves, if it drops the item it will not
156 * remain on the top of it */
157 while (item
!= EMPTY
) {
159 column
= sorter
.column
;
161 // Because the sorter may drop an item, it's current position must
162 // be locked until after it moves.
163 pthread_mutex_lock((((board_t
*)board
)->lock
)[line
]+column
);
164 drop_item(sorter
.line
, sorter
.column
, board
, &item
);
165 move_sorter(board
, &sorter
);
166 pthread_mutex_unlock((((board_t
*)board
)->lock
)[line
]+column
);
171 /* leave the premisses */
172 //for (r = 0; (r < 10); ++r) {
173 // move(board, sorter);
180 /* Try to pick an item. Don't pick other sorters :) */
181 void pick_item (unsigned int line
, unsigned int column
, board_t
*board
, item_t
*item
)
183 assert (board
!= NULL
);
184 assert (board
->size_valid
);
185 assert (board
->surface_valid
);
186 assert (line
< board
->height
);
187 assert (column
< board
->width
);
188 assert (item
!= NULL
);
189 assert (*item
== EMPTY
);
191 int newline
, newcolumn
;
193 int h
, v
; /* h: horizontal; v: vertical */
195 for (h
= -1; (h
<= 1) && (*item
== EMPTY
); ++h
) {
196 for (v
= -1; (v
<= 1) && (*item
== EMPTY
); ++v
) {
197 /* newline and newcolumn need to be allawys positive */
198 newline
= (line
+(h
+HEIGHT
))%HEIGHT
;
199 newcolumn
= (column
+(v
+WIDTH
))%WIDTH
;
201 pthread_mutex_lock((board
->lock
)[newline
]+newcolumn
);
202 peek_item(newline
, newcolumn
, board
, &newitem
);
203 if((newitem
!= EMPTY
) && (newitem
!= SORTER
)) {
205 poke_item(newline
, newcolumn
, board
, EMPTY
);
206 /* we have the item */
208 pthread_mutex_unlock((board
->lock
)[newline
]+newcolumn
);
212 /* we either pick an item or leave it as it is */
213 assert ((*item
== newitem
) || (*item
== EMPTY
));
214 assert (*item
!= SORTER
);
217 /* Try to drop one item. The item needs to be droped in a position adjacent to
220 void drop_item (unsigned int line
, unsigned int column
, board_t
*board
, item_t
*item
)
222 assert (board
!= NULL
);
223 assert (board
->size_valid
);
224 assert (board
->surface_valid
);
225 assert (line
< board
->height
);
226 assert (column
< board
->width
);
227 assert (item
!= NULL
);
228 assert (*item
!= EMPTY
);
229 assert (*item
!= SORTER
);
231 int newline
, newcolumn
;
232 int h
, v
; /* h: horizontal, v: vertical */
235 for (h
= -1; (h
<= 1) && (*item
!= EMPTY
); ++h
) {
236 for (v
= -1; (v
<= 1) && (*item
!= EMPTY
); ++v
) {
237 /* newline and newcolumn need to be allawys positive */
238 newline
= (line
+h
+HEIGHT
)%HEIGHT
;
239 newcolumn
= (column
+v
+WIDTH
)%WIDTH
;
241 pthread_mutex_lock((board
->lock
)[newline
]+newcolumn
);
242 peek_item(newline
, newcolumn
, board
, &newitem
);
243 if(newitem
== *item
) {
244 poke_item(line
, column
, board
, newitem
);
246 /* and the item is down */
248 pthread_mutex_unlock((board
->lock
)[newline
]+newcolumn
);
253 /* Print the base of the board, without any items. */
254 void print_board(board_t
*board
)
256 assert(board
->size_valid
);
257 assert(board
->surface_valid
);
260 conglMoveCursor(1, 1);
263 void print_item(unsigned int line
, unsigned int column
, item_t item
)
265 struct properties_s
{
270 typedef struct properties_s properties_t
;
272 properties_t properties
[] = {
273 {congl_magenta
, congl_white
, ' '},
274 {congl_green
, congl_white
, ' '},
275 {congl_black
, congl_white
, ' '}
276 //{'i'}, {'f'}, {'s'}
281 p
= properties
[item
];
282 conglDrawCharFull(line
+1, column
+1, p
.symbol
, p
.fg
, p
.bg
);
284 //printf("%c: %d,%d\n", p.symbol, column, line);
287 /* Read an item from the board. */
288 void peek_item (unsigned int line
, unsigned int column
, board_t
*board
, item_t
*item
)
290 assert(board
!= NULL
);
291 assert(board
->size_valid
);
292 assert(board
->surface_valid
);
293 assert(line
< board
->height
);
294 assert(column
< board
->width
);
295 assert(item
!= NULL
);
297 *item
= board
->surface
[line
][column
];
299 assert(*item
== board
->surface
[line
][column
]);
302 /* Write an item in position (column,line) of the board. At the same time update
305 void poke_item (unsigned int line
, unsigned int column
, board_t
*board
, item_t item
)
307 assert(board
!= NULL
);
308 assert(board
->size_valid
);
309 assert(board
->surface_valid
);
310 assert(line
< board
->height
);
311 assert(column
< board
->width
);
313 board
->surface
[line
][column
] = item
;
315 print_item (line
, column
, item
);
317 assert(board
->surface
[line
][column
] == item
);
320 /* Fetch an item from somewhere. The purpose of this functin is to allow the
321 * user to load one pattern every time the program runs. For now all items are
324 void fetch_item(unsigned int line
, unsigned int column
, item_t
*item
)
326 assert(item
!= NULL
);
330 /* generate one block in every FREE_PER_ITEM board cells */
331 number
= random()%FREE_PER_ITEM
;
341 void setup_board(board_t
*board
)
343 assert (board
->size_valid
);
344 assert (!board
->surface_valid
);
346 unsigned int column
, line
;
349 //printf("Preparing board.\n");
351 /* allocate space for the board */
352 board
->surface
= (item_t
**)calloc (board
->height
, sizeof(board
->surface
));
353 board
->lock
= (pthread_mutex_t
**)calloc (board
->height
, sizeof(board
->lock
));
354 if (board
->surface
== NULL
) {
355 perror("board_setup");
358 if (board
->lock
== NULL
) {
359 perror("board_setup");
362 for (line
= 0; line
< board
->height
; ++line
) {
363 board
->surface
[line
] = (item_t
*)calloc (board
->width
, sizeof(item_t
));
364 board
->lock
[line
] = (pthread_mutex_t
*)calloc (board
->width
, sizeof(pthread_mutex_t
));
365 if (board
->surface
[line
] == NULL
) {
366 perror("board_setup");
369 if (board
->lock
[line
] == NULL
) {
370 perror("board_setup");
374 board
->surface_valid
= true;
376 //printf("Printing board.\n");
378 //printf("Board printed.\n");
380 /* fill the board with initial values */
381 for (line
= 0; line
< board
->height
; ++line
) {
382 for (column
= 0; column
< board
->width
; ++column
) {
383 fetch_item(line
, column
, &item
);
384 poke_item(line
, column
, board
, item
);
385 pthread_mutex_init((board
->lock
)[line
]+column
, NULL
);
389 assert (board
->surface_valid
);
392 void finish_board (board_t
*board
)
394 assert (board
->surface_valid
);
398 for (line
= 0; line
< board
->height
; ++line
) {
399 free (board
->surface
[line
]);
400 free (board
->lock
[line
]);
402 free (board
->surface
);
404 board
->surface_valid
= false;
408 assert (!board
->surface_valid
);
411 void count_items(board_t
*board
, item_t
*item
, unsigned int *nitems
)
413 assert (board
!= NULL
);
414 assert (item
!= NULL
);
415 assert (nitems
!= NULL
);
417 unsigned int line
, column
;
422 for (line
= 0; line
< board
->height
; ++line
) {
423 for (column
= 0; column
< board
->width
; ++column
) {
424 peek_item(line
, column
, board
, &this_item
);
425 if (this_item
== *item
) count
++;
432 int main (int argc
, char *argv
[])
435 unsigned int nitems_begin
, nitems_finish
;
436 pthread_attr_t thread_attributes
;
437 pthread_t sorter
[NSORTERS
];
442 board
.height
= HEIGHT
;
443 board
.size_valid
= true;
444 board
.surface_valid
= false;
446 /* intialize random number generator */
447 srandom((unsigned int)time(NULL
));
449 setup_board (&board
);
453 count_items(&board
, &item
, &nitems_begin
);
456 pthread_attr_init(&thread_attributes
);
457 for (n
= 0; n
< NSORTERS
; ++n
) {
458 pthread_create(sorter
+n
, &thread_attributes
, launch_sorter
, (void *)(&board
));
461 /* wait for keypress (return) */
465 count_items(&board
, &item
, &nitems_finish
);
467 /* make shure the sorters didn't ate items (they can have one with
469 assert((nitems_begin
- nitems_finish
) <= NSORTERS
);
471 for (n
= 0; n
< NSORTERS
; ++n
) {
472 pthread_cancel(sorter
[n
]);
475 finish_board (&board
);
477 printf("All good.\n");