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

Last change on this file since 584 was 566, checked in by alain, 6 years ago

Complete restructuration of kernel locks.

File size: 6.4 KB
RevLine 
[1]1/*
[23]2 * sys_condvar.c - Access a POSIX condvar.
[1]3 *
[440]4 * Author    Alain Greiner  (2016,2017,2018)
[1]5 *
[23]6 * Copyright (c) UPMC Sorbonne Universites
[1]7 *
[23]8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
[1]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 *
[23]14 * ALMOS-MKH is distributed in the hope that it will be useful, but
[1]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
[23]20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
[1]21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
[457]24#include <hal_kernel_types.h>
[23]25#include <hal_special.h>
[1]26#include <errno.h>
27#include <thread.h>
[23]28#include <printk.h>
[1]29#include <vmm.h>
[23]30#include <syscalls.h>
31#include <remote_condvar.h>
[440]32#include <remote_mutex.h>
[1]33
[566]34
35#if DEBUG_SYS_CONDVAR
36/////////////////////////////////////////////////////
37static char * sys_convar_op_str( uint32_t operation )
38{
39        if     ( operation == CONDVAR_INIT      ) return "INIT";
40        else if( operation == CONDVAR_WAIT      ) return "WAIT";
41        else if( operation == CONDVAR_SIGNAL    ) return "SIGNAL";
42        else if( operation == CONDVAR_BROADCAST ) return "BROADCAST";
43        else if( operation == CONDVAR_DESTROY   ) return "DESTROY";
44        else                                      return "undefined";
45}
46#endif
47
48//////////////////////////////////////
[23]49int sys_condvar( void         * condvar,
50                 uint32_t       operation,
51                 void         * mutex )
[1]52{
[566]53    vseg_t    * vseg;         // for condvar check
[440]54        error_t     error;
[1]55 
[440]56    thread_t  * this    = CURRENT_THREAD;
57    process_t * process = this->process;
[1]58
[566]59#if DEBUG_SYS_CONDVAR
60uint64_t    tm_start;
61uint64_t    tm_end;
62tm_start = hal_get_cycles();
63if( DEBUG_SYS_CONDVAR < tm_start )
64printk("\n[DBG] %s : thread %x in process %x enter for %s / cycle %d\n",
65__FUNCTION__, this->trdid, process->pid, sys_condvar_op_str( operation ), (uint32_t)tm_start );
66#endif
67
[23]68    // check condvar in user vspace
[440]69        error = vmm_get_vseg( process , (intptr_t)condvar , &vseg );
70
[23]71        if( error )
72    {
[440]73
74#if DEBUG_SYSCALLS_ERROR
75printk("\n[ERROR] in %s : unmapped condvar %x / thread %x / process %x\n",
76__FUNCTION__ , (intptr_t)condvar , this->trdid , process->pid );
77vmm_display( process , false );
78#endif
[23]79        this->errno = error;
80        return -1;
81    }
[1]82
[23]83    // execute requested operation
84        switch( operation )
[1]85        {
[23]86        //////////////////
87        case CONDVAR_INIT:
88        {
89            error = remote_condvar_create( (intptr_t)condvar );
[1]90   
[23]91                    if( error )
92            {
[440]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
[23]98                this->errno = error;
99                return -1;
100            }
101                    break;
102        }
103        //////////////////
104            case CONDVAR_WAIT:
105        {
106            // check mutex in user vspace
[440]107                error = vmm_get_vseg( process , (intptr_t)mutex , &vseg );
[23]108
109                if( error )
110            {
[440]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
[23]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            {
[440]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
[23]129                this->errno = EINVAL;
130                return -1;
131            }
[1]132   
[440]133            xptr_t mutex_xp = remote_mutex_from_ident( (intptr_t)mutex );
134
[23]135            if( mutex_xp == XPTR_NULL )     // user error
136            {
[440]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
[23]142                this->errno = EINVAL;
143                return -1;
144            }
[1]145
[23]146            remote_condvar_wait( condvar_xp , mutex_xp );
[1]147
[23]148            break;
149        }
150        ////////////////////
151            case CONDVAR_SIGNAL:
152        {
153            xptr_t condvar_xp = remote_condvar_from_ident( (intptr_t)condvar );
[1]154
[23]155            if( condvar_xp == XPTR_NULL )     // user error
156            {
[440]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
[23]162                this->errno = EINVAL;
163                return -1;
164            }
[1]165
[23]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 );
[1]174
[23]175            if( condvar_xp == XPTR_NULL )     // user error
176            {
[440]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
[23]182                this->errno = EINVAL;
183                return -1;
184            }
[1]185
[23]186            remote_condvar_broadcast( condvar_xp );
[1]187
[23]188            break;
189        }
190        /////////////////////
191            case CONDVAR_DESTROY:
192        {
193            xptr_t condvar_xp = remote_condvar_from_ident( (intptr_t)condvar );
[1]194
[23]195            if( condvar_xp == XPTR_NULL )     // user error
196            {
[440]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
[23]202                this->errno = EINVAL;
203                return -1;
204            }
[1]205
[23]206            remote_condvar_destroy( condvar_xp ); 
[1]207
[23]208            break;
209                }
210        /////////
[508]211        default: {
212            assert ( false, "illegal operation type <%x>\n", operation );
[23]213        }
214        }   // end switch
[1]215
[566]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
[23]225        return 0;
[1]226
[23]227}  // enc sys_condvar()
228
Note: See TracBrowser for help on using the repository browser.