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 * It SHALL fail if: 11 * 12 * [EINVAL] - The process or thread would have blocked, and the abs_timeout parameter 13 * specified in nano-seconds field value is less than 0 or greater than or equal 14 * to 1,000 million. 15 * 16 * Steps: 17 * 18 * 1. Create a thread. 19 * 2. Call pthread_mutex_timedlock inside of the thread passing to it a negative number in the 20 * nano-seconds field of the 'abs_timeout'. 21 * 3. Save the return value of pthread_mutex_timedlock(). It should be EINVAL. 22 * 23 */ 24 25#define _XOPEN_SOURCE 600 26 27#include <time.h> 28#include <pthread.h> 29#include <stdio.h> 30#include <unistd.h> 31#include <errno.h> 32#include "posixtest.h" 33 34#define INVALID_TIME -1 /* Invalid value of negative value 35 in the nano-seonds field of 36 'abs_timeout. */ 37#define TIMEOUT 3 /* 3 seconds of timeout time for 38 pthread_mutex_timedlock(). */ 39void *f1(void *parm); 40 41int ret; /* Save return value of 42 pthread_mutex_timedlock(). */ 43pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /* The mutex */ 44time_t currsec1, currsec2; /* Variables for saving time before 45 and afer locking the mutex using 46 pthread_mutex_timedlock(). */ 47/**************************** 48 * 49 * MAIN() 50 * 51 * *************************/ 52int main() 53{ 54 pthread_t new_th; 55 56 /* Create a thread that will call pthread_mutex_timedlock */ 57 if(pthread_create(&new_th, NULL, f1, NULL) != 0) 58 { 59 perror("Error in pthread_create().\n"); 60 return PTS_UNRESOLVED; 61 } 62 63 /* Wait for thread to end. */ 64 if(pthread_join(new_th, NULL) != 0) 65 { 66 perror("Error in pthread_join().\n"); 67 return PTS_UNRESOLVED; 68 } 69 70 /* Check the return status of pthread_mutex_timedlock(). */ 71 if(ret != EINVAL) 72 { 73 printf("Test FAILED: Expected return code EINVAL, got: %d.\n", ret); 74 return PTS_FAIL; 75 } 76 77 printf("Test PASSED\n"); 78 return PTS_PASS; 79} 80 81/**************************** 82 * 83 * Thread's start routine. 84 * f1() 85 * 86 * *************************/ 87void *f1(void *parm) 88{ 89 struct timespec timeout; 90 91 /* Lock the mutex */ 92 if(pthread_mutex_lock(&mutex) != 0) 93 { 94 perror("Error in pthread_mutex_lock()\n"); 95 pthread_exit((void*)PTS_UNRESOLVED); 96 return (void*)PTS_UNRESOLVED; 97 } 98 99 /* Set nano-seconds to negative value. */ 100 timeout.tv_sec = time(NULL) + TIMEOUT; 101 timeout.tv_nsec = INVALID_TIME; 102 103 /* This should return EINVAL */ 104 ret = pthread_mutex_timedlock(&mutex, &timeout); 105 106 /* Cleaning up the mutexes. */ 107 if(pthread_mutex_unlock(&mutex) != 0) 108 { 109 perror("Error in pthread_mutex_unlock().\n"); 110 pthread_exit((void*)PTS_UNRESOLVED); 111 return (void*)PTS_UNRESOLVED; 112 } 113 if(pthread_mutex_destroy(&mutex) != 0) 114 { 115 perror("Error in pthread_mutex_destroy().\n"); 116 pthread_exit((void*)PTS_UNRESOLVED); 117 return (void*)PTS_UNRESOLVED; 118 } 119 120 pthread_exit(0); 121 return (void*)(0); 122} 123