Skip to content

Commit 5f9ee87

Browse files
committed
nodepp V1.4.0_2
1 parent a2c4b1f commit 5f9ee87

File tree

13 files changed

+123
-124
lines changed

13 files changed

+123
-124
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <nodepp/nodepp.h>
2+
#include <nodepp/worker.h>
3+
#include <nodepp/timer.h>
4+
#include <nodepp/atomic.h>
5+
6+
using namespace nodepp;
7+
8+
void onWorker(){
9+
10+
worker::add([=](){
11+
12+
process::add( coroutine::add( COROUTINE(){
13+
coBegin
14+
15+
while( true ){
16+
console::log( "hello world", (void*) worker::pid() );
17+
coDelay(1000); process::clear(); }
18+
19+
coFinish
20+
}));
21+
22+
process::wait(); return -1; });
23+
24+
}
25+
26+
void onMain(){
27+
28+
process::add( coroutine::add( COROUTINE(){
29+
coBegin
30+
31+
while( true ){
32+
while( process::size() > MAX_BATCH ){ coDelay(100); }
33+
console::log( "--->", process::size() );
34+
onWorker();
35+
coNext; }
36+
37+
coFinish
38+
}));
39+
40+
}

include/nodepp/evloop.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ namespace nodepp { namespace process {
5252

5353
/*─······································································─*/
5454

55-
inline bool should_close(){ return NODEPP_EVLOOP().empty() || *NODEPP_EVLOOP().should_close(); }
55+
inline bool should_close(){ return NODEPP_EVLOOP().should_close(); }
5656
inline bool empty(){ return NODEPP_EVLOOP().empty(); }
5757
inline ulong size(){ return NODEPP_EVLOOP().size (); }
5858
inline void clear(){ /*--*/ NODEPP_EVLOOP().clear(); }
@@ -62,9 +62,10 @@ namespace nodepp { namespace process {
6262
inline int next(){ return NODEPP_EVLOOP().next(); }
6363

6464
inline void exit( int err=0 ){
65-
if( should_close() ) /*--------*/ { goto DONE; }
66-
*NODEPP_EVLOOP().should_close() = true; clear();
67-
DONE:; ::exit(err); }
65+
if( should_close() ){ goto DONE; } do {
66+
auto *raw = &SHOULD_CLOSE();
67+
*raw = true; clear();
68+
} while(0); DONE:; ::exit(err); }
6869

6970
}}
7071

include/nodepp/loop.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ namespace nodepp { class loop_t {
5858
/*─······································································─*/
5959

6060
inline int normal_queue_next() const {
61-
61+
6262
if( obj->normal.empty() ) /*-*/ { return -1; } do {
6363
if( obj->normal.get()==nullptr ){ return -1; }
6464

@@ -79,6 +79,7 @@ namespace nodepp { class loop_t {
7979
int c=0; ulong d=0; while( ([&](){
8080

8181
do{ c=y->data.first(); auto z=coroutine::getno();
82+
if( obj->normal.empty() ){ return -1; }
8283
if( c==1 && z.flag&coroutine::STATE::CO_STATE_DELAY )
8384
{ d=z.delay; goto GOT3; } switch(c) {
8485
case 1 : goto GOT1; break;
@@ -150,6 +151,7 @@ public: loop_t() noexcept : obj( new NODE() ) {}
150151
/*─······································································─*/
151152

152153
inline int next() const /*--*/ {
154+
if( SHOULD_CLOSE() ){ return -1; }
153155
/*--*/ blocked_queue_next();
154156
return normal_queue_next ();
155157
}

include/nodepp/posix/kernel.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ namespace nodepp { class kernel_t {
174174

175175
void clear() const noexcept { /*--*/ obj->ev_queue.clear(); obj->kv_queue.clear(); obj->probe.clear(); }
176176

177-
bool* should_close() const noexcept { return &SHOULD_CLOSE(); }
177+
bool should_close() const noexcept { return empty() || SHOULD_CLOSE(); }
178178

179179
bool empty() const noexcept { return size()==0; }
180180

@@ -461,7 +461,7 @@ namespace nodepp { class kernel_t {
461461

462462
void clear() const noexcept { /*--*/ obj->ev_queue.clear(); obj->kv_queue.clear(); obj->probe.clear(); }
463463

464-
bool* should_close() const noexcept { return &SHOULD_CLOSE(); }
464+
bool should_close() const noexcept { return empty() || SHOULD_CLOSE(); }
465465

466466
bool empty() const noexcept { return size()==0; }
467467

@@ -648,7 +648,7 @@ namespace nodepp { class kernel_t {
648648

649649
void clear() const noexcept { /*--*/ obj->ev_queue.clear(); obj->probe.clear(); }
650650

651-
bool* should_close() const noexcept { return &SHOULD_CLOSE(); }
651+
bool should_close() const noexcept { return empty() || SHOULD_CLOSE(); }
652652

653653
bool empty() const noexcept { return size()==0; }
654654

include/nodepp/posix/mutex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace nodepp { namespace worker {
2323
inline void delay( ulong time ){ process::delay(time); }
2424
inline void yield(){ delay(TIMEOUT); sched_yield(); }
2525
inline pthread_t pid(){ return pthread_self(); }
26-
inline void exit(){ pthread_exit(NULL); }
26+
// inline void exit(){ pthread_exit(NULL); } <- insecure
2727

2828
}}
2929

include/nodepp/posix/worker.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ namespace nodepp { class worker_t {
8686
/*─······································································─*/
8787

8888
int emit() const noexcept {
89-
if( obj->state != STATE::WK_STATE_UNKNOWN ){ return 0; }
89+
if( obj->state != STATE::WK_STATE_UNKNOWN && !SHOULD_CLOSE() ){ return 0; }
9090

9191
auto krn = type::bind( process::NODEPP_EVLOOP() );
9292
auto self= type::bind( this );
@@ -110,7 +110,7 @@ namespace nodepp { class worker_t {
110110
/*─······································································─*/
111111

112112
int await() const noexcept {
113-
if( obj->state != STATE::WK_STATE_UNKNOWN ){ return 0; }
113+
if( obj->state != STATE::WK_STATE_UNKNOWN && !SHOULD_CLOSE() ){ return 0; }
114114

115115
auto krn = type::bind( process::NODEPP_EVLOOP() );
116116
auto self= type::bind( this );

include/nodepp/windows/kernel.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ namespace nodepp { class kernel_t {
151151
ulong size() const noexcept { return obj->ev_queue.size() + obj->kv_queue.size() + obj->probe.get() + obj.count()-1; }
152152

153153
void clear() const noexcept { /*--*/ obj->ev_queue.clear(); obj->kv_queue.clear(); obj->probe.clear(); }
154-
155-
bool* should_close() const noexcept { return &SHOULD_CLOSE(); }
154+
155+
bool should_close() const noexcept { return empty() || SHOULD_CLOSE(); }
156156

157157
bool empty() const noexcept { return size()==0; }
158158

@@ -326,8 +326,8 @@ namespace nodepp { class kernel_t {
326326
ulong size() const noexcept { return obj->ev_queue.size() + obj->probe.get() + obj.count()-1; }
327327

328328
void clear() const noexcept { /*--*/ obj->ev_queue.clear(); obj->probe.clear(); }
329-
330-
bool* should_close() const noexcept { return &SHOULD_CLOSE(); }
329+
330+
bool should_close() const noexcept { return empty() || SHOULD_CLOSE(); }
331331

332332
bool empty() const noexcept { return size()==0; }
333333

include/nodepp/windows/mutex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace nodepp { namespace worker {
2424
inline void delay( ulong time ){ process::delay(time); }
2525
inline void yield(){ delay(TIMEOUT); SwitchToThread(); }
2626
inline DWORD pid(){ return GetCurrentThreadId(); }
27-
inline void exit(){ ExitThread(0); }
27+
// inline void exit(){ ExitThread(0); } <- insecure
2828

2929
}}
3030

include/nodepp/windows/worker.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ namespace nodepp { class worker_t {
8686
/*─······································································─*/
8787

8888
int emit() const noexcept {
89-
if( obj->state != STATE::WK_STATE_UNKNOWN ){ return 0; }
89+
if( obj->state != STATE::WK_STATE_UNKNOWN && !SHOULD_CLOSE() ){ return 0; }
9090

9191
auto krn = type::bind( process::NODEPP_EVLOOP() );
9292
auto self= type::bind( this );
@@ -110,7 +110,7 @@ namespace nodepp { class worker_t {
110110
/*─······································································─*/
111111

112112
int await() const noexcept {
113-
if( obj->state != STATE::WK_STATE_UNKNOWN ){ return 0; }
113+
if( obj->state != STATE::WK_STATE_UNKNOWN && !SHOULD_CLOSE() ){ return 0; }
114114

115115
auto krn = type::bind( process::NODEPP_EVLOOP() );
116116
auto self= type::bind( this );

main.cpp

Lines changed: 22 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,40 @@
11
#include <nodepp/nodepp.h>
22
#include <nodepp/worker.h>
3-
#include <nodepp/http.h>
4-
#include <nodepp/date.h>
5-
6-
/*────────────────────────────────────────────────────────────────────────────*/
3+
#include <nodepp/timer.h>
4+
#include <nodepp/atomic.h>
75

86
using namespace nodepp;
97

10-
/*────────────────────────────────────────────────────────────────────────────*/
11-
12-
inline tcp_t start_http_server(){
13-
14-
return http::server([=]( http_t cli ){
15-
16-
cli.write_header( 200, header_t({
17-
{ "content-type", "text/html" }
18-
}) );
19-
20-
if( cli.method == "POST" && cli.headers.has("Content-Length") ){
21-
ulong len = string::to_ulong( cli.headers["Content-Length"] );
22-
23-
ptr_t<ulong> tmp ( 0UL );
24-
ptr_t<generator::file::read> _read_ ( 0UL );
25-
26-
process::add( coroutine::add( COROUTINE(){
27-
coBegin
28-
29-
while( !cli.is_closed() && *tmp < len ){
30-
if ( (*_read_)( &cli, len - *tmp )==1 ){ coNext; }
31-
console::log( "stamp", _read_->data.size(), _read_->data );
32-
*tmp += _read_->data.size(); }
33-
34-
cli.write( string::format( "message-size: %d", len ) );
35-
cli.close();
36-
37-
coFinish
38-
}));
8+
void onWorker(){
399

40-
} else {
10+
worker::add([=](){
4111

42-
cli.write( "hello world" );
43-
44-
}
45-
46-
});
47-
48-
}
49-
50-
/*────────────────────────────────────────────────────────────────────────────*/
51-
52-
inline void start_http_client(){
53-
54-
file_t file ( "LICENSE", "r" );
55-
fetch_t arg;
56-
arg.url = rand()%2==0 ? "http://localhost:8000" : "http://[localhost]:8000";
57-
arg.method = rand()%2==0 ? "POST" : "GET";
58-
arg.headers= header_t({
59-
{ "content-length", string::format( "%lu", file.size() ) }
60-
});
12+
process::add( coroutine::add( COROUTINE(){
13+
coBegin
6114

62-
http::fetch( arg, nullptr, [=]( http_t cli ){
63-
if( arg.method == "GET" ){ return; }
64-
file.onData([=]( string_t data ){
65-
cli.write( data );
66-
}); stream::pipe( file );
67-
})
15+
while( true ){
16+
console::log( "hello world", (void*) worker::pid() );
17+
coDelay( rand() % 1000 ); process::clear(); }
6818

69-
.then([=]( http_t cli ){
70-
console::log( "<<", cli.read() );
71-
})
19+
coFinish
20+
}));
7221

73-
.fail([=]( except_t err ){
74-
console::error( err.what() );
75-
});
22+
process::wait(); return -1; });
7623

7724
}
7825

79-
/*────────────────────────────────────────────────────────────────────────────*/
80-
8126
void onMain(){
8227

83-
promise_t<null_t,except_t>([=](
84-
res_t<null_t> res, rej_t<except_t> rej
85-
){
86-
87-
worker::add([=](){
88-
89-
tcp_t server = start_http_server();
90-
91-
server.onError.once([=]( except_t err ){
92-
rej( err );
93-
});
94-
95-
server.listen( "[::0]", 8000, [=]( socket_t ){
96-
res( nullptr );
97-
});
98-
99-
process::wait(); return -1; });
100-
101-
})
102-
103-
.then([=]( null_t /*unused*/ ){
104-
console::done( "server started successfuly" );
28+
process::add( coroutine::add( COROUTINE(){
29+
coBegin
10530

106-
worker::add( coroutine::add( COROUTINE(){
107-
coBegin
108-
109-
while( true ){
110-
start_http_client();
111-
process::wait(); }
31+
while( true ){
32+
while( process::size() > MAX_BATCH ){ coDelay(100); }
33+
console::log( "--->", process::size() );
34+
onWorker();
35+
coNext; }
11236

113-
coFinish
114-
}));
115-
116-
})
117-
118-
.fail([=]( except_t err ){
119-
console::error( err.what() );
120-
});
121-
122-
}
37+
coFinish
38+
}));
12339

124-
/*────────────────────────────────────────────────────────────────────────────*/
40+
}

0 commit comments

Comments
 (0)