home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / library / createtask.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  3KB  |  116 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public
  16.  *  License along with this library; if not, write to the Free
  17.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *  createtask.c,v 1.1.1.1 1994/04/04 04:30:45 amiga Exp
  20.  *
  21.  *  createtask.c,v
  22.  * Revision 1.1.1.1  1994/04/04  04:30:45  amiga
  23.  * Initial CVS check in.
  24.  *
  25.  *  Revision 1.1  1992/05/14  19:55:40  mwild
  26.  *  Initial revision
  27.  *
  28.  *
  29.  *  Code is based on the example given in the RKM Libraries & Devices.
  30.  */
  31.  
  32. #define _KERNEL
  33. #include "ixemul.h"
  34.  
  35. #include <exec/tasks.h>
  36. #include <exec/memory.h>
  37.  
  38. /* the template for the mementries. Unfortunately, this is hard to
  39.  * do from C; mementries have unions, and they cannot be statically
  40.  * initialized...
  41.  *
  42.  * In the interest of simplicity I recreate the mem entry structures
  43.  * here with appropriate sizes. We will copy this to a local
  44.  * variable and set the stack size to what the user spedified,
  45.  * then attempt to actually allocate the memory.
  46.  *
  47.  * NOTE: static data here ok with shared library, since never changed
  48.  *       directly
  49.  */
  50. #define ME_TASK    0
  51. #define ME_STACK   1
  52. #define NUMENTRIES 2
  53.  
  54. struct FakeMemEntry {
  55.   ULONG fme_Reqs;
  56.   ULONG fme_Length;
  57. };
  58.  
  59. static 
  60. struct FakeMemList {
  61.   struct Node fml_Node;
  62.   UWORD fml_NumEntries;
  63.   struct FakeMemEntry fml_ME[NUMENTRIES];
  64. } TaskMemTemplate = {
  65.   { 0, },
  66.   NUMENTRIES,
  67.   {
  68.     { MEMF_PUBLIC | MEMF_CLEAR, sizeof(struct Task) }, /* task */
  69.     { MEMF_CLEAR, 0 },                       /* stack */
  70.   }
  71. };
  72.  
  73. struct Task *
  74. CreateTask (unsigned char *name, long pri, void *initPC, u_long stackSize)
  75. {
  76.   struct Task *newTask;
  77.   struct FakeMemList fakememlist;
  78.   struct MemList *ml;
  79.   
  80.   /* round the stack up to longwords... */
  81.   stackSize = (stackSize + 3) & ~3;
  82.  
  83.   /* this will allocate two chunks of memory, task of PUBLIC and
  84.    * stack of PRIVATE
  85.    */
  86.   fakememlist = TaskMemTemplate;
  87.   fakememlist.fml_ME[ME_STACK].fme_Length = stackSize;
  88.  
  89.   ml = (struct MemList *) AllocEntry ((struct MemList *)&fakememlist);
  90.   
  91.   /* strange way of returning an error... */
  92.   if ((u_int)ml & (1<<31)) return NULL;
  93.   
  94.   /* set the stack accounting stuff */
  95.   newTask = (struct Task *) ml->ml_ME[ME_TASK].me_Addr;
  96.   
  97.   newTask->tc_SPLower = ml->ml_ME[ME_STACK].me_Addr;
  98.   newTask->tc_SPUpper = (APTR)((ULONG)(newTask->tc_SPLower) + stackSize);
  99.   newTask->tc_SPReg = newTask->tc_SPUpper;
  100.   
  101.   /* misc task data structures */
  102.   newTask->tc_Node.ln_Type = NT_TASK;
  103.   newTask->tc_Node.ln_Pri = pri;
  104.   newTask->tc_Node.ln_Name = name;
  105.   
  106.   /* add it to the tasks memory list */
  107.   NewList (&newTask->tc_MemEntry);
  108.   AddHead (&newTask->tc_MemEntry, (struct Node *)ml);
  109.   
  110.   /* add the task to the system -- use the default final PC */
  111.   AddTask (newTask, initPC, 0);
  112.   
  113.   /* something has changed here for after V36, but I stick to what I know.. */
  114.   return newTask;
  115. }
  116.