1 /* 2 SDL - Simple DirectMedia Layer 3 Copyright (C) 1997-2006 Sam Lantinga 4 5 This library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 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 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with this library; if not, write to the Free Software 17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 19 Sam Lantinga 20 slouken (at) libsdl.org 21 */ 22 #include "SDL_config.h" 23 24 /* An implementation of semaphores using mutexes and condition variables */ 25 26 #include "SDL_thread.h" 27 #include "SDL_systhread_c.h" 28 29 30 struct SDL_semaphore 31 { 32 struct SignalSemaphore Sem; 33 }; 34 35 #undef D 36 37 #define D(x) 38 39 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) 40 { 41 SDL_sem *sem; 42 43 sem = (SDL_sem *)SDL_malloc(sizeof(*sem)); 44 45 if ( ! sem ) { 46 SDL_OutOfMemory(); 47 return(0); 48 } 49 50 D(bug("Creating semaphore %lx...\n",sem)); 51 52 SDL_memset(sem,0,sizeof(*sem)); 53 54 InitSemaphore(&sem->Sem); 55 56 return(sem); 57 } 58 59 void SDL_DestroySemaphore(SDL_sem *sem) 60 { 61 D(bug("Destroying semaphore %lx...\n",sem)); 62 63 if ( sem ) { 64 // Condizioni per liberare i task in attesa? 65 SDL_free(sem); 66 } 67 } 68 69 int SDL_SemTryWait(SDL_sem *sem) 70 { 71 if ( ! sem ) { 72 SDL_SetError("Passed a NULL semaphore"); 73 return -1; 74 } 75 76 D(bug("TryWait semaphore...%lx\n",sem)); 77 78 ObtainSemaphore(&sem->Sem); 79 // ReleaseSemaphore(&sem->Sem); 80 81 return 1; 82 } 83 84 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) 85 { 86 int retval; 87 88 89 if ( ! sem ) { 90 SDL_SetError("Passed a NULL semaphore"); 91 return -1; 92 } 93 94 D(bug("WaitTimeout (%ld) semaphore...%lx\n",timeout,sem)); 95 96 /* A timeout of 0 is an easy case */ 97 if ( timeout == 0 ) { 98 ObtainSemaphore(&sem->Sem); 99 return 1; 100 } 101 if(!(retval=AttemptSemaphore(&sem->Sem))) 102 { 103 SDL_Delay(timeout); 104 retval=AttemptSemaphore(&sem->Sem); 105 } 106 107 if(retval==TRUE) 108 { 109 // ReleaseSemaphore(&sem->Sem); 110 retval=1; 111 } 112 113 return retval; 114 } 115 116 int SDL_SemWait(SDL_sem *sem) 117 { 118 ObtainSemaphore(&sem->Sem); 119 return 0; 120 } 121 122 Uint32 SDL_SemValue(SDL_sem *sem) 123 { 124 Uint32 value; 125 126 value = 0; 127 if ( sem ) { 128 #ifdef STORMC4_WOS 129 value = sem->Sem.ssppc_SS.ss_NestCount; 130 #else 131 value = sem->Sem.ss_NestCount; 132 #endif 133 } 134 return value; 135 } 136 137 int SDL_SemPost(SDL_sem *sem) 138 { 139 if ( ! sem ) { 140 SDL_SetError("Passed a NULL semaphore"); 141 return -1; 142 } 143 D(bug("SemPost semaphore...%lx\n",sem)); 144 145 ReleaseSemaphore(&sem->Sem); 146 return 0; 147 } 148 149