• R/O
  • SSH
  • HTTPS

globalbase: 提交


Commit MetaInfo

修订版5880 (tree)
时间2017-09-13 12:31:59
作者joshua

Log Message

thread cleanup

更改概述

差异

--- modules/tinyState/trunk/src/arch/posix/h/std/stdOpenSSL.h (revision 5879)
+++ modules/tinyState/trunk/src/arch/posix/h/std/stdOpenSSL.h (revision 5880)
@@ -5,13 +5,27 @@
55
66 #include <pthread.h>
77 #include "ts/c++/stdArray.h"
8+#include "ts/c++/tinyState.h"
9+#include "ts/c++/tsThread.h"
810
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;
922 class stdOpenSSL : public stdObject {
1023 public:
11- stdOpenSSL();
24+ stdOpenSSL(tsApplication * app);
1225 ~stdOpenSSL();
13- static stdOpenSSL * New();
26+ static stdOpenSSL * New(tsApplication * app);
1427 private:
28+ stdThreadOpenSSL * thread;
1529 static stdOpenSSL * current;
1630 static void pthreads_locking_callback(
1731 int mode,
--- modules/tinyState/trunk/src/arch/posix/std/stdOpenSSL.cpp (revision 5879)
+++ modules/tinyState/trunk/src/arch/posix/std/stdOpenSSL.cpp (revision 5880)
@@ -2,8 +2,10 @@
22 #include <stdlib.h>
33 #include <string.h>
44 #include <errno.h>
5-
5+#include <openssl/engine.h>
6+#include <openssl/ssl.h>
67 #include <openssl/lhash.h>
8+#include <openssl/err.h>
79
810 #include "std/stdOpenSSL.h"
911
@@ -13,17 +15,22 @@
1315 stdOpenSSL::lock_cs;
1416
1517 stdOpenSSL *
16-stdOpenSSL::New()
18+stdOpenSSL::New(tsApplication * app)
1719 {
1820 if ( current )
1921 return current;
20- return NEW stdOpenSSL();
22+ return NEW stdOpenSSL(app);
2123 }
2224
23-stdOpenSSL::stdOpenSSL()
25+stdOpenSSL::stdOpenSSL(tsApplication * app)
2426 {
2527 int i;
2628 current = this;
29+ app->set_global("openSSL",this);
30+
31+ SSL_load_error_strings();
32+ SSL_library_init();
33+
2734 REF_SET(locks,(NEW stdArray<pthread_mutex_t,false>
2835 (CRYPTO_num_locks())));
2936 lock_cs = &locks->ary[0];
@@ -32,6 +39,10 @@
3239 }
3340 CRYPTO_set_id_callback(pthreads_thread_id);
3441 CRYPTO_set_locking_callback(pthreads_locking_callback);
42+
43+ REF_SET(thread,NEW stdThreadOpenSSL(this));
44+ tsThread::ins_setup(thread);
45+
3546 }
3647
3748 stdOpenSSL::~stdOpenSSL()
@@ -51,6 +62,13 @@
5162 REF_SET(locks,0);
5263
5364 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");
5472 }
5573
5674
@@ -89,3 +107,23 @@
89107 ret=(unsigned long)pthread_self();
90108 return(ret);
91109 }
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+}
--- modules/tinyState/trunk/src/classes/ts/c++/co_tsRSAread_PEM_privateKey.cpp (revision 5879)
+++ modules/tinyState/trunk/src/classes/ts/c++/co_tsRSAread_PEM_privateKey.cpp (revision 5880)
@@ -69,9 +69,7 @@
6969 {
7070 if ( rsa->key )
7171 RSA_free(rsa->key);
72-::printf("RSA-KEY-1 %p\n",rsa->key);
7372 rsa->key = PEM_read_RSAPrivateKey(fd,0,0,0);
74-::printf("RSA-KEY-2 %p\n",rsa->key);
7573 if ( rsa->key == 0 ) {
7674 error_char = ERR_error_string(
7775 ERR_get_error(),0);
--- modules/tinyState/trunk/src/classes/ts/c++/stdEVP.cpp (revision 5879)
+++ modules/tinyState/trunk/src/classes/ts/c++/stdEVP.cpp (revision 5880)
@@ -1,22 +1,27 @@
11
2-
32 #include "ts/c++/stdEVP.h"
43
54
65 stdEVP::stdEVP(
6+ tsApplication * app,
77 const EVP_CIPHER * pCipher,
88 char direction,
99 unsigned char * pKey,
1010 unsigned char * pIV)
11+ :
12+ stdSSL(app)
1113 {
1214 start(pCipher,direction,pKey,pIV);
1315 }
1416
1517 stdEVP::stdEVP(
18+ tsApplication * app,
1619 const EVP_CIPHER * pCipher,
1720 char direction,
1821 stdString * pKey,
1922 stdString * pIV)
23+ :
24+ stdSSL(app)
2025 {
2126 start(pCipher,
2227 direction,
--- modules/tinyState/trunk/src/classes/ts/c++/stdRSA.cpp (revision 5879)
+++ modules/tinyState/trunk/src/classes/ts/c++/stdRSA.cpp (revision 5880)
@@ -15,7 +15,9 @@
1515
1616
1717
18-stdRSA::stdRSA()
18+stdRSA::stdRSA(tsApplication * app)
19+ :
20+ stdSSL(app)
1921 {
2022 REF_SET(lock,NEW stdMutex());
2123 }
--- modules/tinyState/trunk/src/classes/ts/c++/stdSSL.cpp (revision 5879)
+++ modules/tinyState/trunk/src/classes/ts/c++/stdSSL.cpp (revision 5880)
@@ -1,20 +1,10 @@
11
2-#include <openssl/engine.h>
3-#include <openssl/err.h>
4-#include <openssl/ssl.h>
52 #include "ts/c++/stdSSL.h"
63
7-uint8_t
8-stdSSL::initialize_flag;
94
10-stdSSL::stdSSL()
5+stdSSL::stdSSL(tsApplication * app)
116 {
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));
188 }
199
2010 stdSSL::~stdSSL()
@@ -22,16 +12,3 @@
2212 REF_SET(parent,0);
2313 }
2414
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-}
--- modules/tinyState/trunk/src/classes/ts/c++/tsApplication.cpp (revision 5879)
+++ modules/tinyState/trunk/src/classes/ts/c++/tsApplication.cpp (revision 5880)
@@ -2,7 +2,6 @@
22 #include "_ts/c++/tsApplication_.h"
33 #include "ts/c++/stdString.h"
44 #include "ts/c++/tsThread.h"
5-#include "ts/c++/stdSSL.h"
65
76 CLASS_TINYSTATE(tsApplication,tinyState);
87
@@ -211,6 +210,7 @@
211210 return 0;
212211 if ( C_TEST(gc->state,C_ZOM) == 0 )
213212 return 0;
213+ tsThread::stop();
214214 REF_SET(this->fwClass,0);
215215 REF_SET(this->global,0);
216216 REF_SET(insensitive,0);
@@ -218,7 +218,6 @@
218218 restart_flag = 0;
219219 return rDO|INI_START;
220220 }
221- stdSSL::cleanup();
222221 return rDO|FIN_TINYSTATE_START;
223222 }
224223
--- modules/tinyState/trunk/src/classes/ts/c++/tsThread.cpp (revision 5879)
+++ modules/tinyState/trunk/src/classes/ts/c++/tsThread.cpp (revision 5880)
@@ -62,7 +62,10 @@
6262 void kill_finish();
6363 void thr_set_kill_state(pthread_t * me=0);
6464
65+ static void ins_setup(stdThread * thr);
66+ static void del_setup(stdThread * thr);
6567 static int get_current_tsThread_nos();
68+ static void stop();
6669
6770 static int max_thread();
6871 static void max_thread(int nos);
@@ -83,6 +86,9 @@
8386
8487 private:
8588
89+ static void _do_setup();
90+ static void _do_cleanup();
91+
8692 fwIO * io;
8793
8894 static tsThread * thread_head;
@@ -92,6 +98,7 @@
9298 static int current_thread_nos;
9399 static int current_tsThread_nos;
94100 static int run_thread_nos;
101+ static uint8_t thread_stop;
95102 static pthread_mutex_t mu;
96103 static pthread_cond_t cond;
97104 static int pipe_fd[2];
@@ -107,6 +114,8 @@
107114 static INTEGER64 total_run_time;
108115 static INTEGER64 last_dec_time;
109116
117+ static stdQueue<stdThread> * setup_list;
118+
110119 static void lock();
111120 static void unlock();
112121 static void cond_signal();
@@ -195,6 +204,27 @@
195204
196205
197206 /*******************************************
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+/*******************************************
198228 PUBLIC FUNCTIONS
199229 ********************************************/
200230
@@ -217,6 +247,8 @@
217247 tsThread_::cond;
218248 int
219249 tsThread_::pipe_fd[2];
250+uint8_t
251+tsThread_::thread_stop;
220252
221253 tsThread *
222254 tsThread_::host_thread;
@@ -236,7 +268,45 @@
236268 tsThread_::last_dec_time;
237269 int
238270 tsThread_::pipe_count;
271+stdQueue<stdThread> *
272+tsThread_::setup_list;
239273
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+
240310 int
241311 tsThread_::get_current_tsThread_nos()
242312 {
@@ -430,7 +500,19 @@
430500 cond_signal();
431501 }
432502
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+}
433514
515+
434516 void
435517 tsThread_::acc_max_thread_nos(int inc)
436518 {
@@ -515,16 +597,21 @@
515597
516598 self_id = pthread_self();
517599 lock();
600+ _do_setup();
518601 for ( ; ; ) {
519602 target = ready->del();
520603 if ( target == 0 ) {
521604 if ( (max_thread_nos > 0 &&
522605 current_thread_nos > max_thread_nos) ||
606+ thread_stop ||
523607 cond_timed_wait() < 0 ) {
524608 target = ready->del();
525609 if ( target )
526610 goto exec;
611+ _do_cleanup();
527612 current_thread_nos --;
613+ if ( current_thread_nos == 0 )
614+ cond_signal();
528615 unlock();
529616 return 0;
530617 }
@@ -543,8 +630,8 @@
543630 acc_max_thread_nos(-1);
544631 _push_pipe();
545632 }
633+ _do_cleanup();
546634 unlock();
547- stdSSL::thread_cleanup();
548635 return 0;
549636 }
550637
--- modules/tinyState/trunk/src/h/ts/c++/stdEVP.h (revision 5879)
+++ modules/tinyState/trunk/src/h/ts/c++/stdEVP.h (revision 5880)
@@ -9,11 +9,15 @@
99
1010 class stdEVP : public stdSSL {
1111 public:
12- stdEVP(const EVP_CIPHER * pCipher,
12+ stdEVP(
13+ tsApplication * app,
14+ const EVP_CIPHER * pCipher,
1315 char direction,
1416 unsigned char * pKey=0,
1517 unsigned char * pIV=0);
16- stdEVP(const EVP_CIPHER * pCipher,
18+ stdEVP(
19+ tsApplication * app,
20+ const EVP_CIPHER * pCipher,
1721 char direction,
1822 stdString * pKey,
1923 stdString * pIV);
--- modules/tinyState/trunk/src/h/ts/c++/stdRSA.h (revision 5879)
+++ modules/tinyState/trunk/src/h/ts/c++/stdRSA.h (revision 5880)
@@ -23,9 +23,10 @@
2323 class tinyState;
2424 class stdMutex;
2525 class stdString;
26+class tsApplication;
2627 class stdRSA : public stdSSL {
2728 public:
28- stdRSA();
29+ stdRSA(tsApplication * app);
2930 ~stdRSA();
3031
3132 int size();
--- modules/tinyState/trunk/src/h/ts/c++/stdSSL.h (revision 5879)
+++ modules/tinyState/trunk/src/h/ts/c++/stdSSL.h (revision 5880)
@@ -8,12 +8,9 @@
88
99 class stdSSL : public stdObject {
1010 public:
11- stdSSL();
11+ stdSSL(tsApplication * app);
1212 ~stdSSL();
13- static void cleanup();
14- static void thread_cleanup();
1513 protected:
16- static uint8_t initialize_flag;
1714 stdOpenSSL * parent;
1815 };
1916
--- modules/tinyState/trunk/src/h/ts/c++/tsThread.h (revision 5879)
+++ modules/tinyState/trunk/src/h/ts/c++/tsThread.h (revision 5880)
@@ -7,6 +7,17 @@
77 #include <signal.h>
88 typedef void (*THR_FUNC)(tinyState * THIS,void*msg);
99
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+
1021 #include "ts/c++/tinyState.h"
1122 #include "_ts/c++/tsThread_pb.h"
1223
Show on old repository browser