Mercurial > dwindows
comparison dwtest.c @ 1200:88b3f27542b0
Added thread/event tab to the test program.
Wrote it to demonstrate the thread/event functionality and decided it would be useful to include in the test program as an example and to make sure things function as expected.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Fri, 30 Sep 2011 03:19:16 +0000 |
parents | 76262040ed5f |
children | 196cd8a8e6a8 |
comparison
equal
deleted
inserted
replaced
1199:af4ca6ccbdff | 1200:88b3f27542b0 |
---|---|
80 notebookbox4, | 80 notebookbox4, |
81 notebookbox5, | 81 notebookbox5, |
82 notebookbox6, | 82 notebookbox6, |
83 notebookbox7, | 83 notebookbox7, |
84 notebookbox8, | 84 notebookbox8, |
85 notebookbox9, | |
85 html, | 86 html, |
86 rawhtml, | 87 rawhtml, |
87 notebook, | 88 notebook, |
88 vscrollbar, | 89 vscrollbar, |
89 hscrollbar, | 90 hscrollbar, |
1324 entryarray[i] = dw_entryfield_new( buf , i ); | 1325 entryarray[i] = dw_entryfield_new( buf , i ); |
1325 dw_box_pack_start( tmpbox, entryarray[i], 0, 20, TRUE, FALSE, 0); | 1326 dw_box_pack_start( tmpbox, entryarray[i], 0, 20, TRUE, FALSE, 0); |
1326 } | 1327 } |
1327 } | 1328 } |
1328 | 1329 |
1330 /* Section for thread/event test */ | |
1331 HWND threadmle, startbutton; | |
1332 HMTX mutex; | |
1333 HEV workevent, controlevent; | |
1334 int finished = FALSE; | |
1335 int ready = 0; | |
1336 #define BUF_SIZE 1024 | |
1337 void DWSIGNAL run_thread(void *data); | |
1338 void DWSIGNAL control_thread(void *data); | |
1339 | |
1340 void update_mle(char *text, int lock) | |
1341 { | |
1342 static unsigned int pos = 0; | |
1343 | |
1344 /* Protect pos from being changed by different threads */ | |
1345 if(lock) | |
1346 dw_mutex_lock(mutex); | |
1347 pos = dw_mle_import(threadmle, text, pos); | |
1348 dw_mle_set_cursor(threadmle, pos); | |
1349 if(lock) | |
1350 dw_mutex_unlock(mutex); | |
1351 } | |
1352 | |
1353 int DWSIGNAL start_threads_button_callback(HWND window, void *data) | |
1354 { | |
1355 dw_window_disable(startbutton); | |
1356 dw_mutex_lock(mutex); | |
1357 controlevent = dw_event_new(); | |
1358 dw_event_reset(workevent); | |
1359 finished = FALSE; | |
1360 ready = 0; | |
1361 update_mle("Starting thread 1\r\n", FALSE); | |
1362 dw_thread_new(DW_SIGNAL_FUNC(run_thread), DW_INT_TO_POINTER(1), 10000); | |
1363 update_mle("Starting thread 2\r\n", FALSE); | |
1364 dw_thread_new(DW_SIGNAL_FUNC(run_thread), DW_INT_TO_POINTER(2), 10000); | |
1365 update_mle("Starting thread 3\r\n", FALSE); | |
1366 dw_thread_new(DW_SIGNAL_FUNC(run_thread), DW_INT_TO_POINTER(3), 10000); | |
1367 update_mle("Starting thread 4\r\n", FALSE); | |
1368 dw_thread_new(DW_SIGNAL_FUNC(run_thread), DW_INT_TO_POINTER(4), 10000); | |
1369 update_mle("Starting control thread\r\n", FALSE); | |
1370 dw_thread_new(DW_SIGNAL_FUNC(control_thread), DW_INT_TO_POINTER(0), 10000); | |
1371 dw_mutex_unlock(mutex); | |
1372 return 0; | |
1373 } | |
1374 | |
1375 void thread_add(void) | |
1376 { | |
1377 HWND tmpbox; | |
1378 char buf[100]; | |
1379 int i; | |
1380 | |
1381 /* create a box to pack into the notebook page */ | |
1382 tmpbox = dw_box_new(DW_VERT, 0); | |
1383 dw_box_pack_start(notebookbox9, tmpbox, 0, 0, TRUE, TRUE, 1); | |
1384 | |
1385 startbutton = dw_button_new( "Start Threads", 0 ); | |
1386 dw_box_pack_start( tmpbox, startbutton, 100, 30, FALSE, FALSE, 0 ); | |
1387 dw_signal_connect( startbutton, DW_SIGNAL_CLICKED, DW_SIGNAL_FUNC(start_threads_button_callback), NULL ); | |
1388 | |
1389 /* Create the base threading components */ | |
1390 threadmle = dw_mle_new(0); | |
1391 dw_box_pack_start(tmpbox, threadmle, 1, 1, TRUE, TRUE, 0); | |
1392 mutex = dw_mutex_new(); | |
1393 workevent = dw_event_new(); | |
1394 } | |
1395 | |
1396 void DWSIGNAL run_thread(void *data) | |
1397 { | |
1398 int threadnum = DW_POINTER_TO_INT(data); | |
1399 char buf[BUF_SIZE]; | |
1400 | |
1401 sprintf(buf, "Thread %d started.\r\n", threadnum); | |
1402 update_mle(buf, TRUE); | |
1403 | |
1404 /* Increment the ready count while protected by mutex */ | |
1405 dw_mutex_lock(mutex); | |
1406 ready++; | |
1407 /* If all 4 threads have incrememted the ready count... | |
1408 * Post the control event semaphore so things will get started. | |
1409 */ | |
1410 if(ready == 4) | |
1411 dw_event_post(controlevent); | |
1412 dw_mutex_unlock(mutex); | |
1413 | |
1414 while(!finished) | |
1415 { | |
1416 int result = dw_event_wait(workevent, 2000); | |
1417 | |
1418 if(result == DW_ERROR_TIMEOUT) | |
1419 { | |
1420 sprintf(buf, "Thread %d timeout waiting for event.\r\n", threadnum); | |
1421 update_mle(buf, TRUE); | |
1422 } | |
1423 else if(result == DW_ERROR_NONE) | |
1424 { | |
1425 sprintf(buf, "Thread %d doing some work.\r\n", threadnum); | |
1426 update_mle(buf, TRUE); | |
1427 /* Pretend to do some work */ | |
1428 dw_main_sleep(1000 * threadnum); | |
1429 | |
1430 /* Increment the ready count while protected by mutex */ | |
1431 dw_mutex_lock(mutex); | |
1432 ready++; | |
1433 sprintf(buf, "Thread %d work done. ready=%d", threadnum, ready); | |
1434 /* If all 4 threads have incrememted the ready count... | |
1435 * Post the control event semaphore so things will get started. | |
1436 */ | |
1437 if(ready == 4) | |
1438 { | |
1439 dw_event_post(controlevent); | |
1440 strcat(buf, " Control posted."); | |
1441 } | |
1442 dw_mutex_unlock(mutex); | |
1443 strcat(buf, "\r\n"); | |
1444 update_mle(buf, TRUE); | |
1445 } | |
1446 else | |
1447 { | |
1448 sprintf(buf, "Thread %d error %d.\r\n", threadnum, result); | |
1449 update_mle(buf, TRUE); | |
1450 dw_main_sleep(10000); | |
1451 } | |
1452 } | |
1453 sprintf(buf, "Thread %d finished.\r\n", threadnum); | |
1454 update_mle(buf, TRUE); | |
1455 } | |
1456 | |
1457 void DWSIGNAL control_thread(void *data) | |
1458 { | |
1459 int inprogress = 5; | |
1460 char buf[BUF_SIZE]; | |
1461 | |
1462 while(inprogress) | |
1463 { | |
1464 int result = dw_event_wait(controlevent, 2000); | |
1465 | |
1466 if(result == DW_ERROR_TIMEOUT) | |
1467 { | |
1468 update_mle("Control thread timeout waiting for event.\r\n", TRUE); | |
1469 } | |
1470 else if(result == DW_ERROR_NONE) | |
1471 { | |
1472 /* Reset the control event */ | |
1473 dw_event_reset(controlevent); | |
1474 ready = 0; | |
1475 sprintf(buf,"Control thread starting worker threads. Inprogress=%d\r\n", inprogress); | |
1476 update_mle(buf, TRUE); | |
1477 /* Start the work threads */ | |
1478 dw_event_post(workevent); | |
1479 dw_main_sleep(100); | |
1480 /* Reset the work event */ | |
1481 dw_event_reset(workevent); | |
1482 inprogress--; | |
1483 } | |
1484 else | |
1485 { | |
1486 sprintf(buf, "Control thread error %d.\r\n", result); | |
1487 update_mle(buf, TRUE); | |
1488 dw_main_sleep(10000); | |
1489 } | |
1490 } | |
1491 /* Tell the other threads we are done */ | |
1492 finished = TRUE; | |
1493 dw_event_post(workevent); | |
1494 /* Close the control event */ | |
1495 dw_event_close(&controlevent); | |
1496 update_mle("Control thread finished.\r\n", TRUE); | |
1497 dw_window_enable(startbutton); | |
1498 } | |
1499 | |
1500 | |
1329 /* | 1501 /* |
1330 * Let's demonstrate the functionality of this library. :) | 1502 * Let's demonstrate the functionality of this library. :) |
1331 */ | 1503 */ |
1332 int main(int argc, char *argv[]) | 1504 int main(int argc, char *argv[]) |
1333 { | 1505 { |
1337 ULONG notebookpage4; | 1509 ULONG notebookpage4; |
1338 ULONG notebookpage5; | 1510 ULONG notebookpage5; |
1339 ULONG notebookpage6; | 1511 ULONG notebookpage6; |
1340 ULONG notebookpage7; | 1512 ULONG notebookpage7; |
1341 ULONG notebookpage8; | 1513 ULONG notebookpage8; |
1514 ULONG notebookpage9; | |
1342 | 1515 |
1343 dw_init(TRUE, argc, argv); | 1516 dw_init(TRUE, argc, argv); |
1344 | 1517 |
1345 mainwindow = dw_window_new( HWND_DESKTOP, "dwindows test", flStyle | DW_FCF_SIZEBORDER | DW_FCF_MINMAX); | 1518 mainwindow = dw_window_new( HWND_DESKTOP, "dwindows test", flStyle | DW_FCF_SIZEBORDER | DW_FCF_MINMAX); |
1346 dw_window_set_icon(mainwindow, fileicon); | 1519 dw_window_set_icon(mainwindow, fileicon); |
1406 notebookpage8 = dw_notebook_page_new( notebook, 1, FALSE ); | 1579 notebookpage8 = dw_notebook_page_new( notebook, 1, FALSE ); |
1407 dw_notebook_pack( notebook, notebookpage8, notebookbox8 ); | 1580 dw_notebook_pack( notebook, notebookpage8, notebookbox8 ); |
1408 dw_notebook_page_set_text( notebook, notebookpage8, "scrollbox"); | 1581 dw_notebook_page_set_text( notebook, notebookpage8, "scrollbox"); |
1409 scrollbox_add(); | 1582 scrollbox_add(); |
1410 | 1583 |
1584 notebookbox9 = dw_box_new( BOXVERT, 8 ); | |
1585 notebookpage9 = dw_notebook_page_new( notebook, 1, FALSE ); | |
1586 dw_notebook_pack( notebook, notebookpage9, notebookbox9 ); | |
1587 dw_notebook_page_set_text( notebook, notebookpage9, "thread/event"); | |
1588 thread_add(); | |
1589 | |
1411 dw_signal_connect(mainwindow, DW_SIGNAL_DELETE, DW_SIGNAL_FUNC(exit_callback), (void *)mainwindow); | 1590 dw_signal_connect(mainwindow, DW_SIGNAL_DELETE, DW_SIGNAL_FUNC(exit_callback), (void *)mainwindow); |
1412 timerid = dw_timer_connect(2000, DW_SIGNAL_FUNC(timer_callback), 0); | 1591 timerid = dw_timer_connect(2000, DW_SIGNAL_FUNC(timer_callback), 0); |
1413 dw_window_set_size(mainwindow, 640, 520); | 1592 dw_window_set_size(mainwindow, 640, 520); |
1414 dw_window_show(mainwindow); | 1593 dw_window_show(mainwindow); |
1415 | 1594 |