#include #include #include #include #include #include FILE* pipe(int size) { fdPIPE* stream; stream = (fdPIPE*)fdev_create(sizeof(PIPE),(fdevput)pipe_put,(fdevget)pipe_get,NULL,NULL); if (!stream) { return NULL; }; stream->pipe.size = size; stream->pipe.buffer = malloc(size); if (!stream->pipe.buffer) { free(stream); return NULL; }; return (FILE*)stream; }; int pipe_get(FILE* stream) { ATOMIC int c; if (pipe_empty( stream ) ) { if (fdPIPE(stream)->pipe.options & IO_BLOCK_RD) { fdPIPE(stream)->pipe.wait_get = current_thread(); thread_sleep(current_thread()); fdPIPE(stream)->pipe.wait_get = NULL; } else return -1; }; c = fdPIPE(stream)->pipe.buffer[ fdPIPE(stream)->pipe.read++ ]; if (fdPIPE(stream)->pipe.read == fdPIPE(stream)->pipe.size) fdPIPE(stream)->pipe.read = 0; if (fdPIPE(stream)->pipe.wait_put) thread_wake( fdPIPE(stream)->pipe.wait_put ); return c; }; int pipe_put(char c,FILE* stream) { ATOMIC if (pipe_full( stream ) ) { if (fdPIPE(stream)->pipe.options & IO_BLOCK_WR) { fdPIPE(stream)->pipe.wait_put = current_thread(); thread_sleep(current_thread()); fdPIPE(stream)->pipe.wait_put = NULL; } else return -1; }; fdPIPE(stream)->pipe.buffer[ fdPIPE(stream)->pipe.write++ ] = c; if (fdPIPE(stream)->pipe.write == fdPIPE(stream)->pipe.size) fdPIPE(stream)->pipe.write = 0; if (fdPIPE(stream)->pipe.wait_get) thread_wake( fdPIPE(stream)->pipe.wait_get ); return 0; };