Skip to content
Snippets Groups Projects
Commit 018d6187 authored by rswindell's avatar rswindell
Browse files

If RINGBUF_MUTEX is defined, ringbuffers are mutex-protected (thread-safe).

Experimenting with this in sbbs to see if there are any race conditions between
threads that manipulate the I/O buffers (e.g. Ctrl-C in input_thread clears
output buffer).
parent 044003ad
No related branches found
No related tags found
No related merge requests found
...@@ -93,6 +93,9 @@ int RINGBUFCALL RingBufInit( RingBuf* rb, DWORD size ...@@ -93,6 +93,9 @@ int RINGBUFCALL RingBufInit( RingBuf* rb, DWORD size
rb->pHead=rb->pTail=rb->pStart; rb->pHead=rb->pTail=rb->pStart;
rb->pEnd=rb->pStart+size; rb->pEnd=rb->pStart+size;
rb->size=size; rb->size=size;
#ifdef RINGBUF_MUTEX
pthread_mutex_init(&rb->mutex,NULL);
#endif
return(0); return(0);
} }
...@@ -100,6 +103,9 @@ void RINGBUFCALL RingBufDispose( RingBuf* rb) ...@@ -100,6 +103,9 @@ void RINGBUFCALL RingBufDispose( RingBuf* rb)
{ {
if(rb->pStart!=NULL) if(rb->pStart!=NULL)
os_free(rb->pStart); os_free(rb->pStart);
#ifdef RINGBUF_MUTEX
pthread_mutex_destroy(&rb->mutex);
#endif
memset(rb,0,sizeof(RingBuf)); memset(rb,0,sizeof(RingBuf));
} }
...@@ -107,6 +113,10 @@ DWORD RINGBUFCALL RingBufFull( RingBuf* rb ) ...@@ -107,6 +113,10 @@ DWORD RINGBUFCALL RingBufFull( RingBuf* rb )
{ {
DWORD head,tail,retval; DWORD head,tail,retval;
#ifdef RINGBUF_MUTEX
pthread_mutex_lock(&rb->mutex);
#endif
head = (DWORD) rb->pHead; head = (DWORD) rb->pHead;
tail = (DWORD) rb->pTail; tail = (DWORD) rb->pTail;
...@@ -115,6 +125,10 @@ DWORD RINGBUFCALL RingBufFull( RingBuf* rb ) ...@@ -115,6 +125,10 @@ DWORD RINGBUFCALL RingBufFull( RingBuf* rb )
else else
retval = rb->size - (tail - head); retval = rb->size - (tail - head);
#ifdef RINGBUF_MUTEX
pthread_mutex_unlock(&rb->mutex);
#endif
return(retval); return(retval);
} }
...@@ -134,6 +148,10 @@ DWORD RINGBUFCALL RingBufWrite( RingBuf* rb, BYTE* src, DWORD cnt ) ...@@ -134,6 +148,10 @@ DWORD RINGBUFCALL RingBufWrite( RingBuf* rb, BYTE* src, DWORD cnt )
if(rb->pStart==NULL) if(rb->pStart==NULL)
return(0); return(0);
#ifdef RINGBUF_MUTEX
pthread_mutex_lock(&rb->mutex);
#endif
/* allowed to write at pEnd */ /* allowed to write at pEnd */
max = (((DWORD) rb->pEnd) - ((DWORD) rb->pHead)) + 1; max = (((DWORD) rb->pEnd) - ((DWORD) rb->pHead)) + 1;
...@@ -164,6 +182,10 @@ DWORD RINGBUFCALL RingBufWrite( RingBuf* rb, BYTE* src, DWORD cnt ) ...@@ -164,6 +182,10 @@ DWORD RINGBUFCALL RingBufWrite( RingBuf* rb, BYTE* src, DWORD cnt )
if(rb->pHead > rb->pEnd) if(rb->pHead > rb->pEnd)
rb->pHead = rb->pStart; rb->pHead = rb->pStart;
#ifdef RINGBUF_MUTEX
pthread_mutex_unlock(&rb->mutex);
#endif
return(cnt); return(cnt);
} }
...@@ -176,6 +198,10 @@ DWORD RINGBUFCALL RingBufRead( RingBuf* rb, BYTE* dst, DWORD cnt ) ...@@ -176,6 +198,10 @@ DWORD RINGBUFCALL RingBufRead( RingBuf* rb, BYTE* dst, DWORD cnt )
if( len == 0 ) if( len == 0 )
return(0); return(0);
#ifdef RINGBUF_MUTEX
pthread_mutex_lock(&rb->mutex);
#endif
if( len < cnt ) if( len < cnt )
cnt = len; cnt = len;
...@@ -207,6 +233,10 @@ DWORD RINGBUFCALL RingBufRead( RingBuf* rb, BYTE* dst, DWORD cnt ) ...@@ -207,6 +233,10 @@ DWORD RINGBUFCALL RingBufRead( RingBuf* rb, BYTE* dst, DWORD cnt )
if(rb->pTail > rb->pEnd) if(rb->pTail > rb->pEnd)
rb->pTail = rb->pStart; rb->pTail = rb->pStart;
#ifdef RINGBUF_MUTEX
pthread_mutex_unlock(&rb->mutex);
#endif
return(cnt); return(cnt);
} }
...@@ -218,6 +248,10 @@ DWORD RINGBUFCALL RingBufPeek( RingBuf* rb, BYTE* dst, DWORD cnt) ...@@ -218,6 +248,10 @@ DWORD RINGBUFCALL RingBufPeek( RingBuf* rb, BYTE* dst, DWORD cnt)
if( len == 0 ) if( len == 0 )
return(0); return(0);
#ifdef RINGBUF_MUTEX
pthread_mutex_lock(&rb->mutex);
#endif
if( len < cnt ) if( len < cnt )
cnt = len; cnt = len;
...@@ -239,13 +273,23 @@ DWORD RINGBUFCALL RingBufPeek( RingBuf* rb, BYTE* dst, DWORD cnt) ...@@ -239,13 +273,23 @@ DWORD RINGBUFCALL RingBufPeek( RingBuf* rb, BYTE* dst, DWORD cnt)
rb_memcpy( dst, rb->pStart, remain ); rb_memcpy( dst, rb->pStart, remain );
} }
#ifdef RINGBUF_MUTEX
pthread_mutex_unlock(&rb->mutex);
#endif
return(cnt); return(cnt);
} }
/* Reset head and tail pointers */ /* Reset head and tail pointers */
void RINGBUFCALL RingBufReInit(RingBuf* rb) void RINGBUFCALL RingBufReInit(RingBuf* rb)
{ {
#ifdef RINGBUF_MUTEX
pthread_mutex_lock(&rb->mutex);
#endif
rb->pHead = rb->pTail = rb->pStart; rb->pHead = rb->pTail = rb->pStart;
#ifdef RINGBUF_MUTEX
pthread_mutex_unlock(&rb->mutex);
#endif
} }
/* End of RINGBUF.C */ /* End of RINGBUF.C */
...@@ -42,6 +42,10 @@ ...@@ -42,6 +42,10 @@
#ifndef _RINGBUF_H_ #ifndef _RINGBUF_H_
#define _RINGBUF_H_ #define _RINGBUF_H_
#ifdef RINGBUF_MUTEX
#include "threadwrap.h"
#endif
#ifndef DWORD #ifndef DWORD
#define DWORD unsigned long #define DWORD unsigned long
#endif #endif
...@@ -73,6 +77,9 @@ typedef struct { ...@@ -73,6 +77,9 @@ typedef struct {
BYTE* pTail; /* next byte to be consumed */ BYTE* pTail; /* next byte to be consumed */
BYTE* pEnd; /* end of the buffer, used for wrap around */ BYTE* pEnd; /* end of the buffer, used for wrap around */
DWORD size; DWORD size;
#ifdef RINGBUF_MUTEX
pthread_mutex_t mutex;
#endif
} RingBuf; } RingBuf;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment