Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

kernel/HOLD/semaphore.c

Go to the documentation of this file.
00001 
00006 /*
00007  *  The contents of this file are subject to the Mozilla Public License
00008  *  Version 1.0 (the "License"); you may not use this file except in
00009  *  compliance with the License. You may obtain a copy of the License at
00010  *  http://www.mozilla.org/MPL/
00011  *
00012  *  Software distributed under the License is distributed on an "AS IS"
00013  *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
00014  *  License for the specific language governing rights and limitations
00015  *  under the License.
00016  *
00017  *  The Original Code is legOS code, released October 17, 1999.
00018  *
00019  *  The Initial Developer of the Original Code is Markus L. Noga.
00020  *  Portions created by Markus L. Noga are Copyright (C) 1999
00021  *  Markus L. Noga. All Rights Reserved.
00022  *
00023  *  Contributor(s): Markus L. Noga <markus@noga.de>
00024  */
00025 
00026 #include <semaphore.h>
00027 
00028 #ifdef CONF_SEMAPHORES
00029 
00030 #include <unistd.h>
00031 
00033 //
00034 // Functions
00035 //
00037 
00039 
00041 wakeup_t sem_event_wait(wakeup_t data) {
00042         sem_t *sem=(sem_t*) ((unsigned)data);
00043         
00044         // we're called by the scheduler, therefore in an IRQ handler,
00045         // so no worrying about IRQs.
00046         //
00047         if(*sem) {
00048                 (*sem)--;
00049                 return 1;                       // sem!=0 -> wakeup
00050         }
00051         return 0;
00052 }
00053 
00055 
00063 int sem_wait(sem_t * sem) {
00064         // check if semaphore is available, if not, go to sleep
00065         
00066         if(sem_trywait(sem))
00067                 if (wait_event(sem_event_wait,(unsigned long) ((unsigned)sem)) == 0)
00068                         return -1;
00069         
00070         return 0;
00071 }
00072 
00074 
00084 int sem_trywait(sem_t * sem);
00085 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00086 __asm__("
00087 .text
00088 .align 1
00089 .globl _sem_trywait
00090 _sem_trywait:
00091         stc ccr,r1h                             ; save flags    
00092         orc #0x80,ccr                           ; block all but NMI
00093         mov.b @r0,r1l
00094         beq sem_fail                            ; !=0 -> decrease, return 0
00095           dec r1l
00096           mov.b r1l,@r0
00097           sub.w r0,r0
00098           bra sem_ok
00099  sem_fail:mov #0xffff,r0                                ; else return 0xffff
00100  sem_ok:ldc r1h,ccr                             ; restore flags
00101         rts
00102         ");
00103 #endif // DOXYGEN_SHOULD_SKIP_THIS
00104         
00106 
00112 int sem_post(sem_t * sem);
00113 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00114 __asm__("
00115 .text
00116 .align 1
00117 .globl _sem_post
00118 _sem_post:
00119         stc ccr,r1h                             ; save flags
00120         orc #0x80,ccr                           ; disable all but NMI
00121         mov.b @r0,r1l
00122         inc r1l
00123         mov.b r1l,@r0
00124         ldc r1h,ccr                             ; restore flags
00125 
00126         sub r0,r0                               ; return 0
00127         rts
00128         ");
00129 #endif // DOXYGEN_SHOULD_SKIP_THIS
00130 
00131 
00132 #endif // CONF_SEMAPHORES

brickOS is released under the Mozilla Public License.
Original code copyright 1998-2002 by the authors.

Generated on Mon Feb 16 21:02:10 2004 for brickOS Kernel Developer by doxygen 1.3.5