Skip to content
This repository has been archived by the owner on Jan 22, 2019. It is now read-only.

Commit

Permalink
* Correct usage of Mutex and Conditionals during emulated service mode.
Browse files Browse the repository at this point in the history
  (fixes compilation under FB > 0.18.3b).
  • Loading branch information
luislavena committed Apr 17, 2008
1 parent 3438351 commit 9fab571
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*SVN*
*0.3.2*

* Activate FB pedantic warnings by default (is really useful).

Expand All @@ -10,6 +10,9 @@

* Wrap onStart thread calling due function signature changes in FB 0.18.

* Correct usage of Mutex and Conditionals during emulated service mode.
(fixes compilation under FB > 0.18.3b).

*0.3.0* (February 12, 2007)

* ServiceFB is now under MIT license.
Expand Down
17 changes: 15 additions & 2 deletions lib/ServiceFB/ServiceFB_Utils.bas
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace svc
namespace utils '# fb.svc.utils
'# private (internals) for ServiceProcess.Console()
dim shared _svc_stop_signal as any ptr
dim shared _svc_stop_mutex as any ptr
dim shared _svc_stopped as BOOL
dim shared _svc_in_console as ServiceProcess ptr
dim shared _svc_in_console_stop_flag as BOOL

Expand Down Expand Up @@ -168,6 +170,7 @@ namespace utils '# fb.svc.utils

'# create the signal used to stop the service thread.
_svc_stop_signal = condcreate()
_svc_stop_mutex = mutexcreate()

'# register the Console Handler
SetConsoleCtrlHandler(@_console_handler, TRUE)
Expand All @@ -189,6 +192,9 @@ namespace utils '# fb.svc.utils
if not (service->onStart = 0) then
'# create the thread
working_thread = threadcreate(@ServiceProcess.call_onStart, service)
if (working_thread = 0) then
print "Failed to create working thread."
end if
end if

print "Service is in running state."
Expand All @@ -197,7 +203,11 @@ namespace utils '# fb.svc.utils
'# now that onStart is running, must monitor the stop_signal
'# in case it arrives, the service state must change to exit the
'# working thread.
condwait(_svc_stop_signal)
mutexlock(_svc_stop_mutex)
do while (_svc_stopped = FALSE)
condwait(_svc_stop_signal, _svc_stop_mutex)
loop
mutexunlock(_svc_stop_mutex)

print "Stop signal received, stopping..."

Expand All @@ -222,7 +232,7 @@ namespace utils '# fb.svc.utils

'# now that service was stopped, destroy the references.
conddestroy(_svc_stop_signal)

mutexdestroy(_svc_stop_mutex)
print "Done."
end if
else
Expand Down Expand Up @@ -287,9 +297,12 @@ namespace utils '# fb.svc.utils

'# now fire the signal
_dprint("fire stop signal")
mutexlock(_svc_stop_mutex)
condsignal(_svc_stop_signal)
result = TRUE
_svc_in_console_stop_flag = FALSE
_svc_stopped = TRUE
mutexunlock(_svc_stop_mutex)

case else:
_dprint("unsupported CTRL EVENT")
Expand Down
2 changes: 2 additions & 0 deletions lib/ServiceFB/_utils_internals.bi
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ namespace utils '# fb.svc.utils
'# will raise in case Ctrl+C / Ctrl+Break or other events are
'# received.
extern _svc_stop_signal as any ptr
extern _svc_stop_mutex as any ptr
extern _svc_stopped as BOOL
extern _svc_in_console as ServiceProcess ptr
extern _svc_in_console_stop_flag as BOOL
end namespace '# fb.svc.utils
Expand Down

0 comments on commit 9fab571

Please sign in to comment.