| 1 | /* |
|---|
| 2 | * remote_barrier.h - Access a POSIX barrier. |
|---|
| 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 _REMOTE_BARRIER_H_ |
|---|
| 25 | #define _REMOTE_BARRIER_H_ |
|---|
| 26 | |
|---|
| 27 | #include <kernel_config.h> |
|---|
| 28 | #include <hal_types.h> |
|---|
| 29 | #include <remote_spinlock.h> |
|---|
| 30 | #include <xlist.h> |
|---|
| 31 | |
|---|
| 32 | /*************************************************************************************** |
|---|
| 33 | * This file defines a POSIX compliant barrier. |
|---|
| 34 | * |
|---|
| 35 | * It is used by multi-threaded applications to synchronise threads running in |
|---|
| 36 | * different clusters, as all access functions uses hal_remote_lw() / hal_remote_sw() |
|---|
| 37 | * portable remote access primitives. |
|---|
| 38 | * |
|---|
| 39 | * A barrier is declared by a given user process as a "pthread_barrier_t" global variable. |
|---|
| 40 | * This user type is implemented as an unsigned long, but the value is not used by the |
|---|
| 41 | * kernel. ALMOS-MKH uses only the barrier virtual address as an identifier. |
|---|
| 42 | * For each user barrier, ALMOS-MKH creates a kernel "remote_barrier_t" structure, |
|---|
| 43 | * dynamically allocated in the reference cluster by the remote_barrier_create() function, |
|---|
| 44 | * and destroyed by the remote_barrier_destroy() function, using RPC if the calling thread |
|---|
| 45 | * is not running in the reference cluster. |
|---|
| 46 | * |
|---|
| 47 | * The blocking "remote_barrier_wait()" function implements a descheduling policy when |
|---|
| 48 | * the calling thread is not the last expected thread: the calling thread is registered |
|---|
| 49 | * in a waiting queue, rooted in the barrier structure, and the the calling thread |
|---|
| 50 | * is blocked on the THREAD_BLOCKED_USERSYNC condition. The last arrived thread |
|---|
| 51 | * unblocks all registtered waiting threads. |
|---|
| 52 | * |
|---|
| 53 | * Implementation note: |
|---|
| 54 | * This barrier is also used by the kernel in the parallel kernel_init phase, as the |
|---|
| 55 | * remote_barrier() function does not require barrier initialisation, when the barrier |
|---|
| 56 | * is statically allocated by the compiler in the kdata segment. |
|---|
| 57 | * **************************************************************************************/ |
|---|
| 58 | |
|---|
| 59 | /***************************************************************************************** |
|---|
| 60 | * This structure defines the barrier descriptor. |
|---|
| 61 | * - It contains an xlist of all barriers dynamically created by a given process, |
|---|
| 62 | * rooted in the reference process descriptor. |
|---|
| 63 | * - It contains the root of another xlist to register all arrived threads. |
|---|
| 64 | ****************************************************************************************/ |
|---|
| 65 | |
|---|
| 66 | typedef struct remote_barrier_s |
|---|
| 67 | { |
|---|
| 68 | remote_spinlock_t lock; /*! lock protecting list of arrived threads */ |
|---|
| 69 | intptr_t ident; /*! virtual address in user space == identifier */ |
|---|
| 70 | uint32_t current; /*! number of arrived threads */ |
|---|
| 71 | uint32_t sense; /*! barrier state (toggle) */ |
|---|
| 72 | uint32_t nb_threads; /*! number of expected threads */ |
|---|
| 73 | xlist_entry_t list; /*! member of list of barriers in same process */ |
|---|
| 74 | xlist_entry_t root; /*! root of list of arrived threads */ |
|---|
| 75 | } |
|---|
| 76 | remote_barrier_t; |
|---|
| 77 | |
|---|
| 78 | /***************************************************************************************** |
|---|
| 79 | * This function is directly used by the kernel in the kernel_init phase, |
|---|
| 80 | * because it does not require barrier state initialisation. |
|---|
| 81 | * It returns only when the <count> expected threads reach the barrier. |
|---|
| 82 | ***************************************************************************************** |
|---|
| 83 | * @ barrier_xp : extended pointer on barrier descriptor. |
|---|
| 84 | * @ count : number of expected threads. |
|---|
| 85 | ****************************************************************************************/ |
|---|
| 86 | inline void remote_barrier( xptr_t barrier_xp, |
|---|
| 87 | uint32_t count ); |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | /***************************************************************************************** |
|---|
| 91 | * This function returns an extended pointer on the remote barrier identified |
|---|
| 92 | * by its virtual address in a given user process. It makes an associative search, |
|---|
| 93 | * scanning the list of barriers rooted in the reference process descriptor. |
|---|
| 94 | ***************************************************************************************** |
|---|
| 95 | * @ ident : barrier virtual address, used as identifier. |
|---|
| 96 | * @ returns extended pointer on barrier if success / returns XPTR_NULL if not found. |
|---|
| 97 | ****************************************************************************************/ |
|---|
| 98 | xptr_t remote_barrier_from_ident( intptr_t ident ); |
|---|
| 99 | |
|---|
| 100 | /***************************************************************************************** |
|---|
| 101 | * This function implement the pthread_barrier_init() syscall. |
|---|
| 102 | * It allocates memory for the barrier descriptor in the reference cluster for |
|---|
| 103 | * the calling process, it initializes the barrier state, and register it in the |
|---|
| 104 | * list of barriers owned by the reference process. |
|---|
| 105 | ***************************************************************************************** |
|---|
| 106 | * @ count : number of expected threads. |
|---|
| 107 | * @ ident : barrier identifier (virtual address in user space). |
|---|
| 108 | * @ return 0 if success / return ENOMEM if failure. |
|---|
| 109 | ****************************************************************************************/ |
|---|
| 110 | error_t remote_barrier_create( intptr_t ident, |
|---|
| 111 | uint32_t count ); |
|---|
| 112 | |
|---|
| 113 | /***************************************************************************************** |
|---|
| 114 | * This function implement the pthread_barrier_destroy() syscall. |
|---|
| 115 | * It releases thr memory allocated for the barrier descriptor, and remove the barrier |
|---|
| 116 | * from the list of barriers owned by the reference process. |
|---|
| 117 | ***************************************************************************************** |
|---|
| 118 | * @ barrier_xp : extended pointer on barrier descriptor. |
|---|
| 119 | ****************************************************************************************/ |
|---|
| 120 | void remote_barrier_destroy( xptr_t barrier_xp ); |
|---|
| 121 | |
|---|
| 122 | /***************************************************************************************** |
|---|
| 123 | * This function implement the pthread_barrier_wait() syscall. |
|---|
| 124 | * It returns only when the number of expected threads (registered in the barrier |
|---|
| 125 | * dexcriptor) reach the barrier. |
|---|
| 126 | ***************************************************************************************** |
|---|
| 127 | * @ barrier_xp : extended pointer on barrier descriptor. |
|---|
| 128 | ****************************************************************************************/ |
|---|
| 129 | void remote_barrier_wait( xptr_t barrier_xp ); |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | #endif /* _REMOTE_BARRIER_H_ */ |
|---|