<commit_before>/****************** <VPR heading BEGIN do not edit this line> *****************
 *
 * VR Juggler Portable Runtime
 *
 * Original Authors:
 *   <NAME>, <NAME>, <NAME>, <NAME>
 *
 ****************** <VPR heading END do not edit this line> ******************/

/*************** <auto-copyright.pl BEGIN do not edit this line> **************
 *
 * VR Juggler is (C) Copyright 1998-2006 by Iowa State University
 *
 * Original Authors:
 *   <NAME>, <NAME>,
 *   <NAME>, <NAME>,
 *   <NAME>, <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 *************** <auto-copyright.pl END do not edit this line> ***************/

/**
 * \file
 *
 * @note This file must be included by vpr/Sync/Semaphore.h, not the other way
 *       around.
 */

#ifndef _VPR_SEMAPHORE_POSIX_H_
#define _VPR_SEMAPHORE_POSIX_H_

#include <vpr/vprConfig.h>

#include <stdio.h>
#include <semaphore.h>
#include <errno.h>
#include <assert.h>

#include <vpr/Sync/LockException.h>

#if defined(VPR_OS_Darwin) && VPR_OS_RELEASE_MAJOR <= 8
#define VPR_USE_NAMED_SEMAPHORE 1
#endif


extern int errno;

namespace vpr
{

/** \class SemaphorePosix SemaphorePosix.h vpr/Sync/Semaphore.h
 *
 * Semaphore wrapper for POSIX.4-compliant systems.  This is typedef'd to
 * vpr::Semaphore.
 */
class SemaphorePosix
{
public:
   /**
    * Constructor for vpr::SemaphorePosix class.
    *
    * @post The semaphore variable for this object is initialized as an
    *       unnamed semaphore on all platforms except Darwin (Mac OS X).
    *       On Darwin 7.5 and earlier, unnamed semaphores are not
    *       supported.
    *
    * @param initialValue The initial number of resources controlled by
    *                     the semaphore.  If not specified, the default
    *                     value is 1.
    *
    * @throw vpr::ResourceException is thrown if the semaphore cannot be
    *        allocated.
    */
   SemaphorePosix(const int initialValue = 1);

   /**
    * Destructor for vpr::SemaphorePosix class.
    *
    * @post The resources used by the semaphore variable are freed.
    */
   ~SemaphorePosix();

   /**
    * Locks this semaphore.
    *
    * @post The calling thread either acquires the semaphore until
    *       release() is called, or the caller is put at the tail of a wait
    *       and is suspended until such time as it can be freed and allowed
    *       to acquire the semaphore itself.
    *
    * @throw vpr::LockException is thrown if the current thread has already
    *        locked this semaphore.
    */
   void acquire()
   {
      int result;

      do
      {
         result = sem_wait(mSema);
      }
      while ( -1 == result && EINTR == errno );

      if ( -1 == result && EDEADLK == errno )
      {
         throw vpr::LockException(
            "Tried to lock semaphore twice in the same thread", VPR_LOCATION
         );
      }

      assert(result == 0);
   }

   /**
    * Acquires a read lock on this semaphore.
    *
    * @post The calling thread either acquires the semaphore until
    *       release() is called, or the caller is put at the tail of a wait
    *       and is suspended until such time as it can be freed and allowed
    *       to acquire the semaphore itself.
    *
    * @return vpr::ReturnStatus::Succeed is returned if the read lock is
    *         acquired.
    * @return vpr::ReturnStatus::Fail is returnd if an error occurred.
    *
    * @note There is no special read lock for now.
    */
   void acquireRead()
   {
      this->acquire();
   }

   /**
    * Acquires a write lock on this semaphore.
    *
    * @post The calling thread either acquires the semaphore until
    *       release() is called, or the caller is put at the tail of a wait
    *       and is suspended until such time as it can be freed and allowed
    *       to acquire the semaphore itself.
    *
    * @return vpr::ReturnStatus::Succeed is returned if the write lock is
    *         acquired.
    * @return vpr::ReturnStatus::Fail is returnd if an error occurred.
    *
    * @note There is no special write lock for now.
    */
   void acquireWrite()
   {
      return this->acquire();
   }

