unsigned int kfifo_out(struct kfifo *fifo, void *buf, unsigned int len) { len = kfifo_out_peek(fifo, buf, len); fifo->out += len; return len; }
static int __init testfunc(void) { char buf[100]; unsigned int i; unsigned int ret; struct { unsigned char buf[6]; } hello = { "hello" }; printk(KERN_INFO "record fifo test start\n"); kfifo_in(&test, &hello, sizeof(hello)); /* show the size of the next record in the fifo */ printk(KERN_INFO "fifo peek len: %u\n", kfifo_peek_len(&test)); /* put in variable length data */ for (i = 0; i < 10; i++) { memset(buf, 'a' + i, i + 1); kfifo_in(&test, buf, i + 1); } printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test)); /* show the first record without removing from the fifo */ ret = kfifo_out_peek(&test, buf, sizeof(buf)); if (ret) printk(KERN_INFO "%.*s\n", ret, buf); /* print out all records in the fifo */ while (!kfifo_is_empty(&test)) { ret = kfifo_out(&test, buf, sizeof(buf)); printk(KERN_INFO "%.*s\n", ret, buf); } return 0; }
static ssize_t ssp_sensorhub_read(struct file *file, char __user *buf, size_t count, loff_t *pos) { struct ssp_sensorhub_data *hub_data = container_of(file->private_data, struct ssp_sensorhub_data, sensorhub_device); struct sensorhub_event *event; int retries = MAX_DATA_COPY_TRY; int length = 0; int ret = 0; spin_lock_bh(&hub_data->sensorhub_lock); if (unlikely(kfifo_is_empty(&hub_data->fifo))) { sensorhub_info("no library data"); goto err; } /* first in first out */ ret = kfifo_out_peek(&hub_data->fifo, &event, sizeof(void *)); if (unlikely(!ret)) { sensorhub_err("kfifo out peek err(%d)", ret); ret = EIO; goto err; } length = event->library_length; while (retries--) { ret = copy_to_user(buf, event->library_data, event->library_length); if (likely(!ret)) break; } if (unlikely(ret)) { sensorhub_err("read library data err(%d)", ret); goto err; } ssp_sensorhub_log(__func__, event->library_data, event->library_length); /* remove first event from the list */ ret = kfifo_out(&hub_data->fifo, &event, sizeof(void *)); if (unlikely(ret != sizeof(void *))) { sensorhub_err("kfifo out err(%d)", ret); ret = EIO; goto err; } complete(&hub_data->read_done); spin_unlock_bh(&hub_data->sensorhub_lock); return length; err: spin_unlock_bh(&hub_data->sensorhub_lock); return ret ? -ret : 0; }
static void print_fifo(struct kfifo *fifo) { int i = 1; int vals[10]; int l = kfifo_len(fifo); int l1; if (l < ARRAY_SIZE(vals)) l = ARRAY_SIZE(vals); printk("Copying %d elements\n", l); /* take a snapshot of the entire queue */ l1 = kfifo_out_peek(fifo, &vals[0], sizeof(vals[0])); printk("peek returned count of %d\n", l1); while (l--) { printk("item[%d]: %d\n", i, vals[i]); i++; } }
static int has_chunk(demux_t *dm, uint32_t thread_id, demux_chunk_t *dchunk) { uint32_t proc_id; int ret; demux_ent_t *ent; uint64_t curr_ticket; curr_ticket = get_current_ticket(dm, thread_id); for(proc_id = 0; proc_id < NUM_CHUNK_PROC; proc_id++) { ent = dm->entries + proc_id; if(kfifo_len(&ent->fifo) >= sizeof(demux_chunk_t)) { // leave the chunk in place on the queue to make sure // that no other threads execute on behalf of this // processor until we are done ret = kfifo_out_peek(&ent->fifo, dchunk, sizeof(demux_chunk_t)); BUG_ON(ret != sizeof(demux_chunk_t)); BUG_ON(dchunk->chunk.processor_id != proc_id); if((thread_id == dchunk->chunk.thread_id) && (curr_ticket == dchunk->ticket)) return 1; } } return 0; }