Beispiel #1
0
static Py_ssize_t
get_char_buffer (PyObject *self, Py_ssize_t segment, char **ptrptr)
{
    void *ptr = NULL;
    Py_ssize_t ret;

    ret = get_read_buffer (self, segment, &ptr);
    *ptrptr = (char *) ptr;

    return ret;
}
Beispiel #2
0
Datei: test.c Projekt: wtracy/dog
void test_fill_drain_drain() {
  int i;
  int* pointer;

  buffer_init(2);
  for (i = 0; i < 1023; ++i) {
    pointer = (int*)get_write_buffer();
    *pointer = i;
    g_assert(buffer_push(sizeof(int)));
  }
  for (i = 0; i < 1023; ++i) {
    pointer = (int*)get_read_buffer(0);
    g_assert_cmpint(*pointer, ==, i);
    g_assert(buffer_pop(sizeof(int), 0));
  }
  for (i = 0; i < 1023; ++i) {
    pointer = (int*)get_read_buffer(1);
    g_assert_cmpint(*pointer, ==, i);
    g_assert(buffer_pop(sizeof(int), 1));
  }
  buffer_free();
}
Beispiel #3
0
Datei: test.c Projekt: wtracy/dog
void test_read_write_one_at_a_time() {
  int i;
  int* pointer;

  buffer_init(1);
  for (i = 0; i < 9000; ++i) {
    g_assert_cmpint(get_available_to_write(), >=, sizeof(int));
    pointer = (int*)get_write_buffer();
    *pointer = i;
    g_assert(buffer_push(sizeof(int)));
    g_assert_cmpint(get_available_to_read(0), >=, sizeof(int));
    pointer = (int*)get_read_buffer(0);
    g_assert_cmpint(*pointer, ==, i);
    g_assert(buffer_pop(sizeof(int), 0));
  }
  buffer_free();
}
int main(int argc, char *argv[]) {
	pthread_t tid;
	int is_running = 1;
	uint32_t frames_rdy;
	uint32_t frames_req;
	buffer_cblk_t *pcblk = &g_buffer_cblk;
	void *ptr1;
	uint32_t size1;
	void *ptr2;
	uint32_t size2;	
	uint32_t delay = -1;
	uint32_t cnt;
    char *buf = NULL;

	if (argc > 1) {
		delay = atoi(argv[1]);
	}
	
	memset(pcblk, 0, sizeof(*pcblk));

    pthread_mutex_init(&pcblk->lock, NULL);
    pthread_cond_init(&pcblk->cond, NULL);
    
	buf = malloc(BUF_FRAME_COUNT*BUF_FRAME_SIZE);
    buffer_cblk_init(pcblk, buf, BUF_FRAME_COUNT, BUF_FRAME_SIZE);

	printf("[init] buf = 0x%x, end = 0x%x\n", (unsigned int)pcblk->buffer, (unsigned int)pcblk->buffer + BUF_FRAME_SIZE*BUF_FRAME_COUNT);

	pthread_create(&tid, NULL, thread_client, pcblk);

	cnt = 0;
	while (is_running && g_client_runnig) {
		frames_rdy = frames_read_available(pcblk);        
		if (frames_rdy == 0) {
            printf("[s]wait client\n");
			usleep(100);
			continue;
		}

        frames_req = rand() & (BUF_FRAME_REQ_S - 1);
        if (frames_req == 0) {            
		  frames_req = BUF_FRAME_REQ_S;
        }
		if (frames_req > frames_rdy) {
			frames_req = frames_rdy;
		}
		
		frames_req = get_read_buffer(pcblk, frames_req, &ptr1, &size1, &ptr2, &size2);
		if (frames_req > 0) {
			printf("[s]frames_req = 0x%x\n", frames_req);
			printf("[s]buf1 = 0x%x, size1 = 0x%x, buf2 = 0x%x, size2 = 0x%x\n", (unsigned int)ptr1, size1, (unsigned int)ptr2, size2);
            
            pthread_mutex_lock(&pcblk->lock);            
            
			advance_read_index(pcblk, frames_req);
            
            pthread_cond_signal(&pcblk->cond);	
            pthread_mutex_unlock(&pcblk->lock);
		}
		
		usleep(DELAY_S*1000);
		cnt++;
		if (cnt >= (1000/DELAY_S)) {
			cnt = 0;
			delay--;
			if (delay == 0) {
				printf("exit main loop\n");
				is_running = 0;
			}
		}
	}

	g_client_runnig = 0;
	pthread_cond_signal(&pcblk->cond);
	pthread_join(tid, NULL);

	pthread_cond_destroy(&pcblk->cond);
	pthread_mutex_destroy(&pcblk->lock);

	if (pcblk->buffer) {
		free(pcblk->buffer);
		pcblk->buffer = NULL;
	}
    
    return 0;
}
Beispiel #5
0
static Py_ssize_t
get_write_buffer (PyObject *self, Py_ssize_t segment, void **ptrptr)
{
    return get_read_buffer (self, segment, ptrptr);
}
Beispiel #6
0
static pc_bool_t
perform_read(pc_channel_t *channel)
{
    read_buffer_t *rb = get_read_buffer(channel->ctx->cctx);

    /* We are going to loop on the socket until we've drained it of all
       available data, or when the callback signals it does not want any
       more data at this time.  */
    while (1)
    {
        ssize_t amt;
        ssize_t consumed;
        pc_error_t *err;

        do
        {
            amt = read(channel->fd, rb->buf, rb->len);

            /* Data should be immediately available, but maybe we'll need to
               try a couple times.  */
        } while (amt == -1 && errno == EINTR);

        if (amt == -1 || amt == 0)
        {
            /* Return the read buffer to the context.  */
            rb->next = channel->ctx->cctx->avail;
            channel->ctx->cctx->avail = rb;

            if (amt == 0)
            {
                /* The other end closed the socket -- we just hit EOF.

                   ### signal the app somehow.
                   ### for now, turn off reading.  */
                channel->desire_read = FALSE;
                return TRUE;
            }

            if (errno == EAGAIN || errno == EWOULDBLOCK)
            {
                /* Tell the callback that we are done for the moment.  */
                err = channel->callbacks->read_cb(
                        &consumed,
                        NULL /* buf */, 0 /* len */,
                        channel, channel->cb_baton,
                        channel->ctx->cctx->callback_scratch);
                pc_pool_clear(channel->ctx->cctx->callback_scratch);

                /* ### what to do with an error... */
                pc_error_handled(err);

                if (consumed == PC_CONSUMED_STOP)
                {
                    /* Stop reading from the socket.  */
                    channel->desire_read = FALSE;
                    return TRUE;
                }
                /* ### assert consumed == PC_CONSUMED_CONTINUE  */

                /* No changes to our desire to read (waiting for more)  */
                return FALSE;
            }

            if (errno == ECONNRESET)
            {
                /* ### signal the problem somehow  */
            }
            else
            {
                /* ### what others errors, and how to signal?  */
            }

            /* Stop reading from this socket.  */
            channel->desire_read = FALSE;
            return TRUE;
        }

        err = channel->callbacks->read_cb(
                &consumed, rb->buf, amt, channel,
                channel->cb_baton,
                channel->ctx->cctx->callback_scratch);
        pc_pool_clear(channel->ctx->cctx->callback_scratch);

        /* ### what to do with an error... */
        pc_error_handled(err);

        /* ### assert consumed >= 0 && consumed <= amt  */

        if (consumed < amt)
        {
            /* Store the read buffer in our list of pending reads.  */
            rb->channel = channel;
            rb->current = rb->buf + consumed;
            rb->remaining = amt - consumed;
            rb->next = channel->ctx->cctx->pending;
            channel->ctx->cctx->pending = rb;

            /* Stop reading.  */
            channel->desire_read = FALSE;
            return TRUE;
        }

        /* The callback read all the data provided. Loop back to read
           more data from the socket (if any).  */
    }

    /* NOTREACHED  */
}