[23] | 1 | /* |
---|
[563] | 2 | * remote_mutex.h - POSIX mutex definition. |
---|
[23] | 3 | * |
---|
[563] | 4 | * Authors Alain Greiner (2016,2017,2018) |
---|
[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 |
---|
| 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 | #ifndef _REMOTE_MUTEX_H_ |
---|
| 25 | #define _REMOTE_MUTEX_H_ |
---|
| 26 | |
---|
| 27 | #include <kernel_config.h> |
---|
[457] | 28 | #include <hal_kernel_types.h> |
---|
[23] | 29 | #include <xlist.h> |
---|
| 30 | |
---|
| 31 | /*************************************************************************************** |
---|
[563] | 32 | * This file defines an user level POSIX compliant mutex. |
---|
[23] | 33 | * |
---|
[563] | 34 | * It can be used by muti-threaded user applications to synchronise user threads |
---|
| 35 | * running in different clusters. |
---|
[23] | 36 | * |
---|
| 37 | * A mutex is declared by a given user process as a "pthread_mutex_t" global variable. |
---|
| 38 | * This user type is implemented as an unsigned long, but the value is not used by the |
---|
| 39 | * kernel. ALMOS-MKH uses only the mutex virtual address as an identifier. |
---|
| 40 | * For each user mutex, ALMOS-MKH creates a kernel "remote_mutex_t" structure, |
---|
| 41 | * dynamically allocated in the reference cluster by the remote_mutex_create() function, |
---|
[563] | 42 | * and destroyed by the remote_mutex_destroy() function, using RPC if the calling thread |
---|
[23] | 43 | * is not running in the reference cluster. |
---|
| 44 | * |
---|
| 45 | * The blocking "remote_mutex_lock()" function implements a descheduling policy when |
---|
| 46 | * the lock is already taken by another thread : the calling thread is registered |
---|
| 47 | * in a waiting queue, rooted in the mutex structure, and the the calling thread |
---|
| 48 | * is blocked on the THREAD_BLOCKED_MUTEX condition. |
---|
| 49 | * The "remote_mutex_unlock()" function unblocks the first waiting thread in the queue |
---|
| 50 | * without releasing the mutex if queue is not empty. |
---|
| 51 | **************************************************************************************/ |
---|
| 52 | |
---|
| 53 | /***************************************************************************************** |
---|
[563] | 54 | * This structure defines the kernel implementation of an user level mutex. |
---|
[23] | 55 | ****************************************************************************************/ |
---|
| 56 | |
---|
| 57 | typedef struct remote_mutex_s |
---|
| 58 | { |
---|
[563] | 59 | remote_busylock_t lock; /*! lock protecting the mutex state */ |
---|
[23] | 60 | intptr_t ident; /*! mutex identifier (vaddr in user space) */ |
---|
[563] | 61 | uint32_t taken; /*! mutex non allocated if 0 */ |
---|
[23] | 62 | xlist_entry_t list; /*! member of list of mutex in same process */ |
---|
| 63 | xlist_entry_t root; /*! root of list of waiting threads */ |
---|
[563] | 64 | xptr_t owner; /*! extended pointer on owner thread */ |
---|
[23] | 65 | } |
---|
| 66 | remote_mutex_t; |
---|
| 67 | |
---|
| 68 | /*************************************************************************************** |
---|
| 69 | * This function returns an extended pointer on the remote mutex, identified |
---|
| 70 | * by its virtual address in a given user process. It makes an associative search, |
---|
| 71 | * scanning the list of mutex rooted in the reference process descriptor. |
---|
| 72 | *************************************************************************************** |
---|
| 73 | * @ ident : mutex virtual address, used as identifier. |
---|
| 74 | * @ returns extended pointer on mutex if success / returns XPTR_NULL if not found. |
---|
| 75 | **************************************************************************************/ |
---|
| 76 | xptr_t remote_mutex_from_ident( intptr_t ident ); |
---|
| 77 | |
---|
| 78 | /*************************************************************************************** |
---|
[563] | 79 | * This function implements the pthread_mutex_init() syscall. |
---|
[23] | 80 | * It allocates memory for the mutex descriptor in the reference cluster for |
---|
| 81 | * the calling process, it initializes the mutex state, and register it in the |
---|
| 82 | * list of mutex owned by the reference process. |
---|
| 83 | *************************************************************************************** |
---|
| 84 | * @ ident : mutex identifier (virtual address in user space). |
---|
[563] | 85 | * @ return 0 if success / ENOMEM if no memory / EINVAL if invalid argument. |
---|
[23] | 86 | **************************************************************************************/ |
---|
| 87 | error_t remote_mutex_create( intptr_t ident ); |
---|
| 88 | |
---|
| 89 | /*************************************************************************************** |
---|
[563] | 90 | * This function implements the pthread_mutex_destroy() syscall. |
---|
[23] | 91 | * It releases thr memory allocated for the mutex descriptor, and remove the mutex |
---|
| 92 | * from the list of mutex owned by the reference process. |
---|
| 93 | *************************************************************************************** |
---|
| 94 | * @ mutex_xp : extended pointer on mutex descriptor. |
---|
| 95 | **************************************************************************************/ |
---|
| 96 | void remote_mutex_destroy( xptr_t mutex_xp ); |
---|
| 97 | |
---|
| 98 | /*************************************************************************************** |
---|
[563] | 99 | * This blocking function implements the pthread_mutex_lock() syscall. |
---|
| 100 | * It returns only when the ownership of the mutex identified by the <mutex_xp> |
---|
| 101 | * argument has been obtained by the calling thread. It register in the mutex waiting |
---|
| 102 | * queue when the mutex is already taken by another thread. |
---|
[23] | 103 | *************************************************************************************** |
---|
| 104 | * @ mutex_xp : extended pointer on mutex descriptor. |
---|
| 105 | **************************************************************************************/ |
---|
| 106 | void remote_mutex_lock( xptr_t mutex_xp ); |
---|
| 107 | |
---|
| 108 | /*************************************************************************************** |
---|
[563] | 109 | * This function implements the pthread_mutex_unlock() syscall. |
---|
| 110 | * It cheks that the calling thread is actually the mutex owner. |
---|
| 111 | * It reset the "taken" & "owner" fields for the mutex identified by <mutex_xp>. |
---|
| 112 | * It unblocks the first thread registered in the mutex waiting queue, when the |
---|
| 113 | * queue is not empty. |
---|
[23] | 114 | *************************************************************************************** |
---|
| 115 | * @ mutex_xp : extended pointer on mutex descriptor. |
---|
[563] | 116 | * @ return 0 if success / return non zero if calling thread is not mutex owner. |
---|
[23] | 117 | **************************************************************************************/ |
---|
[563] | 118 | error_t remote_mutex_unlock( xptr_t mutex_xp ); |
---|
[23] | 119 | |
---|
[563] | 120 | /*************************************************************************************** |
---|
| 121 | * This non blocking function function attempts to lock a mutex without blocking. |
---|
| 122 | *************************************************************************************** |
---|
| 123 | * @ mutex_xp : extended pointer on mutex descriptor. |
---|
| 124 | * @ return 0 if success / return non zero if already taken. |
---|
| 125 | **************************************************************************************/ |
---|
| 126 | error_t remote_mutex_trylock( xptr_t mutex_xp ); |
---|
[23] | 127 | |
---|
| 128 | |
---|
[563] | 129 | |
---|
[23] | 130 | #endif |
---|