blob: b4009a4d37f3694b9187fc18f2778eb215a4314e [file] [log] [blame]
Garrett Cooper2c282152010-12-16 00:55:50 -08001/*
robbiew0dc07652005-06-03 16:29:48 +00002 * 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 Cooper2c282152010-12-16 00:55:50 -08005 * of this license, see the COPYING file at the top level of this
robbiew0dc07652005-06-03 16:29:48 +00006 * source tree.
7
8 * Test that pthread_mutex_timedlock()
Garrett Cooper2c282152010-12-16 00:55:50 -08009 *
robbiew0dc07652005-06-03 16:29:48 +000010 * It SHALL fail if:
Garrett Cooper2c282152010-12-16 00:55:50 -080011 *
robbiew0dc07652005-06-03 16:29:48 +000012 * [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 Cooper2c282152010-12-16 00:55:50 -080016 * Steps:
robbiew0dc07652005-06-03 16:29:48 +000017 *
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 Gao354ebb42012-12-07 10:10:04 +080034#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(). */
robbiew0dc07652005-06-03 16:29:48 +000039void *f1(void *parm);
40
Wanlong Gao354ebb42012-12-07 10:10:04 +080041int ret; /* Save return value of
42 pthread_mutex_timedlock(). */
robbiew0dc07652005-06-03 16:29:48 +000043pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /* The mutex */
Wanlong Gao354ebb42012-12-07 10:10:04 +080044time_t currsec1, currsec2; /* Variables for saving time before
45 and afer locking the mutex using
46 pthread_mutex_timedlock(). */
robbiew0dc07652005-06-03 16:29:48 +000047/****************************
48 *
49 * MAIN()
50 *
51 * *************************/
52int main()
53{
54 pthread_t new_th;
55
Garrett Cooper2c282152010-12-16 00:55:50 -080056 /* Create a thread that will call pthread_mutex_timedlock */
Wanlong Gao354ebb42012-12-07 10:10:04 +080057 if (pthread_create(&new_th, NULL, f1, NULL) != 0) {
robbiew0dc07652005-06-03 16:29:48 +000058 perror("Error in pthread_create().\n");
59 return PTS_UNRESOLVED;
60 }
61
62 /* Wait for thread to end. */
Wanlong Gao354ebb42012-12-07 10:10:04 +080063 if (pthread_join(new_th, NULL) != 0) {
robbiew0dc07652005-06-03 16:29:48 +000064 perror("Error in pthread_join().\n");
65 return PTS_UNRESOLVED;
66 }
67
68 /* Check the return status of pthread_mutex_timedlock(). */
Wanlong Gao354ebb42012-12-07 10:10:04 +080069 if (ret != EINVAL) {
70 printf("Test FAILED: Expected return code EINVAL, got: %d.\n",
71 ret);
robbiew0dc07652005-06-03 16:29:48 +000072 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 * *************************/
85void *f1(void *parm)
86{
87 struct timespec timeout;
Garrett Cooper2c282152010-12-16 00:55:50 -080088
robbiew0dc07652005-06-03 16:29:48 +000089 /* Lock the mutex */
Wanlong Gao354ebb42012-12-07 10:10:04 +080090 if (pthread_mutex_lock(&mutex) != 0) {
robbiew0dc07652005-06-03 16:29:48 +000091 perror("Error in pthread_mutex_lock()\n");
Wanlong Gao354ebb42012-12-07 10:10:04 +080092 pthread_exit((void *)PTS_UNRESOLVED);
93 return (void *)PTS_UNRESOLVED;
robbiew0dc07652005-06-03 16:29:48 +000094 }
95
96 /* Set nano-seconds to negative value. */
97 timeout.tv_sec = time(NULL) + TIMEOUT;
Garrett Cooper2c282152010-12-16 00:55:50 -080098 timeout.tv_nsec = INVALID_TIME;
robbiew0dc07652005-06-03 16:29:48 +000099
100 /* This should return EINVAL */
101 ret = pthread_mutex_timedlock(&mutex, &timeout);
102
103 /* Cleaning up the mutexes. */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800104 if (pthread_mutex_unlock(&mutex) != 0) {
robbiew0dc07652005-06-03 16:29:48 +0000105 perror("Error in pthread_mutex_unlock().\n");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800106 pthread_exit((void *)PTS_UNRESOLVED);
107 return (void *)PTS_UNRESOLVED;
robbiew0dc07652005-06-03 16:29:48 +0000108 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800109 if (pthread_mutex_destroy(&mutex) != 0) {
robbiew0dc07652005-06-03 16:29:48 +0000110 perror("Error in pthread_mutex_destroy().\n");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800111 pthread_exit((void *)PTS_UNRESOLVED);
112 return (void *)PTS_UNRESOLVED;
robbiew0dc07652005-06-03 16:29:48 +0000113 }
114
Wanlong Gao354ebb42012-12-07 10:10:04 +0800115 pthread_exit(0);
116 return (void *)(0);
Chris Dearmanec6edca2012-10-17 19:54:01 -0700117}