[comp.sys.amiga.tech] CreateTask - Why do I crash

sjorr@rose.waterloo.edu (Stephen Orr) (03/18/90)

Below is an example of my failure to get CreateTask to behave the
way I expect it to. If anybody has any suggestions I would
be very thankful. The code is compiled under Lattice, and while
it will sometimes run fine, if I reboot the machine and run the
same code it will lock up the machine (ie no Task Error, no Guru
just everything stops). Upon reboot I get a Guru.

					Thanks in Advance
					Stephen Orr
					SandIsoft
---------------8<-------------------8<-----------------------
/*
 * Here is example code to run two tasks on an Amiga,
 * compile it under Lattice with
 *
 * LC -L twotasks
 *
 * the __saveds keyword IS neccessary in order to ensure that
 * the new task(s) re-access the shared data space (ds)
 *
 * Initial test to open up two tasks, and have them close down
 * without crashing the Amiga ! (I hope)
 */
#include <exec/types.h>
#include <exec/memory.h>
#include <intuition/intuition.h>
#include <exec/tasks.h>
#include <proto/all.h>
#define STACK 4096

struct NewWindow	nw1 = {0,0,640,100,0,1,CLOSEWINDOW,WINDOWCLOSE|WINDOWDEPTH|WINDOWDRAG|ACTIVATE,NULL,NULL,"Hello",NULL,NULL,0,0,0,0,WBENCHSCREEN};
struct NewWindow	nw2 = {0,100,640,100,0,1,CLOSEWINDOW,WINDOWCLOSE|WINDOWDEPTH|WINDOWDRAG|ACTIVATE,NULL,NULL,"There",NULL,NULL,0,0,0,0,WBENCHSCREEN};

volatile int task2 = 0;/* prevent register optimizations	*/

__saveds void OpenAWindow1(void);
__saveds void OpenAWindow2(void);

/*
 * here is our main code module
 */
main()
{
struct Task *subtask;

IntuitionBase = (void *) OpenLibrary("intuition.library",0);
GfxBase = (void *) OpenLibrary("graphics.library",0);

if (IntuitionBase && GfxBase) {
	/*
	 * Here we create the other task
	 * and call our own
	 */
	subtask = CreateTask("Task Two",0,(APTR)OpenAWindow2, (unsigned long)STACK);
	OpenAWindow1();
	/*
	 * here's the really crude 'exit code', it ensures the sub-task
	 * is done before killing it. The Delay(1) polls the sub task every
	 * second (?) checking to see if it's done yet. There are other,
	 * better ways to do this, but this one works for now.
	 */
	while (!task2) Delay(1);
	DeleteTask(subtask);
}
if (IntuitionBase) CloseLibrary(IntuitionBase);
if (GfxBase) CloseLibrary(GfxBase);
} /* end of main */

/*
 * This function opens a window as our main task
 */
__saveds void OpenAWindow1()
{
struct Window	*win;
struct Message	*msg;

	win = OpenWindow(&nw1);
	/*
	 * now wait for the closewindow message to come
	 */
	if (win) {
		Wait(1<<win->UserPort->mp_SigBit);
		while (msg=GetMsg(win->UserPort)) ReplyMsg(msg);
		CloseWindow(win);
	}
}

/*
 * This function is our 'sub task', it also opens a window
 */
__saveds void OpenAWindow2()
{
struct Window	*win;
struct Message	*msg;

	win = OpenWindow(&nw2);
	if (win) {
		/*
		 * now wait for the closewindow message to come
		 */
		Wait(1<<win->UserPort->mp_SigBit);
		while (msg=GetMsg(win->UserPort)) ReplyMsg(msg);
		CloseWindow(win);
	}
	task2 = 1;
	SetTaskPri(FindTask(0),-120);
	while (1);	/* wait to die	*/
}