libc: io: implement file io buffering and internal locking for concurrency
This commit is contained in:
+12
-16
@@ -3,29 +3,25 @@
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int fgetc(struct __opaque_file *stream)
|
||||
int __fgetc(struct __opaque_file *stream)
|
||||
{
|
||||
if (stream->f_flags & (FILE_EOF | FILE_ERR)) {
|
||||
return EOF;
|
||||
}
|
||||
|
||||
size_t available = __libc_file_available(stream);
|
||||
if (available == 0) {
|
||||
int err = __libc_file_refill(stream);
|
||||
if (err != SUCCESS) {
|
||||
__set_errno(err);
|
||||
stream->f_flags |= FILE_ERR;
|
||||
return EOF;
|
||||
}
|
||||
|
||||
available = __libc_file_available(stream);
|
||||
}
|
||||
|
||||
if (available == 0) {
|
||||
stream->f_flags |= FILE_EOF;
|
||||
char c = 0;
|
||||
int ret = __libc_file_read(stream, &c, 1);
|
||||
if (ret < 1) {
|
||||
return EOF;
|
||||
}
|
||||
|
||||
char c = stream->f_buf[stream->f_buf_readptr++];
|
||||
return c;
|
||||
}
|
||||
|
||||
int fgetc(struct __opaque_file *stream)
|
||||
{
|
||||
__libc_file_lock(stream);
|
||||
int ret = __fgetc(stream);
|
||||
__libc_file_unlock(stream);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user