| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2007, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: bfile_async.c $ |
|---|
| 11 | * $brcm_Revision: 1 $ |
|---|
| 12 | * $brcm_Date: 6/26/07 2:39p $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * Single thread asynchronous I/O |
|---|
| 17 | * |
|---|
| 18 | * Revision History: |
|---|
| 19 | * |
|---|
| 20 | * $brcm_Log: /BSEAV/lib/bfile/bfile_async.c $ |
|---|
| 21 | * |
|---|
| 22 | * 1 6/26/07 2:39p vsilyaev |
|---|
| 23 | * PR 31887: single thread asynchronous I/O |
|---|
| 24 | * |
|---|
| 25 | *******************************************************************************/ |
|---|
| 26 | #include "bstd.h" |
|---|
| 27 | #include "bfile_async.h" |
|---|
| 28 | #include "blst_queue.h" |
|---|
| 29 | |
|---|
| 30 | BDBG_MODULE(bfile_async); |
|---|
| 31 | |
|---|
| 32 | typedef struct b_file_async_item { |
|---|
| 33 | BLST_Q_ENTRY(b_file_async_item) queue; |
|---|
| 34 | void *buf; /* copy of buf */ |
|---|
| 35 | size_t length; |
|---|
| 36 | void *cntx; |
|---|
| 37 | bool write; |
|---|
| 38 | void (*cont)(void *cntx, ssize_t size); |
|---|
| 39 | union { |
|---|
| 40 | bfile_io_read_t read; |
|---|
| 41 | bfile_io_write_t write; |
|---|
| 42 | } fd; |
|---|
| 43 | } b_file_async_item; |
|---|
| 44 | |
|---|
| 45 | BLST_Q_HEAD(b_file_async_queue, b_file_async_item); |
|---|
| 46 | |
|---|
| 47 | static struct { |
|---|
| 48 | struct b_file_async_queue queue; |
|---|
| 49 | struct b_file_async_queue free; |
|---|
| 50 | b_file_async_item items[30]; |
|---|
| 51 | } b_file_async; |
|---|
| 52 | |
|---|
| 53 | void |
|---|
| 54 | bfile_async_init(void) |
|---|
| 55 | { |
|---|
| 56 | unsigned i; |
|---|
| 57 | |
|---|
| 58 | BLST_Q_INIT(&b_file_async.queue); |
|---|
| 59 | BLST_Q_INIT(&b_file_async.free); |
|---|
| 60 | for(i=0;i<sizeof(b_file_async.items)/sizeof(b_file_async.items[0]);i++) { |
|---|
| 61 | BLST_Q_INSERT_TAIL(&b_file_async.free, &b_file_async.items[i], queue); |
|---|
| 62 | } |
|---|
| 63 | return; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | void |
|---|
| 67 | bfile_async_shutdown(void) |
|---|
| 68 | { |
|---|
| 69 | return; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | void |
|---|
| 73 | bfile_async_write(void *sync_cnxt, bfile_io_write_t fd, const void *buf, size_t length, void (*cont)(void *, ssize_t ), void *cntx) |
|---|
| 74 | { |
|---|
| 75 | b_file_async_item *item; |
|---|
| 76 | BSTD_UNUSED(sync_cnxt); |
|---|
| 77 | item = BLST_Q_FIRST(&b_file_async.free); |
|---|
| 78 | if(item){ |
|---|
| 79 | BLST_Q_REMOVE_HEAD(&b_file_async.free, queue); |
|---|
| 80 | item->buf = (void *)buf; |
|---|
| 81 | item->length = length; |
|---|
| 82 | item->cntx = cntx; |
|---|
| 83 | item->cont = cont; |
|---|
| 84 | item->write = true; |
|---|
| 85 | item->fd.write = fd; |
|---|
| 86 | BLST_Q_INSERT_TAIL(&b_file_async.queue, item, queue); |
|---|
| 87 | return; |
|---|
| 88 | } |
|---|
| 89 | BDBG_ERR(("bfile_async_write: No free I/O entries avaliable")); |
|---|
| 90 | cont(cntx,-1); |
|---|
| 91 | return; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | void |
|---|
| 95 | bfile_async_read(void *sync_cnxt, bfile_io_read_t fd, void *buf, size_t length, void (*cont)(void *, ssize_t ), void *cntx) |
|---|
| 96 | { |
|---|
| 97 | b_file_async_item *item; |
|---|
| 98 | BSTD_UNUSED(sync_cnxt); |
|---|
| 99 | item = BLST_Q_FIRST(&b_file_async.free); |
|---|
| 100 | if(item){ |
|---|
| 101 | BLST_Q_REMOVE_HEAD(&b_file_async.free, queue); |
|---|
| 102 | item->buf = buf; |
|---|
| 103 | item->length = length; |
|---|
| 104 | item->cntx = cntx; |
|---|
| 105 | item->cont = cont; |
|---|
| 106 | item->write = false; |
|---|
| 107 | item->fd.read = fd; |
|---|
| 108 | BLST_Q_INSERT_TAIL(&b_file_async.queue, item, queue); |
|---|
| 109 | return; |
|---|
| 110 | } |
|---|
| 111 | BDBG_ERR(("bfile_async_write: No free I/O entries avaliable")); |
|---|
| 112 | cont(cntx,-1); |
|---|
| 113 | return; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | void |
|---|
| 117 | bfile_async_process(void) |
|---|
| 118 | { |
|---|
| 119 | b_file_async_item *item; |
|---|
| 120 | |
|---|
| 121 | while( NULL!=(item = BLST_Q_FIRST(&b_file_async.queue))) { |
|---|
| 122 | ssize_t rc; |
|---|
| 123 | if(item->write) { |
|---|
| 124 | rc = item->fd.write->write(item->fd.write, item->buf, item->length); |
|---|
| 125 | } else { |
|---|
| 126 | rc = item->fd.read->read(item->fd.read, item->buf, item->length); |
|---|
| 127 | } |
|---|
| 128 | BLST_Q_REMOVE_HEAD(&b_file_async.queue, queue); |
|---|
| 129 | BLST_Q_INSERT_HEAD(&b_file_async.free, item, queue); |
|---|
| 130 | item->cont(item->cntx, rc); |
|---|
| 131 | } |
|---|
| 132 | return; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | |
|---|