Changeset 0b9ac3c in mainline for uspace/lib/libc/generic/async.c


Ignore:
Timestamp:
2010-02-23T19:03:28Z (14 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
c62d2e1
Parents:
1ccafee (diff), 5e50394 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/libc/generic/async.c

    r1ccafee r0b9ac3c  
    11011101}
    11021102
     1103/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
     1104 *
     1105 * Ask through phone for a new connection to some service.
     1106 *
     1107 * @param phoneid       Phone handle used for contacting the other side.
     1108 * @param arg1          User defined argument.
     1109 * @param arg2          User defined argument.
     1110 * @param arg3          User defined argument.
     1111 *
     1112 * @return              New phone handle on success or a negative error code.
     1113 */
     1114int
     1115async_connect_me_to(int phoneid, ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3)
     1116{
     1117        int rc;
     1118        ipcarg_t newphid;
     1119
     1120        rc = async_req_3_5(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3, NULL,
     1121            NULL, NULL, NULL, &newphid);
     1122       
     1123        if (rc != EOK) 
     1124                return rc;
     1125
     1126        return newphid;
     1127}
     1128
     1129/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
     1130 *
     1131 * Ask through phone for a new connection to some service and block until
     1132 * success.
     1133 *
     1134 * @param phoneid       Phone handle used for contacting the other side.
     1135 * @param arg1          User defined argument.
     1136 * @param arg2          User defined argument.
     1137 * @param arg3          User defined argument.
     1138 *
     1139 * @return              New phone handle on success or a negative error code.
     1140 */
     1141int
     1142async_connect_me_to_blocking(int phoneid, ipcarg_t arg1, ipcarg_t arg2,
     1143    ipcarg_t arg3)
     1144{
     1145        int rc;
     1146        ipcarg_t newphid;
     1147
     1148        rc = async_req_4_5(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3,
     1149            IPC_FLAG_BLOCKING, NULL, NULL, NULL, NULL, &newphid);
     1150       
     1151        if (rc != EOK) 
     1152                return rc;
     1153
     1154        return newphid;
     1155}
     1156
    11031157/** Wrapper for making IPC_M_SHARE_IN calls using the async framework.
    11041158 *
     
    12871341}
    12881342
     1343/** Wrapper for forwarding any read request
     1344 *
     1345 *
     1346 */
     1347int async_data_read_forward_fast(int phoneid, ipcarg_t method, ipcarg_t arg1,
     1348    ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipc_call_t *dataptr)
     1349{
     1350        ipc_callid_t callid;
     1351        if (!async_data_read_receive(&callid, NULL)) {
     1352                ipc_answer_0(callid, EINVAL);
     1353                return EINVAL;
     1354        }
     1355       
     1356        aid_t msg = async_send_fast(phoneid, method, arg1, arg2, arg3, arg4,
     1357            dataptr);
     1358        if (msg == 0) {
     1359                ipc_answer_0(callid, EINVAL);
     1360                return EINVAL;
     1361        }
     1362       
     1363        int retval = ipc_forward_fast(callid, phoneid, 0, 0, 0,
     1364            IPC_FF_ROUTE_FROM_ME);
     1365        if (retval != EOK) {
     1366                async_wait_for(msg, NULL);
     1367                ipc_answer_0(callid, retval);
     1368                return retval;
     1369        }
     1370       
     1371        ipcarg_t rc;
     1372        async_wait_for(msg, &rc);
     1373       
     1374        return (int) rc;
     1375}
     1376
    12891377/** Wrapper for making IPC_M_DATA_WRITE calls using the async framework.
    12901378 *
    1291  * @param phoneid       Phone that will be used to contact the receiving side.
    1292  * @param src           Address of the beginning of the source buffer.
    1293  * @param size          Size of the source buffer.
    1294  *
    1295  * @return              Zero on success or a negative error code from errno.h.
     1379 * @param phoneid Phone that will be used to contact the receiving side.
     1380 * @param src     Address of the beginning of the source buffer.
     1381 * @param size    Size of the source buffer.
     1382 *
     1383 * @return Zero on success or a negative error code from errno.h.
     1384 *
    12961385 */
    12971386int async_data_write_start(int phoneid, const void *src, size_t size)
     
    13081397 * So far, this wrapper is to be used from within a connection fibril.
    13091398 *
    1310  * @param callid        Storage where the hash of the IPC_M_DATA_WRITE call will
    1311  *                      be stored.
    1312  * @param size          Storage where the suggested size will be stored. May be
    1313  *                      NULL
    1314  *
    1315  * @return              Non-zero on success, zero on failure.
     1399 * @param callid Storage where the hash of the IPC_M_DATA_WRITE call will
     1400 *               be stored.
     1401 * @param size   Storage where the suggested size will be stored. May be
     1402 *               NULL
     1403 *
     1404 * @return Non-zero on success, zero on failure.
     1405 *
    13161406 */
    13171407int async_data_write_receive(ipc_callid_t *callid, size_t *size)
     
    13201410       
    13211411        assert(callid);
    1322 
     1412       
    13231413        *callid = async_get_call(&data);
    13241414        if (IPC_GET_METHOD(data) != IPC_M_DATA_WRITE)
    13251415                return 0;
     1416       
    13261417        if (size)
    13271418                *size = (size_t) IPC_GET_ARG2(data);
     1419       
    13281420        return 1;
    13291421}
     
    13341426 * so that the user doesn't have to remember the meaning of each IPC argument.
    13351427 *
    1336  * @param callid        Hash of the IPC_M_DATA_WRITE call to answer.
    1337  * @param dst           Final destination address for the IPC_M_DATA_WRITE call.
    1338  * @param size          Final size for the IPC_M_DATA_WRITE call.
    1339  *
    1340  * @return              Zero on success or a value from @ref errno.h on failure.
     1428 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
     1429 * @param dst    Final destination address for the IPC_M_DATA_WRITE call.
     1430 * @param size   Final size for the IPC_M_DATA_WRITE call.
     1431 *
     1432 * @return Zero on success or a value from @ref errno.h on failure.
     1433 *
    13411434 */
    13421435int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
     
    13451438}
    13461439
    1347 /** Wrapper for receiving blobs via the async_data_write_*
     1440/** Wrapper for receiving binary data or strings
    13481441 *
    13491442 * This wrapper only makes it more comfortable to use async_data_write_*
    1350  * functions to receive blobs.
    1351  *
    1352  * @param blob     Pointer to data pointer (which should be later disposed
    1353  *                 by free()). If the operation fails, the pointer is not
    1354  *                 touched.
    1355  * @param max_size Maximum size (in bytes) of the blob to receive. 0 means
    1356  *                 no limit.
    1357  * @param received If not NULL, the size of the received data is stored here.
     1443 * functions to receive binary data or strings.
     1444 *
     1445 * @param data       Pointer to data pointer (which should be later disposed
     1446 *                   by free()). If the operation fails, the pointer is not
     1447 *                   touched.
     1448 * @param nullterm   If true then the received data is always zero terminated.
     1449 *                   This also causes to allocate one extra byte beyond the
     1450 *                   raw transmitted data.
     1451 * @param min_size   Minimum size (in bytes) of the data to receive.
     1452 * @param max_size   Maximum size (in bytes) of the data to receive. 0 means
     1453 *                   no limit.
     1454 * @param granulariy If non-zero then the size of the received data has to
     1455 *                   be divisible by this value.
     1456 * @param received   If not NULL, the size of the received data is stored here.
    13581457 *
    13591458 * @return Zero on success or a value from @ref errno.h on failure.
    13601459 *
    13611460 */
    1362 int async_data_blob_receive(char **blob, const size_t max_size, size_t *received)
     1461int async_data_write_accept(void **data, const bool nullterm,
     1462    const size_t min_size, const size_t max_size, const size_t granularity,
     1463    size_t *received)
    13631464{
    13641465        ipc_callid_t callid;
     
    13691470        }
    13701471       
     1472        if (size < min_size) {
     1473                ipc_answer_0(callid, EINVAL);
     1474                return EINVAL;
     1475        }
     1476       
    13711477        if ((max_size > 0) && (size > max_size)) {
    13721478                ipc_answer_0(callid, EINVAL);
     
    13741480        }
    13751481       
    1376         char *data = (char *) malloc(size);
    1377         if (data == NULL) {
     1482        if ((granularity > 0) && ((size % granularity) != 0)) {
     1483                ipc_answer_0(callid, EINVAL);
     1484                return EINVAL;
     1485        }
     1486       
     1487        void *_data;
     1488       
     1489        if (nullterm)
     1490                _data = malloc(size + 1);
     1491        else
     1492                _data = malloc(size);
     1493       
     1494        if (_data == NULL) {
    13781495                ipc_answer_0(callid, ENOMEM);
    13791496                return ENOMEM;
    13801497        }
    13811498       
    1382         int rc = async_data_write_finalize(callid, data, size);
     1499        int rc = async_data_write_finalize(callid, _data, size);
    13831500        if (rc != EOK) {
    1384                 free(data);
     1501                free(_data);
    13851502                return rc;
    13861503        }
    13871504       
    1388         *blob = data;
     1505        if (nullterm)
     1506                ((char *) _data)[size] = 0;
     1507       
     1508        *data = _data;
    13891509        if (received != NULL)
    13901510                *received = size;
     
    13931513}
    13941514
    1395 /** Wrapper for receiving strings via the async_data_write_*
    1396  *
    1397  * This wrapper only makes it more comfortable to use async_data_write_*
    1398  * functions to receive strings.
    1399  *
    1400  * @param str      Pointer to string pointer (which should be later disposed
    1401  *                 by free()). If the operation fails, the pointer is not
    1402  *                 touched.
    1403  * @param max_size Maximum size (in bytes) of the string to receive. 0 means
    1404  *                 no limit.
    1405  *
    1406  * @return Zero on success or a value from @ref errno.h on failure.
    1407  *
    1408  */
    1409 int async_data_string_receive(char **str, const size_t max_size)
     1515/** Wrapper for voiding any data that is about to be received
     1516 *
     1517 * This wrapper can be used to void any pending data
     1518 *
     1519 * @param retval Error value from @ref errno.h to be returned to the caller.
     1520 *
     1521 */
     1522void async_data_write_void(const int retval)
    14101523{
    14111524        ipc_callid_t callid;
    1412         size_t size;
    1413         if (!async_data_write_receive(&callid, &size)) {
     1525        async_data_write_receive(&callid, NULL);
     1526        ipc_answer_0(callid, retval);
     1527}
     1528
     1529/** Wrapper for forwarding any data that is about to be received
     1530 *
     1531 *
     1532 */
     1533int async_data_write_forward_fast(int phoneid, ipcarg_t method, ipcarg_t arg1,
     1534    ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipc_call_t *dataptr)
     1535{
     1536        ipc_callid_t callid;
     1537        if (!async_data_write_receive(&callid, NULL)) {
    14141538                ipc_answer_0(callid, EINVAL);
    14151539                return EINVAL;
    14161540        }
    14171541       
    1418         if ((max_size > 0) && (size > max_size)) {
     1542        aid_t msg = async_send_fast(phoneid, method, arg1, arg2, arg3, arg4,
     1543            dataptr);
     1544        if (msg == 0) {
    14191545                ipc_answer_0(callid, EINVAL);
    14201546                return EINVAL;
    14211547        }
    14221548       
    1423         char *data = (char *) malloc(size + 1);
    1424         if (data == NULL) {
    1425                 ipc_answer_0(callid, ENOMEM);
    1426                 return ENOMEM;
    1427         }
    1428        
    1429         int rc = async_data_write_finalize(callid, data, size);
    1430         if (rc != EOK) {
    1431                 free(data);
    1432                 return rc;
    1433         }
    1434        
    1435         data[size] = 0;
    1436         *str = data;
    1437         return EOK;
     1549        int retval = ipc_forward_fast(callid, phoneid, 0, 0, 0,
     1550            IPC_FF_ROUTE_FROM_ME);
     1551        if (retval != EOK) {
     1552                async_wait_for(msg, NULL);
     1553                ipc_answer_0(callid, retval);
     1554                return retval;
     1555        }
     1556       
     1557        ipcarg_t rc;
     1558        async_wait_for(msg, &rc);
     1559       
     1560        return (int) rc;
    14381561}
    14391562
Note: See TracChangeset for help on using the changeset viewer.