Home | History | Annotate | Download | only in pthread_mutex_timedlock
      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_timedlock()
      9  *
     10  * Upon success, it returns 0.
     11  *
     12  * Steps:
     13  *
     14  * 1. Create a thread, and call pthread_mutex_timedlock inside of it.  It should not block
     15  *    and should return 0 since it will be the only one owning the mutex.
     16  * 2. Save the return value of pthread_mutex_timedlock().  It should be 0.
     17  *
     18  */
     19 
     20 #define _XOPEN_SOURCE 600
     21 
     22 #include <time.h>
     23 #include <pthread.h>
     24 #include <stdio.h>
     25 #include <unistd.h>
     26 #include <errno.h>
     27 #include "posixtest.h"
     28 
     29 #define TIMEOUT 3		/* 3 seconds of timeout time for
     30 				   pthread_mutex_timedlock(). */
     31 void *f1(void *parm);
     32 
     33 int ret;			/* Save return value of
     34 				   pthread_mutex_timedlock(). */
     35 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;	/* The mutex */
     36 time_t currsec1, currsec2;	/* Variables for saving time before
     37 				   and afer locking the mutex using
     38 				   pthread_mutex_timedlock(). */
     39 /****************************
     40  *
     41  * MAIN()
     42  *
     43  * *************************/
     44 int main(void)
     45 {
     46 	pthread_t new_th;
     47 
     48 	/* Create a thread that will call pthread_mutex_timedlock */
     49 	if (pthread_create(&new_th, NULL, f1, NULL) != 0) {
     50 		perror("Error in pthread_create().\n");
     51 		return PTS_UNRESOLVED;
     52 	}
     53 
     54 	/* Wait for thread to end. */
     55 	if (pthread_join(new_th, NULL) != 0) {
     56 		perror("Error in pthread_join().\n");
     57 		return PTS_UNRESOLVED;
     58 	}
     59 
     60 	/* Check the return status of pthread_mutex_timedlock(). */
     61 	if (ret != 0) {
     62 		printf("Test FAILED: Expected return code 0, got: %d.\n", ret);
     63 		return PTS_FAIL;
     64 	}
     65 
     66 	printf("Test PASSED\n");
     67 	return PTS_PASS;
     68 }
     69 
     70 /****************************
     71  *
     72  * Thread's start routine.
     73  * f1()
     74  *
     75  * *************************/
     76 void *f1(void *parm)
     77 {
     78 	struct timespec timeout;
     79 
     80 	timeout.tv_sec = time(NULL) + TIMEOUT;
     81 	timeout.tv_nsec = 0;
     82 
     83 	/* This should not block since the mutex is not owned by anyone right now.
     84 	 * Save the return value. */
     85 	ret = pthread_mutex_timedlock(&mutex, &timeout);
     86 
     87 	/* Cleaning up the mutexes. */
     88 	if (pthread_mutex_unlock(&mutex) != 0) {
     89 		perror("Error in pthread_mutex_unlock().\n");
     90 		pthread_exit((void *)PTS_UNRESOLVED);
     91 		return (void *)PTS_UNRESOLVED;
     92 	}
     93 	if (pthread_mutex_destroy(&mutex) != 0) {
     94 		perror("Error in pthread_mutex_destroy().\n");
     95 		pthread_exit((void *)PTS_UNRESOLVED);
     96 		return (void *)PTS_UNRESOLVED;
     97 	}
     98 
     99 	pthread_exit(0);
    100 	return (void *)(0);
    101 }
    102