]> git.saurik.com Git - apple/security.git/blob - SecurityTests/clxutils/clAppUtils/ringBufferIo.h
Security-57336.1.9.tar.gz
[apple/security.git] / SecurityTests / clxutils / clAppUtils / ringBufferIo.h
1 /*
2 * Ring buffer I/O for sslThroughput test.
3 */
4
5 #ifndef _RING_BUFFER_IO_
6 #define _RING_BUFFER_IO_
7
8 #include <sys/types.h>
9 #include <pthread.h>
10 #include <MacTypes.h>
11 #include <Security/SecureTransport.h>
12
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16
17 /* Reasonable defaults for Ring Buffer params */
18 #define DEFAULT_NUM_RB_BUFS 16
19 #define DEFAULT_BUF_RB_SIZE 2048 /* in the ring buffers */
20
21
22 /* one element in a ring buffer */
23 typedef struct {
24 size_t validBytes; // bytes written and not yet consumed
25 size_t readOffset; // next byte to be read from this offset
26 size_t capacity; // mallocd size of buf
27 unsigned char *buf;
28 } RingElement;
29
30 /*
31 * A ring buffer shared between one writer thread and one reader thread.
32 * Per the DeMoney Theorem, we don't need to provide any locking between
33 * the two threads if we have the appropriate protocol, which is as follows:
34 *
35 * -- the RingElements at which the reader and writer are currently
36 * processing are indicated by readerDex and writerDex.
37 * -- the writer thread never advances writerDex to a RingElement
38 * currently in use by the reader thread.
39 * -- the reader thread can advance to a RingElement in use by
40 * the writer thread, but it can't read from that RingElement
41 * until the writer thread has advanced past that RingElement.
42 */
43 typedef struct {
44 size_t numElements;
45 RingElement *elements;
46 unsigned writerDex; // writer thread is working on this one
47 unsigned readerDex; // read thread is working on this one
48 const char *bufName; // e.g. serverToClient
49
50 /*
51 * Flag to emulate closing of socket. There's only one since the thread
52 * that sets this presumably will not be reading from or writing to
53 * this RingBuffer again; the "other" thread will detect this and abort
54 * as appropriate.
55 */
56 bool closed;
57 } RingBuffer;
58
59 /*
60 * A pair of RingBuffer ptrs suitable for use as the SSLConnectionRef
61 * for ringReadFunc() and ringWriteFunc().
62 */
63 typedef struct {
64 RingBuffer *rdBuf;
65 RingBuffer *wrtBuf;
66 } RingBuffers;
67
68 void ringBufSetup(
69 RingBuffer *ring,
70 const char *bufName,
71 size_t numElements,
72 size_t bufSize);
73
74 void ringBufferReset(
75 RingBuffer *ring);
76
77 /*
78 * The "I/O" callbacks for SecureTransport.
79 * The SSLConnectionRef is a RingBuffers *.
80 */
81 OSStatus ringReadFunc(
82 SSLConnectionRef connRef,
83 void *data,
84 size_t *dataLen); /* IN/OUT */
85 OSStatus ringWriteFunc(
86 SSLConnectionRef connRef,
87 const void *data,
88 size_t *dataLen); /* IN/OUT */
89
90 /* close both sides of a RingBuffers */
91 void ringBuffersClose(
92 RingBuffers *rbs);
93
94 /* to coordinate stdio from multi threads */
95 extern pthread_mutex_t printfMutex;
96
97 #ifdef __cplusplus
98 }
99 #endif
100
101 #endif /* _RING_BUFFER_IO_ */