source: trunk/kernel/libk/remote_condvar.c

Last change on this file was 683, checked in by alain, 4 years ago

All modifications required to support the <tcp_chat> application
including error recovery in case of packet loss.A

File size: 8.2 KB
Line 
1/*
2 * remote_condvar.c - remote kernel condition variable implementation.
3 *
4 * Authors     Alain Greiner (2016,2017,2018,2019,2020)
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 <kernel_config.h>
25#include <hal_kernel_types.h>
26#include <thread.h>
27#include <scheduler.h>
28#include <xlist.h>
29#include <remote_mutex.h>
30#include <remote_busylock.h>
31#include <remote_condvar.h>
32
33
34///////////////////////////////////////////////////
35xptr_t remote_condvar_from_ident( intptr_t  ident )
36{
37    // get pointer on local process_descriptor
38    process_t * process = CURRENT_THREAD->process;
39
40    // get extended pointer on reference process
41    xptr_t      ref_xp = process->ref_xp;
42
43    // get cluster and local pointer on reference process
44    cxy_t          ref_cxy = GET_CXY( ref_xp );
45    process_t    * ref_ptr = GET_PTR( ref_xp );
46
47    // get extended pointer on condvars list
48    xptr_t root_xp = XPTR( ref_cxy , &ref_ptr->condvar_root );
49    xptr_t lock_xp = XPTR( ref_cxy , &ref_ptr->sync_lock );
50
51    // get lock protecting synchro lists
52    remote_queuelock_acquire( lock_xp );
53 
54    // scan reference process condvar list
55    xptr_t             iter_xp;
56    xptr_t             condvar_xp;
57    cxy_t              condvar_cxy;
58    remote_condvar_t * condvar_ptr;
59    intptr_t           current;
60    bool_t             found = false;
61           
62    XLIST_FOREACH( root_xp , iter_xp )
63    {
64        condvar_xp  = XLIST_ELEMENT( iter_xp , remote_condvar_t , list );
65        condvar_cxy = GET_CXY( condvar_xp );
66        condvar_ptr = GET_PTR( condvar_xp );
67        current = (intptr_t)hal_remote_lpt( XPTR( condvar_cxy , &condvar_ptr->ident ) );   
68
69        if( current == ident )
70        {
71            found = true;
72            break;
73        }
74    }
75
76    // relese lock protecting synchros lists
77    remote_queuelock_release( lock_xp );
78 
79    if( found == false )  return XPTR_NULL;
80    else                  return condvar_xp;
81
82}  // end remote_condvar_from_ident()
83
84/////////////////////////////////////////////////
85error_t remote_condvar_create( intptr_t   ident )
86{
87    remote_condvar_t * condvar_ptr;
88
89    // get pointer on local process descriptor
90    process_t * process = CURRENT_THREAD->process;
91
92    // get extended pointer on reference process
93    xptr_t      ref_xp = process->ref_xp;
94
95    // get reference process cluster and local pointer
96    cxy_t       ref_cxy = GET_CXY( ref_xp );
97    process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
98
99    // allocate memory for condvar descriptor
100    condvar_ptr = kmem_alloc( bits_log2(sizeof(remote_condvar_t)) , AF_ZERO );
101
102    if( condvar_ptr == NULL )
103    {
104        printk("\n[ERROR] in %s : cannot create condvar\n", __FUNCTION__ );
105        return -1;
106    }
107
108    // initialise condvar
109        hal_remote_spt( XPTR( ref_cxy , &condvar_ptr->ident ) , (void *)ident );
110        xlist_root_init( XPTR( ref_cxy , &condvar_ptr->root ) );
111        xlist_entry_init( XPTR( ref_cxy , &condvar_ptr->list ) );
112    remote_busylock_init( XPTR( ref_cxy , &condvar_ptr->lock ), LOCK_CONDVAR_STATE );
113
114    // register condvar in reference process xlist
115    xptr_t root_xp = XPTR( ref_cxy , &ref_ptr->condvar_root );
116    xptr_t list_xp = XPTR( ref_cxy , &condvar_ptr->list );
117
118    remote_queuelock_acquire( XPTR( ref_cxy , &ref_ptr->sync_lock ) );
119    xlist_add_first( root_xp , list_xp );
120    remote_queuelock_release( XPTR( ref_cxy , &ref_ptr->sync_lock ) );
121
122    return 0;
123
124}  // end remote_condvar_create()
125
126////////////////////////////////////////////////
127void remote_condvar_destroy( xptr_t condvar_xp )
128{
129    // get pointer on local process descriptor
130    process_t * process = CURRENT_THREAD->process;
131
132    // get extended pointer on reference process
133    xptr_t      ref_xp = process->ref_xp;
134
135    // get reference process cluster and local pointer
136    cxy_t       ref_cxy = GET_CXY( ref_xp );
137    process_t * ref_ptr = GET_PTR( ref_xp );
138
139    // get condvar cluster and local pointer
140    cxy_t              condvar_cxy = GET_CXY( condvar_xp );
141    remote_condvar_t * condvar_ptr = GET_PTR( condvar_xp );
142
143    // get remote pointer on waiting queue root
144    xptr_t root_xp = XPTR( condvar_cxy , &condvar_ptr->root );
145 
146    if( !xlist_is_empty( root_xp ) )   // user error
147    {
148        printk("WARNING in %s for thread %x in process %x : "
149               "destroy condvar, but  waiting threads queue not empty\n", 
150               __FUNCTION__ , CURRENT_THREAD->trdid , CURRENT_THREAD->process->pid );
151    }
152
153    // remove condvar from reference process xlist
154    remote_queuelock_acquire( XPTR( ref_cxy , &ref_ptr->sync_lock ) );
155    xlist_unlink( XPTR( condvar_cxy , &condvar_ptr->list ) );
156    remote_queuelock_release( XPTR( ref_cxy , &ref_ptr->sync_lock ) );
157
158    // release memory allocated for condvar descriptor
159    kmem_remote_free( ref_cxy , condvar_ptr , bits_log2(sizeof(remote_condvar_t)) );
160
161}  // end remote_convar_destroy()
162
163////////////////////////////////////////////
164void remote_condvar_wait( xptr_t condvar_xp,
165                          xptr_t mutex_xp )
166{
167    thread_t * this = CURRENT_THREAD;
168
169    // check calling thread can yield
170    thread_assert_can_yield( this , __FUNCTION__ );
171
172    // get condvar cluster and local pointer
173    remote_condvar_t * condvar_ptr = GET_PTR( condvar_xp );
174    cxy_t              condvar_cxy = GET_CXY( condvar_xp );
175
176    // register the calling thread in condvar waiting queue
177    xlist_add_last( XPTR( condvar_cxy , &condvar_ptr->root ),
178                    XPTR( local_cxy   , &this->wait_xlist ) );
179
180    // block the calling thread
181    thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_USERSYNC );
182
183    // release the mutex
184    remote_mutex_unlock( mutex_xp );
185
186    // deschedule
187    sched_yield("blocked on condvar");
188
189    // re-acquire the mutex
190    remote_mutex_lock( mutex_xp );
191
192}  // end remote_condvar_wait()
193
194///////////////////////////////////////////////
195void remote_condvar_signal( xptr_t condvar_xp )
196{
197    // get condvar cluster and local pointer
198    remote_condvar_t * condvar_ptr = GET_PTR( condvar_xp );
199    cxy_t              condvar_cxy = GET_CXY( condvar_xp );
200
201    // does nothing if waiting queue empty
202    if( xlist_is_empty( XPTR( condvar_cxy, &condvar_ptr->root ) ) == false )
203    {
204         // get first waiting thread
205         xptr_t thread_xp = XLIST_FIRST( XPTR( condvar_cxy , &condvar_ptr->root ),
206                                         thread_t , wait_xlist );
207
208         // remove this waiting thread from queue
209         thread_t * thread_ptr = GET_PTR( thread_xp );
210         cxy_t      thread_cxy = GET_CXY( thread_xp );
211         xlist_unlink( XPTR( thread_cxy , &thread_ptr->wait_xlist ) );
212
213         // unblock this waiting thread
214         thread_unblock( thread_xp , THREAD_BLOCKED_USERSYNC );
215    }
216
217}  // end remote_condvar_signal()
218
219//////////////////////////////////////////////////
220void remote_condvar_broadcast( xptr_t condvar_xp )
221{
222    // get condvar cluster and local pointer
223    remote_condvar_t * condvar_ptr = GET_PTR( condvar_xp );
224    cxy_t              condvar_cxy = GET_CXY( condvar_xp );
225
226    // does nothing if waiting queue empty
227    while( xlist_is_empty( XPTR( condvar_cxy , &condvar_ptr->root ) ) == false )
228    {
229         // get first waiting thread
230         xptr_t thread_xp = XLIST_FIRST( XPTR( condvar_cxy , &condvar_ptr->root ),
231                                         thread_t , wait_xlist );
232
233         // remove this waiting thread from queue
234         thread_t * thread_ptr = GET_PTR( thread_xp );
235         cxy_t      thread_cxy = GET_CXY( thread_xp );
236         xlist_unlink( XPTR( thread_cxy , &thread_ptr->wait_xlist ) );
237
238         // unblock this waiting thread
239         thread_unblock( thread_xp , THREAD_BLOCKED_USERSYNC );
240    }
241}  // end remote_condvar_broadcast()
242
Note: See TracBrowser for help on using the repository browser.