misc.
@@ -42,11 +42,11 @@ | ||
42 | 42 | @(eq,${MACHINE},Linux) set MACHINE_DEF Linux |
43 | 43 | |
44 | 44 | |
45 | -@ set MACRO_INCLUDES "-I. -I${SRC}/arch/posix/h -I${SRC}/arch/posix/machine/${MACHINE_DIR}/h -I${SRC}/arch/posix/machine/${MACHINE_DIR}/${RELEASE}/h -I${SRC}/h -I/usr/local/ssl/include -I/usr/local/opt/openssl/include " | |
45 | +@ set MACRO_INCLUDES "-I. -I${SRC}/arch/posix/h -I${SRC}/arch/posix/machine/${MACHINE_DIR}/h -I${SRC}/arch/posix/machine/${MACHINE_DIR}/${RELEASE}/h -I${SRC}/h -I/usr/local/ssl/include -I/usr/local/opt/openssl/include -I/usr/local/opt/openssl@1.1/include " | |
46 | 46 | @ set INCLUDES "-I${src_dir} ${MACRO_INCLUDES} " |
47 | 47 | # -Wuninitialized |
48 | 48 | @ set CFLAGS "-g -c -Wreturn-type -D__EXTENSIONS__ -D${MACHINE_DEF} -D__ARCINFO__=\\"${ARCINFO}\\" " |
49 | -@ set LIB " ${LIBS} -L/usr/local/opt/openssl/lib " | |
49 | +@ set LIB " ${LIBS} -L/usr/local/opt/openssl/lib -L/usr/local/opt/openssl@1.1/lib " | |
50 | 50 | @ set LIBCPP " -lstdc++ ${LIBS} " |
51 | 51 | @ set CPPFLAGS " -frtti ${CFLAGS} " |
52 | 52 |
@@ -85,7 +85,7 @@ | ||
85 | 85 | #---------------------------------------------------- |
86 | 86 | |
87 | 87 | @ make suffix .t from suffix - .gtt |
88 | - ${GTT} ${from} --dep=${to}.gtt.ap --base=${SRC} -inc . -inc /usr/include -inc /usr/local/include | |
88 | + ${GTT} ${from} --dep=${to}.gtt.ap --base=${SRC} -inc . -inc /usr/include -inc /usr/local/include -inc /usr/local/opt/openssl@1.1/include | |
89 | 89 | touch ${to}.t |
90 | 90 | |
91 | 91 | @ make suffix .t from suffix - .c |
@@ -45,8 +45,19 @@ | ||
45 | 45 | |
46 | 46 | INTEGER64 |
47 | 47 | sObject::total_mem_max = 0; |
48 | +pthread_mutex_t | |
49 | +sObject::refMutex; | |
50 | +uint8_t | |
51 | +sObject::refMutex_init; | |
52 | +uint8_t | |
53 | +sObject::refMutex_debug; | |
54 | +pthread_t | |
55 | +sObject::refMutex_thread; | |
56 | +int | |
57 | +sObject::refMutex_count; | |
48 | 58 | |
49 | 59 | |
60 | + | |
50 | 61 | static int |
51 | 62 | get_cp_hash(const char * file,int line) |
52 | 63 | { |
@@ -160,6 +171,7 @@ | ||
160 | 171 | throw(std::bad_alloc) |
161 | 172 | #endif |
162 | 173 | { |
174 | + sObject::refLock(); | |
163 | 175 | MEM_HEADER * mem = (MEM_HEADER*)malloc(cbSize+sizeof(MEM_HEADER)); |
164 | 176 | mem->size = cbSize; |
165 | 177 | mem->cp = get_cp(__file,__line,ref_subline,cp_hash_mem); |
@@ -169,6 +181,8 @@ | ||
169 | 181 | mem ++; |
170 | 182 | memset(mem,0,cbSize); |
171 | 183 | //printf("new %p %lu %s %i\n",mem,cbSize,__file,__line); |
184 | + | |
185 | + sObject::refUnlock(); | |
172 | 186 | return (void*)mem; |
173 | 187 | } |
174 | 188 |
@@ -183,6 +197,8 @@ | ||
183 | 197 | |
184 | 198 | MEM_HEADER * mem; |
185 | 199 | size_t cbSize; |
200 | + | |
201 | + refLock(); | |
186 | 202 | mem = (MEM_HEADER*)pv; |
187 | 203 | mem --; |
188 | 204 | cp_set_size(mem->cp,-mem->size); |
@@ -191,6 +207,7 @@ | ||
191 | 207 | cbSize = mem->size; |
192 | 208 | memset(pv,0xaa,cbSize); |
193 | 209 | free(mem); |
210 | + refUnlock(); | |
194 | 211 | } |
195 | 212 | |
196 | 213 | void |
@@ -197,11 +214,14 @@ | ||
197 | 214 | sObject::refine_sobj(void * mp,const char * file,int line) |
198 | 215 | { |
199 | 216 | MEM_HEADER * mem; |
217 | + | |
218 | + refLock(); | |
200 | 219 | mem = (MEM_HEADER*)mp; |
201 | 220 | mem--; |
202 | 221 | cp_set_size(mem->cp,-mem->size); |
203 | 222 | mem->cp = get_cp(file,line,0,cp_hash_mem); |
204 | 223 | cp_set_size(mem->cp,mem->size); |
224 | + refUnlock(); | |
205 | 225 | } |
206 | 226 | |
207 | 227 | void |
@@ -215,9 +235,14 @@ | ||
215 | 235 | sObject::get_sobj_file() |
216 | 236 | { |
217 | 237 | MEM_HEADER * mem; |
238 | +const char * ret; | |
239 | + | |
240 | + refLock(); | |
218 | 241 | mem = (MEM_HEADER*)this; |
219 | 242 | mem--; |
220 | - return mem->cp->filename; | |
243 | + ret = mem->cp->filename; | |
244 | + refUnlock(); | |
245 | + return ret; | |
221 | 246 | } |
222 | 247 | |
223 | 248 | int |
@@ -224,9 +249,13 @@ | ||
224 | 249 | sObject::get_sobj_line() |
225 | 250 | { |
226 | 251 | MEM_HEADER * mem; |
252 | +int ret; | |
253 | + refLock(); | |
227 | 254 | mem = (MEM_HEADER*)this; |
228 | 255 | mem--; |
229 | - return mem->cp->line; | |
256 | + ret = mem->cp->line; | |
257 | + refUnlock(); | |
258 | + return ret; | |
230 | 259 | } |
231 | 260 | |
232 | 261 | void * |
@@ -237,6 +266,8 @@ | ||
237 | 266 | throw(std::bad_alloc) |
238 | 267 | #endif |
239 | 268 | { |
269 | + | |
270 | + sObject::refLock(); | |
240 | 271 | // ::printf("new[] %lu %s %i\n",cbSize,__file,__line); |
241 | 272 | MEM_HEADER * mem = (MEM_HEADER*)malloc(cbSize+sizeof(MEM_HEADER)); |
242 | 273 | mem->size = cbSize; |
@@ -246,6 +277,7 @@ | ||
246 | 277 | //insert_active_list(mem); |
247 | 278 | mem ++; |
248 | 279 | memset(mem,0,cbSize); |
280 | + sObject::refUnlock(); | |
249 | 281 | return mem; |
250 | 282 | } |
251 | 283 |
@@ -257,6 +289,7 @@ | ||
257 | 289 | throw() |
258 | 290 | #endif |
259 | 291 | { |
292 | + sObject::refLock(); | |
260 | 293 | //printf("DELETE [] %p\n",pv); |
261 | 294 | MEM_HEADER * mem = (MEM_HEADER*)pv; |
262 | 295 | mem --; |
@@ -265,6 +298,7 @@ | ||
265 | 298 | //delete_active_list(mem); |
266 | 299 | memset(pv,0xaa,mem->size); |
267 | 300 | free(mem); |
301 | + sObject::refUnlock(); | |
268 | 302 | } |
269 | 303 | |
270 | 304 | static CODE_POS * cp_hash_fd[CP_HASH_SIZE]; |
@@ -469,3 +503,89 @@ | ||
469 | 503 | descriptor_top = cp; |
470 | 504 | unlock_open(); |
471 | 505 | } |
506 | + | |
507 | + | |
508 | +void | |
509 | +sObject::refLock() { | |
510 | +int er; | |
511 | +pthread_t tid; | |
512 | + | |
513 | + if ( refMutex_init == 0 ) { | |
514 | + pthread_mutexattr_t attr; | |
515 | + pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE); | |
516 | + if ( (er = pthread_mutex_init(&refMutex,&attr)) ) { | |
517 | + refMutex_init = 2; | |
518 | + if ( (er = pthread_mutex_init(&refMutex,0)) ) { | |
519 | + fprintf(stderr,"%s\n",strerror(er)); | |
520 | + stdObject::panic("refLock-0"); | |
521 | + } | |
522 | + } | |
523 | + else | |
524 | + refMutex_init = 1; | |
525 | + } | |
526 | + if ( refMutex_init == 1 ) { | |
527 | + if ( (er=pthread_mutex_lock(&refMutex)) ) { | |
528 | + fprintf(stderr,"%s\n",strerror(er)); | |
529 | + stdObject::panic("refLock-0"); | |
530 | + } | |
531 | + } | |
532 | + else { | |
533 | + tid = pthread_self(); | |
534 | +if ( refMutex_debug ) | |
535 | +::printf("refLock-IN %p %p %i\n",tid,refMutex_thread,refMutex_count); | |
536 | + if ( refMutex_thread == tid ) { | |
537 | + refMutex_count ++; | |
538 | +if ( refMutex_debug ) | |
539 | +::printf("refLock-0 %p %i\n",refMutex_thread,refMutex_count); | |
540 | + return; | |
541 | + } | |
542 | + if ( (er=pthread_mutex_lock(&refMutex)) ) { | |
543 | + fprintf(stderr,"%s\n",strerror(er)); | |
544 | + stdObject::panic("refLock-0"); | |
545 | + } | |
546 | + if ( refMutex_count ) { | |
547 | + if ( refMutex_thread != tid ) | |
548 | + stdObject::panic("refLock-1"); | |
549 | + } | |
550 | + else { | |
551 | + if ( refMutex_thread != 0 ) | |
552 | + stdObject::panic("refLock-2"); | |
553 | + } | |
554 | + refMutex_thread = tid; | |
555 | + refMutex_count ++; | |
556 | +if ( refMutex_debug ) | |
557 | +::printf("refLock-1 %p %i\n",refMutex_thread,refMutex_count); | |
558 | + } | |
559 | +} | |
560 | + | |
561 | + | |
562 | +void | |
563 | +sObject::refUnlock() { | |
564 | +int er; | |
565 | + if ( refMutex_init == 1 ) { | |
566 | + if ( (er=pthread_mutex_unlock(&refMutex)) ) { | |
567 | + fprintf(stderr,"%s\n",strerror(er)); | |
568 | + stdObject::panic("refUnlock-3"); | |
569 | + } | |
570 | + } | |
571 | + else { | |
572 | +if ( refMutex_debug ) | |
573 | +::printf("refUnlock %p %i\n",refMutex_thread,refMutex_count); | |
574 | + if ( refMutex_count == 0 ) | |
575 | + stdObject::panic("refUnlock-1!!"); | |
576 | + pthread_t tid; | |
577 | + tid = pthread_self(); | |
578 | + if ( refMutex_thread != tid ) | |
579 | + stdObject::panic("refUnlock-2!!"); | |
580 | + refMutex_count --; | |
581 | + if ( refMutex_count == 0 ) { | |
582 | + refMutex_thread = 0; | |
583 | +if ( refMutex_debug ) | |
584 | +::printf("refUnlock-1 %p %i\n",refMutex_thread,refMutex_count); | |
585 | + if ( (er=pthread_mutex_unlock(&refMutex)) ) { | |
586 | + fprintf(stderr,"%s\n",strerror(er)); | |
587 | + stdObject::panic("refUnlock-3"); | |
588 | + } | |
589 | + } | |
590 | + } | |
591 | +} |
@@ -13,17 +13,6 @@ | ||
13 | 13 | int |
14 | 14 | stdObject::load_mem_amount; |
15 | 15 | |
16 | -pthread_mutex_t | |
17 | -stdObject::refMutex; | |
18 | -uint8_t | |
19 | -stdObject::refMutex_init; | |
20 | -uint8_t | |
21 | -stdObject::refMutex_debug; | |
22 | -pthread_t | |
23 | -stdObject::refMutex_thread; | |
24 | -int | |
25 | -stdObject::refMutex_count; | |
26 | - | |
27 | 16 | stdObject::stdObject() |
28 | 17 | { |
29 | 18 | refLock(); |
@@ -41,93 +30,8 @@ | ||
41 | 30 | } |
42 | 31 | |
43 | 32 | |
44 | -void | |
45 | -stdObject::refLock() { | |
46 | -int er; | |
47 | -pthread_t tid; | |
48 | 33 | |
49 | - if ( refMutex_init == 0 ) { | |
50 | - pthread_mutexattr_t attr; | |
51 | - pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE); | |
52 | - if ( (er = pthread_mutex_init(&refMutex,&attr)) ) { | |
53 | - refMutex_init = 2; | |
54 | - if ( (er = pthread_mutex_init(&refMutex,0)) ) { | |
55 | - fprintf(stderr,"%s\n",strerror(er)); | |
56 | - stdObject::panic("refLock-0"); | |
57 | - } | |
58 | - } | |
59 | - else | |
60 | - refMutex_init = 1; | |
61 | - } | |
62 | - if ( refMutex_init == 1 ) { | |
63 | - if ( (er=pthread_mutex_lock(&refMutex)) ) { | |
64 | - fprintf(stderr,"%s\n",strerror(er)); | |
65 | - stdObject::panic("refLock-0"); | |
66 | - } | |
67 | - } | |
68 | - else { | |
69 | - tid = pthread_self(); | |
70 | -if ( refMutex_debug ) | |
71 | -::printf("refLock-IN %p %p %i\n",tid,refMutex_thread,refMutex_count); | |
72 | - if ( refMutex_thread == tid ) { | |
73 | - refMutex_count ++; | |
74 | -if ( refMutex_debug ) | |
75 | -::printf("refLock-0 %p %i\n",refMutex_thread,refMutex_count); | |
76 | - return; | |
77 | - } | |
78 | - if ( (er=pthread_mutex_lock(&refMutex)) ) { | |
79 | - fprintf(stderr,"%s\n",strerror(er)); | |
80 | - stdObject::panic("refLock-0"); | |
81 | - } | |
82 | - if ( refMutex_count ) { | |
83 | - if ( refMutex_thread != tid ) | |
84 | - stdObject::panic("refLock-1"); | |
85 | - } | |
86 | - else { | |
87 | - if ( refMutex_thread != 0 ) | |
88 | - stdObject::panic("refLock-2"); | |
89 | - } | |
90 | - refMutex_thread = tid; | |
91 | - refMutex_count ++; | |
92 | -if ( refMutex_debug ) | |
93 | -::printf("refLock-1 %p %i\n",refMutex_thread,refMutex_count); | |
94 | - } | |
95 | -} | |
96 | 34 | |
97 | - | |
98 | -void | |
99 | -stdObject::refUnlock() { | |
100 | -int er; | |
101 | - if ( refMutex_init == 1 ) { | |
102 | - if ( (er=pthread_mutex_unlock(&refMutex)) ) { | |
103 | - fprintf(stderr,"%s\n",strerror(er)); | |
104 | - stdObject::panic("refUnlock-3"); | |
105 | - } | |
106 | - } | |
107 | - else { | |
108 | -if ( refMutex_debug ) | |
109 | -::printf("refUnlock %p %i\n",refMutex_thread,refMutex_count); | |
110 | - if ( refMutex_count == 0 ) | |
111 | - stdObject::panic("refUnlock-1!!"); | |
112 | - pthread_t tid; | |
113 | - tid = pthread_self(); | |
114 | - if ( refMutex_thread != tid ) | |
115 | - stdObject::panic("refUnlock-2!!"); | |
116 | - refMutex_count --; | |
117 | - if ( refMutex_count == 0 ) { | |
118 | - refMutex_thread = 0; | |
119 | -if ( refMutex_debug ) | |
120 | -::printf("refUnlock-1 %p %i\n",refMutex_thread,refMutex_count); | |
121 | - if ( (er=pthread_mutex_unlock(&refMutex)) ) { | |
122 | - fprintf(stderr,"%s\n",strerror(er)); | |
123 | - stdObject::panic("refUnlock-3"); | |
124 | - } | |
125 | - } | |
126 | - } | |
127 | -} | |
128 | - | |
129 | - | |
130 | - | |
131 | 35 | void |
132 | 36 | stdObject::addref() |
133 | 37 | { |
@@ -556,6 +556,8 @@ | ||
556 | 556 | return; |
557 | 557 | if ( max_thread_nos > 0 ) { |
558 | 558 | if ( current_thread_nos >= max_thread_nos ) { |
559 | +if ( run_thread_nos >= current_thread_nos ) | |
560 | +::printf("--------------------------------------------------- LAUNCH-FULL %i %i\n",run_thread_nos,current_thread_nos); | |
559 | 561 | cond_signal(); |
560 | 562 | return; |
561 | 563 | } |
@@ -27,6 +27,11 @@ | ||
27 | 27 | #else |
28 | 28 | throw(); |
29 | 29 | #endif |
30 | + static void refLock(); | |
31 | + static void refUnlock(); | |
32 | + static uint8_t refMutex_debug; | |
33 | + | |
34 | + | |
30 | 35 | static void panic(const char*); |
31 | 36 | |
32 | 37 | static INTEGER64 total_mem_max; |
@@ -58,6 +63,13 @@ | ||
58 | 63 | static void refine_fd(int s,const char * filename,int line); |
59 | 64 | |
60 | 65 | static void fd_stdio(); |
66 | + | |
67 | + protected: | |
68 | + static pthread_mutex_t refMutex; | |
69 | + static uint8_t refMutex_init; | |
70 | + static pthread_t refMutex_thread; | |
71 | + static int refMutex_count; | |
72 | + | |
61 | 73 | }; |
62 | 74 | |
63 | 75 | #define soOPEN(x,y,z) sObject::__open(__FILE__,__LINE__,x,y,z) |
@@ -83,7 +95,7 @@ | ||
83 | 95 | |
84 | 96 | #define COMMA , |
85 | 97 | #define NEW new (__FILE__,__LINE__) |
86 | -#define thNEW(__type,__arg) (stdObject::refLock(),sPtr<__type>(NEW __type __arg,1)) | |
98 | +#define thNEW(__type,__arg) (sObject::refLock(),sPtr<__type>(NEW __type __arg,1)) | |
87 | 99 | |
88 | 100 | |
89 | 101 |
@@ -4,6 +4,8 @@ | ||
4 | 4 | #ifndef ___stdArrayQueue_cpp_H___ |
5 | 5 | #define ___stdArrayQueue_cpp_H___ |
6 | 6 | |
7 | +#define AQ_DEBUG_MODE | |
8 | + | |
7 | 9 | #include <pthread.h> |
8 | 10 | |
9 | 11 | #define AQE_OK 0 |
@@ -19,6 +21,9 @@ | ||
19 | 21 | #define sAQ_DETACH 1 |
20 | 22 | #define sAQ_ATTACH 2 |
21 | 23 | |
24 | + | |
25 | +#define AQ_LINE(x) (x).line(__FILE__,__LINE__); | |
26 | + | |
22 | 27 | template <class __TYPE> |
23 | 28 | class sAQindex; |
24 | 29 |
@@ -35,13 +40,16 @@ | ||
35 | 40 | initial(inp->length()); |
36 | 41 | |
37 | 42 | int i; |
43 | + unsigned int ptr; | |
38 | 44 | lock(); |
39 | 45 | tail = inp->tail; |
40 | 46 | head = inp->head; |
41 | 47 | for ( i = 0 ; i < a->length() ; i ++ ) { |
42 | 48 | a->ary[i] = inp->a->ary[i]; |
43 | - ix->ary[i] = inp->ix->ary[i]; | |
49 | + ix->ary[i] = AQ_EMPTY; | |
44 | 50 | } |
51 | + for ( ptr = tail ; ptr != head ; ptr ++ ) | |
52 | + ix->ary[ptr%ix->length()] = 0; | |
45 | 53 | unlock(); |
46 | 54 | } |
47 | 55 | ~stdArrayQueue() { |
@@ -66,7 +74,28 @@ | ||
66 | 74 | int length() { |
67 | 75 | return a->length(); |
68 | 76 | } |
77 | + int truncate(sAQindex<__TYPE> from,sAQindex<__TYPE> to) { | |
78 | + sAQindex<__TYPE> ptr; | |
79 | + AQ_LINE(ptr); | |
80 | + if ( from > to ) | |
81 | + return -1; | |
82 | + ptr.set(this); | |
83 | + for ( ; ; ) { | |
84 | + ptr.tail(); | |
85 | + if ( ptr == from ) | |
86 | + break; | |
87 | + ptr.del(); | |
88 | + } | |
89 | + for ( ; ; ) { | |
90 | + ptr.head(); | |
91 | + if ( ptr == to ) | |
92 | + break; | |
93 | + ptr.pop(); | |
94 | + } | |
95 | + return 0; | |
96 | + } | |
69 | 97 | |
98 | + | |
70 | 99 | protected: |
71 | 100 | int i_count() { |
72 | 101 | return head - tail; |
@@ -78,6 +107,10 @@ | ||
78 | 107 | len = regular_length(len); |
79 | 108 | n = thNEW(stdArray<__TYPE COMMA false>, (len)); |
80 | 109 | nix = thNEW(stdArray<int COMMA false>, (len)); |
110 | +#ifdef AQ_DEBUG_MODE | |
111 | + sPtr<stdArray<sAQindex<__TYPE>*,false > > dl; | |
112 | + dl = thNEW(stdArray<sAQindex<__TYPE> * COMMA false>,(len)); | |
113 | +#endif | |
81 | 114 | if ( count() < len ) |
82 | 115 | ncount = count(); |
83 | 116 | else { |
@@ -88,10 +121,17 @@ | ||
88 | 121 | for ( i = tail ; i != head ; ) { |
89 | 122 | n->ary[i%n->length()] = a->ary[i%a->length()]; |
90 | 123 | nix->ary[i%n->length()] = ix->ary[i%a->length()]; |
124 | +#ifdef AQ_DEBUG_MODE | |
125 | + dl->ary[i%n->length()] | |
126 | + = debug_list->ary[i%a->length()]; | |
127 | +#endif | |
91 | 128 | i ++; |
92 | 129 | } |
93 | 130 | a = n; |
94 | 131 | ix = nix; |
132 | +#ifdef AQ_DEBUG_MODE | |
133 | + debug_list = dl; | |
134 | +#endif | |
95 | 135 | } |
96 | 136 | int regular_length(int len) { |
97 | 137 | int i; |
@@ -111,69 +151,96 @@ | ||
111 | 151 | |
112 | 152 | pthread_mutex_init(&mu,0); |
113 | 153 | pthread_cond_init(&cond,0); |
154 | + | |
155 | +#ifdef AQ_DEBUG_MODE | |
156 | + debug_list = thNEW(stdArray<sAQindex<__TYPE>* COMMA false>,(len)); | |
157 | +#endif | |
114 | 158 | } |
115 | 159 | __TYPE * get(unsigned int pos) { |
116 | 160 | return &a->ary[pos%a->length()]; |
117 | 161 | } |
118 | 162 | |
119 | - | |
120 | 163 | unsigned int ins() { |
121 | 164 | int ret; |
122 | - if ( count() == a->length() ) | |
165 | + if ( i_count() == a->length() ) | |
123 | 166 | del(); |
124 | 167 | ret = head ++; |
125 | - ix->ary[ret] = AQ_INS; | |
168 | + ix->ary[ret%ix->length()] = AQ_INS; | |
126 | 169 | return ret; |
127 | 170 | } |
128 | 171 | int del() { |
129 | - if ( count() == 0 ) | |
172 | + if ( i_count() == 0 ) | |
130 | 173 | return AQE_ERROR; |
131 | - for ( ; ix->ary[tail] ; ) { | |
174 | + for ( ; ix->ary[tail%a->length()] ; ) { | |
132 | 175 | wait_count ++; |
133 | 176 | pthread_cond_wait(&cond,&mu); |
134 | - lock(); | |
135 | 177 | } |
136 | 178 | tail ++; |
137 | 179 | return 0; |
138 | 180 | } |
139 | - int attach(unsigned int p) { | |
181 | + int pop() { | |
182 | + int ret; | |
183 | + if ( i_count() == 0 ) | |
184 | + return AQE_ERROR; | |
185 | + ret = head - 1; | |
186 | + for ( ; ix->ary[ret%a->length()] ; ) { | |
187 | + wait_count ++; | |
188 | + pthread_cond_wait(&cond,&mu); | |
189 | + } | |
190 | + head = ret; | |
191 | + return 0; | |
192 | + } | |
193 | + int idx(unsigned int p) { | |
194 | + return ix->ary[p%ix->length()]; | |
195 | + } | |
196 | + int attach(unsigned int p,sAQindex<__TYPE> * org) { | |
140 | 197 | for ( ; ; ) { |
141 | 198 | if ( head < tail ) { |
142 | 199 | if ( head <= p && p < tail ) |
143 | 200 | return AQE_ERROR; |
144 | 201 | } |
145 | - else if ( !(head <= p && p < tail) ) | |
202 | + else if ( head <= p || p < tail ) | |
146 | 203 | return AQE_ERROR; |
147 | - if ( ix->ary[p] >= 0 ) | |
204 | + if ( ix->ary[p%ix->length()] >= 0 ) | |
148 | 205 | break; |
149 | 206 | wait_count ++; |
150 | 207 | pthread_cond_wait(&cond,&mu); |
151 | - lock(); | |
152 | 208 | } |
153 | - ix->ary[p] ++; | |
209 | + ix->ary[p%ix->length()] ++; | |
210 | +#ifdef AQ_DEBUG_MODE | |
211 | + org->debug_next = debug_list->ary[p%ix->length()]; | |
212 | + debug_list->ary[p%ix->length()] = org; | |
213 | +#endif | |
154 | 214 | return 0; |
155 | 215 | } |
156 | - int detach(unsigned int p) { | |
157 | - if ( p < 0 ) | |
158 | - return 0; | |
216 | + int detach(unsigned int p,sAQindex<__TYPE> * org) { | |
159 | 217 | if ( head < tail ) { |
160 | 218 | if ( head <= p && p < tail ) |
161 | 219 | return AQE_ERROR; |
162 | 220 | } |
163 | - else if ( !(head <= p && p < tail) ) | |
221 | + else if ( head <= p || p < tail ) | |
164 | 222 | return AQE_ERROR; |
165 | - if ( ix->ary[p] == 0 ) | |
223 | + if ( ix->ary[p%ix->length()] == 0 ) | |
166 | 224 | return AQE_ERROR; |
167 | - if ( ix->ary[p] == AQ_INS ) { | |
168 | - ix->ary[p] = 0; | |
225 | + if ( ix->ary[p%ix->length()] == AQ_INS ) { | |
226 | + ix->ary[p%ix->length()] = 0; | |
169 | 227 | } |
170 | 228 | else { |
171 | - ix->ary[p] --; | |
229 | + ix->ary[p%ix->length()] --; | |
172 | 230 | } |
173 | - if ( ix->ary[p] == 0 ) { | |
231 | + if ( ix->ary[p%ix->length()] == 0 ) { | |
174 | 232 | for ( ; wait_count > 0 ; wait_count -- ) |
175 | 233 | pthread_cond_signal(&cond); |
176 | 234 | } |
235 | +#ifdef AQ_DEBUG_MODE | |
236 | + sAQindex<__TYPE> ** pp; | |
237 | + for ( pp = &debug_list->ary[p%a->length()] ; | |
238 | + *pp && *pp != org ; | |
239 | + pp = &(*pp)->debug_next); | |
240 | + if ( *pp ) | |
241 | + *pp = org->debug_next; | |
242 | + | |
243 | +#endif | |
177 | 244 | return 0; |
178 | 245 | } |
179 | 246 |
@@ -186,6 +253,10 @@ | ||
186 | 253 | |
187 | 254 | sPtr<stdArray<__TYPE,false> > a; |
188 | 255 | sPtr<stdArray<int,false> > ix; |
256 | +#ifdef AQ_DEBUG_MODE | |
257 | + sPtr<stdArray<sAQindex<__TYPE>*, false> > debug_list; | |
258 | +#endif | |
259 | + | |
189 | 260 | unsigned int head; |
190 | 261 | unsigned int tail; |
191 | 262 |
@@ -198,6 +269,8 @@ | ||
198 | 269 | template <class __TYPE> |
199 | 270 | class sAQindex : public sObject { |
200 | 271 | public: |
272 | + friend class stdArrayQueue<__TYPE>; | |
273 | + | |
201 | 274 | sAQindex() { |
202 | 275 | pos = 0; |
203 | 276 | ptr = 0; |
@@ -211,6 +284,10 @@ | ||
211 | 284 | a = inp.a; |
212 | 285 | pos = 0; |
213 | 286 | ptr = 0; |
287 | + if ( a.is_clear() ) { | |
288 | + pos = inp.pos; | |
289 | + return; | |
290 | + } | |
214 | 291 | a->lock(); |
215 | 292 | pos = inp.pos; |
216 | 293 | attach(); |
@@ -217,6 +294,8 @@ | ||
217 | 294 | a->unlock(); |
218 | 295 | } |
219 | 296 | ~sAQindex() { |
297 | + if ( a.is_clear() ) | |
298 | + return; | |
220 | 299 | a->lock(); |
221 | 300 | detach(); |
222 | 301 | a->unlock(); |
@@ -229,24 +308,47 @@ | ||
229 | 308 | return sAQ_ATTACH; |
230 | 309 | } |
231 | 310 | void set(sPtr<stdArrayQueue<__TYPE> > aa) { |
311 | + if ( a ) { | |
312 | + a->lock(); | |
313 | + detach(); | |
314 | + a->unlock(); | |
315 | + } | |
232 | 316 | a = aa; |
317 | + if ( a ) { | |
318 | + a->lock(); | |
319 | + attach(); | |
320 | + a->unlock(); | |
321 | + } | |
233 | 322 | } |
234 | 323 | |
324 | + void line(const char* __file,int __line) { | |
325 | +#ifdef AQ_DEBUG_MODE | |
326 | + _line = __line; | |
327 | + _file = __file; | |
328 | +#endif | |
329 | + } | |
330 | + | |
235 | 331 | __TYPE * get_ptr() { |
236 | 332 | return ptr; |
237 | 333 | } |
238 | 334 | void clear() { |
335 | + if ( a.is_clear() ) | |
336 | + return; | |
239 | 337 | a->lock(); |
240 | 338 | detach(); |
241 | 339 | a->unlock(); |
242 | 340 | } |
243 | 341 | int is_tail() { |
244 | - if ( pos == a->tail ) | |
342 | + if ( a && (pos == a->tail) ) | |
245 | 343 | return 1; |
246 | 344 | return 0; |
247 | 345 | } |
248 | 346 | int is_head() { |
249 | - if ( pos == a->head ) | |
347 | + if ( a.is_clear() ) | |
348 | + return 0; | |
349 | + if ( a->tail == a->head ) | |
350 | + return 0; | |
351 | + if ( pos == a->head-1 ) | |
250 | 352 | return 1; |
251 | 353 | return 0; |
252 | 354 | } |
@@ -253,7 +355,6 @@ | ||
253 | 355 | int number() { |
254 | 356 | return pos - a->tail; |
255 | 357 | } |
256 | - | |
257 | 358 | int tail() { |
258 | 359 | int ret; |
259 | 360 | a->lock(); |
@@ -275,7 +376,7 @@ | ||
275 | 376 | return ret; |
276 | 377 | } |
277 | 378 | int i_head() { |
278 | - if ( a->count() == 0 ) | |
379 | + if ( a->i_count() == 0 ) | |
279 | 380 | return AQE_ERROR; |
280 | 381 | detach(); |
281 | 382 | pos = a->head-1; |
@@ -295,7 +396,8 @@ | ||
295 | 396 | if ( ret < 0 ) |
296 | 397 | return AQE_ERROR; |
297 | 398 | detach(); |
298 | - pos = a->head; | |
399 | + pos = ret; | |
400 | + ptr = a->get(pos); | |
299 | 401 | return 0; |
300 | 402 | } |
301 | 403 | int del() { |
@@ -306,13 +408,30 @@ | ||
306 | 408 | return ret; |
307 | 409 | } |
308 | 410 | int i_del() { |
309 | - int ret; | |
310 | 411 | if ( pos == a->tail ) |
311 | 412 | detach(); |
312 | 413 | return a->del(); |
313 | 414 | } |
415 | + int pop() { | |
416 | + int ret; | |
417 | + a->lock(); | |
418 | + ret = i_pop(); | |
419 | + a->unlock(); | |
420 | + return ret; | |
421 | + } | |
422 | + int i_pop() { | |
423 | + if ( pos == (int)(a->head - 1) ) | |
424 | + detach(); | |
425 | + return a->pop(); | |
426 | + } | |
314 | 427 | sAQindex& operator=(sAQindex inp) { |
315 | - a = inp.a; | |
428 | + if ( a ) { | |
429 | + a->lock(); | |
430 | + detach(); | |
431 | + a->unlock(); | |
432 | + } | |
433 | + if ( inp.a ) | |
434 | + a = inp.a; | |
316 | 435 | a->lock(); |
317 | 436 | pos = inp.pos; |
318 | 437 | attach(); |
@@ -405,6 +524,13 @@ | ||
405 | 524 | return *ptr; |
406 | 525 | } |
407 | 526 | protected: |
527 | + | |
528 | +#ifdef AQ_DEBUG_MODE | |
529 | + sAQindex<__TYPE> * debug_next; | |
530 | + const char * _file; | |
531 | + int _line; | |
532 | +#endif | |
533 | + | |
408 | 534 | sPtr<stdArrayQueue<__TYPE> > a; |
409 | 535 | int pos; |
410 | 536 | __TYPE * ptr; |
@@ -413,7 +539,7 @@ | ||
413 | 539 | int ret; |
414 | 540 | if ( ptr ) |
415 | 541 | return 0; |
416 | - ret = a->attach(pos); | |
542 | + ret = a->attach(pos,this); | |
417 | 543 | if ( ret == 0 ) |
418 | 544 | ptr = a->get(pos); |
419 | 545 | else ptr = 0; |
@@ -422,7 +548,7 @@ | ||
422 | 548 | int detach() { |
423 | 549 | if ( ptr == 0 ) |
424 | 550 | return 0; |
425 | - a->detach(pos); | |
551 | + a->detach(pos,this); | |
426 | 552 | ptr = 0; |
427 | 553 | return 0; |
428 | 554 | } |
@@ -29,11 +29,7 @@ | ||
29 | 29 | static const char * trace_all; |
30 | 30 | static int load_mem_amount; |
31 | 31 | |
32 | - static void refLock(); | |
33 | - static void refUnlock(); | |
34 | 32 | |
35 | - | |
36 | - static uint8_t refMutex_debug; | |
37 | 33 | protected: |
38 | 34 | virtual void refNotice() {} |
39 | 35 | virtual void refNotice1() {} |
@@ -40,11 +36,6 @@ | ||
40 | 36 | virtual void refCount(int ref,int inc_flag) {} |
41 | 37 | |
42 | 38 | private: |
43 | - static pthread_mutex_t refMutex; | |
44 | - static uint8_t refMutex_init; | |
45 | - static pthread_t refMutex_thread; | |
46 | - static int refMutex_count; | |
47 | - | |
48 | 39 | void insert_zombie_list(); |
49 | 40 | void delete_zombie_list(); |
50 | 41 |