semaphore.c

Go to the documentation of this file.
00001 /*
00002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00003 %                                                                             %
00004 %                                                                             %
00005 %                                                                             %
00006 %        SSSSS  EEEEE  M   M   AAA   PPPP   H   H   OOO   RRRR   EEEEE        %
00007 %        SS     E      MM MM  A   A  P   P  H   H  O   O  R   R  E            %
00008 %         SSS   EEE    M M M  AAAAA  PPPP   HHHHH  O   O  RRRR   EEE          %
00009 %           SS  E      M   M  A   A  P      H   H  O   O  R R    E            %
00010 %        SSSSS  EEEEE  M   M  A   A  P      H   H   OOO   R  R   EEEEE        %
00011 %                                                                             %
00012 %                                                                             %
00013 %                        WizardCore Semaphore Methods                         %
00014 %                                                                             %
00015 %                              Software Design                                %
00016 %                             William Radcliffe                               %
00017 %                                John Cristy                                  %
00018 %                                 June 2000                                   %
00019 %                                                                             %
00020 %                                                                             %
00021 %  Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization      %
00022 %  dedicated to making software imaging solutions freely available.           %
00023 %                                                                             %
00024 %  You may not use this file except in compliance with the License.  You may  %
00025 %  obtain a copy of the License at                                            %
00026 %                                                                             %
00027 %    http://www.wizards-toolkit.org/script/license.php                        %
00028 %                                                                             %
00029 %  Unless required by applicable law or agreed to in writing, software        %
00030 %  distributed under the License is distributed on an "AS IS" BASIS,          %
00031 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
00032 %  See the License for the specific language governing permissions and        %
00033 %  limitations under the License.                                             %
00034 %                                                                             %
00035 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00036 %
00037 %
00038 %
00039 */
00040 
00041 /*
00042   Include declarations.
00043 */
00044 #include "wizard/studio.h"
00045 #include "wizard/exception.h"
00046 #include "wizard/exception-private.h"
00047 #include "wizard/memory_.h"
00048 #include "wizard/semaphore.h"
00049 #include "wizard/semaphore-private.h"
00050 #include "wizard/string_.h"
00051 #include "wizard/thread_.h"
00052 #include "wizard/thread-private.h"
00053 
00054 /*
00055   Struct declaractions.
00056 */
00057 struct SemaphoreInfo
00058 {
00059   WizardMutexType
00060     mutex;
00061 
00062   WizardThreadType
00063     id;
00064 
00065   ssize_t
00066     reference_count;
00067 
00068   size_t
00069     signature;
00070 };
00071 
00072 /*
00073 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00074 %                                                                             %
00075 %                                                                             %
00076 %                                                                             %
00077 %   A c q u i r e S e m a p h o r e I n f o                                   %
00078 %                                                                             %
00079 %                                                                             %
00080 %                                                                             %
00081 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00082 %
00083 %  AcquireSemaphoreInfo() acquires a semaphore.
00084 %
00085 %  The format of the AcquireSemaphoreInfo method is:
00086 %
00087 %      void AcquireSemaphoreInfo(SemaphoreInfo **semaphore_info)
00088 %
00089 %  A description of each parameter follows:
00090 %
00091 %    o semaphore_info: Specifies a pointer to an SemaphoreInfo structure.
00092 %
00093 */
00094 WizardExport void AcquireSemaphoreInfo(SemaphoreInfo **semaphore_info)
00095 {
00096   assert(semaphore_info != (SemaphoreInfo **) NULL);
00097   if (*semaphore_info == (SemaphoreInfo *) NULL)
00098     {
00099       LockWizardMutex();
00100       if (*semaphore_info == (SemaphoreInfo *) NULL)
00101         *semaphore_info=AllocateSemaphoreInfo();
00102       UnlockWizardMutex();
00103     }
00104 }
00105 
00106 /*
00107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00108 %                                                                             %
00109 %                                                                             %
00110 %                                                                             %
00111 %   A l l o c a t e S e m a p h o r e I n f o                                 %
00112 %                                                                             %
00113 %                                                                             %
00114 %                                                                             %
00115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00116 %
00117 %  AllocateSemaphoreInfo() initializes the SemaphoreInfo structure.
00118 %
00119 %  The format of the AllocateSemaphoreInfo method is:
00120 %
00121 %      SemaphoreInfo *AllocateSemaphoreInfo(void)
00122 %
00123 */
00124 WizardExport SemaphoreInfo *AllocateSemaphoreInfo(void)
00125 {
00126   SemaphoreInfo
00127     *semaphore_info;
00128 
00129   /*
00130     Allocate semaphore.
00131   */
00132   semaphore_info=(SemaphoreInfo *) AcquireAlignedMemory(1,
00133     sizeof(SemaphoreInfo));
00134   if (semaphore_info == (SemaphoreInfo *) NULL)
00135     ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
00136   (void) ResetWizardMemory(semaphore_info,0,sizeof(SemaphoreInfo));
00137   /*
00138     Initialize the semaphore.
00139   */
00140 #if defined(WIZARDSTOOLKIT_HAVE_PTHREAD)
00141   {
00142     int
00143       status;
00144 
00145     pthread_mutexattr_t
00146       mutex_info;
00147 
00148     status=pthread_mutexattr_init(&mutex_info);
00149     if (status != 0)
00150       {
00151         errno=status;
00152         ThrowFatalException(ResourceFatalError,
00153           "unable to instantiate semaphore `%s'");
00154       }
00155     status=pthread_mutex_init(&semaphore_info->mutex,&mutex_info);
00156     if (status != 0)
00157       {
00158         errno=status;
00159         ThrowFatalException(ResourceFatalError,
00160           "unable to instantiate semaphore `%s'");
00161       }
00162     status=pthread_mutexattr_destroy(&mutex_info);
00163     if (status != 0)
00164       {
00165         errno=status;
00166         ThrowFatalException(ResourceFatalError,
00167           "unable to instantiate semaphore `%s'");
00168       }
00169   }
00170 #elif defined(WIZARDSTOOLKIT_HAVE_WINTHREADS)
00171   InitializeCriticalSection(&semaphore_info->mutex);
00172 #endif
00173   semaphore_info->id=GetWizardThreadId();
00174   semaphore_info->reference_count=0;
00175   semaphore_info->signature=WizardSignature;
00176   return(semaphore_info);
00177 }
00178 
00179 /*
00180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00181 %                                                                             %
00182 %                                                                             %
00183 %                                                                             %
00184 %   D e s t r o y S e m a p h o r e I n f o                                   %
00185 %                                                                             %
00186 %                                                                             %
00187 %                                                                             %
00188 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00189 %
00190 %  DestroySemaphoreInfo() destroys a semaphore.
00191 %
00192 %  The format of the DestroySemaphoreInfo method is:
00193 %
00194 %      void DestroySemaphoreInfo(SemaphoreInfo **semaphore_info)
00195 %
00196 %  A description of each parameter follows:
00197 %
00198 %    o semaphore_info: Specifies a pointer to an SemaphoreInfo structure.
00199 %
00200 */
00201 WizardExport void DestroySemaphoreInfo(SemaphoreInfo **semaphore_info)
00202 {
00203   assert(semaphore_info != (SemaphoreInfo **) NULL);
00204   assert((*semaphore_info) != (SemaphoreInfo *) NULL);
00205   assert((*semaphore_info)->signature == WizardSignature);
00206   LockWizardMutex();
00207 #if defined(WIZARDSTOOLKIT_HAVE_PTHREAD)
00208   {
00209     int
00210       status;
00211 
00212     status=pthread_mutex_destroy(&(*semaphore_info)->mutex);
00213     if (status != 0)
00214       {
00215         errno=status;
00216         ThrowFatalException(ResourceFatalError,
00217           "unable to destroy semaphore `%s'");
00218       }
00219   }
00220 #elif defined(WIZARDSTOOLKIT_HAVE_WINTHREADS)
00221   DeleteCriticalSection(&(*semaphore_info)->mutex);
00222 #endif
00223   (*semaphore_info)->signature=(~WizardSignature);
00224   *semaphore_info=(SemaphoreInfo *) RelinquishAlignedMemory(*semaphore_info);
00225   UnlockWizardMutex();
00226 }
00227 
00228 /*
00229 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00230 %                                                                             %
00231 %                                                                             %
00232 %                                                                             %
00233 %   L o c k S e m a p h o r e I n f o                                         %
00234 %                                                                             %
00235 %                                                                             %
00236 %                                                                             %
00237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00238 %
00239 %  LockSemaphoreInfo() locks a semaphore.
00240 %
00241 %  The format of the LockSemaphoreInfo method is:
00242 %
00243 %      void LockSemaphoreInfo(SemaphoreInfo *semaphore_info)
00244 %
00245 %  A description of each parameter follows:
00246 %
00247 %    o semaphore_info: Specifies a pointer to an SemaphoreInfo structure.
00248 %
00249 */
00250 WizardExport void LockSemaphoreInfo(SemaphoreInfo *semaphore_info)
00251 {
00252   assert(semaphore_info != (SemaphoreInfo *) NULL);
00253   assert(semaphore_info->signature == WizardSignature);
00254 #if defined(WIZARDSTOOLKIT_HAVE_PTHREAD)
00255   {
00256     int
00257       status;
00258 
00259     status=pthread_mutex_lock(&semaphore_info->mutex);
00260     if (status != 0)
00261       {
00262         errno=status;
00263         ThrowFatalException(ResourceFatalError,
00264           "unable to lock semaphore `%s'");
00265       }
00266   }
00267 #elif defined(WIZARDSTOOLKIT_HAVE_WINTHREADS)
00268   EnterCriticalSection(&semaphore_info->mutex);
00269 #endif
00270 #if defined(WIZARDSTOOLKIT_DEBUG)
00271   if ((semaphore_info->reference_count > 0) &&
00272       (IsWizardThreadEqual(semaphore_info->id) != WizardFalse))
00273     {
00274       (void) fprintf(stderr,"Warning: unexpected recursive lock!\n");
00275       (void) fflush(stderr);
00276     }
00277 #endif
00278   semaphore_info->id=GetWizardThreadId();
00279   semaphore_info->reference_count++;
00280 }
00281 
00282 /*
00283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00284 %                                                                             %
00285 %                                                                             %
00286 %                                                                             %
00287 %   R e l i n g u i s h S e m a p h o r e I n f o                             %
00288 %                                                                             %
00289 %                                                                             %
00290 %                                                                             %
00291 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00292 %
00293 %  RelinquishSemaphoreInfo() relinquishes a semaphore.
00294 %
00295 %  The format of the RelinquishSemaphoreInfo method is:
00296 %
00297 %      RelinquishSemaphoreInfo(SemaphoreInfo *semaphore_info)
00298 %
00299 %  A description of each parameter follows:
00300 %
00301 %    o semaphore_info: Specifies a pointer to an SemaphoreInfo structure.
00302 %
00303 */
00304 WizardExport void RelinquishSemaphoreInfo(SemaphoreInfo *semaphore_info)
00305 {
00306   assert(semaphore_info != (SemaphoreInfo *) NULL);
00307   assert(semaphore_info->signature == WizardSignature);
00308   UnlockSemaphoreInfo(semaphore_info);
00309 }
00310 
00311 /*
00312 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00313 %                                                                             %
00314 %                                                                             %
00315 %                                                                             %
00316 %   S e m a p h o r e C o m p o n e n t G e n e s i s                         %
00317 %                                                                             %
00318 %                                                                             %
00319 %                                                                             %
00320 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00321 %
00322 %  SemaphoreComponentGenesis() instantiates the semaphore component.
00323 %
00324 %  The format of the SemaphoreComponentGenesis method is:
00325 %
00326 %      WizardBooleanType SemaphoreComponentGenesis(void)
00327 %
00328 */
00329 WizardExport WizardBooleanType SemaphoreComponentGenesis(void)
00330 {
00331   LockWizardMutex();
00332   UnlockWizardMutex();
00333   return(WizardTrue);
00334 }
00335 
00336 /*
00337 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00338 %                                                                             %
00339 %                                                                             %
00340 %                                                                             %
00341 %   S e m a p h o r e C o m p o n e n t T e r m i n u s                       %
00342 %                                                                             %
00343 %                                                                             %
00344 %                                                                             %
00345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00346 %
00347 %  SemaphoreComponentTerminus() destroys the semaphore environment.
00348 %
00349 %  The format of the SemaphoreComponentTerminus method is:
00350 %
00351 %      SemaphoreComponentTerminus(void)
00352 %
00353 */
00354 WizardExport void SemaphoreComponentTerminus(void)
00355 {
00356 }
00357 
00358 /*
00359 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00360 %                                                                             %
00361 %                                                                             %
00362 %                                                                             %
00363 %   U n l o c k S e m a p h o r e I n f o                                     %
00364 %                                                                             %
00365 %                                                                             %
00366 %                                                                             %
00367 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00368 %
00369 %  UnlockSemaphoreInfo() unlocks a semaphore.
00370 %
00371 %  The format of the UnlockSemaphoreInfo method is:
00372 %
00373 %      void UnlockSemaphoreInfo(SemaphoreInfo *semaphore_info)
00374 %
00375 %  A description of each parameter follows:
00376 %
00377 %    o semaphore_info: Specifies a pointer to an SemaphoreInfo structure.
00378 %
00379 */
00380 WizardExport void UnlockSemaphoreInfo(SemaphoreInfo *semaphore_info)
00381 {
00382   assert(semaphore_info != (SemaphoreInfo *) NULL);
00383   assert(semaphore_info->signature == WizardSignature); 
00384 #if defined(WIZARDSTOOLKIT_DEBUG)
00385   assert(IsWizardThreadEqual(semaphore_info->id) != WizardFalse);
00386   if (semaphore_info->reference_count == 0)
00387     {
00388       (void) fprintf(stderr,"Warning: semaphore lock already unlocked!\n");
00389       (void) fflush(stderr);
00390       return;
00391     }
00392   semaphore_info->reference_count--;
00393 #endif
00394 #if defined(WIZARDSTOOLKIT_HAVE_PTHREAD)
00395   {
00396     int
00397       status;
00398 
00399     status=pthread_mutex_unlock(&semaphore_info->mutex);
00400     if (status != 0)
00401       {
00402         errno=status;
00403         ThrowFatalException(ResourceFatalError,
00404           "unable to unlock semaphore `%s'");
00405       }
00406   }
00407 #elif defined(WIZARDSTOOLKIT_HAVE_WINTHREADS)
00408   LeaveCriticalSection(&semaphore_info->mutex);
00409 #endif
00410 }
Generated by  doxygen 1.6.2-20100208