Mercurial > repos > rhope
comparison runtime/context.c @ 140:c14698c512f1
Untested addition of Pause/Resume
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 20 Nov 2010 20:03:25 +0000 |
parents | a68e6828d896 |
children | f2cb85c53ced |
comparison
equal
deleted
inserted
replaced
139:a68e6828d896 | 140:c14698c512f1 |
---|---|
1 #include "context.h" | 1 #include "context.h" |
2 #include "object.h" | 2 #include "object.h" |
3 #include "thread.h" | |
3 #include <stdlib.h> | 4 #include <stdlib.h> |
4 #include <stddef.h> | 5 #include <stddef.h> |
5 #include <stdio.h> | 6 #include <stdio.h> |
7 | |
8 context * contextqueue[32]; | |
9 int32_t cq_readloc=0; | |
10 int32_t cq_writeloc=0; | |
11 rh_mutex(cq_lock) | |
12 | |
13 void cqueue_init() | |
14 { | |
15 rh_mutex_init(cq_lock); | |
16 } | |
17 | |
18 context * get_cqueue() | |
19 { | |
20 context * ret; | |
21 rh_lock(cq_lock); | |
22 if (cq_readloc == cq_writeloc) | |
23 { | |
24 rh_unlock(cq_lock); | |
25 return NULL; | |
26 } | |
27 ret = contextqueue[cq_readloc++]; | |
28 if (cq_readloc == 32) | |
29 cq_readloc = 0; | |
30 rh_unlock(cq_lock); | |
31 return ret; | |
32 } | |
33 | |
34 int32_t put_cqueue(context * ct) | |
35 { | |
36 rh_lock(cq_lock); | |
37 if ((cq_writeloc+1)&31 == cq_readloc) | |
38 { | |
39 rh_unlock(cq_lock); | |
40 return 0; | |
41 } | |
42 contextqueue[cq_writeloc++] = ct; | |
43 rh_unlock(cq_lock); | |
44 return 1; | |
45 } | |
6 | 46 |
7 stackchunk * new_stack() | 47 stackchunk * new_stack() |
8 { | 48 { |
9 stackchunk * st = malloc(sizeof(stackchunk)); | 49 stackchunk * st = malloc(sizeof(stackchunk)); |
10 st->prev = NULL; | 50 st->prev = NULL; |
16 context * new_context() | 56 context * new_context() |
17 { | 57 { |
18 context * c = malloc(sizeof(context)); | 58 context * c = malloc(sizeof(context)); |
19 c->stack_begin = new_stack(); | 59 c->stack_begin = new_stack(); |
20 c->current_stack = c->stack_begin; | 60 c->current_stack = c->stack_begin; |
21 c->transaction = NULL; | 61 c->resume_cdata = c->runafter = c->transaction = NULL; |
62 c->resumeable = 0; | |
63 c->start_func = -1; | |
22 return c; | 64 return c; |
23 } | 65 } |
24 | 66 |
25 void free_context(context * c) | 67 void free_context(context * c) |
26 { | 68 { |