src/thread/amigaos/SDL_syssem.c

/* [<][>]
[^][v][top][bottom][index][help] */

FUNCTIONS

This source file includes following functions.
  1. D
  2. SDL_CreateSemaphore
  3. SDL_DestroySemaphore
  4. SDL_SemTryWait
  5. SDL_SemWaitTimeout
  6. SDL_SemWait
  7. SDL_SemValue
  8. SDL_SemPost

   1 /*
   2     SDL - Simple DirectMedia Layer
   3     Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
   4 
   5     This library is free software; you can redistribute it and/or
   6     modify it under the terms of the GNU Library General Public
   7     License as published by the Free Software Foundation; either
   8     version 2 of the License, or (at your option) any later version.
   9 
  10     This library is distributed in the hope that it will be useful,
  11     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13     Library General Public License for more details.
  14 
  15     You should have received a copy of the GNU Library General Public
  16     License along with this library; if not, write to the Free
  17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18 
  19     Sam Lantinga
  20     slouken@devolution.com
  21 */
  22 
  23 #ifdef SAVE_RCSID
  24 static char rcsid =
  25  "@(#) $Id: SDL_syssem.c,v 1.1.2.2 2001/02/10 07:20:04 hercules Exp $";
  26 #endif
  27 
  28 /* An native implementation of semaphores on AmigaOS */
  29 
  30 #include "SDL_error.h"
  31 #include "SDL_thread.h"
  32 #include "SDL_systhread_c.h"
  33 
  34 
  35 struct SDL_semaphore
  36 {
  37         struct SignalSemaphore Sem;
  38 };
  39 
  40 #undef D(x)
  41 
  42 #define D(x)
     /* [<][>][^][v][top][bottom][index][help] */
  43 
  44 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
     /* [<][>][^][v][top][bottom][index][help] */
  45 {
  46         SDL_sem *sem;
  47 
  48         sem = (SDL_sem *)malloc(sizeof(*sem));
  49         if ( ! sem ) {
  50                 SDL_OutOfMemory();
  51                 return(0);
  52         }
  53         memset(sem, 0, sizeof(*sem));
  54 
  55         D(bug("Creating semaphore %lx...\n",sem));
  56 
  57         InitSemaphore(&sem->Sem);
  58 #if 1 // Allow multiple obtainings of the semaphore
  59         while ( initial_value-- ) {
  60                 ReleaseSemaphore(&sem->Sem);
  61         }
  62 #endif
  63         return(sem);
  64 }
  65 
  66 void SDL_DestroySemaphore(SDL_sem *sem)
     /* [<][>][^][v][top][bottom][index][help] */
  67 {
  68         D(bug("Destroying semaphore %lx...\n",sem));
  69 
  70         if ( sem ) {
  71 // Condizioni per liberare i task in attesa?
  72                 free(sem);
  73         }
  74 }
  75 
  76 int SDL_SemTryWait(SDL_sem *sem)
     /* [<][>][^][v][top][bottom][index][help] */
  77 {
  78         int retval;
  79 
  80         if ( ! sem ) {
  81                 SDL_SetError("Passed a NULL semaphore");
  82                 return -1;
  83         }
  84 
  85         D(bug("TryWait semaphore...%lx\n",sem));
  86 
  87         retval = SDL_MUTEX_TIMEDOUT;
  88         if ( AttemptSemaphore(&sem->Sem) ) {
  89                 retval = 0;
  90         }
  91         return retval;
  92 }
  93 
  94 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
     /* [<][>][^][v][top][bottom][index][help] */
  95 {
  96         int retval;
  97 
  98         if ( ! sem ) {
  99                 SDL_SetError("Passed a NULL semaphore");
 100                 return -1;
 101         }
 102 
 103         D(bug("WaitTimeout (%ld) semaphore...%lx\n",timeout,sem));
 104 
 105 #if 1 // We need to keep trying the semaphore until the timeout expires
 106         retval = SDL_MUTEX_TIMEDOUT;
 107         then = SDL_GetTicks();
 108         do {
 109                 if ( AttemptSemaphore(&sem->Sem) ) {
 110                         retval = 0;
 111                 }
 112                 now = SDL_GetTicks();
 113         } while ( (retval == SDL_MUTEX_TIMEDOUT) && ((now-then) < timeout) );
 114 #else
 115         if(!(retval=AttemptSemaphore(&sem->Sem)))
 116         {
 117                 SDL_Delay(timeout);
 118                 retval=AttemptSemaphore(&sem->Sem);
 119         }
 120 
 121         if(retval==TRUE)
 122         {
 123 //              ReleaseSemaphore(&sem->Sem);
 124                 retval=1;
 125         }
 126 #endif
 127         return retval;
 128 }
 129 
 130 int SDL_SemWait(SDL_sem *sem)
     /* [<][>][^][v][top][bottom][index][help] */
 131 {
 132         if ( ! sem ) {
 133                 SDL_SetError("Passed a NULL semaphore");
 134                 return -1;
 135         }
 136 #if 1 // This should be an infinite wait - FIXME, what is the return value?
 137         ObtainSemaphore(&sem->Sem);
 138         return 0;
 139 #else
 140         return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
 141 #endif
 142 }
 143 
 144 Uint32 SDL_SemValue(SDL_sem *sem)
     /* [<][>][^][v][top][bottom][index][help] */
 145 {
 146         Uint32 value;
 147         
 148         value = 0;
 149         if ( sem ) {
 150                 value = sem->Sem.ss_NestCount;
 151         }
 152         return value;
 153 }
 154 
 155 int SDL_SemPost(SDL_sem *sem)
     /* [<][>][^][v][top][bottom][index][help] */
 156 {
 157         if ( ! sem ) {
 158                 SDL_SetError("Passed a NULL semaphore");
 159                 return -1;
 160         }
 161         D(bug("SemPost semaphore...%lx\n",sem));
 162 
 163         ReleaseSemaphore(&sem->Sem);
 164         return 0;
 165 }
 166 

/* [<][>][^][v][top][bottom][index][help] */