blob: 472191fb7ec5fc49e8afc1affe1086e8d1615178 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001//#define TESTING_ASSERT(...)
2#define TESTING_ASSERT(cond, desc, args...) if(!(cond)){fprintf(stderr, "error: " desc " at " __FILE__ ": %d\n", ##args, __LINE__);}
3#define TESTING_ASSERT_RETURN(cond, desc, args...) TESTING_ASSERT(cond, desc, ##args); if(!(cond)){return 1;}
4// leave TESTING_ASSERT_RETURN (segfaults result otherwise)
5
6#include "resource_internal.h"
7#include <stdlib.h>
8#include <stdio.h>
9#include <sys/types.h>
10#include <unistd.h>
11#include <signal.h>
12#include <string.h>
13#include <errno.h>
14
15int RESOURCE_KILL_SIGNAL;
16
17__attribute__((constructor)) void __constructor(){
18 RESOURCE_KILL_SIGNAL = SIGRTMIN + 5;
19}
20
21void aos_resource_init(uint16_t num){
22 aos_resource *resource = aos_resource_get(num);
23 resource->num = num;
24 resource->resource_mutex = 0;
25 resource->modify = 0;
26 resource->request = 0;
27 resource->kill = 0;
28 resource->owner = 0;
29 resource->priorities = shm_malloc(sizeof(struct HeapStruct) + AOS_RESOURCE_PRIORITY_STACK_LENGTH * sizeof(uint8_t));
30 resource->priorities->Elements = (uint8_t *)((uintptr_t)resource->priorities + sizeof(struct HeapStruct)); // only do 1 shm_malloc (directly above)
31 Initialize(AOS_RESOURCE_PRIORITY_STACK_LENGTH, resource->priorities);
32 AOS_RESOURCE_STATE_SET_ON(AOS_RESOURCE_STATE_WANTS_IT, num, aos_resource_entity_root_get());
33}
34void aos_resource_entity_root_create(){
35 global_core->mem_struct->resources.root = aos_resource_entity_create(0);
36}
37inline aos_resource_entity *aos_resource_entity_root_get(){
38 return global_core->mem_struct->resources.root;
39}
40inline aos_resource *aos_resource_get(uint16_t num){
41 return &global_core->mem_struct->resources.resources[num];
42}
43
44aos_resource_entity *aos_resource_entity_create(uint8_t base_priority){
45 aos_resource_entity *local = shm_malloc(sizeof(aos_resource_entity));
46 memset(local->state, 0x00, sizeof(local->state));
47 local->base_priority = base_priority;
48 local->parent = NULL; // for the root entity
49 return local;
50}
51int aos_resource_entity_set_parent(aos_resource_entity *local, aos_resource_entity *parent){
52 TESTING_ASSERT_RETURN(local != NULL, "do not have a local entity");
53 TESTING_ASSERT_RETURN(parent != local, "can't set parent to self");
54 TESTING_ASSERT_RETURN(parent != NULL, "have to have a parent to set to");
55 local->parent = parent;
56 if(parent->parent == NULL){
57 local->root_action = getpid();
58 }else{
59 local->root_action = parent->root_action;
60 }
61 if(parent->priority > local->base_priority){
62 local->priority = parent->priority;
63 }else{
64 local->priority = local->base_priority;
65 }
66 if(local->state[0] != 0 || memcmp(local->state, local->state + 1, sizeof(local->state) - 1)){ // if it's not all 0s
67 TESTING_ASSERT(0, "local->state isn't all 0s when changing parents (fixing it)");
68 memset(local->state, 0x00, sizeof(local->state));
69 }
70 return 0;
71}
72
73void aos_resource_kill(pid_t action){
74 union sigval sival;
75 sival.sival_int = 0;
76 if(sigqueue(action, RESOURCE_KILL_SIGNAL, sival) < 0){ // if sending the signal failed
77 fprintf(stderr, "sigqueue RESOURCE_KILL_SIGNAL (which is %d) with pid %d failed with errno %d ", RESOURCE_KILL_SIGNAL, action, errno);
78 perror(NULL);
79 }
80}
81int aos_resource_request(aos_resource_entity *local, aos_resource *resource){
82 TESTING_ASSERT_RETURN(local != NULL, "do not have a local entity");
83
84 if(mutex_lock(&resource->request))
85 return 1;
86 if(mutex_lock(&resource->kill)){
87 mutex_unlock(&resource->request);
88 return 1;
89 }
90 if(mutex_lock(&resource->modify)){
91 mutex_unlock(&resource->kill);
92 mutex_unlock(&resource->request);
93 return 1;
94 }
95
96 aos_resource_entity *c = local;
97 while(c->parent != NULL && !AOS_RESOURCE_STATE_GET_HAS_IT(resource->num, c)){
98 c = c->parent;
99 }
100 TESTING_ASSERT((c->parent == NULL) == (c == aos_resource_entity_root_get()), "found a resource with no parent that isn't the root")
101 if(c->parent == NULL && !AOS_RESOURCE_STATE_GET_WANTS_IT(resource->num, c)){ // if c is the root entity and doesn't want it
102 TESTING_ASSERT(0, "root entity does not want resource %d (will fix it)", resource->num);
103 *((int *)NULL) = 0;
104 AOS_RESOURCE_STATE_SET_ON(AOS_RESOURCE_STATE_WANTS_IT, resource->num, c);
105 }
106 uint8_t locked_resource_mutex = 0;
107 if(AOS_RESOURCE_STATE_GET_HAS_PASSED_DOWN(resource->num, c)){
108 if(c->parent == NULL){
109 if(GetMin(resource->priorities) >= local->priority){
110 mutex_unlock(&resource->modify);
111 aos_resource_kill(resource->owner);
112 if(mutex_lock(&resource->kill)){ // got released by one that got killed (after unlocking resource_mutex)
113 mutex_unlock(&resource->resource_mutex);
114 mutex_unlock(&resource->request);
115 return 1;
116 }
117 if(mutex_lock(&resource->resource_mutex)){ // wait for the other process to release it
118 mutex_unlock(&resource->request);
119 return 1;
120 }
121 locked_resource_mutex = 1;
122 if(mutex_lock(&resource->modify)){
123 mutex_unlock(&resource->request);
124 return 1;
125 }
126 }else{
127 mutex_unlock(&resource->modify);
128 mutex_unlock(&resource->request);
129 return -1;
130 }
131 }else{
132 fprintf(stderr, "PROGRAMMER ERROR!!!!! 2 sub-actions both requested resource %d!!! stopping the root action\n", resource->num);
133 mutex_unlock(&resource->modify);
134 mutex_unlock(&resource->request);
135 return -1;
136 }
137 }
138 AOS_RESOURCE_STATE_SET_ON(AOS_RESOURCE_STATE_WANTS_IT, resource->num, local);
139 aos_resource_entity *c2 = local;
140 do{
141 c2 = c2->parent;
142 TESTING_ASSERT_RETURN(c2 != NULL, "couldn't find the parent that has resource %d", resource->num)
143 AOS_RESOURCE_STATE_SET_ON(AOS_RESOURCE_STATE_HAS_PASSED_DOWN, resource->num, c2);
144 } while(c2 != c);
145 TESTING_ASSERT(c2 == c, "c2 != c");
146 if(c->parent == NULL){ // if you found the root entity
147 resource->owner = local->root_action;
148 if(!locked_resource_mutex){
149 int rv = mutex_trylock(&resource->resource_mutex); // don't deadlock if somebody died or whatever
150 TESTING_ASSERT(rv == 0, "the resource_mutex was already locked when getting %d from the root", resource->num);
151 }
152 }else{
153 TESTING_ASSERT(resource->owner == local->root_action, "my action chain has resource %d, but my chain's root(%d) isn't the owner(%d)", resource->num, local->root_action, resource->owner);
154 TESTING_ASSERT(mutex_trylock(&resource->resource_mutex) != 0, "my action has the resource_mutex for %d, but the resource_mutex wasn't already locked", resource->num);
155 }
156 if(Insert(local->priority, resource->priorities) < 0){
157 fprintf(stderr, "BAD NEWS: ran out of space on the priority heap for resource %d. Increase the size of AOS_RESOURCE_PRIORITY_STACK_LENGTH in resource.h\n", resource->num);
158 mutex_unlock(&resource->modify);
159 mutex_unlock(&resource->request);
160 return -1;
161 }
162 mutex_unlock(&resource->modify);
163 mutex_unlock(&resource->kill);
164 mutex_unlock(&resource->request);
165 return 0;
166}
167
168int aos_resource_release(aos_resource_entity *local, aos_resource *resource){
169 TESTING_ASSERT_RETURN(local != NULL, "do not have a local entity");
170
171 if(mutex_lock(&resource->modify)){
172 return 1;
173 }
174
175 AOS_RESOURCE_STATE_SET_OFF(AOS_RESOURCE_STATE_WANTS_IT, resource->num, local);
176 if(!AOS_RESOURCE_STATE_GET_HAS_PASSED_DOWN(resource->num, local)){ // if we're actually supposed to go release it
177 aos_resource_entity *c = local;
178 while(c->parent != NULL && !AOS_RESOURCE_STATE_GET_WANTS_IT(resource->num, c)){
179 AOS_RESOURCE_STATE_SET_OFF(AOS_RESOURCE_STATE_HAS_PASSED_DOWN, resource->num, c);
180 c = c->parent;
181 }
182 if(c->parent == NULL && !AOS_RESOURCE_STATE_GET_WANTS_IT(resource->num, c)){ // if c is the root entity and doesn't want it
183 TESTING_ASSERT(0, "root entity does not want resource %d (will fix it)", resource->num);
184 AOS_RESOURCE_STATE_SET_ON(AOS_RESOURCE_STATE_WANTS_IT, resource->num, c);
185 }
186 AOS_RESOURCE_STATE_SET_OFF(AOS_RESOURCE_STATE_HAS_PASSED_DOWN, resource->num, c);
187 Remove(local->priority, resource->priorities);
188 TESTING_ASSERT(local->root_action == resource->owner, "freeing a resource (%d) whose owner(%d) isn't my chain's root(%d)", resource->num, resource->owner, local->root_action);
189 if(c->parent == NULL){ // if you gave it back to the root entity (c)
190 TESTING_ASSERT(IsEmpty(resource->priorities), "priority stack isn't empty (size=%d)", GetSize(resource->priorities));
191 resource->owner = 0;
192 mutex_unlock(&resource->resource_mutex);
193 mutex_unlock(&resource->kill);
194 }
195 } // else has passed it down
196 mutex_unlock(&resource->modify);
197 return 0;
198}
199