home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnuc / library / rcs / createtask.c,v < prev    next >
Encoding:
Text File  |  1992-07-04  |  3.4 KB  |  135 lines

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