Home | History | Annotate | Download | only in pthreads-win32
      1 /*
      2  * -------------------------------------------------------------
      3  *
      4  * Module: sem_trywait.c
      5  *
      6  * Purpose:
      7  *	Semaphores aren't actually part of the PThreads standard.
      8  *	They are defined by the POSIX Standard:
      9  *
     10  *		POSIX 1003.1b-1993	(POSIX.1b)
     11  *
     12  * -------------------------------------------------------------
     13  *
     14  * --------------------------------------------------------------------------
     15  *
     16  *      Pthreads-win32 - POSIX Threads Library for Win32
     17  *      Copyright(C) 1998 John E. Bossom
     18  *      Copyright(C) 1999,2005 Pthreads-win32 contributors
     19  *
     20  *      Contact Email: rpj (at) callisto.canberra.edu.au
     21  *
     22  *      The current list of contributors is contained
     23  *      in the file CONTRIBUTORS included with the source
     24  *      code distribution. The list can also be seen at the
     25  *      following World Wide Web location:
     26  *      http://sources.redhat.com/pthreads-win32/contributors.html
     27  *
     28  *      This library is free software; you can redistribute it and/or
     29  *      modify it under the terms of the GNU Lesser General Public
     30  *      License as published by the Free Software Foundation; either
     31  *      version 2 of the License, or (at your option) any later version.
     32  *
     33  *      This library is distributed in the hope that it will be useful,
     34  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
     35  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     36  *      Lesser General Public License for more details.
     37  *
     38  *      You should have received a copy of the GNU Lesser General Public
     39  *      License along with this library in the file COPYING.LIB;
     40  *      if not, write to the Free Software Foundation, Inc.,
     41  *      59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
     42  */
     43 
     44 #include "pthread.h"
     45 #include "semaphore.h"
     46 #include "implement.h"
     47 
     48 
     49 int
     50 sem_trywait (sem_t * sem)
     51      /*
     52       * ------------------------------------------------------
     53       * DOCPUBLIC
     54       *      This function tries to wait on a semaphore.
     55       *
     56       * PARAMETERS
     57       *      sem
     58       *              pointer to an instance of sem_t
     59       *
     60       * DESCRIPTION
     61       *      This function tries to wait on a semaphore. If the
     62       *      semaphore value is greater than zero, it decreases
     63       *      its value by one. If the semaphore value is zero, then
     64       *      this function returns immediately with the error EAGAIN
     65       *
     66       * RESULTS
     67       *              0               successfully decreased semaphore,
     68       *              -1              failed, error in errno
     69       * ERRNO
     70       *              EAGAIN          the semaphore was already locked,
     71       *              EINVAL          'sem' is not a valid semaphore,
     72       *              ENOTSUP         sem_trywait is not supported,
     73       *              EINTR           the function was interrupted by a signal,
     74       *              EDEADLK         a deadlock condition was detected.
     75       *
     76       * ------------------------------------------------------
     77       */
     78 {
     79   int result = 0;
     80   sem_t s = *sem;
     81 
     82   if (s == NULL)
     83     {
     84       result = EINVAL;
     85     }
     86   else if ((result = pthread_mutex_lock (&s->lock)) == 0)
     87     {
     88       /* See sem_destroy.c
     89        */
     90      if (*sem == NULL)
     91         {
     92           (void) pthread_mutex_unlock (&s->lock);
     93           errno = EINVAL;
     94           return -1;
     95         }
     96 
     97       if (s->value > 0)
     98 	{
     99 	  s->value--;
    100 	}
    101       else
    102 	{
    103 	  result = EAGAIN;
    104 	}
    105 
    106       (void) pthread_mutex_unlock (&s->lock);
    107     }
    108 
    109   if (result != 0)
    110     {
    111       errno = result;
    112       return -1;
    113     }
    114 
    115   return 0;
    116 
    117 }				/* sem_trywait */
    118