Changeset 3771:8ff6830f66ee in roaraudio for libroaross


Ignore:
Timestamp:
05/06/10 20:21:28 (14 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

implemented select() using roar_vio_select() for sysio

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libroaross/libroaross.c

    r3668 r3771  
    145145 int     (*dup)(int oldfd); 
    146146 int     (*dup2)(int oldfd, int newfd); 
     147 int     (*select)(int nfds, fd_set *readfds, fd_set *writefds, 
     148                   fd_set *exceptfds, struct timeval *timeout); 
    147149} _os; 
    148150 
     
    241243 _os.dup   = dlsym(REAL_LIBC, "dup"); 
    242244 _os.dup2  = dlsym(REAL_LIBC, "dup2"); 
     245 _os.select= dlsym(REAL_LIBC, "select"); 
    243246} 
    244247 
     
    257260  _init_os(); 
    258261  _init_ptr(); 
     262  roar_vio_select(NULL, 0, NULL, NULL); 
    259263  inited++; 
    260264 } 
     
    13421346} 
    13431347 
     1348int select(int nfds, fd_set *readfds, fd_set *writefds, 
     1349           fd_set *exceptfds, struct timeval *timeout) { 
     1350 struct roar_vio_selecttv rtv; 
     1351 struct roar_vio_select * sv  = NULL; 
     1352 ssize_t ret; 
     1353 size_t num = 0; 
     1354 int idx; 
     1355 int i; 
     1356 int i_r, i_w, i_e; 
     1357 int max_index = -1; 
     1358 volatile static int is_critical = 0; 
     1359 
     1360 _init(); 
     1361 
     1362 if ( is_critical ) 
     1363  return _os.select(nfds, readfds, writefds, exceptfds, timeout); 
     1364 
     1365 ROAR_DBG("select(nfds=%i, readfds=%p, writefds=%p, exceptfds=%p, timeout=%p) = ?", nfds, readfds, writefds, exceptfds, timeout); 
     1366 
     1367 if ( nfds == 0 ) { 
     1368  ROAR_DBG("select(nfds=%i, readfds=%p, writefds=%p, exceptfds=%p, timeout=%p) = 0", nfds, readfds, writefds, exceptfds, timeout); 
     1369  return 0; 
     1370 } 
     1371 
     1372 if ( readfds == NULL && writefds == NULL && exceptfds == NULL ) { 
     1373  ROAR_DBG("select(nfds=%i, readfds=%p, writefds=%p, exceptfds=%p, timeout=%p) = 0", nfds, readfds, writefds, exceptfds, timeout); 
     1374  return 0; 
     1375 } 
     1376 
     1377 if ( timeout != NULL ) { 
     1378  rtv.sec = timeout->tv_sec; 
     1379  rtv.nsec = timeout->tv_usec*1000; 
     1380 } 
     1381 
     1382 // count number of handles: 
     1383 for (i = 0; i < nfds; i++) { 
     1384  ROAR_DBG("select(nfds=%i, readfds=%p, writefds=%p, exceptfds=%p, timeout=%p): i=%i, EXISTS", nfds, readfds, writefds, exceptfds, timeout, i); 
     1385  if ( (readfds   != NULL && FD_ISSET(i, readfds  )) || 
     1386       (writefds  != NULL && FD_ISSET(i, writefds )) || 
     1387       (exceptfds != NULL && FD_ISSET(i, exceptfds)) 
     1388     ) { 
     1389   ROAR_DBG("select(nfds=%i, readfds=%p, writefds=%p, exceptfds=%p, timeout=%p): i=%i, EXISTS", nfds, readfds, writefds, exceptfds, timeout, i); 
     1390   num++; 
     1391   max_index = i; 
     1392  } 
     1393 } 
     1394 
     1395 if ( num == 0 ) { 
     1396  ROAR_DBG("select(nfds=%i, readfds=%p, writefds=%p, exceptfds=%p, timeout=%p) = 0", nfds, readfds, writefds, exceptfds, timeout); 
     1397  return 0; 
     1398 } 
     1399 
     1400 nfds = max_index + 1; 
     1401 
     1402 // create sv; 
     1403 sv = roar_mm_malloc(sizeof(struct roar_vio_select)*num); 
     1404 if ( sv == NULL ) { 
     1405  ROAR_DBG("select(nfds=%i, readfds=%p, writefds=%p, exceptfds=%p, timeout=%p) = -1", nfds, readfds, writefds, exceptfds, timeout); 
     1406  return -1; 
     1407 } 
     1408 
     1409 memset(sv, 0, sizeof(struct roar_vio_select)*num); 
     1410 
     1411 for (i = 0, idx = 0; i < nfds; i++) { 
     1412  if ( idx >= num ) { 
     1413   roar_mm_free(sv); 
     1414   errno = EFAULT; 
     1415   ROAR_DBG("select(nfds=%i, readfds=%p, writefds=%p, exceptfds=%p, timeout=%p) = -1 // i=%i, idx=%i, num=%i", nfds, readfds, writefds, exceptfds, timeout, i, (int)idx, (int)num); 
     1416   return -1; 
     1417  } 
     1418  i_r = readfds   != NULL && FD_ISSET(i, readfds); 
     1419  i_w = writefds  != NULL && FD_ISSET(i, writefds); 
     1420  i_e = exceptfds != NULL && FD_ISSET(i, exceptfds); 
     1421 
     1422  ROAR_DBG("select(nfds=%i, readfds=%p, writefds=%p, exceptfds=%p, timeout=%p): i=%i, i_r=%i, i_w=%i, i_e=%i", nfds, readfds, writefds, exceptfds, timeout, i, i_r, i_w, i_e); 
     1423 
     1424  if ( i_r || i_w || i_e ) { 
     1425   // TODO: use VIO for pointers... 
     1426   sv[idx].vio     = NULL; 
     1427   sv[idx].fh      = i; 
     1428 
     1429   sv[idx].ud.si   = i; 
     1430   sv[idx].eventsq = (i_r ? ROAR_VIO_SELECT_READ   : 0) | 
     1431                     (i_w ? ROAR_VIO_SELECT_WRITE  : 0) | 
     1432                     (i_e ? ROAR_VIO_SELECT_EXCEPT : 0); 
     1433   idx++; 
     1434  } 
     1435 } 
     1436 
     1437 is_critical++; 
     1438 ret = roar_vio_select(sv, num, timeout == NULL ? NULL : &rtv, NULL); 
     1439 is_critical--; 
     1440 
     1441 if ( ret < 1 ) { 
     1442  roar_mm_free(sv); 
     1443  ROAR_DBG("select(nfds=%i, readfds=%p, writefds=%p, exceptfds=%p, timeout=%p) = %i", nfds, readfds, writefds, exceptfds, timeout, (int)ret); 
     1444  return ret; 
     1445 } 
     1446 
     1447 // update readfds, writefds, exceptfds: 
     1448 if ( readfds != NULL ) 
     1449  FD_ZERO(readfds); 
     1450 
     1451 if ( writefds != NULL ) 
     1452  FD_ZERO(writefds); 
     1453 
     1454 if ( exceptfds != NULL ) 
     1455  FD_ZERO(exceptfds); 
     1456 
     1457 for (idx = 0; idx < num; idx++) { 
     1458  if ( sv[idx].eventsa == 0 ) 
     1459   continue; 
     1460 
     1461  if ( sv[idx].eventsa & ROAR_VIO_SELECT_READ ) 
     1462   if ( readfds != NULL ) 
     1463    FD_SET(sv[idx].ud.si, readfds); 
     1464 
     1465  if ( sv[idx].eventsa & ROAR_VIO_SELECT_WRITE ) 
     1466   if ( writefds != NULL ) 
     1467    FD_SET(sv[idx].ud.si, writefds); 
     1468 
     1469  if ( sv[idx].eventsa & ROAR_VIO_SELECT_EXCEPT ) 
     1470   if ( exceptfds != NULL ) 
     1471    FD_SET(sv[idx].ud.si, exceptfds); 
     1472 } 
     1473 
     1474 roar_mm_free(sv); 
     1475 
     1476 ROAR_DBG("select(nfds=%i, readfds=%p, writefds=%p, exceptfds=%p, timeout=%p) = %i", nfds, readfds, writefds, exceptfds, timeout, (int)ret); 
     1477 return ret; 
     1478} 
     1479 
    13441480// ------------------------------------- 
    13451481// emulated stdio functions follow: 
     
    13611497FILE *fopen(const char *path, const char *mode) { 
    13621498 struct roar_vio_calls * vio; 
    1363  struct pointer * pointer; 
    13641499 FILE  * fr; 
    13651500 int     ret; 
Note: See TracChangeset for help on using the changeset viewer.