Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Multi-tasking

This thread is locked; no one can reply to it. rss feed Print
Multi-tasking
essoftgui
Member #1,828
January 2002

I am making a GUI in DJGPP and Allegro and I want it to have multitasking capabilities. I have tried the two multitasking libraries on this site and I can't get either to compile for some reason. Are there other multitasking libraries I can try? and How would I go about writing my own?

axilmar
Member #1,204
April 2001

If you are looking for multitasking for DJGPP, you won't be lucky. If you are looking for multithreading, I've made a co-operative multithreading lib some years ago that I am willing to share.

[ January 12, 2002: Message edited by: achil ]

essoftgui
Member #1,828
January 2002

Thanks, the multi-threading sounds interesting but I'm not totally sure what multi-threading is (I think I know but I'm not sure). What I want to do is create a GUI which runs each program as a separate task (thread?) like Linux and then switch between them with the pre-emptive (but I don't really mind if it's co-operative) method and I also need it to regularly switch from the programs running to the main GUI function to check memory, mouse position etc. and then back to the programs.
I have tried MX-Windows at: http://www.geocities.com/SiliconValley/Peaks/3011/mxwin.htm
and Palantir at:
both of which looked very useful multitasking kernels (I found them on the Miscellaneous files section of allegro.cc) but unfortunately I couldn't get either of them to compile (which is probably my fault) here are the errors I got with DJGPP and Allegro 4.0.0 (which compiled fine):
MX-Windows (the last few errors, there are too many to include them all):
src/mxwin.c:2674: warning: control reaches end of non-void function
src/mxwin.c: In function `HideWindow':
src/mxwin.c:2674: warning: control reaches end of non-void function
src/mxwin.c: In function `GrabEvents':
src/mxwin.c:2674: warning: control reaches end of non-void function
src/mxwin.c: In function `ReleaseEvents':
src/mxwin.c:2674: warning: control reaches end of non-void function
src/mxwin.c: In function `TranslatePoints':
src/mxwin.c:2263: warning: `x' might be used uninitialized in this function
src/mxwin.c:2263: warning: `y' might be used uninitialized in this function
src/mxwin.c: In function `GetEvent':
src/mxwin.c:2674: warning: control reaches end of non-void function
src/mxwin.c: In function `RemoveTimer':
src/mxwin.c:2446: warning: `res' might be used uninitialized in this function
src/mxwin.c: In function `SetCursor':
src/mxwin.c:2674: warning: control reaches end of non-void function
src/mxwin.c: In function `_paint_loop_init':
src/mxwin.c:2674: warning: control reaches end of non-void function
src/mxwin.c: In function `_paint_loop_clipped':
src/mxwin.c:2674: warning: control reaches end of non-void function
src/mxwin.c: In function `_is_proper_cursor':
src/mxwin.c:2674: warning: control reaches end of non-void function
make.exe: *** [obj/mxwin.o] Error 1
and Palantir:
C:\djgpp\allegro\palantir\pdemo>make
make.exe: *** No rule to make target `../../src/djgpp/interndj.h', needed by `ex
1.exe'. Stop.

gillius
Member #119
April 2000

If you want to use multiple threads, your best bet would be to switch to a modern operating system like Linux, Windows, or another UNIX rather than try to hack it into an OS that was dropped 7 years ago.

If you are using Allegro then your code will be the same if you switched to Windows or Linux, assuming you have written it correctly for Allegro 4 (or the WIPs). I would suggest MingGW. You can use it from the command line exactly as you'd use DJGPP, except that it makes Windows programs rather than DOS ones.

Once you get to Windows you'll want to use a threading API. I suggest finding the pthreads-win32 port of pthreads fromRed Hat. That way your program will work in Windows and Linux w/o code modification.

Keep in mind that Allegro makes no guarantees about running multithreaded, so really you should only access it from a single thread at a time by using mutexes. However many parts of Allegro now use threads in the background and many parts are thread safe, but it varies based on platform and again, no guarantees on ANYTHING. Since you can't make a program based on on "maybes" you might want to reconsider your multithreaded approach unless placing all Allegro calls in a guarded section is acceptable for you.

Gillius
Gillius's Programming -- https://gillius.org/

axilmar
Member #1,204
April 2001

I understand that you want to make a graphical shell for DOS, while keeping it multitasking, don't you ? That is a very interested concept and I would like to help you.

Those who are saying 'don't do it because DOS is old news' forget the fun of doing-it-yourself. You do such projects in order to have fun while doing it and learn/understand a few things in a way that you wouldn't learn/understand under other circumstances.

Mx-Windows has not been tested with Allegro 4.0. It works with 3.12, if I recall correctly. It is not a multitasking/multithreading lib. It is a window system, good enough for learning how to manage regions and clip output, and nothing else. The lib I was talking about was about multiple threads in the same DJGPP application by using Allegro. Most probably some change in Allegro results of a symbol not understood by MxWin, so all code below it is reported as errors by the compiler.

As for DOS and multitasking, you will have to replace most functions of DOS (interrupt 0x21 mainly) with your own versions which use mutexes and semaphores to protect the critical parts of the "O/S". That means you have to dig for DOS documentation; there is plenty on the web. You will also have to implement task switching on a timer interrupt.

