source: trunk/kernel/libk/remote_sem.h @ 458

Last change on this file since 458 was 457, checked in by alain, 6 years ago

This version modifies the exec syscall and fixes a large number of small bugs.
The version number has been updated (0.1)

File size: 7.2 KB
Line 
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_kernel_types.h>
28#include <xlist.h>
29#include <remote_spinlock.h>
30
31/*********************************************************************************************
32 *      This file defines a 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 semaphore descriptor.
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
54typedef 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     root;          /*! root of waiting thread queue                       */
60    xlist_entry_t     list;          /*! member of list of semaphores in same process       */
61}
62remote_sem_t;
63
64
65/*********************************************************************************************
66 * This function returns an extended pointer on the remote semaphore identified
67 * by its virtual address in a given user process. It makes an associative search,
68 * scanning the list of semaphores rooted in the reference process descriptor.
69 *********************************************************************************************
70 * @ process  : pointer on local process descriptor.
71 * @ vaddr    : semaphore virtual address, used as identifier.
72 * @ returns extended pointer on semaphore if success / returns XPTR_NULL if not found.
73 ********************************************************************************************/
74xptr_t remote_sem_from_vaddr( intptr_t  vaddr );
75
76/*********************************************************************************************
77 * This function implements the SEM_INIT operation.
78 * It allocates memory for a remote semaphore in reference cluster, using a RPC if required,
79 * and initializes it, using remote accesses from values defined by the <vaddr> and <value>
80 * arguments. It uses also a remote access to return the extended pointer on the semaphore
81 * in the buffer identified by the <buf_xp> argument.
82 *********************************************************************************************
83 * @ vaddr     : [in] semaphore virtual address, used as identifier.
84 * @ value     : [in] semaphore initial value.
85 * @ sem_xp_xp : [out] extended pointer on buffer to store extended pointer on semaphore.
86 * @ returns 0 if success / returns -1 if no memory.
87 ********************************************************************************************/
88error_t remote_sem_create( intptr_t  vaddr,
89                           uint32_t  value,
90                           xptr_t    sem_xp_xp );
91 
92/****************************yy***************************************************************
93 * This function implements the SEM_DESTROY operation.
94 * It desactivates the semaphore, and releases the physical memory allocated in the
95 * reference cluster, using a RPC if required.
96 *********************************************************************************************
97 * @ sem_xp   : [in] extended pointer on semaphore.
98 ********************************************************************************************/
99void remote_sem_destroy( xptr_t sem_xp );
100
101/****************************yy***************************************************************
102 * This blocking function implements the SEM_WAIT operation.
103 * - It returns only when the remote semaphore has a non-zero value,
104 *   and has been atomically decremented.
105 * -  if the semaphore has a zero value, it register the calling thread in the semaphore
106 *    waiting queue, block the thread, and yield.
107 *********************************************************************************************
108 * @ sem_xp   : [in] extended pointer on semaphore.
109 ********************************************************************************************/
110void remote_sem_wait( xptr_t sem_xp );
111
112/****************************yy***************************************************************
113 * This function mplements the SEM_POST operation.
114 * - It atomically increments the remote semaphore if the semaphore waiting queue is empty.
115 * - If the waiting queue is not empty, it wakes up the first waiting thread.
116 *********************************************************************************************
117 * @ sem_xp   : [in] extended pointer on semaphore.
118 ********************************************************************************************/
119void remote_sem_post( xptr_t sem_xp );
120
121/****************************yy***************************************************************
122 * This function implements the SEM_GETVALUE operation.
123 * It returns in the <data> buffer the semaphore current value.
124 *********************************************************************************************
125 * @ sem_xp   : [in]  extended pointer on semaphore.
126 * @ data     : [out] local pointer on buffer for returned value.
127 ********************************************************************************************/
128void remote_sem_get_value( xptr_t      sem_xp,
129                           uint32_t  * data );
130
131
132#endif  /* _SEMAPHORE_H_ */
Note: See TracBrowser for help on using the repository browser.