Coverage Report

Created: 2026-11-12 07:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dcmtk/oflog/libsrc/sleep.cc
Line
Count
Source
1
// Module:  Log4CPLUS
2
// File:    sleep.cxx
3
// Created: 5/2003
4
// Author:  Tad E. Smith
5
//
6
//
7
// Copyright 2003-2010 Tad E. Smith
8
//
9
// Licensed under the Apache License, Version 2.0 (the "License");
10
// you may not use this file except in compliance with the License.
11
// You may obtain a copy of the License at
12
//
13
//     http://www.apache.org/licenses/LICENSE-2.0
14
//
15
// Unless required by applicable law or agreed to in writing, software
16
// distributed under the License is distributed on an "AS IS" BASIS,
17
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
// See the License for the specific language governing permissions and
19
// limitations under the License.
20
21
#include "dcmtk/oflog/config/windowsh.h"
22
#include "dcmtk/oflog/helpers/sleep.h"
23
#include "dcmtk/oflog/helpers/timehelp.h"
24
25
#include <cerrno>
26
#if defined (DCMTK_LOG4CPLUS_HAVE_ERRNO_H)
27
#include <errno.h>
28
#endif
29
30
#if defined (DCMTK_LOG4CPLUS_HAVE_TIME_H)
31
#include <time.h>
32
#endif
33
34
35
namespace dcmtk {
36
namespace log4cplus { namespace helpers {
37
38
39
///////////////////////////////////////////////////////////////////////////////
40
// public methods
41
///////////////////////////////////////////////////////////////////////////////
42
43
int const MILLIS_TO_NANOS = 1000000;
44
int const SEC_TO_MILLIS = 1000;
45
46
47
#if ! defined (_WIN32)
48
static inline
49
int
50
unix_nanosleep (const struct timespec *req, struct timespec *rem)
51
0
{
52
#if defined (DCMTK_LOG4CPLUS_HAVE_CLOCK_NANOSLEEP)
53
    return clock_nanosleep (CLOCK_REALTIME, 0, req, rem);
54
55
#elif defined (DCMTK_LOG4CPLUS_HAVE_NANOSLEEP)
56
    return nanosleep (req, rem);
57
58
#else
59
#  error no nanosleep() or clock_nanosleep()
60
#endif
61
0
}
62
63
#endif
64
65
66
void
67
sleep(unsigned long secs, unsigned long nanosecs)
68
0
{
69
#if defined(_WIN32)
70
    DWORD const MAX_SLEEP_SECONDS = 4294966;        // (2**32-2)/1000
71
72
    DWORD nano_millis = nanosecs / OFstatic_cast(unsigned long, MILLIS_TO_NANOS);
73
    if (secs <= MAX_SLEEP_SECONDS) {
74
        Sleep((secs * SEC_TO_MILLIS) + nano_millis);
75
        return;
76
    }
77
        
78
    DWORD no_of_max_sleeps = secs / MAX_SLEEP_SECONDS;
79
            
80
    for(DWORD i = 0; i < no_of_max_sleeps; i++) {
81
        Sleep(MAX_SLEEP_SECONDS * SEC_TO_MILLIS);
82
    }
83
               
84
    Sleep((secs % MAX_SLEEP_SECONDS) * SEC_TO_MILLIS + nano_millis);
85
#else
86
0
    timespec sleep_time = { OFstatic_cast(STD_NAMESPACE time_t, secs),
87
0
                            OFstatic_cast(long, nanosecs) };
88
0
    timespec remain;
89
0
    while (unix_nanosleep (&sleep_time, &remain)) {
90
0
        if (errno == EINTR) {
91
0
            sleep_time.tv_sec  = remain.tv_sec;
92
0
            sleep_time.tv_nsec = remain.tv_nsec;               
93
0
            continue;
94
0
        }
95
0
        else {
96
0
            return;
97
0
        }
98
0
    }
99
0
#endif
100
0
}
101
102
103
void
104
sleepmillis(unsigned long millis)
105
0
{
106
0
    unsigned long secs = millis / SEC_TO_MILLIS;
107
0
    unsigned long nanosecs = (millis % SEC_TO_MILLIS) * MILLIS_TO_NANOS;
108
0
    sleep(secs, nanosecs);
109
0
}
110
111
} } // namespace log4cplus { namespace helpers {
112
} // end namespace dcmtk