source: lib/data-struct/ring_buffer.h

cachetimestampsdevelopetsiliverc-4.0.3rc-4.0.4ringdecrementfixringperformance
Last change on this file was 49969f2, checked in by Shane Alcock <salcock@…>, 4 years ago

pthread_spinlock.h is now installed in the libtrace/ directory.

Fixes issues when users try to include libtrace/message_queue.h
or libtrace/ring_buffer.h in their own programs.

Also removed relative directory references in the aforementioned
header files.

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/*
2 *
3 * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand.
4 * All rights reserved.
5 *
6 * This file is part of libtrace.
7 *
8 * This code has been developed by the University of Waikato WAND
9 * research group. For further information please see http://www.wand.net.nz/
10 *
11 * libtrace is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * libtrace is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 *
24 *
25 */
26#include <pthread.h>
27#include <semaphore.h>
28#include "libtrace.h"
29#include "pthread_spinlock.h"
30
31#ifndef LIBTRACE_RINGBUFFER_H
32#define LIBTRACE_RINGBUFFER_H
33
34#define LIBTRACE_RINGBUFFER_BLOCKING 0
35#define LIBTRACE_RINGBUFFER_POLLING 1
36
37// All of start, elements and end must be accessed in the listed order
38// if LIBTRACE_RINGBUFFER_POLLING is to work.
39typedef struct libtrace_ringbuffer {
40        volatile size_t start;
41        size_t size;
42        int mode;
43        void *volatile*elements;
44        pthread_mutex_t wlock;
45        pthread_mutex_t rlock;
46        pthread_spinlock_t swlock;
47        pthread_spinlock_t srlock;
48        // We need to ensure that broadcasts dont get lost hence
49        // these locks below
50        // We avoid using semaphores since they don't allow
51        // multiple releases.
52        pthread_mutex_t empty_lock;
53        pthread_mutex_t full_lock;
54        pthread_cond_t empty_cond; // Signal when empties are ready
55        pthread_cond_t full_cond; // Signal when fulls are ready
56        // Aim to get this on a separate cache line to start - important if spinning
57        volatile size_t end;
58} libtrace_ringbuffer_t;
59
60DLLEXPORT int libtrace_ringbuffer_init(libtrace_ringbuffer_t * rb, size_t size, int mode);
61DLLEXPORT void libtrace_zero_ringbuffer(libtrace_ringbuffer_t * rb);
62DLLEXPORT void libtrace_ringbuffer_destroy(libtrace_ringbuffer_t * rb);
63DLLEXPORT int libtrace_ringbuffer_is_empty(const libtrace_ringbuffer_t * rb);
64DLLEXPORT int libtrace_ringbuffer_is_full(const libtrace_ringbuffer_t * rb);
65
66DLLEXPORT void libtrace_ringbuffer_write(libtrace_ringbuffer_t * rb, void* value);
67DLLEXPORT int libtrace_ringbuffer_try_write(libtrace_ringbuffer_t * rb, void* value);
68DLLEXPORT void libtrace_ringbuffer_swrite(libtrace_ringbuffer_t * rb, void* value);
69DLLEXPORT int libtrace_ringbuffer_try_swrite(libtrace_ringbuffer_t * rb, void* value);
70DLLEXPORT int libtrace_ringbuffer_try_swrite_bl(libtrace_ringbuffer_t * rb, void* value);
71
72DLLEXPORT void* libtrace_ringbuffer_read(libtrace_ringbuffer_t *rb) ;
73DLLEXPORT int libtrace_ringbuffer_try_read(libtrace_ringbuffer_t *rb, void ** value);
74DLLEXPORT void * libtrace_ringbuffer_sread(libtrace_ringbuffer_t *rb);
75DLLEXPORT int libtrace_ringbuffer_try_sread(libtrace_ringbuffer_t *rb, void ** value);
76DLLEXPORT int libtrace_ringbuffer_try_sread_bl(libtrace_ringbuffer_t *rb, void ** value);
77
78
79
80DLLEXPORT size_t libtrace_ringbuffer_write_bulk(libtrace_ringbuffer_t *rb, void *values[], size_t nb_buffers, size_t min_nb_buffers);
81DLLEXPORT size_t libtrace_ringbuffer_read_bulk(libtrace_ringbuffer_t *rb, void *values[], size_t nb_buffers, size_t min_nb_buffers);
82DLLEXPORT size_t libtrace_ringbuffer_sread_bulk(libtrace_ringbuffer_t *rb, void *values[], size_t nb_buffers, size_t min_nb_buffers);
83DLLEXPORT size_t libtrace_ringbuffer_swrite_bulk(libtrace_ringbuffer_t *rb, void *values[], size_t nb_buffers, size_t min_nb_buffers);
84
85#endif
Note: See TracBrowser for help on using the repository browser.