| 1 | /* | 
|---|
| 2 |  * pipe.h - single writer, single reader pipe specification.   | 
|---|
| 3 |  * | 
|---|
| 4 |  * Author     Alain Greiner (2016,2017,2018,2019,2020) | 
|---|
| 5 |  * | 
|---|
| 6 |  * Copyright (c) UPMC Sorbonne Universites | 
|---|
| 7 |  * | 
|---|
| 8 |  * This file is part of ALMOS-MKH. | 
|---|
| 9 |  * | 
|---|
| 10 |  * ALMOS-MKH is free software; you can redistribute it and/or modify it | 
|---|
| 11 |  * under the terms of the GNU General Public License as published by | 
|---|
| 12 |  * the Free Software Foundation; version 2.0 of the License. | 
|---|
| 13 |  * | 
|---|
| 14 |  * ALMOS-MKH is distributed in the hope that it will be useful, but | 
|---|
| 15 |  * WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 16 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 17 |  * General Public License for more details. | 
|---|
| 18 |  * | 
|---|
| 19 |  * You should have received a copy of the GNU General Public License | 
|---|
| 20 |  * along with ALMOS-MKH; if not, write to the Free Software Foundation, | 
|---|
| 21 |  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | 
|---|
| 22 |  */ | 
|---|
| 23 |  | 
|---|
| 24 |  | 
|---|
| 25 | #ifndef _PIPE_H_ | 
|---|
| 26 | #define _PIPE_H_ | 
|---|
| 27 |  | 
|---|
| 28 | #include <hal_kernel_types.h> | 
|---|
| 29 |  | 
|---|
| 30 | /******************************************************************************************* | 
|---|
| 31 |  * This structure defines a generic, single writer, single reader, inter-processes | 
|---|
| 32 |  * communication channel, used to implement both the POSIX named fifos, and the POSIX | 
|---|
| 33 |  * anonymous pipes. | 
|---|
| 34 |  * | 
|---|
| 35 |  * It uses the (lower level) remote_buf_t object and access functions, and can be | 
|---|
| 36 |  * allocated in any cluster, and accessed by threads running in any cluster. | 
|---|
| 37 |  * The default buffer size is defined by the CONFIG_PIPE_BUF_SIZE parameter. | 
|---|
| 38 |  * It it accessed by the user processes through the POSIX read/write syscalls, that call | 
|---|
| 39 |  * the pipe_move_user() to move data between the kernel remote buf and the user buffer. | 
|---|
| 40 |  * These access functions are blocking when the kernel buffer is full (for a write), | 
|---|
| 41 |  * or empty (for a read), and use a desheduling policy, blocking on THREAD_ BLOCKED_IO.  | 
|---|
| 42 |  ******************************************************************************************/ | 
|---|
| 43 |   | 
|---|
| 44 | typedef struct pipe_s | 
|---|
| 45 | { | 
|---|
| 46 |     remote_buf_t * buffer;       /*! local pointer on the remote_buf_t                    */ | 
|---|
| 47 |     xptr_t         writer_xp;    /*! extended pointer on the writer thread                */ | 
|---|
| 48 |     xptr_t         reader_xp;    /*! extended pointer on the reader thread                */ | 
|---|
| 49 | } | 
|---|
| 50 | pipe_t; | 
|---|
| 51 |  | 
|---|
| 52 | /******************************************************************************************* | 
|---|
| 53 |  * This function allocates memory for the pipe descriptor, for the remote_buf descriptor, | 
|---|
| 54 |  * and for the associated data buffer, in the cluster defined by the <cxy> argument. | 
|---|
| 55 |  * The buffer size is defined by the <size> argument.  | 
|---|
| 56 |  * The "buffer" field is set in the pipe descriptor, but the "writer_xp" and "reader_xp" | 
|---|
| 57 |  * fields are not set by this function. | 
|---|
| 58 |  ******************************************************************************************* | 
|---|
| 59 |  * @ cxy        : target cluster identifier. | 
|---|
| 60 |  * @ size       : data buffer size (in bytes). | 
|---|
| 61 |  * @ return a local pointer on pipe_t if success /:w NULL if error. | 
|---|
| 62 |  ******************************************************************************************/ | 
|---|
| 63 | pipe_t * pipe_create( cxy_t     cxy, | 
|---|
| 64 |                       uint32_t  size ); | 
|---|
| 65 |  | 
|---|
| 66 | /******************************************************************************************* | 
|---|
| 67 |  * This function releases all memory allocated fo a pipe: pipe descriptor, | 
|---|
| 68 |  * remote_buffer_t descriptor, and associated data buffer. | 
|---|
| 69 |  ******************************************************************************************* | 
|---|
| 70 |  * @ pipe_xp    : extended pointer on target pipe. | 
|---|
| 71 |  ******************************************************************************************/ | 
|---|
| 72 | void pipe_destroy( xptr_t pipe_xp ); | 
|---|
| 73 |  | 
|---|
| 74 | /******************************************************************************************* | 
|---|
| 75 |  * These two functions update the "writer_xp" or "reader_xp" field in the pipe descriptor. | 
|---|
| 76 |  * They are called when the user process open a file descriptor to access the pipe. | 
|---|
| 77 |  ******************************************************************************************* | 
|---|
| 78 |  * @ pipe_xp    : extended pointer on the target thread | 
|---|
| 79 |  * @ thread_xp  : extended pointer on the client (writer/reader) thread. | 
|---|
| 80 |  ******************************************************************************************/ | 
|---|
| 81 | void pipe_register_writer( xptr_t pipe_xp, | 
|---|
| 82 |                            xptr_t thread_xp ); | 
|---|
| 83 |  | 
|---|
| 84 | void pipe_register_reader( xptr_t pipe_xp, | 
|---|
| 85 |                            xptr_t thread_xp ); | 
|---|
| 86 |  | 
|---|
| 87 | /******************************************************************************************* | 
|---|
| 88 |  * This blocking function is called by the sys_read() and sys_write() functions. | 
|---|
| 89 |  * It moves up to <size> bytes between an user buffer, defined by the <u_buf> argument, | 
|---|
| 90 |  * and the kernel pipe, identified by the <file_xp> argument. | 
|---|
| 91 |  * The file descriptor is always located in the same cluster as the pipe descriptor.  | 
|---|
| 92 |  * The read/write direction is defined by the <to_buffer> argument.  | 
|---|
| 93 |  * It returns the number of bytes actually moved. | 
|---|
| 94 |  * It uses a desheduling policy, blocking on THREAD_ BLOCKED_IO.  | 
|---|
| 95 |  ******************************************************************************************* | 
|---|
| 96 |  * @ to_buffer : pipe -> buffer if true / buffer -> pipe if false.  | 
|---|
| 97 |  * @ file_xp   : extended pointer on the remote file descriptor. | 
|---|
| 98 |  * @ u_buf     : user space buffer (virtual address). | 
|---|
| 99 |  * @ size      : requested number of bytes to move. | 
|---|
| 100 |  * @ return number of bytes actually moved if success / -1 if error. | 
|---|
| 101 |  ******************************************************************************************/ | 
|---|
| 102 | int pipe_user_move( bool_t   to_buffer, | 
|---|
| 103 |                     xptr_t   file_xp, | 
|---|
| 104 |                     void   * u_buf, | 
|---|
| 105 |                     uint32_t size ); | 
|---|
| 106 |  | 
|---|
| 107 |  | 
|---|
| 108 | #endif  /* _PIPE_H_ */ | 
|---|