aboutsummaryrefslogtreecommitdiff
path: root/src/util/scheduler.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2013-12-24 11:39:46 +0000
committerChristian Grothoff <christian@grothoff.org>2013-12-24 11:39:46 +0000
commit79d2e869b8c7a4820f63d013024ec34940185461 (patch)
tree861af7b488f3db9076e80d47e7f129cef517aae7 /src/util/scheduler.c
parent03f90f8752d53540ba4389ea258ec5aecedb879d (diff)
downloadgnunet-79d2e869b8c7a4820f63d013024ec34940185461.tar.gz
gnunet-79d2e869b8c7a4820f63d013024ec34940185461.zip
Here's another patch for GNUNet's scheduler. It's a smaller version of my last
patch. This patch extends the scheduler API with two new functions: GNUNET_SCHEDULER_add_file_with_priority GNUNET_SCHEDULER_add_net_with_priority It also re-writes the other net_add/file_add functions to make them call these more generic functions in order to avoid code duplication. - Andrew
Diffstat (limited to 'src/util/scheduler.c')
-rw-r--r--src/util/scheduler.c177
1 files changed, 105 insertions, 72 deletions
diff --git a/src/util/scheduler.c b/src/util/scheduler.c
index cf1e073b1..ba6a04e4f 100644
--- a/src/util/scheduler.c
+++ b/src/util/scheduler.c
@@ -1418,25 +1418,10 @@ GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
1418 struct GNUNET_NETWORK_Handle *rfd, 1418 struct GNUNET_NETWORK_Handle *rfd,
1419 GNUNET_SCHEDULER_Task task, void *task_cls) 1419 GNUNET_SCHEDULER_Task task, void *task_cls)
1420{ 1420{
1421#if MINGW 1421 return GNUNET_SCHEDULER_add_net_with_priority (
1422 struct GNUNET_NETWORK_FDSet *rs; 1422 delay, priority,
1423 GNUNET_SCHEDULER_TaskIdentifier ret; 1423 rfd, true, false,
1424 1424 task, task_cls);
1425 GNUNET_assert (NULL != rfd);
1426 rs = GNUNET_NETWORK_fdset_create ();
1427 GNUNET_NETWORK_fdset_set (rs, rfd);
1428 ret =
1429 GNUNET_SCHEDULER_add_select (priority,
1430 delay, rs, NULL,
1431 task, task_cls);
1432 GNUNET_NETWORK_fdset_destroy (rs);
1433 return ret;
1434#else
1435 return add_without_sets (delay,
1436 priority,
1437 GNUNET_NETWORK_get_fd (rfd), -1, task,
1438 task_cls);
1439#endif
1440} 1425}
1441 1426
1442 1427
@@ -1461,25 +1446,58 @@ GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1461 struct GNUNET_NETWORK_Handle *wfd, 1446 struct GNUNET_NETWORK_Handle *wfd,
1462 GNUNET_SCHEDULER_Task task, void *task_cls) 1447 GNUNET_SCHEDULER_Task task, void *task_cls)
1463{ 1448{
1449 return GNUNET_SCHEDULER_add_net_with_priority (
1450 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1451 wfd, false, true,
1452 task, task_cls);
1453}
1454
1455/**
1456 * Schedule a new task to be run with a specified delay or when the
1457 * specified file descriptor is ready. The delay can be
1458 * used as a timeout on the socket being ready. The task will be
1459 * scheduled for execution once either the delay has expired or the
1460 * socket operation is ready.
1461 *
1462 * @param delay when should this operation time out? Use
1463 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1464 * @param priority priority of the task
1465 * @param fd file-descriptor
1466 * @param on_read whether to poll the file-descriptor for readability
1467 * @param on_write whether to poll the file-descriptor for writability
1468 * @param task main function of the task
1469 * @param task_cls closure of task
1470 * @return unique task identifier for the job
1471 * only valid until "task" is started!
1472 */
1473GNUNET_SCHEDULER_TaskIdentifier
1474GNUNET_SCHEDULER_add_net_with_priority (struct GNUNET_TIME_Relative delay,
1475 enum GNUNET_SCHEDULER_Priority priority,
1476 struct GNUNET_NETWORK_Handle *fd,
1477 bool on_read, bool on_write,
1478 GNUNET_SCHEDULER_Task task, void *task_cls)
1479{
1464#if MINGW 1480#if MINGW
1465 struct GNUNET_NETWORK_FDSet *ws; 1481 struct GNUNET_NETWORK_FDSet *s;
1466 GNUNET_SCHEDULER_TaskIdentifier ret; 1482 GNUNET_SCHEDULER_TaskIdentifier ret;
1467 1483
1468 GNUNET_assert (NULL != wfd); 1484 GNUNET_assert (fd != NULL);
1469 ws = GNUNET_NETWORK_fdset_create (); 1485 s = GNUNET_NETWORK_fdset_create ();
1470 GNUNET_NETWORK_fdset_set (ws, wfd); 1486 GNUNET_NETWORK_fdset_set (s, fd);
1471 ret = 1487 ret = GNUNET_SCHEDULER_add_select (
1472 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, 1488 priority, delay,
1473 delay, NULL, ws, 1489 on_read ? s : NULL,
1474 task, task_cls); 1490 on_write ? s : NULL,
1475 GNUNET_NETWORK_fdset_destroy (ws); 1491 task, task_cls);
1492 GNUNET_NETWORK_fdset_destroy (s);
1476 return ret; 1493 return ret;
1477#else 1494#else
1478 GNUNET_assert (GNUNET_NETWORK_get_fd (wfd) >= 0); 1495 GNUNET_assert (GNUNET_NETWORK_get_fd (fd) >= 0);
1479 return add_without_sets (delay, 1496 return add_without_sets (
1480 GNUNET_SCHEDULER_PRIORITY_DEFAULT, 1497 delay, priority,
1481 -1, GNUNET_NETWORK_get_fd (wfd), task, 1498 on_read ? GNUNET_NETWORK_get_fd (fd) : -1,
1482 task_cls); 1499 on_write ? GNUNET_NETWORK_get_fd (fd) : -1,
1500 task, task_cls);
1483#endif 1501#endif
1484} 1502}
1485 1503
@@ -1504,28 +1522,10 @@ GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1504 const struct GNUNET_DISK_FileHandle *rfd, 1522 const struct GNUNET_DISK_FileHandle *rfd,
1505 GNUNET_SCHEDULER_Task task, void *task_cls) 1523 GNUNET_SCHEDULER_Task task, void *task_cls)
1506{ 1524{
1507#if MINGW 1525 return GNUNET_SCHEDULER_add_file_with_priority (
1508 struct GNUNET_NETWORK_FDSet *rs; 1526 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1509 GNUNET_SCHEDULER_TaskIdentifier ret; 1527 rfd, true, false,
1510 1528 task, task_cls);
1511 GNUNET_assert (NULL != rfd);
1512 rs = GNUNET_NETWORK_fdset_create ();
1513 GNUNET_NETWORK_fdset_handle_set (rs, rfd);
1514 ret =
1515 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1516 delay, rs, NULL,
1517 task, task_cls);
1518 GNUNET_NETWORK_fdset_destroy (rs);
1519 return ret;
1520#else
1521 int fd;
1522
1523 GNUNET_DISK_internal_file_handle_ (rfd, &fd, sizeof (int));
1524 return add_without_sets (delay,
1525 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1526 fd, -1, task, task_cls);
1527
1528#endif
1529} 1529}
1530 1530
1531 1531
@@ -1549,28 +1549,61 @@ GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1549 const struct GNUNET_DISK_FileHandle *wfd, 1549 const struct GNUNET_DISK_FileHandle *wfd,
1550 GNUNET_SCHEDULER_Task task, void *task_cls) 1550 GNUNET_SCHEDULER_Task task, void *task_cls)
1551{ 1551{
1552 return GNUNET_SCHEDULER_add_file_with_priority (
1553 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1554 wfd, false, true,
1555 task, task_cls);
1556}
1557
1558/**
1559 * Schedule a new task to be run with a specified delay or when the
1560 * specified file descriptor is ready. The delay can be
1561 * used as a timeout on the socket being ready. The task will be
1562 * scheduled for execution once either the delay has expired or the
1563 * socket operation is ready.
1564 *
1565 * @param delay when should this operation time out? Use
1566 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1567 * @param priority priority of the task
1568 * @param fd file-descriptor
1569 * @param on_read whether to poll the file-descriptor for readability
1570 * @param on_write whether to poll the file-descriptor for writability
1571 * @param task main function of the task
1572 * @param task_cls closure of task
1573 * @return unique task identifier for the job
1574 * only valid until "task" is started!
1575 */
1576GNUNET_SCHEDULER_TaskIdentifier
1577GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
1578 enum GNUNET_SCHEDULER_Priority priority,
1579 const struct GNUNET_DISK_FileHandle *fd,
1580 bool on_read, bool on_write,
1581 GNUNET_SCHEDULER_Task task, void *task_cls)
1582{
1552#if MINGW 1583#if MINGW
1553 struct GNUNET_NETWORK_FDSet *ws; 1584 struct GNUNET_NETWORK_FDSet *s;
1554 GNUNET_SCHEDULER_TaskIdentifier ret; 1585 GNUNET_SCHEDULER_TaskIdentifier ret;
1555 1586
1556 GNUNET_assert (NULL != wfd); 1587 GNUNET_assert (fd != NULL);
1557 ws = GNUNET_NETWORK_fdset_create (); 1588 s = GNUNET_NETWORK_fdset_create ();
1558 GNUNET_NETWORK_fdset_handle_set (ws, wfd); 1589 GNUNET_NETWORK_fdset_handle_set (s, fd);
1559 ret = 1590 ret = GNUNET_SCHEDULER_add_select (
1560 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, 1591 priority, delay,
1561 delay, NULL, ws, 1592 on_read ? s : NULL,
1562 task, task_cls); 1593 on_write ? s : NULL,
1563 GNUNET_NETWORK_fdset_destroy (ws); 1594 task, task_cls);
1595 GNUNET_NETWORK_fdset_destroy (s);
1564 return ret; 1596 return ret;
1565#else 1597#else
1566 int fd; 1598 int real_fd;
1567 1599
1568 GNUNET_DISK_internal_file_handle_ (wfd, &fd, sizeof (int)); 1600 GNUNET_DISK_internal_file_handle_ (fd, &real_fd, sizeof (int));
1569 GNUNET_assert (fd >= 0); 1601 GNUNET_assert (real_fd > 0);
1570 return add_without_sets (delay, 1602 return add_without_sets (
1571 GNUNET_SCHEDULER_PRIORITY_DEFAULT, 1603 delay, priority,
1572 -1, fd, task, task_cls); 1604 on_read ? real_fd : -1,
1573 1605 on_write ? real_fd : -1,
1606 task, task_cls);
1574#endif 1607#endif
1575} 1608}
1576 1609