Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:40

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  *  @file
0005  *
0006  *  This test exercises the POSIX Spinlock manager.
0007  */
0008 
0009 /*
0010  *  COPYRIGHT (c) 1989-2012.
0011  *  On-Line Applications Research Corporation (OAR).
0012  *
0013  * Redistribution and use in source and binary forms, with or without
0014  * modification, are permitted provided that the following conditions
0015  * are met:
0016  * 1. Redistributions of source code must retain the above copyright
0017  *    notice, this list of conditions and the following disclaimer.
0018  * 2. Redistributions in binary form must reproduce the above copyright
0019  *    notice, this list of conditions and the following disclaimer in the
0020  *    documentation and/or other materials provided with the distribution.
0021  *
0022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0023  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0025  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0026  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0027  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0028  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0029  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0030  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0032  * POSSIBILITY OF SUCH DAMAGE.
0033  */
0034 
0035 #ifdef HAVE_CONFIG_H
0036 #include "config.h"
0037 #endif
0038 
0039 #include "tmacros.h"
0040 #include <stdio.h>
0041 #include <errno.h>
0042 #include <stdlib.h>
0043 
0044 #include <pthread.h>
0045 
0046 #include <rtems.h>  /* for task creation */
0047 
0048 const char rtems_test_name[] = "PSXSPIN 1";
0049 
0050 /* forward declarations to avoid warnings */
0051 int test_main(void);
0052 
0053 /*
0054  *  main entry point to the test
0055  */
0056 
0057 #if defined(__rtems__)
0058 int test_main(void)
0059 #else
0060 int main(
0061   int    argc,
0062   char **argv
0063 )
0064 #endif
0065 {
0066   pthread_spinlock_t    spinlock;
0067   pthread_spinlock_t    spinlock2;
0068   int                   status;
0069 
0070   TEST_BEGIN();
0071 
0072   puts( "pthread_spin_init( &spinlock, PTHREAD_PROCESS_PRIVATE ) -- OK" );
0073   status = pthread_spin_init( &spinlock, PTHREAD_PROCESS_PRIVATE );
0074   rtems_test_assert( status == 0 );
0075 
0076   puts( "pthread_spin_destroy( &spinlock ) -- OK" );
0077   status = pthread_spin_destroy( &spinlock );
0078   rtems_test_assert( status == 0 );
0079 
0080   puts( "pthread_spin_init( &spinlock, PTHREAD_PROCESS_SHARED ) -- OK" );
0081   status = pthread_spin_init( &spinlock, PTHREAD_PROCESS_PRIVATE );
0082   rtems_test_assert( status == 0 );
0083 
0084   puts( "pthread_spin_destroy( &spinlock ) -- OK" );
0085   status = pthread_spin_destroy( &spinlock );
0086   rtems_test_assert( status == 0 );
0087 
0088   puts( "pthread_spin_init( &spinlock, 0x1234 ) -- OK" );
0089   status = pthread_spin_init( &spinlock, 0x1234 );
0090   rtems_test_assert( status == 0 );
0091 
0092   puts( "pthread_spin_init( &spinlock2, 0 ) -- OK" );
0093   status = pthread_spin_init( &spinlock2, 0 );
0094   rtems_test_assert( status == 0 );
0095 
0096   rtems_test_assert( _ISR_Get_level() == 0 );
0097 
0098   puts( "pthread_spin_lock( &spinlock ) -- OK" );
0099   status = pthread_spin_lock( &spinlock );
0100   rtems_test_assert( status == 0 );
0101 
0102   rtems_test_assert( _ISR_Get_level() != 0 );
0103 
0104   puts( "pthread_spin_lock( &spinlock2 ) -- OK" );
0105   status = pthread_spin_lock( &spinlock2 );
0106   rtems_test_assert( status == 0 );
0107 
0108   rtems_test_assert( _ISR_Get_level() != 0 );
0109 
0110   puts( "pthread_spin_unlock( &spinlock2 ) -- OK" );
0111   status = pthread_spin_unlock( &spinlock2 );
0112   rtems_test_assert( status == 0 );
0113 
0114   rtems_test_assert( _ISR_Get_level() != 0 );
0115 
0116   puts( "pthread_spin_unlock( &spinlock ) -- OK" );
0117   status = pthread_spin_unlock( &spinlock );
0118   rtems_test_assert( status == 0 );
0119 
0120   rtems_test_assert( _ISR_Get_level() == 0 );
0121 
0122   puts( "pthread_spin_trylock( &spinlock ) -- OK" );
0123   status = pthread_spin_trylock( &spinlock );
0124   rtems_test_assert( status == 0 );
0125 
0126   rtems_test_assert( _ISR_Get_level() != 0 );
0127 
0128   puts( "pthread_spin_trylock( &spinlock2 ) -- OK" );
0129   status = pthread_spin_trylock( &spinlock2 );
0130   rtems_test_assert( status == 0 );
0131 
0132   rtems_test_assert( _ISR_Get_level() != 0 );
0133 
0134   puts( "pthread_spin_unlock( &spinlock2 ) -- OK" );
0135   status = pthread_spin_unlock( &spinlock2 );
0136   rtems_test_assert( status == 0 );
0137 
0138   rtems_test_assert( _ISR_Get_level() != 0 );
0139 
0140   puts( "pthread_spin_unlock( &spinlock ) -- OK" );
0141   status = pthread_spin_unlock( &spinlock );
0142   rtems_test_assert( status == 0 );
0143 
0144   rtems_test_assert( _ISR_Get_level() == 0 );
0145 
0146   puts( "pthread_spin_destroy( &spinlock2 ) -- OK" );
0147   status = pthread_spin_destroy( &spinlock2 );
0148   rtems_test_assert( status == 0 );
0149 
0150   puts( "pthread_spin_destroy( &spinlock ) -- OK" );
0151   status = pthread_spin_destroy( &spinlock );
0152   rtems_test_assert( status == 0 );
0153 
0154   TEST_END();
0155   exit(0);
0156 }