Home | History | Annotate | Download | only in pthread_mutexattr_settype
      1 /*
      2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
      3  * Created by:  bing.wei.liu REMOVE-THIS AT intel DOT com
      4  * This file is licensed under the GPL license.  For the full content
      5  * of this license, see the COPYING file at the top level of this
      6  * source tree.
      7 
      8  * Test pthread_mutexattr_settype()
      9  *
     10   PTHREAD_MUTEX_ERRORCHECK
     11 
     12   Provides errorchecking.  A thread attempting to relock this mutex without unlocking it
     13   first will return with an error.  A thread attempting to unlock a mutex which another
     14   thread has locked will return with an error.  A thread attempting to unlock an unlocked
     15   mutex will return with an error.
     16  *
     17  * Steps:
     18  * 1.  Initialize a pthread_mutexattr_t object with pthread_mutexattr_init()
     19  * 2   Set the 'type' of the mutexattr object to PTHREAD_MUTEX_ERRORCHECK.
     20  * 3.  Create a mutex with that mutexattr object.
     21  * 4.  Attempt to unlock an unlocked mutex.  It should return an error.
     22  *
     23  */
     24 
     25 #define _XOPEN_SOURCE 600
     26 
     27 #include <pthread.h>
     28 #include <stdio.h>
     29 #include <errno.h>
     30 #include "posixtest.h"
     31 
     32 int main(void)
     33 {
     34 	pthread_mutex_t mutex;
     35 	pthread_mutexattr_t mta;
     36 
     37 	/* Initialize a mutex attributes object */
     38 	if (pthread_mutexattr_init(&mta) != 0) {
     39 		perror("Error at pthread_mutexattr_init()\n");
     40 		return PTS_UNRESOLVED;
     41 	}
     42 
     43 	/* Set the 'type' attribute to be PTHREAD_MUTEX_ERRORCHECK  */
     44 	if (pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK) != 0) {
     45 		printf("Test FAILED: Error setting the attribute 'type'\n");
     46 		return PTS_FAIL;
     47 	}
     48 
     49 	/* Initialize the mutex with that attribute obj. */
     50 	if (pthread_mutex_init(&mutex, &mta) != 0) {
     51 		perror("Error intializing the mutex.\n");
     52 		return PTS_UNRESOLVED;
     53 	}
     54 
     55 	if (pthread_mutex_lock(&mutex) != 0) {
     56 		perror("Error at pthread_mutex_lock().\n");
     57 		return PTS_UNRESOLVED;
     58 	}
     59 
     60 	if (pthread_mutex_unlock(&mutex) != 0) {
     61 		perror("Error at pthread_mutex_unlock().\n");
     62 		return PTS_UNRESOLVED;
     63 	}
     64 
     65 	/* Unlock an already unlocked mutex.  Here, an error should be returned. */
     66 	if (pthread_mutex_unlock(&mutex) == 0) {
     67 		perror
     68 		    ("Test FAILED: Did not return error when unlocking an already unlocked mutex.\n");
     69 		return PTS_FAIL;
     70 	}
     71 
     72 	if (pthread_mutex_destroy(&mutex)) {
     73 		perror("Error at pthread_mutex_destory().\n");
     74 		return PTS_UNRESOLVED;
     75 	}
     76 
     77 	if (pthread_mutexattr_destroy(&mta)) {
     78 		perror("Error at pthread_mutexattr_destory().\n");
     79 		return PTS_UNRESOLVED;
     80 	}
     81 
     82 	printf("Test PASSED\n");
     83 	return PTS_PASS;
     84 }
     85