[23] | 1 | /* |
---|
[563] | 2 | * remote_condvar.h: POSIX condition variable definition. |
---|
[23] | 3 | * |
---|
[635] | 4 | * Authors Alain Greiner (2016,2017,2018,2019) |
---|
[23] | 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 |
---|
[563] | 20 | * along with ALMOS-kernel; if not, write to the Free Software Foundation, |
---|
[23] | 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | */ |
---|
| 23 | |
---|
[563] | 24 | #ifndef _CONDVAR_H_ |
---|
| 25 | #define _CONDVAR_H_ |
---|
[23] | 26 | |
---|
| 27 | #include <kernel_config.h> |
---|
[457] | 28 | #include <hal_kernel_types.h> |
---|
[563] | 29 | #include <remote_busylock.h> |
---|
[23] | 30 | #include <xlist.h> |
---|
| 31 | |
---|
[563] | 32 | /******************************************************************************************* |
---|
[581] | 33 | * This file defines the ALMOS-MKH implentation of an user level, POSIX compliant condvar. |
---|
[23] | 34 | * |
---|
[563] | 35 | * It can be used by muti-threaded user applications to synchronise user threads |
---|
| 36 | * running in different clusters. |
---|
[23] | 37 | * |
---|
| 38 | * A condvar is declared by a given user process as a "pthread_cond_t" global variable. |
---|
| 39 | * This user type is implemented as an unsigned long, but the value is not used by the |
---|
[563] | 40 | * kernel. ALMOS-MKH uses only the mutex virtual address as an identifier. |
---|
[23] | 41 | * For each user condvar, ALMOS-MKH creates a kernel "remote_condvar_t" structure, |
---|
| 42 | * dynamically allocated in the reference cluster by the remote_condvar_create() function, |
---|
[563] | 43 | * and destroyed by the remote_condvar_destroy() function, using RPC if the calling thread |
---|
| 44 | * is not running in the reference cluster. |
---|
| 45 | * |
---|
[581] | 46 | * The blocking "remote_condvar_wait() function allows the calling thread to efficiently |
---|
[563] | 47 | * wait for a change in a shared user object. The calling thread blocks and register in |
---|
| 48 | * a waiting queue attached to the condvar. The blocked thread is unblocked by another |
---|
| 49 | * thread calling the remote_convar signal() or remote_condvar_broadcast(). |
---|
| 50 | * The three associated methods wait(), signal() and broadcast() must be called |
---|
| 51 | * by a thread holding the mutex associated to the condvar. |
---|
| 52 | ******************************************************************************************/ |
---|
[23] | 53 | |
---|
[563] | 54 | /******************************************************************************************* |
---|
| 55 | * This structure defines the kernel implementation of a condvar. |
---|
| 56 | ******************************************************************************************/ |
---|
[23] | 57 | |
---|
| 58 | typedef struct remote_condvar_s |
---|
| 59 | { |
---|
[563] | 60 | remote_busylock_t lock; /*! lock protecting the condvar state */ |
---|
| 61 | intptr_t ident; /*! virtual address in user space == identifier */ |
---|
| 62 | xlist_entry_t root; /*! root of waiting threads queue */ |
---|
| 63 | xlist_entry_t list; /*! member of list of condvars in same process */ |
---|
[23] | 64 | } |
---|
| 65 | remote_condvar_t; |
---|
| 66 | |
---|
[563] | 67 | /********************************************************************************************* |
---|
[23] | 68 | * This function returns an extended pointer on the remote condvar identified |
---|
| 69 | * by its virtual address in a given user process. It makes an associative search, |
---|
[563] | 70 | * scanning the list of user condvars rooted in the reference process descriptor. |
---|
| 71 | ********************************************************************************************* |
---|
| 72 | * @ ident : semaphore virtual address, used as identifier. |
---|
| 73 | * @ returns extended pointer on semaphore if success / returns XPTR_NULL if not found. |
---|
| 74 | ********************************************************************************************/ |
---|
[23] | 75 | xptr_t remote_condvar_from_ident( intptr_t ident ); |
---|
| 76 | |
---|
[563] | 77 | /******************************************************************************************* |
---|
| 78 | * This function implements the CONVAR_INIT operation. |
---|
| 79 | * This function creates and initializes a remote_condvar, identified by its virtual |
---|
[635] | 80 | * address <vaddr> in the client process reference cluster, using remote access. |
---|
[563] | 81 | * It registers this user condvar in the reference process descriptor. |
---|
| 82 | ******************************************************************************************* |
---|
| 83 | * @ vaddr : [in] condvar virtual addresss, used as identifier. |
---|
| 84 | ******************************************************************************************/ |
---|
| 85 | error_t remote_condvar_create( intptr_t vaddr ); |
---|
[23] | 86 | |
---|
[563] | 87 | /******************************************************************************************* |
---|
| 88 | * This function implements the CONVAR_DESTROY operation. |
---|
| 89 | * This function creates and initializes a remote_condvar, identified by its virtual |
---|
| 90 | * address in the client process reference cluster, and registers it in process descriptor. |
---|
| 91 | ******************************************************************************************* |
---|
| 92 | * @ condvar_xp : [in] extended pointer on buffer to store xptr on created condvar. |
---|
| 93 | ******************************************************************************************/ |
---|
| 94 | void remote_condvar_destroy( xptr_t condvar_xp ); |
---|
[23] | 95 | |
---|
[563] | 96 | /******************************************************************************************* |
---|
| 97 | * This function implements the CONDVAR_WAIT operation. |
---|
| 98 | * It atomically releases the mutex identified by the <mutex_xp> argument, |
---|
| 99 | * registers the calling thread in the condvar waiting queue identified by the |
---|
| 100 | * <condvar_xp> argument, blocks and deschedules this calling thread. |
---|
| 101 | * Later, when the calling thread resume, this function re-acquire the mutex and returns. |
---|
| 102 | * WARNING: the calling thread must hold the mutex associated to the condvar. |
---|
| 103 | ******************************************************************************************* |
---|
| 104 | * @ condvar_xp : [in] extended pointer on condvar. |
---|
| 105 | * @ mutex_xp : [in] extended pointer on mutex. |
---|
| 106 | ******************************************************************************************/ |
---|
| 107 | void remote_condvar_wait( xptr_t condvar_xp, |
---|
| 108 | xptr_t mutex_xp ); |
---|
[23] | 109 | |
---|
[563] | 110 | /******************************************************************************************* |
---|
| 111 | * This function implements the CONDVAR_SIGNAL operation. |
---|
| 112 | * It remove one waiting thread from a remote_condvar waiting queue identified by the |
---|
| 113 | * <condvar_xp> argument and unblocks this thread. |
---|
| 114 | * It does nothing if the queue is empty. |
---|
| 115 | * WARNING: the calling thread must hold the mutex associated to the condvar. |
---|
| 116 | ******************************************************************************************* |
---|
| 117 | * @ condvar_xp : extended pointer on remote_condvar. |
---|
| 118 | ******************************************************************************************/ |
---|
| 119 | void remote_condvar_signal( xptr_t condvar_xp ); |
---|
[23] | 120 | |
---|
[563] | 121 | /******************************************************************************************* |
---|
| 122 | * This function implements the CONDVAR_BROADCAST operation. |
---|
| 123 | * It removes all threads from a remote_condvar waiting queue identified by the |
---|
| 124 | * <condvar_xp> argument, and unblocks all these threads. |
---|
| 125 | * It does nothing if the queue is empty. |
---|
| 126 | * WARNING: the calling thread must hold the mutex associated to the condvar. |
---|
| 127 | ******************************************************************************************* |
---|
| 128 | * @ condvar_xp : extended pointer on remote_condvar. |
---|
| 129 | ******************************************************************************************/ |
---|
| 130 | void remote_condvar_broadcast( xptr_t condvar_xp ); |
---|
[23] | 131 | |
---|
[563] | 132 | #endif /* _CONDVAR_H_ */ |
---|