   /**
    * Tries to acquire the semaphore immediately (does not block).
    *
    * @post If the semaphore could be acquired by the caller, the caller
    *       gets control of the semaphore.  If the semaphore was already
    *       locked, the routine returns immediately without suspending the
    *       calling thread.
    *
    * @return \c true is returned if the lock is acquired, and \c false is
    *         returned if the semaphore is already locked.
    */
   bool tryAcquire()
   {
      int result;

      do
      {
         result = sem_trywait(mSema);
      }<commit_msg>Removed comments referring to vpr::ReturnStatus. I thought that I caught all of these.<commit_after>/****************** <VPR heading BEGIN do not edit this line> *****************
 *
 * VR Juggler Portable Runtime
 *
 * Original Authors:
 *   <NAME>, <NAME>, <NAME>, <NAME>
 *
 ****************** <VPR heading END do not edit this line> ******************/

/*************** <auto-copyright.pl BEGIN do not edit this line> **************
 *
 * VR Juggler is (C) Copyright 1998-2006 by Iowa State University
 *
 * Original Authors:
 *   <NAME>, <NAME>,
 *   <NAME>, <NAME>,
 *   <NAME>, <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 *************** <auto-copyright.pl END do not edit this line> ***************/

/**
 * \file
 *
 * @note This file must be included by vpr/Sync/Semaphore.h, not the other way
 *       around.
 */

#ifndef _VPR_SEMAPHORE_POSIX_H_
#define _VPR_SEMAPHORE_POSIX_H_

#include <vpr/vprConfig.h>

#include <stdio.h>
#include <semaphore.h>
#include <errno.h>
#include <assert.h>

#include <vpr/Sync/LockException.h>

#if defined(VPR_OS_Darwin) && VPR_OS_RELEASE_MAJOR <= 8
#define VPR_USE_NAMED_SEMAPHORE 1
#endif


extern int errno;

namespace vpr
{

/** \class SemaphorePosix SemaphorePosix.h vpr/Sync/Semaphore.h
 *
 * Semaphore wrapper for POSIX.4-compliant systems.  This is typedef'd to
 * vpr::Semaphore.
 */
class SemaphorePosix
{
public:
   /**
    * Constructor for vpr::SemaphorePosix class.
    *
    * @post The semaphore variable for this object is initialized as an
    *       unnamed semaphore on all platforms except Darwin (Mac OS X).
    *       On Darwin 7.5 and earlier, unnamed semaphores are not
    *       supported.
    *
    * @param initialValue The initial number of resources controlled by
    *                     the semaphore.  If not specified, the default
    *                     value is 1.
    *
    * @throw vpr::ResourceException is thrown if the semaphore cannot be
    *        allocated.
    */
   SemaphorePosix(const int initialValue = 1);

   /**
    * Destructor for vpr::SemaphorePosix class.
    *
    * @post The resources used by the semaphore variable are freed.
    */
   ~SemaphorePosix();

   /**
    * Locks this semaphore.
    *
    * @post The calling thread either acquires the semaphore until
    *       release() is called, or the caller is put at the tail of a wait
    *       and is suspended until such time as it can be freed and allowed
    *       to acquire the semaphore itself.
    *
    * @throw vpr::LockException is thrown if the current thread has already
    *        locked this semaphore.
    */
   void acquire()
   {
      int result;

      do
      {
         result = sem_wait(mSema);
      }
      while ( -1 == result && EINTR == errno );

      if ( -1 == result && EDEADLK == errno )
      {
         throw vpr::LockException(
            "Tried to lock semaphore twice in the same thread", VPR_LOCATION
         );
      }

      assert(result == 0);
   }

   /**
    * Acquires a read lock on this semaphore.
    *
    * @post The calling thread either acquires the semaphore until
    *       release() is called, or the caller is put at the tail of a wait
    *       and is suspended until such time as it can be freed and allowed
    *       to acquire the semaphore itself.
    *
    * @note There is no special read lock for now.
    */
   void acquireRead()
   {
      this->acquire();
   }

   /**
    * Acquires a write lock on this semaphore.
    *
    * @post The calling thread either acquires the semaphore until
    *       release() is called, or the caller is put at the tail of a wait
    *       and is suspended until such time as it can be freed and allowed
    *       to acquire the semaphore itself.
    *
    * @note There is no special write lock for now.
    */
   void acquireWrite()
   {
      return this->acquire();
   }

   /**
    * Tries to acquire the semaphore immediately (does not block).
    *
    * @post If the semaphore could be acquired by the caller, the caller
    *       gets control of the semaphore.  If the semaphore was already
    *       locked, the routine returns immediately without suspending the
    *       calling thread.
    *
    * @return \c true is returned if the lock is acquired, and \c false is
    *         returned if the semaphore is already locked.
    */
   bool tryAcquire()
   {
      int result;

      do
      {
         result = sem_trywait(mSema);
      }
{'fraction_non_alphanumeric': 0.1041705982260804, 'fraction_numerical': 0.006227590111341763, 'mean_word_length': 2.4739429695181907, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7143178', 'n_tokens_mistral': 3197, 'n_tokens_neox': 3099, 'n_words': 1242}