summaryrefslogblamecommitdiffstats
path: root/cpukit/score/src/coresemseize.c
blob: 71b8a26c8b510cacc19984e1ef2839cb78fd0c3a (plain) (tree)
1
2
3
4
5
6
7
8
9








                                                                              
                            
                                                    


                                                           
                                         



        



                   





























                                                                             



                                                













                                                                 
                                         


                                                                             

                                            
                           

                                                                     


                                                       




                                                                         

   
 
/*
 *  CORE Semaphore Handler
 *
 *  DESCRIPTION:
 *
 *  This package is the implementation of the CORE Semaphore Handler.
 *  This core object utilizes standard Dijkstra counting semaphores to provide
 *  synchronization and mutual exclusion capabilities.
 *
 *  COPYRIGHT (c) 1989-1999.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 *
 *  $Id$
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <rtems/system.h>
#include <rtems/score/isr.h>
#include <rtems/score/coresem.h>
#include <rtems/score/states.h>
#include <rtems/score/thread.h>
#include <rtems/score/threadq.h>
#if defined(RTEMS_MULTIPROCESSING)
#include <rtems/score/mpci.h>
#endif

/*PAGE
 *
 *  _CORE_semaphore_Seize
 *
 *  This routine attempts to allocate a core semaphore to the calling thread.
 *
 *  Input parameters:
 *    the_semaphore - pointer to semaphore control block
 *    id            - id of object to wait on
 *    wait          - TRUE if wait is allowed, FALSE otherwise
 *    timeout       - number of ticks to wait (0 means forever)
 *
 *  Output parameters:  NONE
 *
 *  INTERRUPT LATENCY:
 *    available
 *    wait
 */

void _CORE_semaphore_Seize(
  CORE_semaphore_Control         *the_semaphore,
  Objects_Id                      id,
  Core_semaphore_Blocking_option  wait,
  Watchdog_Interval               timeout
)
{
  Thread_Control *executing;
  ISR_Level       level;

  executing = _Thread_Executing;
  executing->Wait.return_code = CORE_SEMAPHORE_STATUS_SUCCESSFUL;
  _ISR_Disable( level );
  if ( the_semaphore->count != 0 ) {
    the_semaphore->count -= 1;
    _ISR_Enable( level );
    return;
  }

  if ( wait == CORE_SEMAPHORE_NO_WAIT ) {
      _ISR_Enable( level );
      executing->Wait.return_code = CORE_SEMAPHORE_STATUS_UNSATISFIED_NOWAIT;
      return;
  }
  if (wait == CORE_SEMAPHORE_BAD_TIMEOUT ) {
      _ISR_Enable( level );
      executing->Wait.return_code = CORE_SEMAPHORE_BAD_TIMEOUT_VALUE;
      return;
  }
  if (( wait == CORE_SEMAPHORE_BLOCK_FOREVER) ||
      ( wait == CORE_SEMAPHORE_BLOCK_WITH_TIMEOUT ) ) {
      _Thread_queue_Enter_critical_section( &the_semaphore->Wait_queue );
      executing->Wait.queue          = &the_semaphore->Wait_queue;
      executing->Wait.id             = id;
      _ISR_Enable( level );
      _Thread_queue_Enqueue( &the_semaphore->Wait_queue, timeout );
  }

}