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