15 return KE_ILLEGAL_CONTEXT;
18 if (param->attr & ~(VA_THFIFO | VA_THPRI | VA_MEMBTM)) {
19 return KE_ILLEGAL_ATTR;
22 if (param->size == 0) {
23 return KE_ILLEGAL_MEMSIZE;
28 vpl = heap_alloc(TAG_VPL,
sizeof(*vpl));
34 vpl->heap = CreateHeap(param->size, (param->attr & VA_MEMBTM) << 1);
35 vpl->free_size = HeapTotalFreeSize(vpl->heap);
36 vpl->event.attr = param->attr;
37 vpl->event.option = param->option;
39 list_init(&vpl->event.waiters);
40 list_insert(&thctx.vpool, &vpl->vpl_list);
44 return MAKE_HANDLE(vpl);
47int DeleteVpl(
int vplId)
55 return KE_ILLEGAL_CONTEXT;
59 vpl = HANDLE_PTR(vplId);
60 if (!HANDLE_VERIFY(vplId, TAG_VPL)) {
62 return KE_UNKNOWN_VPLID;
65 list_for_each_safe (waiter, &vpl->event.waiters, queue) {
66 waiter->saved_regs->v0 = KE_WAIT_DELETE;
67 waiter->status = THS_READY;
68 list_remove(&waiter->queue);
69 readyq_insert_back(waiter);
72 waiter_count = vpl->event.waiter_count;
73 DeleteHeap(vpl->heap);
74 list_remove(&vpl->vpl_list);
78 thctx.run_next = NULL;
79 return thread_leave(KE_OK, 0, state, 0);
87void *AllocateVpl(
int vplId,
int size)
95 return (
void *)KE_ILLEGAL_CONTEXT;
98 if (
CpuSuspendIntr(&state) == KE_CPUDI && (thctx.debug_flags & 8)) {
99 Kprintf(
"WARNING: AllocateVpl KE_CAN_NOT_WAIT\n");
102 vpl = HANDLE_PTR(vplId);
103 if (!HANDLE_VERIFY(vplId, TAG_VPL)) {
105 return (
void *)KE_UNKNOWN_VPLID;
108 if (!size || vpl->free_size < size) {
110 return (
void *)KE_ILLEGAL_MEMSIZE;
113 mem = AllocHeapMemory(vpl->heap, size);
119 thread = thctx.current_thread;
120 thread->status = THS_WAIT;
121 thread->wait_type = TSW_FPL;
122 thread->wait_event = &vpl->event;
124 thctx.run_next = NULL;
125 vpl->event.waiter_count++;
127 if (vpl->event.attr & VA_THPRI) {
130 list_insert(&vpl->event.waiters, &
thread->queue);
133 return (
void *)thread_leave(size, 0, state, 1);
136void *pAllocateVpl(
int vplId,
int size)
143 return (
void *)KE_ILLEGAL_CONTEXT;
147 vpl = HANDLE_PTR(vplId);
148 if (!HANDLE_VERIFY(vplId, TAG_VPL)) {
150 return (
void *)KE_UNKNOWN_VPLID;
153 if (!size || vpl->free_size < size) {
155 return (
void *)KE_ILLEGAL_MEMSIZE;
158 mem = AllocHeapMemory(vpl->heap, size);
162 return (
void *)KE_NO_MEMORY;
168void *ipAllocateVpl(
int vplId,
int size)
174 return (
void *)KE_ILLEGAL_CONTEXT;
177 vpl = HANDLE_PTR(vplId);
178 if (!HANDLE_VERIFY(vplId, TAG_VPL)) {
179 return (
void *)KE_UNKNOWN_VPLID;
182 mem = AllocHeapMemory(vpl->heap, size);
184 return (
void *)KE_NO_MEMORY;
190int FreeVpl(
int vplId,
void *memory)
198 return KE_ILLEGAL_CONTEXT;
202 vpl = HANDLE_PTR(vplId);
203 if (!HANDLE_VERIFY(vplId, TAG_VPL)) {
207 return KE_UNKNOWN_VPLID;
210 if (FreeHeapMemory(vpl->heap, memory) < 0) {
215 if (vpl->event.waiter_count) {
216 thread = list_first_entry(&vpl->event.waiters,
struct thread, queue);
217 new_mem = AllocHeapMemory(vpl->heap,
thread->saved_regs->v0);
219 vpl->event.waiter_count--;
220 list_remove(&
thread->queue);
221 thread->status = THS_READY;
222 thread->saved_regs->v0 = (u32)new_mem;
224 return thread_start(
thread, state);
239 return KE_ILLEGAL_CONTEXT;
243 vpl = HANDLE_PTR(vplId);
244 if (!HANDLE_VERIFY(vplId, TAG_VPL)) {
246 return KE_UNKNOWN_VPLID;
249 vpl_get_info(vpl,
info);
261 return KE_ILLEGAL_CONTEXT;
264 vpl = HANDLE_PTR(vplId);
265 if (!HANDLE_VERIFY(vplId, TAG_VPL)) {
266 return KE_UNKNOWN_VPLID;
269 vpl_get_info(vpl,
info);
276 info->attr = vpl->event.attr;
277 info->option = vpl->event.option;
278 info->size = vpl->free_size;
279 info->freeSize = HeapTotalFreeSize(vpl->heap);
280 info->numWaitThreads = vpl->event.waiter_count;
int CpuResumeIntr(int state)
int QueryIntrContext(void)
int CpuSuspendIntr(int *state)