[Harbour] Asynchronous procedures
Mindaugas Kavaliauskas
dbtopas at dbtopas.lt
Tue Feb 5 10:19:39 EST 2008
Mindaugas Kavaliauskas wrote:
> It's compilers job to process language structures such as loops and ifs
> to machine code. And the code could be written to be a single thread
> code. So, the question (a little bit philosophical) is, why compiler can
> not help to write this single thread code?
Hi,
I've made a try to implement my ideas. Instead of long descriptions I
present it by sample code:
-------------------------------------
PROC main()
LOCAL hContext
DO WHILE fctxIsContext( hContext := semithread( hContext, 100 ) )
IF INKEY(0) == 27; EXIT
ENDIF
ENDDO
/* Let's print some more and pass a different param */
IF fctxIsContext( hContext ); semithread( hContext, "hello" )
ENDIF
? "main() exit"
RETURN
FUNC semithread( hContext, xParam )
LOCAL nInc, nIndex, nCount
? "semithread()"
/* We assume xParam indicates count. Let's save it in local
variable, since locals are saved in function context */
nCount := xParam
nInc := 0
FOR nIndex := 1 TO nCount
? nIndex
nInc++
IF nInc >= 10
nInc := 0
RETURN_CONTEXT
? "saved nCount:", nCount, "passed param:", xParam
ENDIF
NEXT
? "semithread() exit"
RETURN NIL
-------------------------------------
The idea is implemented in this way:
- write RETURN_CONTEXT instead of RETURN, if you want function to return
it's execution context. Pass this context as a first parameter to
function to continue execution;
- compile .prg code to .hrb file;
- compile .hrb file to .c code. I've mentioned, it's compiler's job to
generate code. Of cause, I was not going to write a new compiler, I just
take compiled Harbour code (.hrb file), change it and generate .c
output. .hrb output is also implemented;
- link fctx.c module to your application.
The implementation is not complete, there are issues related to the
usage of BEGIN/END SEQUENCE, etc., but you can use it if you like :)
Read comments and source of fctx.c, if you are interested in
implementation details.
Best regards,
Mindaugas
P.S. almost forgot to say the main thing - you'll find it at
http://www.dbtopas.lt/hrb/fctx.zip
More information about the Harbour
mailing list