source: trunk/kernel/syscalls/sys_condvar.c @ 646

Last change on this file since 646 was 635, checked in by alain, 5 years ago

This version is a major evolution: The physical memory allocators,
defined in the kmem.c, ppm.c, and kcm.c files have been modified
to support remote accesses. The RPCs that were previously user
to allocate physical memory in a remote cluster have been removed.
This has been done to cure a dead-lock in case of concurrent page-faults.

This version 2.2 has been tested on a (4 clusters / 2 cores per cluster)
TSAR architecture, for both the "sort" and the "fft" applications.

File size: 6.4 KB
Line 
1/*
2 * sys_condvar.c - Access a POSIX condvar.
3 *
4 * Author    Alain Greiner  (2016,2017,2018,2019)
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#include <hal_kernel_types.h>
25#include <hal_special.h>
26#include <hal_vmm.h>
27#include <errno.h>
28#include <thread.h>
29#include <printk.h>
30#include <vmm.h>
31#include <syscalls.h>
32#include <remote_condvar.h>
33#include <remote_mutex.h>
34
35
36#if DEBUG_SYS_CONDVAR
37/////////////////////////////////////////////////////
38static char * sys_convar_op_str( uint32_t operation )
39{
40        if     ( operation == CONDVAR_INIT      ) return "INIT";
41        else if( operation == CONDVAR_WAIT      ) return "WAIT";
42        else if( operation == CONDVAR_SIGNAL    ) return "SIGNAL";
43        else if( operation == CONDVAR_BROADCAST ) return "BROADCAST";
44        else if( operation == CONDVAR_DESTROY   ) return "DESTROY";
45        else                                      return "undefined";
46}
47#endif
48
49//////////////////////////////////////
50int sys_condvar( void         * condvar,
51                 uint32_t       operation,
52                 void         * mutex )
53{
54    vseg_t    * vseg;         // for condvar check
55        error_t     error;
56 
57    thread_t  * this    = CURRENT_THREAD;
58    process_t * process = this->process;
59
60#if DEBUG_SYS_CONDVAR
61uint64_t    tm_start;
62uint64_t    tm_end;
63tm_start = hal_get_cycles();
64if( DEBUG_SYS_CONDVAR < tm_start )
65printk("\n[DBG] %s : thread %x in process %x enter for %s / cycle %d\n",
66__FUNCTION__, this->trdid, process->pid, sys_condvar_op_str( operation ), (uint32_t)tm_start );
67#endif
68
69    // check condvar in user vspace
70        error = vmm_get_vseg( process , (intptr_t)condvar , &vseg );
71
72        if( error )
73    {
74
75#if DEBUG_SYSCALLS_ERROR
76printk("\n[ERROR] in %s : unmapped condvar %x / thread %x / process %x\n",
77__FUNCTION__ , (intptr_t)condvar , this->trdid , process->pid );
78#endif
79        this->errno = error;
80        return -1;
81    }
82
83    // execute requested operation
84        switch( operation )
85        {
86        //////////////////
87        case CONDVAR_INIT:
88        {
89            error = remote_condvar_create( (intptr_t)condvar );
90   
91                    if( error )
92            {
93
94#if DEBUG_SYSCALLS_ERROR
95printk("\n[ERROR] in %s : cannot create condvar %x / thread %x / process %x\n",
96__FUNCTION__ , (intptr_t)condvar , this->trdid , process->pid );
97#endif
98                this->errno = error;
99                return -1;
100            }
101                    break;
102        }
103        //////////////////
104            case CONDVAR_WAIT:
105        {
106            // check mutex in user vspace
107                error = vmm_get_vseg( process , (intptr_t)mutex , &vseg );
108
109                if( error )
110            {
111
112#if DEBUG_SYSCALLS_ERROR
113printk("\n[ERROR] in %s : unmapped mutex %x / thread %x / process %x\n",
114__FUNCTION__ , (intptr_t)mutex , this->trdid , process->pid );
115#endif
116                this->errno = error;
117                return -1;
118            }
119
120            xptr_t condvar_xp = remote_condvar_from_ident( (intptr_t)condvar );
121
122            if( condvar_xp == XPTR_NULL )     // user error
123            {
124
125#if DEBUG_SYSCALLS_ERROR
126printk("\n[ERROR] in %s : condvar %x not registered / thread %x / process %x\n",
127__FUNCTION__ , (intptr_t)condvar , this->trdid , process->pid );
128#endif
129                this->errno = EINVAL;
130                return -1;
131            }
132   
133            xptr_t mutex_xp = remote_mutex_from_ident( (intptr_t)mutex );
134
135            if( mutex_xp == XPTR_NULL )     // user error
136            {
137
138#if DEBUG_SYSCALLS_ERROR
139printk("\n[ERROR] in %s : mutex %x not registered / thread %x / process %x\n",
140__FUNCTION__ , (intptr_t)mutex , this->trdid , process->pid );
141#endif
142                this->errno = EINVAL;
143                return -1;
144            }
145
146            remote_condvar_wait( condvar_xp , mutex_xp );
147
148            break;
149        }
150        ////////////////////
151            case CONDVAR_SIGNAL:
152        {
153            xptr_t condvar_xp = remote_condvar_from_ident( (intptr_t)condvar );
154
155            if( condvar_xp == XPTR_NULL )     // user error
156            {
157
158#if DEBUG_SYSCALLS_ERROR
159printk("\n[ERROR] in %s : condvar %x not registered / thread %x / process %x\n",
160__FUNCTION__ , (intptr_t)condvar , this->trdid , process->pid );
161#endif
162                this->errno = EINVAL;
163                return -1;
164            }
165
166            remote_condvar_signal( condvar_xp ); 
167           
168            break;
169        }
170        ///////////////////////
171            case CONDVAR_BROADCAST:
172        {
173            xptr_t condvar_xp = remote_condvar_from_ident( (intptr_t)condvar );
174
175            if( condvar_xp == XPTR_NULL )     // user error
176            {
177
178#if DEBUG_SYSCALLS_ERROR
179printk("\n[ERROR] in %s : condvar %x not registered / thread %x / process %x\n",
180__FUNCTION__ , (intptr_t)condvar , this->trdid , process->pid );
181#endif
182                this->errno = EINVAL;
183                return -1;
184            }
185
186            remote_condvar_broadcast( condvar_xp );
187
188            break;
189        }
190        /////////////////////
191            case CONDVAR_DESTROY:
192        {
193            xptr_t condvar_xp = remote_condvar_from_ident( (intptr_t)condvar );
194
195            if( condvar_xp == XPTR_NULL )     // user error
196            {
197
198#if DEBUG_SYSCALLS_ERROR
199printk("\n[ERROR] in %s : condvar %x not registered / thread %x / process %x\n",
200__FUNCTION__ , (intptr_t)condvar , this->trdid , process->pid );
201#endif
202                this->errno = EINVAL;
203                return -1;
204            }
205
206            remote_condvar_destroy( condvar_xp ); 
207
208            break;
209                }
210        /////////
211        default: {
212            assert ( false, "illegal operation type <%x>\n", operation );
213        }
214        }   // end switch
215
216    hal_fence();
217
218#if DEBUG_SYS_CONDVAR
219tm_start = hal_get_cycles();
220if( DEBUG_SYS_MUTEX < tm_start )
221printk("\n[DBG] %s : thread %x in process %x exit for %s / cycle %d\n",
222__FUNCTION__, this->trdid, process->pid, sys_condvar_op_str( operation ), (uint32_t)tm_start );
223#endif
224
225        return 0;
226
227}  // enc sys_condvar()
228
Note: See TracBrowser for help on using the repository browser.