Coverage Report

Created: 2025-12-31 10:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/include/osl/thread.hxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
/*
21
 * This file is part of LibreOffice published API.
22
 */
23
24
#ifndef INCLUDED_OSL_THREAD_HXX
25
#define INCLUDED_OSL_THREAD_HXX
26
27
#include "sal/config.h"
28
29
#include <cassert>
30
#include <cstddef>
31
32
#include "osl/time.h"
33
#include "osl/thread.h"
34
#include "rtl/alloc.h"
35
36
namespace osl
37
{
38
/** threadFunc is the function which is executed by the threads
39
    created by the osl::Thread class. The function's signature
40
    matches the one of oslWorkerFunction which is declared in
41
    osl/thread.h
42
*/
43
extern "C" inline void SAL_CALL threadFunc( void* param);
44
45
/**
46
   A thread abstraction.
47
48
   @deprecated use ::salhelper::Thread instead.  Only the static member
49
   functions ::osl::Thread::getCurrentIdentifier, ::osl::Thread::wait, and
50
   ::osl::Thread::yield are not deprecated.
51
 */
52
class Thread
53
{
54
    Thread( const Thread& ) SAL_DELETED_FUNCTION;
55
    Thread& operator= ( const Thread& ) SAL_DELETED_FUNCTION;
56
public:
57
    // these are here to force memory de/allocation to sal lib.
58
    static void * SAL_CALL operator new( size_t nSize )
59
0
        { return ::rtl_allocateMemory( nSize ); }
60
    static void SAL_CALL operator delete( void * pMem )
61
0
        { ::rtl_freeMemory( pMem ); }
62
    static void * SAL_CALL operator new( size_t, void * pMem )
63
0
        { return pMem; }
64
    static void SAL_CALL operator delete( void *, void * )
65
0
        {}
66
67
37.2k
    Thread(): m_hThread(NULL){}
68
69
    virtual  ~Thread() COVERITY_NOEXCEPT_FALSE
70
37.2k
    {
71
37.2k
        osl_destroyThread( m_hThread);
72
37.2k
    }
73
74
    bool SAL_CALL create()
75
37.2k
    {
76
37.2k
        assert(m_hThread == NULL); // only one running thread per instance
77
37.2k
        m_hThread = osl_createSuspendedThread( threadFunc, static_cast<void*>(this));
78
37.2k
        if (m_hThread == NULL)
79
0
        {
80
0
            return false;
81
0
        }
82
37.2k
        osl_resumeThread(m_hThread);
83
37.2k
        return true;
84
37.2k
    }
85
86
    bool SAL_CALL createSuspended()
87
0
    {
88
0
        assert(m_hThread == NULL); // only one running thread per instance
89
0
        if( m_hThread)
90
0
            return false;
91
0
        m_hThread= osl_createSuspendedThread( threadFunc,
92
0
                                             static_cast<void*>(this));
93
0
        return m_hThread != NULL;
94
0
    }
95
96
    virtual void SAL_CALL suspend()
97
0
    {
98
0
        if( m_hThread )
99
0
            osl_suspendThread(m_hThread);
100
0
    }
101
102
    virtual void SAL_CALL resume()
103
0
    {
104
0
        if( m_hThread )
105
0
            osl_resumeThread(m_hThread);
106
0
    }
107
108
    virtual void SAL_CALL terminate()
109
0
    {
110
0
        if( m_hThread )
111
0
            osl_terminateThread(m_hThread);
112
0
    }
113
114
    virtual void SAL_CALL join()
115
54.2k
    {
116
54.2k
        osl_joinWithThread(m_hThread);
117
54.2k
    }
118
119
    bool SAL_CALL isRunning() const
120
0
    {
121
0
        return osl_isThreadRunning(m_hThread);
122
0
    }
123
124
    void SAL_CALL setPriority( oslThreadPriority Priority)
125
0
    {
126
0
        if( m_hThread )
127
0
            osl_setThreadPriority(m_hThread, Priority);
128
0
    }
129
130
    oslThreadPriority SAL_CALL getPriority() const
131
0
    {
132
0
        return m_hThread ? osl_getThreadPriority(m_hThread) : osl_Thread_PriorityUnknown;
133
0
    }
134
135
    oslThreadIdentifier SAL_CALL getIdentifier() const
136
0
    {
137
0
        return osl_getThreadIdentifier(m_hThread);
138
0
    }
139
140
    static oslThreadIdentifier SAL_CALL getCurrentIdentifier()
141
865M
    {
142
865M
        return osl_getThreadIdentifier(NULL);
143
865M
    }
144
145
    static void SAL_CALL wait(const TimeValue& Delay)
146
0
    {
147
0
        osl_waitThread(&Delay);
148
0
    }
149
150
    static void SAL_CALL yield()
151
0
    {
152
0
        osl_yieldThread();
153
0
    }
154
155
37.2k
    static void setName(char const * name) SAL_NOEXCEPT {
156
37.2k
        osl_setThreadName(name);
157
37.2k
    }
158
159
    virtual bool SAL_CALL schedule()
160
0
    {
161
0
        return m_hThread && osl_scheduleThread(m_hThread);
162
0
    }
163
164
    SAL_CALL operator oslThread() const
165
0
    {
166
0
        return m_hThread;
167
0
    }
168
169
protected:
170
171
    /** The thread functions calls the protected functions
172
        run and onTerminated.
173
    */
174
    friend void SAL_CALL threadFunc( void* param);
175
176
    virtual void SAL_CALL run() = 0;
177
178
    virtual void SAL_CALL onTerminated()
179
0
    {
180
0
    }
181
182
private:
183
    oslThread m_hThread;
184
};
185
186
extern "C" inline void SAL_CALL threadFunc( void* param)
187
37.2k
{
188
37.2k
        Thread* pObj= static_cast<Thread*>(param);
189
37.2k
        pObj->run();
190
37.2k
        pObj->onTerminated();
191
37.2k
}
192
193
class ThreadData
194
{
195
    ThreadData( const ThreadData& ) SAL_DELETED_FUNCTION;
196
    ThreadData& operator= (const ThreadData& ) SAL_DELETED_FUNCTION;
197
public:
198
     /// Create a thread specific local data key
199
    ThreadData( oslThreadKeyCallbackFunction pCallback= NULL )
200
0
    {
201
0
        m_hKey = osl_createThreadKey( pCallback );
202
0
    }
203
204
    /// Destroy a thread specific local data key
205
    ~ThreadData()
206
0
    {
207
0
           osl_destroyThreadKey(m_hKey);
208
0
    }
209
210
    /** Set the data associated with the data key.
211
        @returns True if operation was successful
212
    */
213
    bool SAL_CALL setData(void *pData)
214
0
    {
215
0
           return osl_setThreadKeyData(m_hKey, pData);
216
0
    }
217
218
    /** Get the data associated with the data key.
219
        @returns The data associated with the data key or
220
        NULL if no data was set
221
    */
222
    void* SAL_CALL getData()
223
0
    {
224
0
           return osl_getThreadKeyData(m_hKey);
225
0
    }
226
227
    operator oslThreadKey() const
228
0
    {
229
0
        return m_hKey;
230
0
    }
231
232
private:
233
    oslThreadKey m_hKey;
234
};
235
236
} // end namespace osl
237
238
#endif
239
240
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */