sPtr and mutex
@@ -17,24 +17,117 @@ | ||
17 | 17 | stdObject::refMutex; |
18 | 18 | uint8_t |
19 | 19 | stdObject::refMutex_init; |
20 | +uint8_t | |
21 | +stdObject::refMutex_debug; | |
20 | 22 | pthread_t |
21 | 23 | stdObject::refMutex_thread; |
24 | +int | |
25 | +stdObject::refMutex_count; | |
22 | 26 | |
23 | 27 | stdObject::stdObject() |
24 | 28 | { |
29 | + refLock(); | |
25 | 30 | this->ref = 0; |
26 | 31 | this->insert_zombie_list(); |
27 | - | |
32 | + refUnlock(); | |
28 | 33 | } |
29 | 34 | |
30 | 35 | |
31 | 36 | stdObject::~stdObject() |
32 | 37 | { |
38 | + refLock(); | |
33 | 39 | this->delete_zombie_list(); |
40 | + refUnlock(); | |
34 | 41 | } |
35 | 42 | |
36 | 43 | |
44 | +void | |
45 | +stdObject::refLock() { | |
46 | +int er; | |
47 | +pthread_t tid; | |
37 | 48 | |
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 | + | |
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 | + | |
38 | 131 | void |
39 | 132 | stdObject::addref() |
40 | 133 | { |
@@ -634,7 +634,7 @@ | ||
634 | 634 | p = total_run_time * 10 / |
635 | 635 | ((now - acc_start_time) ); |
636 | 636 | //::printf("MAX THREAD NOS-1 %lli %lli %lli\n",p,total_run_time,now - acc_start_time); |
637 | - p = (p + 12)/10; | |
637 | + p = (p + 32)/10; | |
638 | 638 | if ( p < max_thread_nos && max_thread_nos > 1 ) { |
639 | 639 | if ( now - last_dec_time >= |
640 | 640 | DEC_TIMER*1000*1000 ) { |
@@ -72,6 +72,8 @@ | ||
72 | 72 | ptr->addref(); |
73 | 73 | } |
74 | 74 | void _initial(__TYPE * inp) { |
75 | + pthread_t tid; | |
76 | + tid = pthread_self(); | |
75 | 77 | ptr = inp; |
76 | 78 | if ( ptr ) |
77 | 79 | ptr->addref(); |
@@ -28,34 +28,22 @@ | ||
28 | 28 | |
29 | 29 | static const char * trace_all; |
30 | 30 | static int load_mem_amount; |
31 | -/* | |
31 | + | |
32 | 32 | static void refLock(); |
33 | 33 | static void refUnlock(); |
34 | -*/ | |
35 | - static void refLock() { | |
36 | - if ( refMutex_init == 0 ) { | |
37 | - pthread_mutexattr_t attr; | |
38 | - pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE); | |
39 | - pthread_mutex_init(&refMutex,&attr); | |
40 | - refMutex_init = 1; | |
41 | - } | |
42 | - pthread_mutex_lock(&refMutex); | |
43 | - refMutex_thread = pthread_self(); | |
44 | - } | |
45 | - static void refUnlock() { | |
46 | - pthread_mutex_unlock(&refMutex); | |
47 | - } | |
48 | 34 | |
49 | 35 | |
36 | + static uint8_t refMutex_debug; | |
50 | 37 | protected: |
51 | 38 | virtual void refNotice() {} |
52 | 39 | virtual void refNotice1() {} |
53 | 40 | virtual void refCount(int ref,int inc_flag) {} |
54 | 41 | |
42 | +private: | |
55 | 43 | static pthread_mutex_t refMutex; |
56 | 44 | static uint8_t refMutex_init; |
57 | 45 | static pthread_t refMutex_thread; |
58 | -private: | |
46 | + static int refMutex_count; | |
59 | 47 | |
60 | 48 | void insert_zombie_list(); |
61 | 49 | void delete_zombie_list(); |