By the way, multitasking means multiple applications running at in parallel, while multithreading means multiple paths(=threads) of execution in the same application. Threads share memory space, while applications don't(except when there is "shared memory" between apps.

Peter Wang
Member #23
April 2000

I'd also suggest using some form of cooperative multitasking. For example, whenever one application is ready to give up control of the CPU, it should call a "yield" function. That function would switch the execution context to another app, which would continue from where it last yielded. You can implement this yield function using a coroutine library.
Unfortunately the coroutine library I was going to point to disappeared :-( Well, you might get some ideas from this too:
http://www.alphalink.com.au/~tjaden/dev/multitask.c
which is not a library but just a little hack I wrote once.
BTW, I wrote The Paw a few years ago, which was a light desktop using co-op multitasking. Each app basically had an update function which had to be polled. This meant each program was basically a state machine. I definitely don't recommend that approach. You might as well shoot yourself now, because you surely will later :-)

axilmar
Member #1,204
April 2001

Wow, you are the PAW author ? It was quite impressive.

Does longjmp/setjmp save the whole CPU context ? I know it does under DJGPP, but I think it does not under MSVC.

the "pusha" instruction can be used to store the CPU context for the main integer registers, though.

Thomas Fjellstrom
Member #476
June 2000
avatar

heheh. Just tried that multitask hack in Linux. It works. Now i wonder what would happen if instead of having each thread call 'sleep' you setup a timer and had it longjmp(next,1) ;) but using maybe a round robin style scheduler or maybe something a bit more complex?

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Peter Wang
Member #23
April 2000

achil, now I'm sure I know who you are :-) Yes, I wrote The Paw and IIRC we talked when you started MX-Windows.

As for setjmp saving the CPU context in MSVC, I don't know. However, I think it should be okay. One thing that I think I've seen is that longjmp() might not restore local variables in the setjmp'ing function properly. Variables further up the stack seem to be okay though. Maybe it's just me... the last time I used setjmp/longjmp I was also doing somewhat dodgy things to the stack (saving and restoring [obliterating] it :-)

axilmar
Member #1,204
April 2001

Did we ? It must have been so long ago...I don't remember the slightest detail of what we talked about. Do you ?

essoftgui
Member #1,828
January 2002

Thanks for all the help, I must warn you that I have not been programming in C/C++ for long (I have been programming basic for years, but only started learning C recently), only about 3 months so it looks like i'll have to learn a lot more C before I can attempt to add multi-threading to my GUI, it seems much more complicated than I thought. (or maybe i'll just go for the "shoot myself now" option!).
There is another question I have about C though, in QBasic (ahhhhhh!) I could set up something called a subscript variable, below is how it is described in the QBasic help file...
Subscripts
The dimensions of the array. Multiple dimensions can be
declared. The subscript syntax is described below.
---------------------------------------------
Subscripts in DIM statements have the following form:
[lower TO] upper [,[lower TO] upper]...
The TO keyword provides a way to indicate both the lower and the upper
bounds of an array's subscripts. The following statements are
equivalent (if there is no OPTION BASE statement):
DIM A(8,3)
DIM A(0 TO 8, 0 TO 3)
DIM A(8,0 TO 3)
---------------------------------------------
...I was wondering how/if it is possible to do this in C, because there is another program that i'm trying to write that would otherwise require 248 million variables to be declared manually!

essoftgui
Member #1,828
January 2002

Sorry, I forgot to say, I searched for "The Paw" and found this URL http://www.alphalink.com.au/~tjaden/thepaw/, if this is the same one then from the screenshot it looks brilliant, I'm downloading it now.

Falling Sky
Member #1,823
January 2002

To create mutiple dimenionsal arrays use
int array[10][10];

SamuraiCrow
Member #1,733
December 2001

Note: off-topic post (this question should have been a new thread)
quote:
Subscripts in DIM statements have the following form:
[lower TO] upper [,[lower TO] upper]...

The only way I know is to make a macro that adds the missing subtractions:
#define arrayname(x,y) newarrayname[(x)-lowerx][(y)-lowery]
To declare the array do this:
int newarrayname[upperx-lowerx][uppery-lowery];
The usage would look like this (assuming you ranged it with a macro):
z=arrayname(a,b);
Note: If you don't need to scale the offset to a specific range the option base in C is always zero. The usage of a non-macro array is this:
z=newarrayname[a][b];
[ January 15, 2002: Message edited by: SamuraiCrow ]

Peter Wang
Member #23
April 2000

achil, I'm not sure we actually talked. My email archives has this:
quote:
Date: Wed, 04 Aug 1999 23:08:52 +0300
From: Achillefs Margaritis
Subject: Allegro GUI
Since there are so many GUI packages for Allegro and DJGPP (bgui, degui, paw, uniforms, wham etc) we don't we all coordinate our efforts to make a unique solid GUI environment like Windows by using DJGPP and Allegro(plus any other code that is free on the net and might help us) ?

which seems like just a generic message. I don't have my reply around. OT :-)

axilmar
Member #1,204
April 2001

Aaah yes...The ever-going quest for a unified Allegro gui...I've given up. I am going solo.

Go to: