@@ -187,25 +187,6 @@ class Daemon
187
187
SetTheServiceStatus . call ( SERVICE_STOPPED , NO_ERROR , 0 , 0 )
188
188
end
189
189
190
- ThreadProc = FFI ::Function . new ( :ulong , [ :pointer ] ) do |lpParameter |
191
- ste = FFI ::MemoryPointer . new ( SERVICE_TABLE_ENTRYW , 2 )
192
-
193
- s = SERVICE_TABLE_ENTRYW . new ( ste [ 0 ] )
194
- s [ :lpServiceName ] = FFI ::MemoryPointer . from_string ( '' )
195
- s [ :lpServiceProc ] = lpParameter
196
-
197
- s = SERVICE_TABLE_ENTRYW . new ( ste [ 1 ] )
198
- s [ :lpServiceName ] = nil
199
- s [ :lpServiceProc ] = nil
200
-
201
- # No service to step, no service handle, no ruby exceptions, just terminate the thread..
202
- unless StartServiceCtrlDispatcherW ( ste )
203
- return 1
204
- end
205
-
206
- return 0
207
- end
208
-
209
190
# This is a shortcut for Daemon.new + Daemon#mainloop.
210
191
#
211
192
def self . mainloop
@@ -255,26 +236,28 @@ def mainloop
255
236
raise SystemCallError . new ( 'CreateEvent' , FFI . errno )
256
237
end
257
238
258
- hThread = CreateThread ( nil , 0 , ThreadProc , Service_Main , 0 , nil )
239
+ hThread = Thread . new ( Service_Main ) do |lp_proc |
240
+ ste = FFI ::MemoryPointer . new ( SERVICE_TABLE_ENTRYW , 2 )
259
241
260
- if hThread == 0
261
- raise SystemCallError . new ( 'CreateThread' , FFI . errno )
262
- end
242
+ s = SERVICE_TABLE_ENTRYW . new ( ste [ 0 ] )
243
+ s [ :lpServiceName ] = FFI :: MemoryPointer . from_string ( "" )
244
+ s [ :lpServiceProc ] = lp_proc
263
245
264
- events = FFI :: MemoryPointer . new ( :pointer , 2 )
265
- events . put_pointer ( 0 , FFI :: Pointer . new ( hThread ) )
266
- events . put_pointer ( FFI :: Pointer . size , FFI :: Pointer . new ( @@hStartEvent ) )
246
+ s = SERVICE_TABLE_ENTRYW . new ( ste [ 1 ] )
247
+ s [ :lpServiceName ] = nil
248
+ s [ :lpServiceProc ] = nil
267
249
268
- while ( index = WaitForMultipleObjects ( 2 , events , 0 , 1000 ) ) == WAIT_TIMEOUT
250
+ # No service to step, no service handle, no ruby exceptions, just terminate the thread..
251
+ StartServiceCtrlDispatcherW ( ste )
269
252
end
270
253
271
- if index == WAIT_FAILED
272
- raise SystemCallError . new ( 'WaitForMultipleObjects' , FFI . errno )
254
+ while ( index = WaitForSingleObject ( @@hStartEvent , 1000 ) ) == WAIT_TIMEOUT
255
+ # The thread exited, so the show is off.
256
+ raise "Service_Main thread exited abnormally" unless hThread . alive?
273
257
end
274
258
275
- # The thread exited, so the show is off.
276
- if index == WAIT_OBJECT_0
277
- raise "Service_Main thread exited abnormally"
259
+ if index == WAIT_FAILED
260
+ raise SystemCallError . new ( "WaitForSingleObject" , FFI . errno )
278
261
end
279
262
280
263
thr = Thread . new do
0 commit comments