Home | History | Annotate | Download | only in pthread_mutex_unlock
      1 /*
      2  * Copyright (c) 2004, 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.,
     15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     16  *
     17 
     18  * This sample test aims to check the following assertion:
     19  * If the mutex type is PTHREAD_MUTEX_RECURSIVE,
     20  * and a thread attempts to unlock an unlocked mutex,
     21  * an error is returned.
     22 
     23  * The steps are:
     24  *  -> Initialize a recursive mutex
     25  *  -> Attempt to unlock the mutex when it is unlocked
     26  *      and when it has been locked then unlocked.
     27  */
     28 
     29  /*
     30   * - adam.li (at) intel.com 2004-05-20
     31   *   Add to PTS. Please refer to http://nptl.bullopensource.org/phpBB/
     32   *   for general information
     33   */
     34 
     35  /* We are testing conformance to IEEE Std 1003.1, 2003 Edition */
     36 #define _POSIX_C_SOURCE 200112L
     37 
     38  /* We enable the following line to have mutex attributes defined */
     39 #ifndef WITHOUT_XOPEN
     40 #define _XOPEN_SOURCE	600
     41 
     42 /********************************************************************************************/
     43 /****************************** standard includes *****************************************/
     44 /********************************************************************************************/
     45 #include <pthread.h>
     46 #include <unistd.h>
     47 #include <stdlib.h>
     48 #include <stdio.h>
     49 #include <stdarg.h>
     50 
     51 /********************************************************************************************/
     52 /******************************   Test framework   *****************************************/
     53 /********************************************************************************************/
     54 #include "../testfrmw/testfrmw.h"
     55 #include "../testfrmw/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 (error code for example)
     59   * FAILED(descr);
     60   *    where descr is a short text saying why the test has failed.
     61   * PASSED();
     62   *    No parameter.
     63   *
     64   * Both three macros shall terminate the calling process.
     65   * The testcase shall not terminate in any other maneer.
     66   *
     67   * The other file defines the functions
     68   * void output_init()
     69   * void output(char * string, ...)
     70   *
     71   * Those may be used to output information.
     72   */
     73 
     74 /********************************************************************************************/
     75 /********************************** Configuration ******************************************/
     76 /********************************************************************************************/
     77 #ifndef VERBOSE
     78 #define VERBOSE 1
     79 #endif
     80 
     81 /********************************************************************************************/
     82 /***********************************    Test case   *****************************************/
     83 /********************************************************************************************/
     84 
     85 /** parent thread function **/
     86 int main(void)
     87 {
     88 	int ret;
     89 	pthread_mutexattr_t ma;
     90 	pthread_mutex_t m;
     91 
     92 	output_init();
     93 
     94 #if VERBOSE >1
     95 	output("Initialize the PTHREAD_MUTEX_RECURSIVE mutex\n");
     96 #endif
     97 
     98 	ret = pthread_mutexattr_init(&ma);
     99 	if (ret != 0) {
    100 		UNRESOLVED(ret, "Mutex attribute init failed");
    101 	}
    102 
    103 	ret = pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_RECURSIVE);
    104 	if (ret != 0) {
    105 		UNRESOLVED(ret, "Set type recursive failed");
    106 	}
    107 
    108 	ret = pthread_mutex_init(&m, &ma);
    109 	if (ret != 0) {
    110 		UNRESOLVED(ret, "Mutex init failed");
    111 	}
    112 #if VERBOSE >1
    113 	output("Unlock unlocked mutex\n");
    114 #endif
    115 
    116 	ret = pthread_mutex_unlock(&m);
    117 	if (ret == 0) {
    118 		FAILED("Unlocking an unlocked recursive mutex succeeded");
    119 	}
    120 #if VERBOSE >1
    121 	output("Lock and unlock the mutex\n");
    122 #endif
    123 
    124 	ret = pthread_mutex_lock(&m);
    125 	if (ret != 0) {
    126 		UNRESOLVED(ret, "Mutex lock failed");
    127 	}
    128 	ret = pthread_mutex_lock(&m);
    129 	if (ret != 0) {
    130 		UNRESOLVED(ret, "Mutex recursive lock failed");
    131 	}
    132 	ret = pthread_mutex_unlock(&m);
    133 	if (ret != 0) {
    134 		UNRESOLVED(ret, "Mutex unlock failed");
    135 	}
    136 	ret = pthread_mutex_unlock(&m);
    137 	if (ret != 0) {
    138 		UNRESOLVED(ret, "Mutex recursive unlock failed");
    139 	}
    140 
    141 	/* destroy the mutex attribute object */
    142 	ret = pthread_mutexattr_destroy(&ma);
    143 	if (ret != 0) {
    144 		UNRESOLVED(ret, "Mutex attribute destroy failed");
    145 	}
    146 
    147 	ret = pthread_mutex_unlock(&m);
    148 	if (ret == 0) {
    149 		FAILED("Unlocking an unlocked recursive mutex succeeded");
    150 	}
    151 
    152 	PASSED;
    153 }
    154 #else /* WITHOUT_XOPEN */
    155 int main(void)
    156 {
    157 	output_init();
    158 	UNTESTED("This test requires XSI features");
    159 }
    160 #endif
    161