00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "stdio.h"
00016 #include "stdlib.h"
00017 #include "string.h"
00018 #include "task.h"
00019 #include "pq.h"
00020 #include "HeapManager.h"
00021
00028
00029 ECB *TimeoutList;
00030 static ECB *ECBPool;
00045 static ECB *AllocECB(void)
00046 {
00047
00048
00049
00050
00051
00052
00053
00054
00055 ECB *rv;
00056
00057 char sr = Disable();
00058 if (ECBPool)
00059 {
00060 rv = ECBPool;
00061 ECBPool = ECBPool->Tnext;
00062 Enable(sr);
00063 }
00064 else
00065 {
00066 Enable(sr);
00067 rv = (ECB *)malloc(sizeof(ECB));
00068 }
00069 return rv;
00070 }
00071
00083 static void FreeECB(ECB *e)
00084 {
00085
00086
00087
00088
00089
00090
00091 char sr;
00092
00093 sr = Disable();
00094 if(e->SemaphoreMode)
00095 {
00096 if(e == TimeoutList)
00097 TimeoutList = e->Tnext;
00098 if(e->Tprev)
00099 e->Tprev->Tnext = e->Tnext;
00100 if(e->Tnext)
00101 e->Tnext->Tprev = e->Tprev;
00102 }
00103 e->Tnext = ECBPool;
00104 ECBPool = e;
00105 Enable(sr);
00106 }
00107
00122 ECB *NewSemaphore(int InitCount,int Mode,char *name)
00123 {
00124 ECB *r;
00125
00126 if((r = AllocECB()) != NULL)
00127 CreateSemaphore(r,InitCount,Mode,name);
00128 return r;
00129 }
00130
00144 void CreateSemaphore(ECB *e,int InitCount,int Mode,char *n)
00145 {
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 char sr;
00159
00160 e->EventCount = InitCount;
00161 e->SemaphoreMode = Mode;
00162 strcpy(e->name,n);
00163 e->task_h = 0;
00164 e->task_t = 0;
00165
00166
00167
00168 sr = Disable();
00169 if(e->SemaphoreMode == 1)
00170 {
00171 if(TimeoutList)
00172 {
00173 e->Tnext = TimeoutList;
00174 e->Tprev = 0;
00175 TimeoutList->Tprev = e;
00176 TimeoutList = e;
00177 }
00178 else
00179 {
00180 TimeoutList = e;
00181 e->Tnext = 0;
00182 e->Tprev = 0;
00183 }
00184 }
00185 Enable(sr);
00186 }
00187
00200 void DeleteSemaphore(ECB *e)
00201 {
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212 char sr;
00213 TCB *t;
00214
00215
00216 sr = Disable();
00217 while(e->task_h)
00218 {
00219 t = e->task_h;
00220 e->task_h = t->next;
00221 t->next = (TCB *)0;
00222 t->status = EVENT_DELETED;
00223 Insert(&ActiveTasks,t);
00224 }
00225 FreeECB(e);
00226 Enable(sr);
00227 }
00228