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. |
---|
39 | typedef 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 | |
---|
60 | DLLEXPORT int libtrace_ringbuffer_init(libtrace_ringbuffer_t * rb, size_t size, int mode); |
---|
61 | DLLEXPORT void libtrace_zero_ringbuffer(libtrace_ringbuffer_t * rb); |
---|
62 | DLLEXPORT void libtrace_ringbuffer_destroy(libtrace_ringbuffer_t * rb); |
---|
63 | DLLEXPORT int libtrace_ringbuffer_is_empty(const libtrace_ringbuffer_t * rb); |
---|
64 | DLLEXPORT int libtrace_ringbuffer_is_full(const libtrace_ringbuffer_t * rb); |
---|
65 | |
---|
66 | DLLEXPORT void libtrace_ringbuffer_write(libtrace_ringbuffer_t * rb, void* value); |
---|
67 | DLLEXPORT int libtrace_ringbuffer_try_write(libtrace_ringbuffer_t * rb, void* value); |
---|
68 | DLLEXPORT void libtrace_ringbuffer_swrite(libtrace_ringbuffer_t * rb, void* value); |
---|
69 | DLLEXPORT int libtrace_ringbuffer_try_swrite(libtrace_ringbuffer_t * rb, void* value); |
---|
70 | DLLEXPORT int libtrace_ringbuffer_try_swrite_bl(libtrace_ringbuffer_t * rb, void* value); |
---|
71 | |
---|
72 | DLLEXPORT void* libtrace_ringbuffer_read(libtrace_ringbuffer_t *rb) ; |
---|
73 | DLLEXPORT int libtrace_ringbuffer_try_read(libtrace_ringbuffer_t *rb, void ** value); |
---|
74 | DLLEXPORT void * libtrace_ringbuffer_sread(libtrace_ringbuffer_t *rb); |
---|
75 | DLLEXPORT int libtrace_ringbuffer_try_sread(libtrace_ringbuffer_t *rb, void ** value); |
---|
76 | DLLEXPORT int libtrace_ringbuffer_try_sread_bl(libtrace_ringbuffer_t *rb, void ** value); |
---|
77 | |
---|
78 | |
---|
79 | |
---|
80 | DLLEXPORT size_t libtrace_ringbuffer_write_bulk(libtrace_ringbuffer_t *rb, void *values[], size_t nb_buffers, size_t min_nb_buffers); |
---|
81 | DLLEXPORT size_t libtrace_ringbuffer_read_bulk(libtrace_ringbuffer_t *rb, void *values[], size_t nb_buffers, size_t min_nb_buffers); |
---|
82 | DLLEXPORT size_t libtrace_ringbuffer_sread_bulk(libtrace_ringbuffer_t *rb, void *values[], size_t nb_buffers, size_t min_nb_buffers); |
---|
83 | DLLEXPORT size_t libtrace_ringbuffer_swrite_bulk(libtrace_ringbuffer_t *rb, void *values[], size_t nb_buffers, size_t min_nb_buffers); |
---|
84 | |
---|
85 | #endif |
---|