This source file includes following definitions.
- pthread_mutex_timedlock
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #include <config.h>
18
19
20 #include <pthread.h>
21
22 #include <errno.h>
23 #include <limits.h>
24 #include <sys/time.h>
25 #include <time.h>
26
27 int
28 pthread_mutex_timedlock (pthread_mutex_t *mutex, const struct timespec *abstime)
29 {
30
31
32
33
34
35
36
37 for (;;)
38 {
39 int err;
40 struct timeval currtime;
41 unsigned long remaining;
42 struct timespec duration;
43
44 err = pthread_mutex_trylock (mutex);
45 if (err != EBUSY)
46 return err;
47
48 gettimeofday (&currtime, NULL);
49
50 if (currtime.tv_sec > abstime->tv_sec)
51 remaining = 0;
52 else
53 {
54 unsigned long seconds = abstime->tv_sec - currtime.tv_sec;
55 remaining = seconds * 1000000000;
56 if (remaining / 1000000000 != seconds)
57 remaining = ULONG_MAX;
58 else
59 {
60 long nanoseconds =
61 abstime->tv_nsec - currtime.tv_usec * 1000;
62 if (nanoseconds >= 0)
63 {
64 remaining += nanoseconds;
65 if (remaining < nanoseconds)
66 remaining = ULONG_MAX;
67 }
68 else
69 {
70 if (remaining >= - nanoseconds)
71 remaining -= (- nanoseconds);
72 else
73 remaining = 0;
74 }
75 }
76 }
77 if (remaining == 0)
78 return ETIMEDOUT;
79
80
81 duration.tv_sec = 0;
82 duration.tv_nsec = 1000000;
83 if (duration.tv_nsec > remaining)
84 duration.tv_nsec = remaining;
85 nanosleep (&duration, NULL);
86 }
87 }