Skip to content

Commit

Permalink
Add lock feature for istream
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-Mirghasemi committed Jul 20, 2022
1 parent f89f1ca commit 210d867
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Src/InputStream.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,28 @@ void IStream_setCheckReceive(IStream* stream, IStream_CheckReceiveFn fn) {
Stream_LenType IStream_incomingBytes(IStream* stream) {
return stream->IncomingBytes;
}
#if ISTREAM_LOCK
/**
* @brief lock input stream for fixed write
*
* @param stream
* @param lock
* @return Stream_Result
*/
Stream_Result IStream_lock(IStream* stream, IStream* lock, Stream_LenType len) {
Stream_Result res;
if ((res = Stream_lockRead(&stream->Buffer, &lock->Buffer, len) == Stream_Ok)) {
memcpy(&lock->receive, &stream->receive, sizeof(IStream) - sizeof(Stream));
}
return res;
}
/**
* @brief unlock input stream for reading
*
* @param stream
* @param lock
*/
void IStream_unlock(IStream* stream, IStream* lock) {
Stream_unlockRead(&stream->Buffer, &lock->Buffer);
}
#endif // ISTREAM_LOCK
10 changes: 10 additions & 0 deletions Src/InputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ extern "C" {
* @brief enable checkTransmit function
*/
#define ISTREAM_CHECK_RECEIVE 1
/**
* @brief enable lock read feature
*/
#define ISTREAM_LOCK STREAM_READ_LOCK


/************************************************************************/

Expand Down Expand Up @@ -90,6 +95,11 @@ Stream_LenType IStream_incomingBytes(IStream* stream);
void IStream_setCheckReceive(IStream* stream, IStream_CheckReceiveFn fn);
#endif // ISTREAM_CHECK_RECEIVE

#if ISTREAM_LOCK
Stream_Result IStream_lock(IStream* stream, IStream* lock, Stream_LenType len);
void IStream_unlock(IStream* stream, IStream* lock);
#endif // ISTREAM_LOCK

#define IStream_getDataPtr(STREAM) Stream_getWritePtr(&((STREAM)->Buffer))

#define IStream_clear(STREAM) Stream_clear(&((STREAM)->Buffer))
Expand Down

0 comments on commit 210d867

Please sign in to comment.