aboutsummaryrefslogtreecommitdiffstats
path: root/libparc/parc/concurrent/parc_RingBuffer_NxM.c
blob: 15ec849b18e47956b740e162a3bda870f5db94c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * A thread-safe fixed size ring buffer.
 *
 * The multiple producer, multiple consumer version uses a pthread mutex around a NxM ring buffer.
 *
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
#include <string.h>

#include <parc/algol/parc_Memory.h>
#include <parc/algol/parc_Object.h>
#include <LongBow/runtime.h>

#include <parc/concurrent/parc_RingBuffer_1x1.h>
#include <parc/concurrent/parc_RingBuffer_NxM.h>

struct parc_ringbuffer_NxM {
    PARCRingBuffer1x1 *onebyone;

    // This protectes the overall data structure for Acquire and Release
    pthread_mutex_t allocation_mutex;

    pthread_mutex_t writer_mutex;
    pthread_mutex_t reader_mutex;

    RingBufferEntryDestroyer *destroyer;
};

/*
 * Attemps a lock and returns false if we cannot get it.
 *
 * @endcode
 */
static bool
_lock(pthread_mutex_t *mutex)
{
    int failure = pthread_mutex_lock(mutex);
    assertFalse(failure, "Error locking mutex: (%d) %s\n", errno, strerror(errno));
    return true;
}

static bool
_unlock(pthread_mutex_t *mutex)
{
    int failure = pthread_mutex_unlock(mutex);
    assertFalse(failure, "Error unlocking mutex: (%d) %s\n", errno, strerror(errno));
    return true;
}

static void
_destroy(PARCRingBufferNxM **ringptr)
{
    PARCRingBufferNxM *ring = *ringptr;

    if (ring->destroyer) {
        void *ptr = NULL;
        while (parcRingBufferNxM_Get(ring, &ptr)) {
            ring->destroyer(&ptr);
        }
    }
    parcRingBuffer1x1_Release(&ring->onebyone);
}


parcObject_ExtendPARCObject(PARCRingBufferNxM, _destroy, NULL, NULL, NULL, NULL, NULL, NULL);

static PARCRingBufferNxM *
_create(uint32_t elements, RingBufferEntryDestroyer *destroyer)
{
    PARCRingBufferNxM *ring = parcObject_CreateInstance(PARCRingBufferNxM);
    assertNotNull(ring, "parcObject_Create returned NULL");

    ring->onebyone = parcRingBuffer1x1_Create(elements, destroyer);
    ring->destroyer = destroyer;
    pthread_mutex_init(&ring->allocation_mutex, NULL);
    pthread_mutex_init(&ring->writer_mutex, NULL);
    pthread_mutex_init(&ring->reader_mutex, NULL);
    return ring;
}

PARCRingBufferNxM *
parcRingBufferNxM_Create(uint32_t elements, RingBufferEntryDestroyer *destroyer)
{
    return _create(elements, destroyer);
}

PARCRingBufferNxM *
parcRingBufferNxM_Acquire(PARCRingBufferNxM *ring)
{
    PARCRingBufferNxM *acquired;

    _lock(&ring->allocation_mutex);
    acquired = parcObject_Acquire(ring);
    _unlock(&ring->allocation_mutex);

    return acquired;
}

void
parcRingBufferNxM_Release(PARCRingBufferNxM **ringPtr)
{
    PARCRingBufferNxM *ring = *ringPtr;
    _lock(&ring->allocation_mutex);
    parcObject_Release((void **) ringPtr);
    _unlock(&ring->allocation_mutex);
}

/**
 * Put is protected by the writer mutex.  This means that the tail mutex could
 * actually increase while this is happening. That's ok.  Increasing the tail
 * just means there is _more_ room in the ring.  We only modify writer_head.
 */
bool
parcRingBufferNxM_Put(PARCRingBufferNxM *ring, void *data)
{
    // **** LOCK
    _lock(&ring->writer_mutex);
    bool success = parcRingBuffer1x1_Put(ring->onebyone, data);
    // **** UNLOCK
    _unlock(&ring->writer_mutex);
    return success;
}

bool
parcRingBufferNxM_Get(PARCRingBufferNxM *ring, void **outputDataPtr)
{
    // **** LOCK
    _lock(&ring->reader_mutex);
    bool success = parcRingBuffer1x1_Get(ring->onebyone, outputDataPtr);
    // **** UNLOCK
    _unlock(&ring->reader_mutex);
    return success;
}

uint32_t
parcRingBufferNxM_Remaining(PARCRingBufferNxM *ring)
{
    _lock(&ring->writer_mutex);
    _lock(&ring->reader_mutex);

    uint32_t remaining = parcRingBuffer1x1_Remaining(ring->onebyone);

    _unlock(&ring->reader_mutex);
    _unlock(&ring->writer_mutex);

    return remaining;
}