summaryrefslogtreecommitdiffstats
path: root/vcl/inc/osx/runinmain.hxx
blob: e68bc4d350104c0964856cc007e9fbb32ed5fe61 (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#ifndef INCLUDED_VCL_INC_OSX_RUNINMAIN_HXX
#define INCLUDED_VCL_INC_OSX_RUNINMAIN_HXX

/**
 * Runs a command in the main thread.
 *
 * These macros are always used like a recursive calls, so they work like
 * a closure.
 *
 * Uses two conditionals for a two way communication.
 * The data (code block + result) is protected by the SolarMutex.
 *
 * There are three main macros, which act as function initializers:
 *  - OSX_RUNINMAIN - for all functions without return values
 *  - OSX_RUNINMAIN_POINTER - for all functions returning a pointer
 *  - OSX_RUNINMAIN_UNION - for all other return types
 *
 * All types used via OSX_RUNINMAIN_UNION must implement a move constructor,
 * so there is no memory leak!
 */

#include <unordered_map>

#include <Block.h>

#include <osl/thread.h>

#include "saltimer.h"
#include <salframe.hxx>
#include <tools/debug.hxx>

union RuninmainResult
{
    void*                            pointer;
    bool                             boolean;
    struct SalFrame::SalPointerState state;

    RuninmainResult() {}
};

#define OSX_RUNINMAIN_MEMBERS \
    std::mutex              m_runInMainMutex; \
    std::condition_variable m_aInMainCondition; \
    std::condition_variable m_aResultCondition; \
    bool                    m_wakeUpMain = false; \
    bool                    m_resultReady = false; \
    RuninmainBlock          m_aCodeBlock; \
    RuninmainResult         m_aResult;

#define OSX_RUNINMAIN( instance, command ) \
    if ( !instance->IsMainThread() ) \
    { \
        DBG_TESTSOLARMUTEX(); \
        SalYieldMutex *aMutex = static_cast<SalYieldMutex*>(instance->GetYieldMutex()); \
        { \
            std::scoped_lock<std::mutex> g(aMutex->m_runInMainMutex); \
            assert( !aMutex->m_aCodeBlock ); \
            aMutex->m_aCodeBlock = Block_copy(^{ \
                command; \
            }); \
            aMutex->m_wakeUpMain = true; \
            aMutex->m_aInMainCondition.notify_all(); \
        } \
        dispatch_async(dispatch_get_main_queue(),^{ \
            ImplNSAppPostEvent( AquaSalInstance::YieldWakeupEvent, NO ); \
            }); \
        { \
            std::unique_lock<std::mutex> g(aMutex->m_runInMainMutex); \
            aMutex->m_aResultCondition.wait( \
                g, [&aMutex]() { return aMutex->m_resultReady; }); \
            aMutex->m_resultReady = false; \
        } \
        return; \
    }

#define OSX_RUNINMAIN_POINTER( instance, command, type ) \
    if ( !instance->IsMainThread() ) \
    { \
        DBG_TESTSOLARMUTEX(); \
        SalYieldMutex *aMutex = static_cast<SalYieldMutex*>(instance->GetYieldMutex()); \
        { \
            std::scoped_lock<std::mutex> g(aMutex->m_runInMainMutex); \
            assert( !aMutex->m_aCodeBlock ); \
            aMutex->m_aCodeBlock = Block_copy(^{ \
                aMutex->m_aResult.pointer = static_cast<void*>( command ); \
            }); \
            aMutex->m_wakeUpMain = true; \
            aMutex->m_aInMainCondition.notify_all(); \
        } \
        dispatch_async(dispatch_get_main_queue(),^{ \
            ImplNSAppPostEvent( AquaSalInstance::YieldWakeupEvent, NO ); \
            }); \
        { \
            std::unique_lock<std::mutex> g(aMutex->m_runInMainMutex); \
            aMutex->m_aResultCondition.wait( \
                g, [&aMutex]() { return aMutex->m_resultReady; }); \
            aMutex->m_resultReady = false; \
        } \
        return static_cast<type>( aMutex->m_aResult.pointer ); \
    }

#define OSX_RUNINMAIN_UNION( instance, command, member ) \
    if ( !instance->IsMainThread() ) \
    { \
        DBG_TESTSOLARMUTEX(); \
        SalYieldMutex *aMutex = static_cast<SalYieldMutex*>(instance->GetYieldMutex()); \
        { \
            std::scoped_lock<std::mutex> g(aMutex->m_runInMainMutex); \
            assert( !aMutex->m_aCodeBlock ); \
            aMutex->m_aCodeBlock = Block_copy(^{ \
                aMutex->m_aResult.member = command; \
            }); \
            aMutex->m_wakeUpMain = true; \
            aMutex->m_aInMainCondition.notify_all(); \
        } \
        dispatch_async(dispatch_get_main_queue(),^{ \
            ImplNSAppPostEvent( AquaSalInstance::YieldWakeupEvent, NO ); \
            }); \
        { \
            std::unique_lock<std::mutex> g(aMutex->m_runInMainMutex); \
            aMutex->m_aResultCondition.wait( \
                g, [&aMutex]() { return aMutex->m_resultReady; }); \
            aMutex->m_resultReady = false; \
        } \
        return std::move( aMutex->m_aResult.member ); \
    }

/**
 * convenience macros used from SalInstance
 */

#define OSX_INST_RUNINMAIN( command ) \
    OSX_RUNINMAIN( this, command )

#define OSX_INST_RUNINMAIN_POINTER( command, type ) \
    OSX_RUNINMAIN_POINTER( this, command, type )

#define OSX_INST_RUNINMAIN_UNION( command, member ) \
    OSX_RUNINMAIN_UNION( this, command, member )

/**
 * convenience macros using global SalData
 */

#define OSX_SALDATA_RUNINMAIN( command ) \
    OSX_RUNINMAIN( GetSalData()->mpInstance, command )

#define OSX_SALDATA_RUNINMAIN_POINTER( command, type ) \
    OSX_RUNINMAIN_POINTER( GetSalData()->mpInstance, command, type )

#define OSX_SALDATA_RUNINMAIN_UNION( command, member ) \
    OSX_RUNINMAIN_UNION( GetSalData()->mpInstance, command, member )

#endif // INCLUDED_VCL_INC_OSX_RUNINMAIN_HXX

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */