Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 1 | /* |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 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 |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 5 | * of this license, see the COPYING file at the top level of this |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 6 | * source tree. |
| 7 | |
| 8 | * Test that pthread_mutex_timedlock() |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 9 | * |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 10 | * It SHALL fail if: |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 11 | * |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 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 | * |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 16 | * Steps: |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 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 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame^] | 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(). */ |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 39 | void *f1(void *parm); |
| 40 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame^] | 41 | int ret; /* Save return value of |
| 42 | pthread_mutex_timedlock(). */ |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 43 | pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /* The mutex */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame^] | 44 | time_t currsec1, currsec2; /* Variables for saving time before |
| 45 | and afer locking the mutex using |
| 46 | pthread_mutex_timedlock(). */ |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 47 | /**************************** |
| 48 | * |
| 49 | * MAIN() |
| 50 | * |
| 51 | * *************************/ |
| 52 | int main() |
| 53 | { |
| 54 | pthread_t new_th; |
| 55 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 56 | /* Create a thread that will call pthread_mutex_timedlock */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame^] | 57 | if (pthread_create(&new_th, NULL, f1, NULL) != 0) { |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 58 | perror("Error in pthread_create().\n"); |
| 59 | return PTS_UNRESOLVED; |
| 60 | } |
| 61 | |
| 62 | /* Wait for thread to end. */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame^] | 63 | if (pthread_join(new_th, NULL) != 0) { |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 64 | perror("Error in pthread_join().\n"); |
| 65 | return PTS_UNRESOLVED; |
| 66 | } |
| 67 | |
| 68 | /* Check the return status of pthread_mutex_timedlock(). */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame^] | 69 | if (ret != EINVAL) { |
| 70 | printf("Test FAILED: Expected return code EINVAL, got: %d.\n", |
| 71 | ret); |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 72 | return PTS_FAIL; |
| 73 | } |
| 74 | |
| 75 | printf("Test PASSED\n"); |
| 76 | return PTS_PASS; |
| 77 | } |
| 78 | |
| 79 | /**************************** |
| 80 | * |
| 81 | * Thread's start routine. |
| 82 | * f1() |
| 83 | * |
| 84 | * *************************/ |
| 85 | void *f1(void *parm) |
| 86 | { |
| 87 | struct timespec timeout; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 88 | |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 89 | /* Lock the mutex */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame^] | 90 | if (pthread_mutex_lock(&mutex) != 0) { |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 91 | perror("Error in pthread_mutex_lock()\n"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame^] | 92 | pthread_exit((void *)PTS_UNRESOLVED); |
| 93 | return (void *)PTS_UNRESOLVED; |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /* Set nano-seconds to negative value. */ |
| 97 | timeout.tv_sec = time(NULL) + TIMEOUT; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 98 | timeout.tv_nsec = INVALID_TIME; |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 99 | |
| 100 | /* This should return EINVAL */ |
| 101 | ret = pthread_mutex_timedlock(&mutex, &timeout); |
| 102 | |
| 103 | /* Cleaning up the mutexes. */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame^] | 104 | if (pthread_mutex_unlock(&mutex) != 0) { |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 105 | perror("Error in pthread_mutex_unlock().\n"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame^] | 106 | pthread_exit((void *)PTS_UNRESOLVED); |
| 107 | return (void *)PTS_UNRESOLVED; |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 108 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame^] | 109 | if (pthread_mutex_destroy(&mutex) != 0) { |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 110 | perror("Error in pthread_mutex_destroy().\n"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame^] | 111 | pthread_exit((void *)PTS_UNRESOLVED); |
| 112 | return (void *)PTS_UNRESOLVED; |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame^] | 115 | pthread_exit(0); |
| 116 | return (void *)(0); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 117 | } |