source: trunk/kernel/syscalls/sys_mutex.c @ 440

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

1/ Fix a bug in the Multithreaded "sort" applicationr:
The pthread_create() arguments must be declared as global variables.
2/ The exit syscall can be called by any thread of a process..

File size: 4.4 KB
RevLine 
[1]1/*
[23]2 * sys_mutex.c - Access a POSIX mutex.
[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
[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_mutex.h>
[1]32
33
[23]34/////////////////////////////////
35int sys_mutex( void     * vaddr,
36               uint32_t   operation,
37               uint32_t   attr )
[1]38{
[440]39        error_t     error;
40    vseg_t    * vseg;
[1]41
[440]42    thread_t  * this    = CURRENT_THREAD;
43    process_t * process = this->process;
[1]44
[23]45    // check vaddr in user vspace
[440]46        error = vmm_get_vseg( process , (intptr_t)vaddr , &vseg );
47
[23]48        if( error )
49    {
[440]50
51#if DEBUG_SYSCALLS_ERROR
52printk("\n[ERROR] in %s : mutex unmapped %x / thread %x / process %x\n",
53__FUNCTION__ , (intptr_t)vaddr , this->trdid , process->pid );
54vmm_display( process , false );
55#endif
[23]56        this->errno = error;
57        return -1;
58    }
[1]59
[23]60    // execute requested operation
61        switch( operation )
62        {   
63        ////////////////
64            case MUTEX_INIT:
65        { 
66            if( attr != 0 )
67            {
[440]68
69#if DEBUG_SYSCALLS_ERROR
70printk("\n[ERROR] in %s : mutex attribute non supported / thread %x / process %x\n",
71__FUNCTION__ , this->trdid , process->pid );
72#endif
[23]73                this->errno = error;
74                return -1;
75            }
76   
77            error = remote_mutex_create( (intptr_t)vaddr );
[1]78
[23]79            if( error )
80            {
[440]81
82#if DEBUG_SYSCALLS_ERROR
83printk("\n[ERROR] in %s : cannot create mutex / thread %x / process %x\n",
84__FUNCTION__ , this->trdid , process->pid );
85#endif
[23]86                this->errno = error;
87                return -1;
88            } 
89                    break;
90                }
91        ///////////////////
92            case MUTEX_DESTROY:
93        {
94            xptr_t mutex_xp = remote_mutex_from_ident( (intptr_t)vaddr );
[1]95
[23]96            if( mutex_xp == XPTR_NULL )     // user error
97            {
[440]98
99#if DEBUG_SYSCALLS_ERROR
100printk("\n[ERROR] in %s : mutex %x not registered / thread %x / process %x\n",
101__FUNCTION__ , (intptr_t)vaddr , this->trdid , process->pid );
102#endif
[23]103                this->errno = EINVAL;
104                return -1;
105            }
106            else                          // success
107            {
108                remote_mutex_destroy( mutex_xp ); 
109            }
110            break;
111        }
112        ////////////////
113            case MUTEX_LOCK:
114        {
115            xptr_t mutex_xp = remote_mutex_from_ident( (intptr_t)vaddr );
[1]116
[23]117            if( mutex_xp == XPTR_NULL )     // user error
118            {
[440]119
120#if DEBUG_SYSCALLS_ERROR
121printk("\n[ERROR] in %s : mutex %x not registered / thread %x / process %x\n",
122__FUNCTION__ , (intptr_t)vaddr , this->trdid , process->pid );
123#endif
[23]124                this->errno = EINVAL;
125                return -1;
126            }
127            else                          // success
128            {
129                remote_mutex_lock( mutex_xp ); 
130            }
131            break;
132        }
133        //////////////////
134            case MUTEX_UNLOCK:
135        {
136            xptr_t mutex_xp = remote_mutex_from_ident( (intptr_t)vaddr );
[1]137
[23]138            if( mutex_xp == XPTR_NULL )     // user error
139            {
[440]140
141#if DEBUG_SYSCALLS_ERROR
142printk("\n[ERROR] in %s : mutex %x not registered / thread %x / process %x\n",
143__FUNCTION__ , (intptr_t)vaddr , this->trdid , process->pid );
144#endif
[23]145                this->errno = EINVAL;
146                return -1;
147            }
148            else                          // success
149            {
150                remote_mutex_unlock( mutex_xp ); 
151            }
152            break;
153        }
154        ////////
155            default:
156        {
157            printk("\n[PANIC] in %s : illegal operation type\n", __FUNCTION__ );
158                    hal_core_sleep();
159        }
160        }
[1]161
[23]162        return 0;
[1]163
[23]164}  // end sys_mutex()
[1]165
Note: See TracBrowser for help on using the repository browser.