thread cleanup
@@ -5,13 +5,27 @@ | ||
5 | 5 | |
6 | 6 | #include <pthread.h> |
7 | 7 | #include "ts/c++/stdArray.h" |
8 | +#include "ts/c++/tinyState.h" | |
9 | +#include "ts/c++/tsThread.h" | |
8 | 10 | |
11 | +class stdOpenSSL; | |
12 | +class stdThreadOpenSSL : public stdThread { | |
13 | +public: | |
14 | + stdThreadOpenSSL(stdOpenSSL * parent); | |
15 | + ~stdThreadOpenSSL(); | |
16 | + | |
17 | + virtual void setup(); | |
18 | + virtual void cleanup(); | |
19 | +}; | |
20 | + | |
21 | +class tsApplication; | |
9 | 22 | class stdOpenSSL : public stdObject { |
10 | 23 | public: |
11 | - stdOpenSSL(); | |
24 | + stdOpenSSL(tsApplication * app); | |
12 | 25 | ~stdOpenSSL(); |
13 | - static stdOpenSSL * New(); | |
26 | + static stdOpenSSL * New(tsApplication * app); | |
14 | 27 | private: |
28 | + stdThreadOpenSSL * thread; | |
15 | 29 | static stdOpenSSL * current; |
16 | 30 | static void pthreads_locking_callback( |
17 | 31 | int mode, |
@@ -2,8 +2,10 @@ | ||
2 | 2 | #include <stdlib.h> |
3 | 3 | #include <string.h> |
4 | 4 | #include <errno.h> |
5 | - | |
5 | +#include <openssl/engine.h> | |
6 | +#include <openssl/ssl.h> | |
6 | 7 | #include <openssl/lhash.h> |
8 | +#include <openssl/err.h> | |
7 | 9 | |
8 | 10 | #include "std/stdOpenSSL.h" |
9 | 11 |
@@ -13,17 +15,22 @@ | ||
13 | 15 | stdOpenSSL::lock_cs; |
14 | 16 | |
15 | 17 | stdOpenSSL * |
16 | -stdOpenSSL::New() | |
18 | +stdOpenSSL::New(tsApplication * app) | |
17 | 19 | { |
18 | 20 | if ( current ) |
19 | 21 | return current; |
20 | - return NEW stdOpenSSL(); | |
22 | + return NEW stdOpenSSL(app); | |
21 | 23 | } |
22 | 24 | |
23 | -stdOpenSSL::stdOpenSSL() | |
25 | +stdOpenSSL::stdOpenSSL(tsApplication * app) | |
24 | 26 | { |
25 | 27 | int i; |
26 | 28 | current = this; |
29 | + app->set_global("openSSL",this); | |
30 | + | |
31 | + SSL_load_error_strings(); | |
32 | + SSL_library_init(); | |
33 | + | |
27 | 34 | REF_SET(locks,(NEW stdArray<pthread_mutex_t,false> |
28 | 35 | (CRYPTO_num_locks()))); |
29 | 36 | lock_cs = &locks->ary[0]; |
@@ -32,6 +39,10 @@ | ||
32 | 39 | } |
33 | 40 | CRYPTO_set_id_callback(pthreads_thread_id); |
34 | 41 | CRYPTO_set_locking_callback(pthreads_locking_callback); |
42 | + | |
43 | + REF_SET(thread,NEW stdThreadOpenSSL(this)); | |
44 | + tsThread::ins_setup(thread); | |
45 | + | |
35 | 46 | } |
36 | 47 | |
37 | 48 | stdOpenSSL::~stdOpenSSL() |
@@ -51,6 +62,13 @@ | ||
51 | 62 | REF_SET(locks,0); |
52 | 63 | |
53 | 64 | current = 0; |
65 | + | |
66 | + CRYPTO_cleanup_all_ex_data(); | |
67 | + | |
68 | + tsThread::del_setup(thread); | |
69 | + REF_SET(thread,0); | |
70 | + | |
71 | +::printf("stdOpenSSL finish\n"); | |
54 | 72 | } |
55 | 73 | |
56 | 74 |
@@ -89,3 +107,23 @@ | ||
89 | 107 | ret=(unsigned long)pthread_self(); |
90 | 108 | return(ret); |
91 | 109 | } |
110 | + | |
111 | + | |
112 | + | |
113 | +/*************************************************/ | |
114 | + | |
115 | +stdThreadOpenSSL::stdThreadOpenSSL(stdOpenSSL * parent) | |
116 | + : | |
117 | + stdThread(parent) | |
118 | +{ | |
119 | +} | |
120 | + | |
121 | +stdThreadOpenSSL::~stdThreadOpenSSL() | |
122 | +{ | |
123 | +} | |
124 | + | |
125 | +void | |
126 | +stdThreadOpenSSL::cleanup() | |
127 | +{ | |
128 | + ERR_remove_state(0); | |
129 | +} |
@@ -69,9 +69,7 @@ | ||
69 | 69 | { |
70 | 70 | if ( rsa->key ) |
71 | 71 | RSA_free(rsa->key); |
72 | -::printf("RSA-KEY-1 %p\n",rsa->key); | |
73 | 72 | rsa->key = PEM_read_RSAPrivateKey(fd,0,0,0); |
74 | -::printf("RSA-KEY-2 %p\n",rsa->key); | |
75 | 73 | if ( rsa->key == 0 ) { |
76 | 74 | error_char = ERR_error_string( |
77 | 75 | ERR_get_error(),0); |
@@ -1,22 +1,27 @@ | ||
1 | 1 | |
2 | - | |
3 | 2 | #include "ts/c++/stdEVP.h" |
4 | 3 | |
5 | 4 | |
6 | 5 | stdEVP::stdEVP( |
6 | + tsApplication * app, | |
7 | 7 | const EVP_CIPHER * pCipher, |
8 | 8 | char direction, |
9 | 9 | unsigned char * pKey, |
10 | 10 | unsigned char * pIV) |
11 | + : | |
12 | + stdSSL(app) | |
11 | 13 | { |
12 | 14 | start(pCipher,direction,pKey,pIV); |
13 | 15 | } |
14 | 16 | |
15 | 17 | stdEVP::stdEVP( |
18 | + tsApplication * app, | |
16 | 19 | const EVP_CIPHER * pCipher, |
17 | 20 | char direction, |
18 | 21 | stdString * pKey, |
19 | 22 | stdString * pIV) |
23 | + : | |
24 | + stdSSL(app) | |
20 | 25 | { |
21 | 26 | start(pCipher, |
22 | 27 | direction, |
@@ -15,7 +15,9 @@ | ||
15 | 15 | |
16 | 16 | |
17 | 17 | |
18 | -stdRSA::stdRSA() | |
18 | +stdRSA::stdRSA(tsApplication * app) | |
19 | + : | |
20 | + stdSSL(app) | |
19 | 21 | { |
20 | 22 | REF_SET(lock,NEW stdMutex()); |
21 | 23 | } |
@@ -1,20 +1,10 @@ | ||
1 | 1 | |
2 | -#include <openssl/engine.h> | |
3 | -#include <openssl/err.h> | |
4 | -#include <openssl/ssl.h> | |
5 | 2 | #include "ts/c++/stdSSL.h" |
6 | 3 | |
7 | -uint8_t | |
8 | -stdSSL::initialize_flag; | |
9 | 4 | |
10 | -stdSSL::stdSSL() | |
5 | +stdSSL::stdSSL(tsApplication * app) | |
11 | 6 | { |
12 | - if ( initialize_flag == 0 ) { | |
13 | - SSL_load_error_strings(); | |
14 | - SSL_library_init(); | |
15 | - initialize_flag = 1; | |
16 | - } | |
17 | - REF_SET(parent,stdOpenSSL::New()); | |
7 | + REF_SET(parent,stdOpenSSL::New(app)); | |
18 | 8 | } |
19 | 9 | |
20 | 10 | stdSSL::~stdSSL() |
@@ -22,16 +12,3 @@ | ||
22 | 12 | REF_SET(parent,0); |
23 | 13 | } |
24 | 14 | |
25 | -void | |
26 | -stdSSL::cleanup() | |
27 | -{ | |
28 | - if ( initialize_flag == 0 ) | |
29 | - return; | |
30 | - CRYPTO_cleanup_all_ex_data(); | |
31 | -} | |
32 | - | |
33 | -void | |
34 | -stdSSL::thread_cleanup() | |
35 | -{ | |
36 | - ERR_remove_state(0); | |
37 | -} |
@@ -2,7 +2,6 @@ | ||
2 | 2 | #include "_ts/c++/tsApplication_.h" |
3 | 3 | #include "ts/c++/stdString.h" |
4 | 4 | #include "ts/c++/tsThread.h" |
5 | -#include "ts/c++/stdSSL.h" | |
6 | 5 | |
7 | 6 | CLASS_TINYSTATE(tsApplication,tinyState); |
8 | 7 |
@@ -211,6 +210,7 @@ | ||
211 | 210 | return 0; |
212 | 211 | if ( C_TEST(gc->state,C_ZOM) == 0 ) |
213 | 212 | return 0; |
213 | + tsThread::stop(); | |
214 | 214 | REF_SET(this->fwClass,0); |
215 | 215 | REF_SET(this->global,0); |
216 | 216 | REF_SET(insensitive,0); |
@@ -218,7 +218,6 @@ | ||
218 | 218 | restart_flag = 0; |
219 | 219 | return rDO|INI_START; |
220 | 220 | } |
221 | - stdSSL::cleanup(); | |
222 | 221 | return rDO|FIN_TINYSTATE_START; |
223 | 222 | } |
224 | 223 |
@@ -62,7 +62,10 @@ | ||
62 | 62 | void kill_finish(); |
63 | 63 | void thr_set_kill_state(pthread_t * me=0); |
64 | 64 | |
65 | + static void ins_setup(stdThread * thr); | |
66 | + static void del_setup(stdThread * thr); | |
65 | 67 | static int get_current_tsThread_nos(); |
68 | + static void stop(); | |
66 | 69 | |
67 | 70 | static int max_thread(); |
68 | 71 | static void max_thread(int nos); |
@@ -83,6 +86,9 @@ | ||
83 | 86 | |
84 | 87 | private: |
85 | 88 | |
89 | + static void _do_setup(); | |
90 | + static void _do_cleanup(); | |
91 | + | |
86 | 92 | fwIO * io; |
87 | 93 | |
88 | 94 | static tsThread * thread_head; |
@@ -92,6 +98,7 @@ | ||
92 | 98 | static int current_thread_nos; |
93 | 99 | static int current_tsThread_nos; |
94 | 100 | static int run_thread_nos; |
101 | + static uint8_t thread_stop; | |
95 | 102 | static pthread_mutex_t mu; |
96 | 103 | static pthread_cond_t cond; |
97 | 104 | static int pipe_fd[2]; |
@@ -107,6 +114,8 @@ | ||
107 | 114 | static INTEGER64 total_run_time; |
108 | 115 | static INTEGER64 last_dec_time; |
109 | 116 | |
117 | + static stdQueue<stdThread> * setup_list; | |
118 | + | |
110 | 119 | static void lock(); |
111 | 120 | static void unlock(); |
112 | 121 | static void cond_signal(); |
@@ -195,6 +204,27 @@ | ||
195 | 204 | |
196 | 205 | |
197 | 206 | /******************************************* |
207 | + RELATED FUNCTIONS | |
208 | +********************************************/ | |
209 | + | |
210 | +stdThread::stdThread(stdObject *parent) | |
211 | +{ | |
212 | + REF_SET(this->parent,parent); | |
213 | +} | |
214 | +stdThread::~stdThread() | |
215 | +{ | |
216 | + REF_SET(parent,0); | |
217 | +} | |
218 | +void | |
219 | +stdThread::setup() | |
220 | +{ | |
221 | +} | |
222 | +void | |
223 | +stdThread::cleanup() | |
224 | +{ | |
225 | +} | |
226 | + | |
227 | +/******************************************* | |
198 | 228 | PUBLIC FUNCTIONS |
199 | 229 | ********************************************/ |
200 | 230 |
@@ -217,6 +247,8 @@ | ||
217 | 247 | tsThread_::cond; |
218 | 248 | int |
219 | 249 | tsThread_::pipe_fd[2]; |
250 | +uint8_t | |
251 | +tsThread_::thread_stop; | |
220 | 252 | |
221 | 253 | tsThread * |
222 | 254 | tsThread_::host_thread; |
@@ -236,7 +268,45 @@ | ||
236 | 268 | tsThread_::last_dec_time; |
237 | 269 | int |
238 | 270 | tsThread_::pipe_count; |
271 | +stdQueue<stdThread> * | |
272 | +tsThread_::setup_list; | |
239 | 273 | |
274 | +void | |
275 | +tsThread_::ins_setup(stdThread * thr) | |
276 | +{ | |
277 | + lock(); | |
278 | + if ( setup_list == 0 ) | |
279 | + REF_SET(setup_list,(NEW stdQueue<stdThread>())); | |
280 | + setup_list->ins(MAX_INTEGER64,thr); | |
281 | + unlock(); | |
282 | +} | |
283 | + | |
284 | +void | |
285 | +tsThread_::del_setup(stdThread * thr) | |
286 | +{ lock(); | |
287 | + if ( setup_list ) | |
288 | + setup_list->del(thr,0); | |
289 | + unlock(); | |
290 | +} | |
291 | + | |
292 | +void | |
293 | +tsThread_::_do_setup() | |
294 | +{ | |
295 | +stdQueueElement<stdThread> * elp; | |
296 | + if ( setup_list ) | |
297 | + for ( elp = setup_list->head ; elp ; elp = elp->next ) | |
298 | + elp->data->setup(); | |
299 | +} | |
300 | + | |
301 | +void | |
302 | +tsThread_::_do_cleanup() | |
303 | +{ | |
304 | +stdQueueElement<stdThread> * elp; | |
305 | + if ( setup_list ) | |
306 | + for ( elp = setup_list->head ; elp ; elp = elp->next ) | |
307 | + elp->data->cleanup(); | |
308 | +} | |
309 | + | |
240 | 310 | int |
241 | 311 | tsThread_::get_current_tsThread_nos() |
242 | 312 | { |
@@ -430,7 +500,19 @@ | ||
430 | 500 | cond_signal(); |
431 | 501 | } |
432 | 502 | |
503 | +void | |
504 | +tsThread_::stop() | |
505 | +{ | |
506 | + lock(); | |
507 | + thread_stop = 1; | |
508 | + for ( ; current_thread_nos ; ) { | |
509 | + cond_signal(); | |
510 | + cond_timed_wait(); | |
511 | + } | |
512 | + unlock(); | |
513 | +} | |
433 | 514 | |
515 | + | |
434 | 516 | void |
435 | 517 | tsThread_::acc_max_thread_nos(int inc) |
436 | 518 | { |
@@ -515,16 +597,21 @@ | ||
515 | 597 | |
516 | 598 | self_id = pthread_self(); |
517 | 599 | lock(); |
600 | + _do_setup(); | |
518 | 601 | for ( ; ; ) { |
519 | 602 | target = ready->del(); |
520 | 603 | if ( target == 0 ) { |
521 | 604 | if ( (max_thread_nos > 0 && |
522 | 605 | current_thread_nos > max_thread_nos) || |
606 | + thread_stop || | |
523 | 607 | cond_timed_wait() < 0 ) { |
524 | 608 | target = ready->del(); |
525 | 609 | if ( target ) |
526 | 610 | goto exec; |
611 | + _do_cleanup(); | |
527 | 612 | current_thread_nos --; |
613 | + if ( current_thread_nos == 0 ) | |
614 | + cond_signal(); | |
528 | 615 | unlock(); |
529 | 616 | return 0; |
530 | 617 | } |
@@ -543,8 +630,8 @@ | ||
543 | 630 | acc_max_thread_nos(-1); |
544 | 631 | _push_pipe(); |
545 | 632 | } |
633 | + _do_cleanup(); | |
546 | 634 | unlock(); |
547 | - stdSSL::thread_cleanup(); | |
548 | 635 | return 0; |
549 | 636 | } |
550 | 637 |
@@ -9,11 +9,15 @@ | ||
9 | 9 | |
10 | 10 | class stdEVP : public stdSSL { |
11 | 11 | public: |
12 | - stdEVP(const EVP_CIPHER * pCipher, | |
12 | + stdEVP( | |
13 | + tsApplication * app, | |
14 | + const EVP_CIPHER * pCipher, | |
13 | 15 | char direction, |
14 | 16 | unsigned char * pKey=0, |
15 | 17 | unsigned char * pIV=0); |
16 | - stdEVP(const EVP_CIPHER * pCipher, | |
18 | + stdEVP( | |
19 | + tsApplication * app, | |
20 | + const EVP_CIPHER * pCipher, | |
17 | 21 | char direction, |
18 | 22 | stdString * pKey, |
19 | 23 | stdString * pIV); |
@@ -23,9 +23,10 @@ | ||
23 | 23 | class tinyState; |
24 | 24 | class stdMutex; |
25 | 25 | class stdString; |
26 | +class tsApplication; | |
26 | 27 | class stdRSA : public stdSSL { |
27 | 28 | public: |
28 | - stdRSA(); | |
29 | + stdRSA(tsApplication * app); | |
29 | 30 | ~stdRSA(); |
30 | 31 | |
31 | 32 | int size(); |
@@ -8,12 +8,9 @@ | ||
8 | 8 | |
9 | 9 | class stdSSL : public stdObject { |
10 | 10 | public: |
11 | - stdSSL(); | |
11 | + stdSSL(tsApplication * app); | |
12 | 12 | ~stdSSL(); |
13 | - static void cleanup(); | |
14 | - static void thread_cleanup(); | |
15 | 13 | protected: |
16 | - static uint8_t initialize_flag; | |
17 | 14 | stdOpenSSL * parent; |
18 | 15 | }; |
19 | 16 |
@@ -7,6 +7,17 @@ | ||
7 | 7 | #include <signal.h> |
8 | 8 | typedef void (*THR_FUNC)(tinyState * THIS,void*msg); |
9 | 9 | |
10 | + | |
11 | +class stdThread : public stdObject { | |
12 | +public: | |
13 | + stdThread(stdObject * parent); | |
14 | + ~stdThread(); | |
15 | + virtual void setup(); | |
16 | + virtual void cleanup(); | |
17 | +private: | |
18 | + stdObject * parent; | |
19 | +}; | |
20 | + | |
10 | 21 | #include "ts/c++/tinyState.h" |
11 | 22 | #include "_ts/c++/tsThread_pb.h" |
12 | 23 |