1

I want to add multithreading to a language (BlitzBasic) that doesn't support multithreading or function pointers, but does support running functions from a .dll

The language's compiler compiles to some form of C which is then compiled to native code. It is Windows-only.

My idea so far was to make a function in a dll that would be called from within BlitzBasic. This function would check the call stack to find out from where it was called. It would then fork a new thread and return. The new thread would wait for a short while (10ms or so) and then resume execution at the point from where the function was called in BlitzBasic. In BlitzBasic you'd then have to control the rest of the execution based on that delay (so the first thread to get out of that function would e.g. set a variable that would tell the second thread to execute some different function.

I know that multithreading introduces a lot of other problems, but I'll cross that bridge when I get there.

Is there a way to find the execution point of a function in the callstack?

3 Answers 3

1

A function pointer is insufficient in this context because a function pointer only lets you invoke the function from its beginning, but you are already in the middle of the function.

You could use setjmp/longjmp, but unless the original language was designed with thread safety in mind you are probably going to run into lots of issues.

In particular if your language has any global state, you are going to have to create locks around it.

Personally, I would just use the stack itself for resumption and have your function block until it is ready to return. You can still spawn a new thread. This will be a lot easier. You can also start with a simpler cooperative threading model instead of starting out with preemption. This will let you see if it will work at all.

Sign up to request clarification or add additional context in comments.

Comments

1

This is like setjmp and longjmp.

You gotta be careful with these, because normal cleanup-on-exit doesn't happen.

Comments

1

You're going to have to use assembly language. You would need an assembly language language function that walks up one stack stack frame.

Usually the Frame Pointer register points to the current stack frame. Then you need to examine the frame and to locate the saved frame pointer (that of the caller of your routine). The frame pointer usually points to the saved address of the next instruction to execute upon return.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.