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

Last change on this file since 374 was 23, checked in by alain, 7 years ago

Introduce syscalls.

File size: 6.8 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_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.
80 *********************************************************************************************
81 * @ vaddr    : semaphore virtual address, used as identifier.
82 * @ value    : semaphore initial value.
83 * @ returns 0 if success / returns ENOMEM if error.
84 ********************************************************************************************/
85error_t remote_sem_create( intptr_t  vaddr,
86                           uint32_t  value );
87 
88/****************************yy***************************************************************
89 * This function implements the SEM_DESTROY operation.
90 * It desactivates the semaphore, and releases the physical memory allocated in the
91 * reference cluster, using a RPC if required.
92 *********************************************************************************************
93 * @ sem_xp   : extended pointer on semaphore.
94 ********************************************************************************************/
95void remote_sem_destroy( xptr_t sem_xp );
96
97/****************************yy***************************************************************
98 * This blocking function implements the SEM_WAIT operation.
99 * - It returns only when the remote semaphore has a non-zero value,
100 *   and has been atomically decremented.
101 * -  if the semaphore has a zero value, it register the calling thread in the semaphore
102 *    waiting queue, block the thread, and yield.
103 *********************************************************************************************
104 * @ sem_xp   : extended pointer on semaphore.
105 ********************************************************************************************/
106void remote_sem_wait( xptr_t sem_xp );
107
108/****************************yy***************************************************************
109 * This function mplements the SEM_POST operation.
110 * - It atomically increments the remote semaphore if the semaphore waiting queue is empty.
111 * - If the waiting queue is not empty, it wakes up the first waiting thread.
112 *********************************************************************************************
113 * @ sem_xp   : extended pointer on semaphore.
114 ********************************************************************************************/
115void remote_sem_post( xptr_t sem_xp );
116
117/****************************yy***************************************************************
118 * This function implements the SEM_GETVALUE operation.
119 * It returns in the <data> buffer the semaphore current value.
120 *********************************************************************************************
121 * @ sem_xp   : extended pointer on semaphore.
122 * @ data     : [out] returned value.
123 ********************************************************************************************/
124void remote_sem_get_value( xptr_t      sem_xp,
125                           uint32_t  * data );
126
127
128#endif  /* _SEMAPHORE_H_ */
Note: See TracBrowser for help on using the repository browser.