1/* 2* Copyright (c) 2005, Bull S.A.. All rights reserved. 3* Created by: Sebastien Decugis 4 5* This program is free software; you can redistribute it and/or modify it 6* under the terms of version 2 of the GNU General Public License as 7* published by the Free Software Foundation. 8* 9* This program is distributed in the hope that it would be useful, but 10* WITHOUT ANY WARRANTY; without even the implied warranty of 11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12* 13* You should have received a copy of the GNU General Public License along 14* with this program; if not, write the Free Software Foundation, Inc., 59 15* Temple Place - Suite 330, Boston MA 02111-1307, USA. 16 17 18* This sample test aims to check the following assertion: 19* 20* If the semaphore is locked, the sval value is set to 0 or to a negative 21* value representing the number of waiters for the semaphore. 22 23 24* The steps are: 25* -> init a semaphore (value = 0) 26* -> create a thread which waits for the semaphore 27* -> call sem_getvalue and check value of the semaphore 28* -> sem_post and destroy everything 29 30* The test fails if value is not as expected. 31 32*/ 33 34 35/* We are testing conformance to IEEE Std 1003.1, 2003 Edition */ 36#define _POSIX_C_SOURCE 200112L 37 38/******************************************************************************/ 39/*************************** standard includes ********************************/ 40/******************************************************************************/ 41#include <pthread.h> 42#include <stdarg.h> 43#include <stdio.h> 44#include <stdlib.h> 45#include <string.h> 46#include <unistd.h> 47 48#include <semaphore.h> 49#include <errno.h> 50 51/******************************************************************************/ 52/*************************** Test framework *******************************/ 53/******************************************************************************/ 54#include "testfrmw.h" 55#include "testfrmw.c" 56/* This header is responsible for defining the following macros: 57 * UNRESOLVED(ret, descr); 58 * where descr is a description of the error and ret is an int 59 * (error code for example) 60 * FAILED(descr); 61 * where descr is a short text saying why the test has failed. 62 * PASSED(); 63 * No parameter. 64 * 65 * Both three macros shall terminate the calling process. 66 * The testcase shall not terminate in any other maneer. 67 * 68 * The other file defines the functions 69 * void output_init() 70 * void output(char * string, ...) 71 * 72 * Those may be used to output information. 73 */ 74 75/******************************************************************************/ 76/**************************** Configuration ***********************************/ 77/******************************************************************************/ 78#ifndef VERBOSE 79#define VERBOSE 1 80#endif 81 82/******************************************************************************/ 83/*************************** Test case ***********************************/ 84/******************************************************************************/ 85 86void * threaded ( void * arg ) 87{ 88 int ret; 89 90 do 91 { 92 ret = sem_wait( arg ); 93 } 94 while ( ( ret != 0 ) && ( errno == EINTR ) ); 95 96 if ( ret != 0 ) 97 { 98 UNRESOLVED( errno, "Failed to wait for the semaphore" ); 99 } 100 101 return NULL; 102} 103 104 105/* The main test function. */ 106int main( int argc, char * argv[] ) 107{ 108 int ret, val; 109 sem_t sem; 110 pthread_t th; 111 112 /* Initialize output */ 113 output_init(); 114 115 /* Initialize semaphore */ 116 ret = sem_init( &sem, 0, 0 ); 117 118 if ( ret != 0 ) 119 { 120 UNRESOLVED( errno, "Failed to init semaphore" ); 121 } 122 123 /* Create the thread */ 124 ret = pthread_create( &th, NULL, threaded, &sem ); 125 126 if ( ret != 0 ) 127 { 128 UNRESOLVED( ret, "Failed to create the thread" ); 129 } 130 131 /* Sleep 1 sec so the thread enters the sem_wait call */ 132 sleep( 1 ); 133 134 /* Check value */ 135 ret = sem_getvalue( &sem, &val ); 136 137 if ( ret != 0 ) 138 { 139 UNRESOLVED( errno, "Failed to get semaphore value" ); 140 } 141 142 if ( ( val != 0 ) && ( val != -1 ) ) 143 { 144 output( "Val: %d\n", val ); 145 FAILED( "Semaphore count is neither 0 nor # of waiting processes" ); 146 } 147 148 /* Post the semaphore */ 149 ret = sem_post( &sem ); 150 151 if ( ret != 0 ) 152 { 153 UNRESOLVED( errno, "Failed to post the semaphore" ); 154 } 155 156 /* Join the thread */ 157 ret = pthread_join( th, NULL ); 158 159 if ( ret != 0 ) 160 { 161 UNRESOLVED( ret, "Failed to join the thread" ); 162 } 163 164 165 /* Destroy the semaphore */ 166 ret = sem_destroy( &sem ); 167 168 if ( ret != 0 ) 169 { 170 UNRESOLVED( errno, "Failed to sem_destroy" ); 171 } 172 173 /* Test passed */ 174#if VERBOSE > 0 175 176 output( "Test passed\n" ); 177 178#endif 179 180 PASSED; 181} 182 183 184