Skip to content

Commit

Permalink
Add lock feature for ostream
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-Mirghasemi committed Jul 20, 2022
1 parent 4b29031 commit f89f1ca
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
26 changes: 25 additions & 1 deletion Src/OutputStream.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,28 @@ Stream_LenType OStream_space(OStream* stream) {
Stream_LenType OStream_outgoingBytes(OStream* stream) {
return stream->OutgoingBytes;
}

#if OSTREAM_LOCK
/**
* @brief lock output stream for fixed write
*
* @param stream
* @param lock
* @return Stream_Result
*/
Stream_Result OStream_lock(OStream* stream, OStream* lock, Stream_LenType len) {
Stream_Result res;
if ((res = Stream_lockWrite(&stream->Buffer, &lock->Buffer, len) == Stream_Ok)) {
memcpy(&lock->transmit, &stream->transmit, sizeof(OStream) - sizeof(Stream));
}
return res;
}
/**
* @brief unlock output stream
*
* @param stream
* @param lock
*/
void OStream_unlock(OStream* stream, OStream* lock) {
Stream_unlockWrite(&stream->Buffer, &lock->Buffer);
}
#endif // OSTREAM_LOCK
9 changes: 9 additions & 0 deletions Src/OutputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ extern "C" {
* @brief enable checkTransmit function
*/
#define OSTREAM_CHECK_TRANSMIT 1
/**
* @brief enable lock write feature
*/
#define OSTREAM_LOCK STREAM_WRITE_LOCK

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

Expand Down Expand Up @@ -89,6 +93,11 @@ Stream_LenType OStream_outgoingBytes(OStream* stream);
void OStream_setCheckTransmit(OStream* stream, OStream_CheckTransmitFn fn);
#endif // OSTREAM_CHECK_TRANSMIT

#if OSTREAM_LOCK
Stream_Result OStream_lock(OStream* stream, OStream* lock, Stream_LenType len);
void OStream_unlock(OStream* stream, OStream* lock);
#endif // OSTREAM_LOCK

/**
* @brief return number of bytes waiting for transmit
*/
Expand Down

0 comments on commit f89f1ca

Please sign in to comment.