Home | History | Annotate | Download | only in pthread_mutex_trylock
      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 that pthread_mutex_trylock()
      9  *   Upon succesful completion, it shall return a 0
     10  *
     11  */
     12 
     13 #include <pthread.h>
     14 #include <stdio.h>
     15 #include <errno.h>
     16 #include "posixtest.h"
     17 
     18 int main(void)
     19 {
     20 	pthread_mutex_t mutex;
     21 	int rc;
     22 
     23 	/* Initialize a mutex object with the default mutex attributes */
     24 	if ((rc = pthread_mutex_init(&mutex, NULL)) != 0) {
     25 		fprintf(stderr, "Error at pthread_mutex_init(), rc=%d\n", rc);
     26 		return PTS_UNRESOLVED;
     27 	}
     28 
     29 	/* Try to lock the mutex using pthread_mutex_trylock() */
     30 	if ((rc = pthread_mutex_trylock(&mutex)) == 0) {
     31 		pthread_mutex_unlock(&mutex);
     32 		printf("Test PASSED\n");
     33 		return PTS_PASS;
     34 	}
     35 
     36 	/* Check if returned values are tolerable */
     37 	/* PATCH: since we are using the mutex properly, */
     38 	/*  errors are NOT tolerable here */
     39 	else if (rc == EBUSY) {
     40 		fprintf(stderr, "The mutex was already locked\n");
     41 	} else if (rc == EINVAL) {
     42 		fprintf(stderr, "Invalid mutex object\n");
     43 	} else if (rc == EAGAIN) {
     44 		fprintf(stderr,
     45 			"The maximum number of recursive locks has been exceeded\n");
     46 	}
     47 
     48 	/* Any other returned value means the test failed */
     49 	printf("Test FAILED\n");
     50 	return PTS_FAIL;
     51 }
     52