[1] | 1 | /* |
---|
| 2 | * remote_sem.h - POSIX unnammed semaphore definition. |
---|
| 3 | * |
---|
| 4 | * Author Alain Greiner (2016) |
---|
| 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 _SEMAPHORE_H_ |
---|
| 25 | #define _SEMAPHORE_H_ |
---|
| 26 | |
---|
| 27 | #include <hal_types.h> |
---|
| 28 | #include <xlist.h> |
---|
| 29 | #include <remote_spinlock.h> |
---|
| 30 | |
---|
| 31 | /********************************************************************************************* |
---|
| 32 | * This file defines the the POSIX compliant unnamed semaphore. |
---|
| 33 | * |
---|
| 34 | * A semaphore is declared by a given user process as a "sem_t" global variable. |
---|
| 35 | * The user "sem_t" structure is implemented as an unsigned long, but the value is not |
---|
| 36 | * used by the kernel. ALMOS-MKH uses only the sem_t virtual address as an identifier. |
---|
| 37 | * For each user semaphore, ALMOS-MKH creates a kernel "remote_sem_t" structure. |
---|
| 38 | * - This structure is allocated in the reference cluster by the sem_init() syscall, |
---|
| 39 | * and destroyed by the sem_destroy() syscall, using RPC if the calling thread is not |
---|
| 40 | * running in the reference cluster. |
---|
| 41 | * - The threads accessing the semaphore with the sem_get_value(), sem_wait(), sem_post() |
---|
| 42 | * syscalls can be running in any cluster, as these syscalls access the "remote_sem_t" |
---|
| 43 | * structure with remote_read / remote_write access functions. |
---|
| 44 | ********************************************************************************************/ |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | /********************************************************************************************* |
---|
| 48 | * This structure defines the kernel remote_sem_t structure. |
---|
| 49 | * It contains the root of a waiting threads queue, implemented as a distributed xlist. |
---|
| 50 | * It contains an xlist of all semaphores, rooted in the reference process descriptor. |
---|
| 51 | * It contains a lock protecting both the semaphore value and the waiting queue. |
---|
| 52 | ********************************************************************************************/ |
---|
| 53 | |
---|
| 54 | typedef struct remote_sem_s |
---|
| 55 | { |
---|
| 56 | remote_spinlock_t lock; /*! lock protecting both count and wait_queue */ |
---|
| 57 | uint32_t count; /*! current value */ |
---|
| 58 | intptr_t ident; /*! virtual address in user space == identifier */ |
---|
| 59 | xlist_entry_t wait_queue; /*! root of waiting thread queue */ |
---|
| 60 | xlist_entry_t sem_list; /*! member of list of semaphores in same process */ |
---|
| 61 | } |
---|
| 62 | remote_sem_t; |
---|
| 63 | |
---|
| 64 | /********************************************************************************************* |
---|
| 65 | * This enum defines the semaphore operation types supported by the sys_sem() function. |
---|
| 66 | * It must be consistent with the values defined in the semaphore.c file (user library). |
---|
| 67 | ********************************************************************************************/ |
---|
| 68 | |
---|
| 69 | typedef enum |
---|
| 70 | { |
---|
| 71 | SEM_INIT, |
---|
| 72 | SEM_GETVALUE, |
---|
| 73 | SEM_WAIT, |
---|
| 74 | SEM_POST, |
---|
| 75 | SEM_DESTROY |
---|
| 76 | } |
---|
| 77 | sem_operation_t; |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | /********************************************************************************************* |
---|
| 82 | * This function returns an extended pointer on the remote semaphore identified |
---|
| 83 | * by its virtual address in a given user process. It makes an associative search, scanning |
---|
| 84 | * the list of semaphores rooted in the reference process descriptor. |
---|
| 85 | ********************************************************************************************* |
---|
| 86 | * @ process : pointer on local process descriptor. |
---|
| 87 | * @ vaddr : semaphore virtual address, used as identifier. |
---|
| 88 | * @ returns extended pointer on semaphore if success / returns XPTR_NULL if not found. |
---|
| 89 | ********************************************************************************************/ |
---|
| 90 | xptr_t remote_sem_from_vaddr( intptr_t vaddr ); |
---|
| 91 | |
---|
| 92 | /********************************************************************************************* |
---|
| 93 | * This function implements the SEM_INIT operation. |
---|
| 94 | * It allocates memory for a remote semaphore in reference cluster, using a RPC if required, |
---|
| 95 | * and initializes it, using remote accesses. |
---|
| 96 | ********************************************************************************************* |
---|
| 97 | * @ vaddr : semaphore virtual address, used as identifier. |
---|
| 98 | * @ value : semaphore initial value. |
---|
| 99 | * @ returns 0 if success / returns ENOMEM if error. |
---|
| 100 | ********************************************************************************************/ |
---|
| 101 | error_t remote_sem_init( intptr_t vaddr, |
---|
| 102 | uint32_t value ); |
---|
| 103 | |
---|
| 104 | /****************************yy*************************************************************** |
---|
| 105 | * This blocking function implements the SEM_WAIT operation. |
---|
| 106 | * - It returns only when the remote semaphore has a non-zero value, |
---|
| 107 | * and has been atomically decremented. |
---|
| 108 | * - if the semaphore has a zero value, it register the calling thread in the semaphore |
---|
| 109 | * waiting queue, block the thread, and yield. |
---|
| 110 | ********************************************************************************************* |
---|
| 111 | * @ sem_xp : extended pointer on semaphore. |
---|
| 112 | ********************************************************************************************/ |
---|
| 113 | void remote_sem_wait( xptr_t sem_xp ); |
---|
| 114 | |
---|
| 115 | /****************************yy*************************************************************** |
---|
| 116 | * This function mplements the SEM_POST operation. |
---|
| 117 | * - It atomically increments the remote semaphore if the semaphore waiting queue is empty. |
---|
| 118 | * - If the waiting queue is not empty, it wakes up the first waiting thread. |
---|
| 119 | ********************************************************************************************* |
---|
| 120 | * @ sem_xp : extended pointer on semaphore. |
---|
| 121 | ********************************************************************************************/ |
---|
| 122 | void remote_sem_post( xptr_t sem_xp ); |
---|
| 123 | |
---|
| 124 | /****************************yy*************************************************************** |
---|
| 125 | * This function implements the SEM_DESTROY operation. |
---|
| 126 | * It desactivates the semaphore, and releases the physical memory allocated in the |
---|
| 127 | * reference cluster, using a RPC if required. |
---|
| 128 | ********************************************************************************************* |
---|
| 129 | * @ sem_xp : extended pointer on semaphore. |
---|
| 130 | ********************************************************************************************/ |
---|
| 131 | void remote_sem_destroy( xptr_t sem_xp ); |
---|
| 132 | |
---|
| 133 | /****************************yy*************************************************************** |
---|
| 134 | * This function implements the SEM_GETVALUE operation. |
---|
| 135 | * It returns in the <data> buffer the semaphore current value. |
---|
| 136 | ********************************************************************************************* |
---|
| 137 | * @ sem_xp : extended pointer on semaphore. |
---|
| 138 | * @ data : [out] returned value. |
---|
| 139 | ********************************************************************************************/ |
---|
| 140 | void remote_sem_get_value( xptr_t sem_xp, |
---|
| 141 | uint32_t * data ); |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | #endif /* _SEMAPHORE_H_ */ |
---|