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

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

Introduce syscalls.

File size: 4.9 KB
RevLine 
[1]1/*
[23]2 * sys_condvar.c - Access a POSIX condvar.
[1]3 *
[23]4 * Author    Alain Greiner  (2016,2017)
[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
[23]24#include <hal_types.h>
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>
[1]32
[23]33////////////////////////////////////////
34int sys_condvar( void         * condvar,
35                 uint32_t       operation,
36                 void         * mutex )
[1]37{
[23]38        error_t    error;
39    paddr_t    paddr;
[1]40 
[23]41    thread_t * this = CURRENT_THREAD;
[1]42
[23]43    // check condvar in user vspace
44        error = vmm_v2p_translate( false , condvar , &paddr );
45        if( error )
46    {
47        printk("\n[ERROR] in %s : illegal condvar virtual address = %x\n",
48               __FUNCTION__ , (intptr_t)condvar );
49        this->errno = error;
50        return -1;
51    }
[1]52
[23]53    // execute requested operation
54        switch( operation )
[1]55        {
[23]56        //////////////////
57        case CONDVAR_INIT:
58        {
59            error = remote_condvar_create( (intptr_t)condvar );
[1]60   
[23]61                    if( error )
62            {
63                printk("\n[ERROR] in %s : cannot create condvar = %x\n",
64                       __FUNCTION__ , (intptr_t)condvar );
65                this->errno = error;
66                return -1;
67            }
68                    break;
69        }
70        //////////////////
71            case CONDVAR_WAIT:
72        {
73            // check mutex in user vspace
74                error = vmm_v2p_translate( false , mutex , &paddr );
75
76                if( error )
77            {
78                printk("\n[ERROR] in %s : illegal condvar virtual address = %x\n",
79                       __FUNCTION__ , (intptr_t)condvar );
80                this->errno = error;
81                return -1;
82            }
83
84            xptr_t condvar_xp = remote_condvar_from_ident( (intptr_t)condvar );
85
86            if( condvar_xp == XPTR_NULL )     // user error
87            {
88                printk("\n[ERROR] in %s : condvar %x not registered\n",
89                       __FUNCTION__ , (intptr_t)condvar );
90                this->errno = EINVAL;
91                return -1;
92            }
[1]93   
[23]94            xptr_t mutex_xp = remote_condvar_from_ident( (intptr_t)condvar );
95            if( mutex_xp == XPTR_NULL )     // user error
96            {
97                printk("\n[ERROR] in %s : mutex %x not registered\n",
98                       __FUNCTION__ , (intptr_t)condvar );
99                this->errno = EINVAL;
100                return -1;
101            }
[1]102
[23]103            remote_condvar_wait( condvar_xp , mutex_xp );
[1]104
[23]105            break;
106        }
107        ////////////////////
108            case CONDVAR_SIGNAL:
109        {
110            xptr_t condvar_xp = remote_condvar_from_ident( (intptr_t)condvar );
[1]111
[23]112            if( condvar_xp == XPTR_NULL )     // user error
113            {
114                printk("\n[ERROR] in %s : condvar %x not registered\n",
115                       __FUNCTION__ , (intptr_t)condvar );
116                this->errno = EINVAL;
117                return -1;
118            }
[1]119
[23]120            remote_condvar_signal( condvar_xp ); 
121           
122            break;
123        }
124        ///////////////////////
125            case CONDVAR_BROADCAST:
126        {
127            xptr_t condvar_xp = remote_condvar_from_ident( (intptr_t)condvar );
[1]128
[23]129            if( condvar_xp == XPTR_NULL )     // user error
130            {
131                printk("\n[ERROR] in %s : condvar %x not registered\n",
132                       __FUNCTION__ , (intptr_t)condvar );
133                this->errno = EINVAL;
134                return -1;
135            }
[1]136
[23]137            remote_condvar_broadcast( condvar_xp );
[1]138
[23]139            break;
140        }
141        /////////////////////
142            case CONDVAR_DESTROY:
143        {
144            xptr_t condvar_xp = remote_condvar_from_ident( (intptr_t)condvar );
[1]145
[23]146            if( condvar_xp == XPTR_NULL )     // user error
147            {
148                printk("\n[ERROR] in %s : condvar %x not registered\n",
149                       __FUNCTION__ , (intptr_t)condvar );
150                this->errno = EINVAL;
151                return -1;
152            }
[1]153
[23]154            remote_condvar_destroy( condvar_xp ); 
[1]155
[23]156            break;
157                }
158        /////////
159            default:
160        {
161            printk("\n[PANIC] in %s : illegal operation type\n", __FUNCTION__ );
162            hal_core_sleep();
163        }
164        }   // end switch
[1]165
[23]166        return 0;
[1]167
[23]168}  // enc sys_condvar()
169
Note: See TracBrowser for help on using the repository browser.