apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b..@apache.org
Subject cvs commit: apr/poll/os2 pollset.c
Date Thu, 26 Sep 2002 13:05:40 GMT
bjh         2002/09/26 06:05:39

  Modified:    poll/os2 pollset.c
  Log:
  OS/2: Implement apr_pollset_*()
  
  Revision  Changes    Path
  1.2       +159 -5    apr/poll/os2/pollset.c
  
  Index: pollset.c
  ===================================================================
  RCS file: /home/cvs/apr/poll/os2/pollset.c,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- pollset.c	9 Aug 2002 09:11:52 -0000	1.1
  +++ pollset.c	26 Sep 2002 13:05:39 -0000	1.2
  @@ -60,21 +60,43 @@
   
   struct apr_pollset_t {
       apr_pool_t *pool;
  +    apr_uint32_t nelts;
  +    apr_uint32_t nalloc;
  +    int *pollset;
  +    int num_read;
  +    int num_write;
  +    int num_except;
  +    int num_total;
  +    apr_pollfd_t *query_set;
  +    apr_pollfd_t *result_set;
   };
   
  +
  +
   APR_DECLARE(apr_status_t) apr_pollset_create(apr_pollset_t **pollset,
                                                apr_uint32_t size,
                                                apr_pool_t *p,
                                                apr_uint32_t flags)
   {
  -    return APR_ENOTIMPL;
  +    *pollset = apr_palloc(p, sizeof(**pollset));
  +    (*pollset)->pool = p;
  +    (*pollset)->nelts = 0;
  +    (*pollset)->nalloc = size;
  +    (*pollset)->pollset = apr_palloc(p, size * sizeof(int) * 3);
  +    (*pollset)->query_set = apr_palloc(p, size * sizeof(apr_pollfd_t));
  +    (*pollset)->result_set = apr_palloc(p, size * sizeof(apr_pollfd_t));
  +    (*pollset)->num_read = -1;
  +    return APR_SUCCESS;
   }
   
   
   
   APR_DECLARE(apr_status_t) apr_pollset_destroy(apr_pollset_t *pollset)
   {
  -    return APR_ENOTIMPL;
  +    /* A no-op function for now.  If we later implement /dev/poll
  +     * support, we'll need to close the /dev/poll fd here
  +     */
  +    return APR_SUCCESS;
   }
   
   
  @@ -82,7 +104,19 @@
   APR_DECLARE(apr_status_t) apr_pollset_add(apr_pollset_t *pollset,
                                             const apr_pollfd_t *descriptor)
   {
  -    return APR_ENOTIMPL;
  +    if (pollset->nelts == pollset->nalloc) {
  +        return APR_ENOMEM;
  +    }
  +
  +    pollset->query_set[pollset->nelts] = *descriptor;
  +
  +    if (descriptor->desc_type != APR_POLL_SOCKET) {
  +        return APR_EBADF;
  +    }
  +
  +    pollset->nelts++;
  +    pollset->num_read = -1;
  +    return APR_SUCCESS;
   }
   
   
  @@ -90,7 +124,67 @@
   APR_DECLARE(apr_status_t) apr_pollset_remove(apr_pollset_t *pollset,
                                                const apr_pollfd_t *descriptor)
   {
  -    return APR_ENOTIMPL;
  +    apr_uint32_t i;
  +
  +    for (i = 0; i < pollset->nelts; i++) {
  +        if (descriptor->desc.s == pollset->query_set[i].desc.s) {
  +            /* Found an instance of the fd: remove this and any other copies */
  +            apr_uint32_t dst = i;
  +            apr_uint32_t old_nelts = pollset->nelts;
  +            pollset->nelts--;
  +
  +            for (i++; i < old_nelts; i++) {
  +                if (descriptor->desc.s == pollset->query_set[i].desc.s) {
  +                    pollset->nelts--;
  +                }
  +                else {
  +                    pollset->pollset[dst] = pollset->pollset[i];
  +                    pollset->query_set[dst] = pollset->query_set[i];
  +                    dst++;
  +                }
  +            }
  +
  +            pollset->num_read = -1;
  +            return APR_SUCCESS;
  +        }
  +    }
  +
  +    return APR_NOTFOUND;
  +}
  +
  +
  +
  +static void make_pollset(apr_pollset_t *pollset)
  +{
  +    int i;
  +    int pos = 0;
  +
  +    pollset->num_read = 0;
  +    pollset->num_write = 0;
  +    pollset->num_except = 0;
  +
  +    for (i = 0; i < pollset->nelts; i++) {
  +        if (pollset->query_set[i].reqevents & APR_POLLIN) {
  +            pollset->pollset[pos++] = pollset->query_set[i].desc.s->socketdes;
  +            pollset->num_read++;
  +        }
  +    }
  +
  +    for (i = 0; i < pollset->nelts; i++) {
  +        if (pollset->query_set[i].reqevents & APR_POLLOUT) {
  +            pollset->pollset[pos++] = pollset->query_set[i].desc.s->socketdes;
  +            pollset->num_write++;
  +        }
  +    }
  +
  +    for (i = 0; i < pollset->nelts; i++) {
  +        if (pollset->query_set[i].reqevents & APR_POLLPRI) {
  +            pollset->pollset[pos++] = pollset->query_set[i].desc.s->socketdes;
  +            pollset->num_except++;
  +        }
  +    }
  +
  +    pollset->num_total = pollset->num_read + pollset->num_write + pollset->num_except;
   }
   
   
  @@ -100,5 +194,65 @@
                                              apr_int32_t *num,
                                              const apr_pollfd_t **descriptors)
   {
  -    return APR_ENOTIMPL;
  +    int rv;
  +    apr_uint32_t i;
  +    int *pollresult;
  +    int read_pos, write_pos, except_pos;
  +
  +    if (pollset->num_read < 0) {
  +        make_pollset(pollset);
  +    }
  +
  +    pollresult = alloca(sizeof(int) * pollset->num_total);
  +    memcpy(pollresult, pollset->pollset, sizeof(int) * pollset->num_total);
  +
  +    if (timeout > 0) {
  +        timeout /= 1000;
  +    }
  +
  +    rv = select(pollresult, pollset->num_read, pollset->num_write, pollset->num_except,
timeout);
  +
  +    if (rv < 0) {
  +        return APR_FROM_OS_ERROR(sock_errno());
  +    }
  +
  +    if (rv == 0) {
  +        return APR_TIMEUP;
  +    }
  +
  +    read_pos = 0;
  +    write_pos = pollset->num_read;
  +    except_pos = pollset->num_read + pollset->num_write;
  +    (*num) = 0;
  +
  +    for (i = 0; i < pollset->nelts; i++) {
  +        int rtnevents = 0;
  +
  +        if (pollset->query_set[i].reqevents & APR_POLLIN) {
  +            if (pollresult[read_pos++] != -1) {
  +                rtnevents |= APR_POLLIN;
  +            }
  +        }
  +
  +        if (pollset->query_set[i].reqevents & APR_POLLOUT) {
  +            if (pollresult[write_pos++] != -1) {
  +                rtnevents |= APR_POLLOUT;
  +            }
  +        }
  +
  +        if (pollset->query_set[i].reqevents & APR_POLLPRI) {
  +            if (pollresult[except_pos++] != -1) {
  +                rtnevents |= APR_POLLPRI;
  +            }
  +        }
  +
  +        if (rtnevents) {
  +            pollset->result_set[*num] = pollset->query_set[i];
  +            pollset->result_set[*num].rtnevents = rtnevents;
  +            (*num)++;
  +        }
  +    }
  +
  +    *descriptors = pollset->result_set;
  +    return APR_SUCCESS;
   }
  
  
  

Mime
View raw message