-
Notifications
You must be signed in to change notification settings - Fork 0
/
jbuffer.h
66 lines (56 loc) · 1.85 KB
/
jbuffer.h
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
/**
* @file jbuffer.h
* @brief Bounded-buffer implementation to manage integer values, supporting
* multiple readers but only a single writer.
*
* The bbuffer module uses the sem module API to synchronize concurrent access
* of readers and writers to the bounded buffer.
*/
#ifndef JBUFFER_H
#define JBUFFER_H
#include <stdlib.h>
/** Opaque type of a bounded buffer. */
typedef struct BNDBUF BNDBUF;
/**
* @brief Creates a new bounded buffer.
*
* This function creates a new bounded buffer and all the required helper data
* structures, including semaphores for synchronization. If an error occurs
* during the initialization, the implementation frees all resources already
* allocated by then.
*
* @param size The number of integers that can be stored in the bounded buffer.
* @return Handle for the created bounded buffer, or @c NULL if an error
* occurred.
*/
BNDBUF *bbCreate(size_t size);
/**
* @brief Destroys a bounded buffer.
*
* All resources associated with the bounded buffer are released.
*
* @param bb Handle of the bounded buffer that shall be freed. If a @c NULL
* pointer is passed, the implementation does nothing.
*/
void bbDestroy(BNDBUF *bb);
/**
* @brief Adds an element to a bounded buffer.
*
* This function adds an element to a bounded buffer. If the buffer is full, the
* function blocks until an element has been removed from it.
*
* @param bb Handle of the bounded buffer.
* @param value Value that shall be added to the buffer.
*/
void bbPut(BNDBUF *bb, int value);
/**
* @brief Retrieves an element from a bounded buffer.
*
* This function removes an element from a bounded buffer. If the buffer is
* empty, the function blocks until an element has been added.
*
* @param bb Handle of the bounded buffer.
* @return The integer element.
*/
int bbGet(BNDBUF *bb);
#endif /* JBUFFER_H */