22
22
23
23
#include < mutex>
24
24
25
- #include < unistd.h>
26
- #include < errno.h>
27
- #include < fcntl.h>
28
25
#include < pthread.h>
29
26
#include < sys/time.h>
30
27
#include < poll.h>
@@ -76,14 +73,13 @@ do { \
76
73
#define DBG_TESTSVPYIELDMUTEX () ((void )0 )
77
74
#endif
78
75
79
- #if !defined(ANDROID) && !defined(IOS)
76
+ #if !defined(ANDROID) && !defined(IOS) && !defined(EMSCRIPTEN)
80
77
81
78
static void atfork_child ()
82
79
{
83
80
if (SvpSalInstance::s_pDefaultInstance != nullptr )
84
81
{
85
- SvpSalInstance::s_pDefaultInstance->CloseWakeupPipe (false );
86
- SvpSalInstance::s_pDefaultInstance->CreateWakeupPipe (false );
82
+ SvpSalInstance::s_pDefaultInstance->CloseWakeupPipe ();
87
83
}
88
84
}
89
85
@@ -97,10 +93,9 @@ SvpSalInstance::SvpSalInstance( std::unique_ptr<SalYieldMutex> pMutex )
97
93
m_nTimeoutMS = 0 ;
98
94
99
95
m_MainThread = osl::Thread::getCurrentIdentifier ();
100
- CreateWakeupPipe (true );
101
96
if ( s_pDefaultInstance == nullptr )
102
97
s_pDefaultInstance = this ;
103
- #if !defined(ANDROID) && !defined(IOS)
98
+ #if !defined(ANDROID) && !defined(IOS) && !defined(EMSCRIPTEN)
104
99
pthread_atfork (nullptr , nullptr , atfork_child);
105
100
#endif
106
101
}
@@ -109,62 +104,16 @@ SvpSalInstance::~SvpSalInstance()
109
104
{
110
105
if ( s_pDefaultInstance == this )
111
106
s_pDefaultInstance = nullptr ;
112
- CloseWakeupPipe (true );
107
+ CloseWakeupPipe ();
113
108
}
114
109
115
- void SvpSalInstance::CloseWakeupPipe (bool log )
110
+ void SvpSalInstance::CloseWakeupPipe ()
116
111
{
117
112
SvpSalYieldMutex *const pMutex (dynamic_cast <SvpSalYieldMutex*>(GetYieldMutex ()));
118
113
if (!pMutex)
119
114
return ;
120
- if (pMutex->m_FeedbackFDs [0 ] != -1 )
121
- {
122
- if (log )
123
- {
124
- SAL_INFO (" vcl.headless" , " CloseWakeupPipe: Closing inherited feedback pipe: [" << pMutex->m_FeedbackFDs [0 ] << " ," << pMutex->m_FeedbackFDs [1 ] << " ]" );
125
- }
126
- close (pMutex->m_FeedbackFDs [0 ]);
127
- close (pMutex->m_FeedbackFDs [1 ]);
128
- pMutex->m_FeedbackFDs [0 ] = pMutex->m_FeedbackFDs [1 ] = -1 ;
129
- }
130
- }
131
-
132
- void SvpSalInstance::CreateWakeupPipe (bool log)
133
- {
134
- SvpSalYieldMutex *const pMutex (dynamic_cast <SvpSalYieldMutex*>(GetYieldMutex ()));
135
- if (!pMutex)
136
- return ;
137
- if (pipe (pMutex->m_FeedbackFDs ) == -1 )
138
- {
139
- if (log )
140
- {
141
- SAL_WARN (" vcl.headless" , " Could not create feedback pipe: " << strerror (errno));
142
- std::abort ();
143
- }
144
- }
145
- else
146
- {
147
- if (log )
148
- {
149
- SAL_INFO (" vcl.headless" , " CreateWakeupPipe: Created feedback pipe: [" << pMutex->m_FeedbackFDs [0 ] << " ," << pMutex->m_FeedbackFDs [1 ] << " ]" );
150
- }
151
-
152
- int flags;
153
-
154
- // set close-on-exec descriptor flag.
155
- if ((flags = fcntl (pMutex->m_FeedbackFDs [0 ], F_GETFD)) != -1 )
156
- {
157
- flags |= FD_CLOEXEC;
158
- (void ) fcntl (pMutex->m_FeedbackFDs [0 ], F_SETFD, flags);
159
- }
160
- if ((flags = fcntl (pMutex->m_FeedbackFDs [1 ], F_GETFD)) != -1 )
161
- {
162
- flags |= FD_CLOEXEC;
163
- (void ) fcntl (pMutex->m_FeedbackFDs [1 ], F_SETFD, flags);
164
- }
165
-
166
- // retain the default blocking I/O for feedback pipe
167
- }
115
+ while (!pMutex->m_FeedbackPipe .empty ())
116
+ pMutex->m_FeedbackPipe .pop ();
168
117
}
169
118
170
119
void SvpSalInstance::TriggerUserEventProcessing ()
@@ -328,9 +277,6 @@ void SvpSalInstance::ProcessEvent( SalUserEvent aEvent )
328
277
329
278
SvpSalYieldMutex::SvpSalYieldMutex ()
330
279
{
331
- #ifndef IOS
332
- m_FeedbackFDs[0 ] = m_FeedbackFDs[1 ] = -1 ;
333
- #endif
334
280
}
335
281
336
282
SvpSalYieldMutex::~SvpSalYieldMutex ()
@@ -367,11 +313,11 @@ void SvpSalYieldMutex::doAcquire(sal_uInt32 const nLockCount)
367
313
m_bNoYieldLock = true ;
368
314
bool const bEvents = pInst->DoYield (false , request == SvpRequest::MainThreadDispatchAllEvents);
369
315
m_bNoYieldLock = false ;
370
- if (write (m_FeedbackFDs[1 ], &bEvents, sizeof (bool )) != sizeof (bool ))
371
316
{
372
- SAL_WARN ( " vcl.headless " , " Could not write: " << strerror (errno) );
373
- std::abort ( );
317
+ std::lock_guard lock (m_FeedbackMutex );
318
+ m_FeedbackPipe. push (bEvents );
374
319
}
320
+ m_FeedbackCV.notify_all ();
375
321
}
376
322
}
377
323
while (true );
@@ -534,8 +480,12 @@ bool SvpSalInstance::DoYield(bool bWait, bool bHandleAllCurrentEvents)
534
480
: SvpRequest::MainThreadDispatchOneEvent);
535
481
536
482
// blocking read (for synchronisation)
537
- auto const nRet = read (pMutex->m_FeedbackFDs [0 ], &bWasEvent, sizeof (bool ));
538
- assert (nRet == 1 ); (void ) nRet;
483
+ {
484
+ std::unique_lock lock (pMutex->m_FeedbackMutex );
485
+ pMutex->m_FeedbackCV .wait (lock, [pMutex] { return !pMutex->m_FeedbackPipe .empty (); });
486
+ bWasEvent = pMutex->m_FeedbackPipe .front ();
487
+ pMutex->m_FeedbackPipe .pop ();
488
+ }
539
489
if (!bWasEvent && bWait)
540
490
{
541
491
// block & release YieldMutex until the main thread does something
0 commit comments