Skip to content

Commit c9e9891

Browse files
committed
📌 Nodepp | epoll bug fixing | V1.3.2 📌
1 parent 7067adc commit c9e9891

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+805
-388
lines changed

examples/57-mutex.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ void onMain(){
4545

4646
while( true ){
4747
// executed in critical section method D
48+
// the whole coroutine is executed in critical section
4849
coNext; }
4950

5051
coFinish

examples/59-worker-pool.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <nodepp/nodepp.h>
2+
#include <nodepp/wpool.h>
3+
4+
using namespace nodepp;
5+
6+
void onMain(){
7+
8+
wpool_t wpool; // or wpool_t wpool( pool_size ) <- default MAX_POOL_SIZE
9+
10+
wpool.add( coroutine::add( COROUTINE(){
11+
coBegin
12+
13+
while( true ){
14+
console::log( "-> task 1" );
15+
coDelay(100); }
16+
17+
coFinish
18+
}));
19+
20+
wpool.add( coroutine::add( COROUTINE(){
21+
coBegin
22+
23+
while( true ){
24+
console::log( "-> task 2" );
25+
coDelay(1000); }
26+
27+
coFinish
28+
}));
29+
30+
process::add( coroutine::add( COROUTINE(){
31+
coBegin
32+
33+
while ( !wpool.empty() ){ wpool.next();
34+
coDelay( wpool.get_delay() ); }
35+
36+
coFinish
37+
}));
38+
39+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <nodepp/nodepp.h>
2+
#include <nodepp/loop.h>
3+
4+
using namespace nodepp;
5+
6+
void onMain(){
7+
8+
loop_t evloop;
9+
10+
evloop.add( coroutine::add( COROUTINE(){
11+
coBegin
12+
13+
while( true ){
14+
console::log( "-> task 1" );
15+
coDelay(100); }
16+
17+
coFinish
18+
}));
19+
20+
evloop.add( coroutine::add( COROUTINE(){
21+
coBegin
22+
23+
while( true ){
24+
console::log( "-> task 2" );
25+
coDelay(1000); }
26+
27+
coFinish
28+
}));
29+
30+
process::add( coroutine::add( COROUTINE(){
31+
coBegin
32+
33+
while( !evloop.empty() ){ evloop.next(); coNext; }
34+
35+
coFinish
36+
}));
37+
38+
}

include/nodepp/array.h

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class array_t {
2020

2121
ptr_t<T> buffer;
2222

23-
type::optional<ulong[3]> get_slice_range( long x, long y ) const noexcept {
23+
ptr_t<ulong> get_slice_range( long x, long y ) const noexcept {
2424

2525
if( empty() || x == y ){ return nullptr; } if( y>0 ){ --y; }
2626

@@ -32,13 +32,13 @@ class array_t {
3232
ulong b = clamp( first() + x, 0UL, a );
3333
ulong c = a - b + 1;
3434

35-
ulong arr[3]; /*-----------------------*/
36-
arr[0] = b; arr[1] = a; arr[2] = c;
35+
ulong* arr = new ulong[3]; /*-----------*/
36+
arr[0] = b; arr[1] = a; arr[2] = c;
3737

38-
return arr;
38+
return ptr_t<ulong>( arr, 3 );
3939
}
4040

41-
type::optional<ulong[3]> get_splice_range( long x, ulong y ) const noexcept {
41+
ptr_t<ulong> get_splice_range( long x, ulong y ) const noexcept {
4242

4343
if( empty() || y == 0 ){ return nullptr; }
4444

@@ -50,10 +50,10 @@ class array_t {
5050
ulong b = clamp( first() + x, 0UL, a );
5151
ulong c = a - b + 1;
5252

53-
ulong arr[3]; /*-----------------------*/
54-
arr[0] = b; arr[1] = a; arr[2] = c;
53+
ulong* arr = new ulong[3]; /*-----------*/
54+
arr[0] = b; arr[1] = a; arr[2] = c;
5555

56-
return arr;
56+
return ptr_t<ulong>( arr, 3 );
5757
}
5858

5959
public:
@@ -303,19 +303,19 @@ class array_t {
303303
/*─······································································─*/
304304

305305
void erase( ulong index ) noexcept {
306-
auto r = get_slice_range( index, size() ); if( !r.has_value() ){ return; }
307-
else { auto z = *r.get(); auto n_buffer = ptr_t<T>( size() - 1 );
308-
type::copy( begin()+z[0]+1, end() , n_buffer.begin()+z[0] );
309-
type::copy( begin() , begin()+z[0], n_buffer.begin() );
306+
auto r = get_slice_range( index, size() ); if ( r.null() ){ return; }
307+
else { auto n_buffer = ptr_t<T>( size() - 1 );
308+
type::copy( begin()+r[0]+1, end() , n_buffer.begin()+r[0] );
309+
type::copy( begin() , begin()+r[0], n_buffer.begin() );
310310
buffer = n_buffer;
311311
}
312312
}
313313

314314
void erase( ulong start, ulong stop ) noexcept {
315-
auto r = get_slice_range( start, stop ); if( !r.has_value() ){ return; }
316-
else { auto z = *r.get(); auto n_buffer = ptr_t<T>( size() - z[2] );
317-
type::copy( begin()+z[1]+1, end() , n_buffer.begin()+z[0] );
318-
type::copy( begin() , begin()+z[0], n_buffer.begin() );
315+
auto r = get_slice_range( start, stop ); if ( r.null() ){ return; }
316+
else { auto n_buffer = ptr_t<T>( size()-r[2] );
317+
type::copy( begin()+r[1]+1, end() , n_buffer.begin()+r[0] );
318+
type::copy( begin() , begin()+r[0], n_buffer.begin() );
319319
buffer = n_buffer;
320320
}
321321
}
@@ -348,11 +348,10 @@ class array_t {
348348
array_t slice( long start ) const noexcept {
349349

350350
auto r = get_slice_range( start, size() );
351-
if( !r.has_value() ){ return nullptr; }
351+
if ( r.null() ){ return nullptr; }
352352

353-
auto z = *r.get();
354-
auto n_buffer = ptr_t<T>( z[2] );
355-
type::copy( begin()+z[0], begin()+z[0]+z[2], n_buffer.begin() );
353+
auto n_buffer = ptr_t<T>( r[2] );
354+
type::copy( begin()+r[0], begin()+r[0]+r[2], n_buffer.begin() );
356355

357356
return n_buffer;
358357
}
@@ -362,11 +361,10 @@ class array_t {
362361
array_t slice( long start, long stop ) const noexcept {
363362

364363
auto r = get_slice_range( start, stop );
365-
if( !r.has_value() ){ return nullptr; }
364+
if ( r.null() ){ return nullptr; }
366365

367-
auto z = *r.get();
368-
auto n_buffer = ptr_t<T>( z[2] );
369-
type::copy( begin()+z[0], begin()+z[0]+z[2], n_buffer.begin() );
366+
auto n_buffer = ptr_t<T>( r[2] );
367+
type::copy( begin()+r[0], begin()+r[0]+r[2], n_buffer.begin() );
370368

371369
return n_buffer;
372370
}
@@ -376,26 +374,24 @@ class array_t {
376374
array_t splice( long start, ulong stop ) noexcept {
377375

378376
auto r = get_splice_range( start, stop );
379-
if( !r.has_value() ){ return nullptr; }
377+
if ( r.null() ){ return nullptr; }
380378

381-
auto z = *r.get();
382-
auto n_buffer = ptr_t<T>( z[2] );
383-
type::copy( begin()+z[0], begin()+z[0]+z[2], n_buffer.begin() );
379+
auto n_buffer = ptr_t<T>( r[2] );
380+
type::copy( begin()+r[0], begin()+r[0]+r[2], n_buffer.begin() );
384381

385-
erase( z[0], z[0] + z[2] ); return n_buffer;
382+
erase( r[0], r[0] + r[2] ); return n_buffer;
386383
}
387384

388385
template< class V, ulong N >
389386
array_t splice( long start, ulong stop, const V (&value)[N] ) noexcept {
390387

391388
auto r = get_splice_range( start, stop );
392-
if( !r.has_value() ){ return nullptr; }
389+
if ( r.null() ){ return nullptr; }
393390

394-
auto z = *r.get();
395-
auto n_buffer = ptr_t<T>( z[2] );
396-
type::copy( begin()+z[0], begin()+z[0]+z[2], n_buffer.begin() );
391+
auto n_buffer = ptr_t<T>( r[2] );
392+
type::copy( begin()+r[0], begin()+r[0]+r[2], n_buffer.begin() );
397393

398-
erase( z[0], z[0]+z[2] ); insert( z[0], value ); return n_buffer;
394+
erase( r[0], r[0]+r[2] ); insert( r[0], value ); return n_buffer;
399395

400396
}
401397

include/nodepp/atomic.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424

2525
/*────────────────────────────────────────────────────────────────────────────*/
2626

27-
namespace nodepp {
28-
atomic_t<bool> _EXIT_( false );
29-
atomic_t<int> _TASK_( int(0) );
27+
namespace nodepp {
28+
atomic_t<bool> _EXIT_( false );
29+
atomic_t<ulong> _STMP_( 0x00 );
3030
}
3131

3232
/*────────────────────────────────────────────────────────────────────────────*/

include/nodepp/cookie.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace nodepp { using cookie_t = map_t< string_t, string_t >;
2323
namespace cookie {
2424

2525
inline query_t parse( string_t data ){
26-
static regex_t reg( "([^= ;]+)=([^;]+)" );
26+
thread_local static regex_t reg( "([^= ;]+)=([^;]+)" );
2727

2828
if( data.empty() ){ return query_t(); } query_t out;
2929

include/nodepp/coroutine.h

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@
1414

1515
/*────────────────────────────────────────────────────────────────────────────*/
1616

17-
namespace nodepp { struct generator_t { protected:
18-
ulong _time_ = 0; int _state_= 0;
19-
}; }
20-
21-
/*────────────────────────────────────────────────────────────────────────────*/
22-
2317
namespace nodepp { class coroutine_t {
2418
private:
2519

@@ -63,20 +57,55 @@ namespace nodepp { class coroutine_t {
6357
coEmit() const { return next(); }
6458

6559
int next() const {
66-
if ( !obj->alive ){ return -1; }
67-
return obj->callback( obj->state, obj->time );
60+
if ( !obj->alive ){ return -1; }
61+
return obj->callback( obj->state, obj->time );
6862
}
6963

7064
}; }
7165

7266
/*────────────────────────────────────────────────────────────────────────────*/
7367

68+
namespace nodepp {
69+
struct co_state_t { uint flag =0; ulong delay=0; int state=0; };
70+
struct generator_t { ulong _time_=0; int _state_=0; };
71+
namespace coroutine { enum STATE {
72+
CO_STATE_START = 0b00000001,
73+
CO_STATE_YIELD = 0b00000010,
74+
CO_STATE_BLOCK = 0b00000000,
75+
CO_STATE_DELAY = 0b00000100,
76+
CO_STATE_END = 0b00001000
77+
}; }}
78+
79+
/*────────────────────────────────────────────────────────────────────────────*/
80+
7481
namespace nodepp { namespace coroutine {
7582
inline coroutine_t add( function_t<int,int&,ulong&> callback ) {
76-
return coroutine_t( callback );
77-
}
83+
return coroutine_t( callback ); }
7884
}}
7985

8086
/*────────────────────────────────────────────────────────────────────────────*/
8187

82-
#endif
88+
namespace nodepp { namespace coroutine {
89+
inline co_state_t getno( int state=0, int _state_=0, ulong time=0 ){
90+
thread_local static co_state_t tmp; co_state_t out;
91+
92+
memcpy( &out, &tmp, sizeof(co_state_t) ); tmp = co_state_t { 0x00 };
93+
94+
tmp.state=_state_; if( time>0 ){
95+
tmp.delay=time; /*------------*/
96+
tmp.flag =STATE::CO_STATE_DELAY; goto DONE;
97+
} switch(state) {
98+
case -1: tmp.flag=STATE::CO_STATE_END; break;
99+
case 0: tmp.flag=STATE::CO_STATE_BLOCK; break;
100+
case 1: tmp.flag=STATE::CO_STATE_YIELD; break;
101+
default: tmp.flag=STATE::CO_STATE_START; break;
102+
}
103+
104+
DONE:; return out; }
105+
}}
106+
107+
/*────────────────────────────────────────────────────────────────────────────*/
108+
109+
#endif
110+
111+
/*────────────────────────────────────────────────────────────────────────────*/

include/nodepp/generator.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -438,9 +438,9 @@ namespace nodepp { namespace generator { namespace ws {
438438
template< class T > bool server( T& cli ) { do {
439439
auto data = cli.read(); cli.set_borrow( data );
440440

441-
int c=0; while( (c=cli.read_header())==1 )
442-
/*---------*/ { process::next(); }
443-
if( c != 0 ){ break; }
441+
int c=0; process::await([&](){
442+
while((c=cli.read_header())==1 ){ return 1; }
443+
return -1; }); if( c==-1 ){ break; }
444444

445445
if( cli.headers.has("Sec-Websocket-Key") ){
446446

@@ -473,8 +473,9 @@ namespace nodepp { namespace generator { namespace ws {
473473
});
474474

475475
cli.write_header( "GET", url::path(url), "HTTP/1.1", header );
476-
int c=0; while( (c=cli.read_header())==1 )
477-
/*---------*/ { process::next(); }
476+
int c=0; process::await([&](){
477+
while((c=cli.read_header())==1 ){ return 1; }
478+
return -1; });
478479

479480
if( c != 0 ){
480481
cli.onError.emit("Could not connect to server");

include/nodepp/http.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,13 @@ namespace nodepp { class http_t : public socket_t, public generator_t {
239239
namespace nodepp { namespace http {
240240

241241
inline tcp_t server( function_t<void,http_t> cb, agent_t* opt=nullptr ){
242-
return tcp_t([=]( http_t cli ){
242+
return tcp_t([=]( http_t cli ){ process::await([&](){
243243

244-
int c=0; while((c=cli.read_header())==1)
245-
/*------*/{ process::next(); }
246-
if( c==0 ){ cb(cli); return; }
244+
int c =0; while((c=cli.read_header())==1)
245+
/*--------------*/ { return 1; }
246+
if( c==0 ){ cb(cli); return -1; }
247247

248-
cli.close(); }, opt );
248+
cli.close(); return -1; }); }, opt );
249249
}
250250

251251
/*─······································································─*/
@@ -265,8 +265,12 @@ namespace nodepp { namespace http {
265265

266266
auto skt = tcp_t([=]( http_t cli ){
267267

268-
cli.set_timeout( fetch->timeout ); cli.write_header( fetch, dir );
269-
int c=0; while((c=cli.read_header())==1){ process::next(); }
268+
cli.set_timeout ( fetch->timeout );
269+
cli.write_header( fetch, dir );
270+
271+
int c=0; process::await([&](){
272+
while((c=cli.read_header())==1){ return 1; }
273+
return -1; });
270274

271275
if( c==0 ){ res( cli ); return; } cli.close();
272276
rej(except_t("Could not connect to server"));

0 commit comments

Comments
 (0)