SimpleOS

LXR

Navigation



Site hébergé par : enix

The LXR Cross Referencer for SOS

source navigation ]
diff markup ]
identifier search ]
general search ]
 
 
Article:1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 6.5 ] [ 7 ] [ 7.5 ] [ 8 ] [ 9 ] [ 9.5 ]

Diff markup

Differences between /sos/ksynch.c (Article 8) and /sos/ksynch.c (Article 6.5)


001 /* Copyright (C) 2004 David Decotigny             001 /* Copyright (C) 2004 David Decotigny
002                                                   002 
003    This program is free software; you can redi    003    This program is free software; you can redistribute it and/or
004    modify it under the terms of the GNU Genera    004    modify it under the terms of the GNU General Public License
005    as published by the Free Software Foundatio    005    as published by the Free Software Foundation; either version 2
006    of the License, or (at your option) any lat    006    of the License, or (at your option) any later version.
007                                                   007    
008    This program is distributed in the hope tha    008    This program is distributed in the hope that it will be useful,
009    but WITHOUT ANY WARRANTY; without even the     009    but WITHOUT ANY WARRANTY; without even the implied warranty of
010    MERCHANTABILITY or FITNESS FOR A PARTICULAR    010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
011    GNU General Public License for more details    011    GNU General Public License for more details.
012                                                   012    
013    You should have received a copy of the GNU     013    You should have received a copy of the GNU General Public License
014    along with this program; if not, write to t    014    along with this program; if not, write to the Free Software
015    Foundation, Inc., 59 Temple Place - Suite 3    015    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
016    USA.                                           016    USA. 
017 */                                                017 */
018                                                   018 
019                                                   019 
020 #include <hwcore/irq.h>                           020 #include <hwcore/irq.h>
021                                                   021 
022                                                   022 
023 #include "ksynch.h"                               023 #include "ksynch.h"
024                                                   024 
025                                                   025 
026 sos_ret_t sos_ksema_init(struct sos_ksema *sem    026 sos_ret_t sos_ksema_init(struct sos_ksema *sema, const char *name,
027                          int initial_value,    !! 027                          int initial_value)
028                          sos_kwaitq_ordering_t << 
029 {                                                 028 {
030   sema->value = initial_value;                    029   sema->value = initial_value;
031   return sos_kwaitq_init(& sema->kwaitq, name, !! 030   return sos_kwaitq_init(& sema->kwaitq, name);
032 }                                                 031 }
033                                                   032 
034                                                   033 
035 sos_ret_t sos_ksema_dispose(struct sos_ksema *    034 sos_ret_t sos_ksema_dispose(struct sos_ksema *sema)
036 {                                                 035 {
037   return sos_kwaitq_dispose(& sema->kwaitq);      036   return sos_kwaitq_dispose(& sema->kwaitq);
038 }                                                 037 }
039                                                   038 
040                                                   039 
041 sos_ret_t sos_ksema_down(struct sos_ksema *sem    040 sos_ret_t sos_ksema_down(struct sos_ksema *sema,
042                          struct sos_time *time    041                          struct sos_time *timeout)
043 {                                                 042 {
044   sos_ui32_t flags;                               043   sos_ui32_t flags;
045   sos_ret_t retval;                               044   sos_ret_t retval;
046                                                   045 
047   sos_disable_IRQs(flags);                        046   sos_disable_IRQs(flags);
048   retval = SOS_OK;                                047   retval = SOS_OK;
049                                                   048 
050   sema->value --;                                 049   sema->value --;
051   if (sema->value < 0)                            050   if (sema->value < 0)
052     {                                             051     {
053       /* Wait for somebody to wake us */          052       /* Wait for somebody to wake us */
054       retval = sos_kwaitq_wait(& sema->kwaitq,    053       retval = sos_kwaitq_wait(& sema->kwaitq, timeout);
055                                                   054 
056       /* Something wrong happened (timeout, ex    055       /* Something wrong happened (timeout, external wakeup, ...) ? */
057       if (SOS_OK != retval)                       056       if (SOS_OK != retval)
058         {                                         057         {
059           /* Yes: pretend we did not ask for t    058           /* Yes: pretend we did not ask for the semaphore */
060           sema->value ++;                         059           sema->value ++;
061         }                                         060         }
062     }                                             061     }
063                                                   062 
064   sos_restore_IRQs(flags);                        063   sos_restore_IRQs(flags);
065   return retval;                                  064   return retval;
066 }                                                 065 }
067                                                   066 
068                                                   067 
069 sos_ret_t sos_ksema_trydown(struct sos_ksema *    068 sos_ret_t sos_ksema_trydown(struct sos_ksema *sema)
070 {                                                 069 {
071   sos_ui32_t flags;                               070   sos_ui32_t flags;
072   sos_ret_t retval;                               071   sos_ret_t retval;
073                                                   072 
074   sos_disable_IRQs(flags);                        073   sos_disable_IRQs(flags);
075                                                   074 
076   /* Can we take the semaphore without blockin    075   /* Can we take the semaphore without blocking ? */
077   if (sema->value >= 1)                           076   if (sema->value >= 1)
078     {                                             077     {
079       /* Yes: we take it now */                   078       /* Yes: we take it now */
080       sema->value --;                             079       sema->value --;      
081       retval = SOS_OK;                            080       retval = SOS_OK;
082     }                                             081     }
083   else                                            082   else
084     {                                             083     {
085       /* No: we signal it */                      084       /* No: we signal it */
086       retval = -SOS_EBUSY;                        085       retval = -SOS_EBUSY;
087     }                                             086     }
088                                                   087 
089   sos_restore_IRQs(flags);                        088   sos_restore_IRQs(flags);
090   return retval;                                  089   return retval;
091 }                                                 090 }
092                                                   091 
093                                                   092 
094 sos_ret_t sos_ksema_up(struct sos_ksema *sema)    093 sos_ret_t sos_ksema_up(struct sos_ksema *sema)
095 {                                                 094 {
096   sos_ui32_t flags;                               095   sos_ui32_t flags;
097   sos_ret_t retval;                               096   sos_ret_t retval;
098                                                   097 
099   sos_disable_IRQs(flags);                        098   sos_disable_IRQs(flags);
100                                                   099 
101   sema->value ++;                                 100   sema->value ++;
102   retval = sos_kwaitq_wakeup(& sema->kwaitq, 1    101   retval = sos_kwaitq_wakeup(& sema->kwaitq, 1, SOS_OK);
103                                                   102 
104   sos_restore_IRQs(flags);                        103   sos_restore_IRQs(flags);
105   return retval;                                  104   return retval;
106 }                                                 105 }
107                                                   106 
108                                                   107 
109 sos_ret_t sos_kmutex_init(struct sos_kmutex *m !! 108 sos_ret_t sos_kmutex_init(struct sos_kmutex *mutex, const char *name)
110                           sos_kwaitq_ordering_ << 
111 {                                                 109 {
112   mutex->owner = NULL;                            110   mutex->owner = NULL;
113   return sos_kwaitq_init(& mutex->kwaitq, name !! 111   return sos_kwaitq_init(& mutex->kwaitq, name);
114 }                                                 112 }
115                                                   113 
116                                                   114 
117 sos_ret_t sos_kmutex_dispose(struct sos_kmutex    115 sos_ret_t sos_kmutex_dispose(struct sos_kmutex *mutex)
118 {                                                 116 {
119   return sos_kwaitq_dispose(& mutex->kwaitq);     117   return sos_kwaitq_dispose(& mutex->kwaitq);
120 }                                                 118 }
121                                                   119 
122                                                   120 
123 /*                                                121 /*
124  * Implementation based on ownership transfer     122  * Implementation based on ownership transfer (ie no while()
125  * loop). The only assumption is that the thre    123  * loop). The only assumption is that the thread awoken by
126  * kmutex_unlock is not suppressed before effe    124  * kmutex_unlock is not suppressed before effectively waking up: in
127  * that case the mutex will be forever locked     125  * that case the mutex will be forever locked AND unlockable (by
128  * nobody other than the owner, but this is no    126  * nobody other than the owner, but this is not natural since this
129  * owner already issued an unlock()...). The s    127  * owner already issued an unlock()...). The same problem happens with
130  * the semaphores, but in a less obvious manne    128  * the semaphores, but in a less obvious manner.
131  */                                               129  */
132 sos_ret_t sos_kmutex_lock(struct sos_kmutex *m    130 sos_ret_t sos_kmutex_lock(struct sos_kmutex *mutex,
133                           struct sos_time *tim    131                           struct sos_time *timeout)
134 {                                                 132 {
135   __label__ exit_kmutex_lock;                     133   __label__ exit_kmutex_lock;
136   sos_ui32_t flags;                               134   sos_ui32_t flags;
137   sos_ret_t retval;                               135   sos_ret_t retval;
138                                                   136 
139   sos_disable_IRQs(flags);                        137   sos_disable_IRQs(flags);
140   retval = SOS_OK;                                138   retval = SOS_OK;
141                                                   139 
142   /* Mutex already owned ? */                     140   /* Mutex already owned ? */
143   if (NULL != mutex->owner)                       141   if (NULL != mutex->owner)
144     {                                             142     {
145       /* Owned by us or by someone else ? */      143       /* Owned by us or by someone else ? */
146       if (sos_thread_get_current() == mutex->o    144       if (sos_thread_get_current() == mutex->owner)
147         {                                         145         {
148           /* Owned by us: do nothing */           146           /* Owned by us: do nothing */
149           retval = -SOS_EBUSY;                    147           retval = -SOS_EBUSY;
150           goto exit_kmutex_lock;                  148           goto exit_kmutex_lock;
151         }                                         149         }
152                                                   150 
153       /* Wait for somebody to wake us */          151       /* Wait for somebody to wake us */
154       retval = sos_kwaitq_wait(& mutex->kwaitq    152       retval = sos_kwaitq_wait(& mutex->kwaitq, timeout);
155                                                   153 
156       /* Something wrong happened ? */            154       /* Something wrong happened ? */
157       if (SOS_OK != retval)                       155       if (SOS_OK != retval)
158         {                                         156         {
159           goto exit_kmutex_lock;                  157           goto exit_kmutex_lock;
160         }                                         158         }
161     }                                             159     }
162                                                   160 
163   /* Ok, the mutex is available to us: take it    161   /* Ok, the mutex is available to us: take it */
164   mutex->owner = sos_thread_get_current();        162   mutex->owner = sos_thread_get_current();
165                                                   163 
166  exit_kmutex_lock:                                164  exit_kmutex_lock:
167   sos_restore_IRQs(flags);                        165   sos_restore_IRQs(flags);
168   return retval;                                  166   return retval;
169 }                                                 167 }
170                                                   168 
171                                                   169 
172 sos_ret_t sos_kmutex_trylock(struct sos_kmutex    170 sos_ret_t sos_kmutex_trylock(struct sos_kmutex *mutex)
173 {                                                 171 {
174   sos_ui32_t flags;                               172   sos_ui32_t flags;
175   sos_ret_t retval;                               173   sos_ret_t retval;
176                                                   174 
177   sos_disable_IRQs(flags);                        175   sos_disable_IRQs(flags);
178                                                   176 
179   /* Mutex available to us ? */                   177   /* Mutex available to us ? */
180   if (NULL == mutex->owner)                       178   if (NULL == mutex->owner)
181     {                                             179     {
182       /* Great ! Take it now */                   180       /* Great ! Take it now */
183       mutex->owner = sos_thread_get_current();    181       mutex->owner = sos_thread_get_current();
184                                                   182 
185       retval = SOS_OK;                            183       retval = SOS_OK;
186     }                                             184     }
187   else                                            185   else
188     {                                             186     {
189       /* No: signal it */                         187       /* No: signal it */
190       retval = -SOS_EBUSY;                        188       retval = -SOS_EBUSY;
191     }                                             189     }
192                                                   190 
193   sos_restore_IRQs(flags);                        191   sos_restore_IRQs(flags);
194   return retval;                                  192   return retval;
195 }                                                 193 }
196                                                   194 
197                                                   195 
198 sos_ret_t sos_kmutex_unlock(struct sos_kmutex     196 sos_ret_t sos_kmutex_unlock(struct sos_kmutex *mutex)
199 {                                                 197 {
200   sos_ui32_t flags;                               198   sos_ui32_t flags;
201   sos_ret_t  retval;                              199   sos_ret_t  retval;
202                                                   200 
203   sos_disable_IRQs(flags);                        201   sos_disable_IRQs(flags);
204                                                   202 
205   if (sos_thread_get_current() != mutex->owner    203   if (sos_thread_get_current() != mutex->owner)
206     retval = -SOS_EPERM;                          204     retval = -SOS_EPERM;
207                                                   205 
208   else if (sos_kwaitq_is_empty(& mutex->kwaitq    206   else if (sos_kwaitq_is_empty(& mutex->kwaitq))
209     {                                             207     {
210       /*                                          208       /*
211        * There is NOT ANY thread waiting => we    209        * There is NOT ANY thread waiting => we really mark the mutex
212        * as FREE                                  210        * as FREE
213        */                                         211        */
214       mutex->owner = NULL;                        212       mutex->owner = NULL;
215       retval = SOS_OK;                            213       retval = SOS_OK;
216     }                                             214     }
217   else                                            215   else
218     {                                             216     {
219       /*                                          217       /*
220        * There is at least 1 thread waiting =>    218        * There is at least 1 thread waiting => we DO NOT mark the
221        * mutex as free !                          219        * mutex as free !
222        * Actually, we should have written:        220        * Actually, we should have written:
223        *   mutex->owner = thread_that_is_woken    221        *   mutex->owner = thread_that_is_woken_up;
224        * But the real Id of the next thread ow    222        * But the real Id of the next thread owning the mutex is not
225        * that important. What is important her    223        * that important. What is important here is that mutex->owner
226        * IS NOT NULL. Otherwise there will be     224        * IS NOT NULL. Otherwise there will be a possibility for the
227        * thread woken up here to have the mute    225        * thread woken up here to have the mutex stolen by a thread
228        * locking the mutex in the meantime.       226        * locking the mutex in the meantime.
229        */                                         227        */
230       retval = sos_kwaitq_wakeup(& mutex->kwai    228       retval = sos_kwaitq_wakeup(& mutex->kwaitq, 1, SOS_OK);
231     }                                             229     } 
232                                                   230  
233   sos_restore_IRQs(flags);                        231   sos_restore_IRQs(flags);
234   return retval;                                  232   return retval;
235 }                                                 233 }
                                                      

source navigation ] diff markup ] identifier search ] general search ]