Skip to content

Commit 175de37

Browse files
committed
📌 Nodepp | Atomicity update | V1.3.2 📌
1 parent b2d069a commit 175de37

File tree

8 files changed

+48
-40
lines changed

8 files changed

+48
-40
lines changed

include/nodepp/atomic.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,11 @@
2424

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

27+
namespace nodepp {
28+
atomic_t<bool> _EXIT_( false );
29+
atomic_t<int> _TASK_( int(0) );
30+
}
31+
32+
/*────────────────────────────────────────────────────────────────────────────*/
33+
2734
#endif

include/nodepp/import.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424

2525
#include "macros.h"
2626
#include "type.h"
27+
#include "atomic.h"
28+
29+
/*────────────────────────────────────────────────────────────────────────────*/
30+
31+
#include "ptr.h"
2732
#include "ref.h"
2833

2934
/*────────────────────────────────────────────────────────────────────────────*/

include/nodepp/macros.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,6 @@ template< class T > T clamp( const T& val, const T& _min, const T& _max ){ retur
8888

8989
/*────────────────────────────────────────────────────────────────────────────*/
9090

91-
bool _EXIT_ = false;
92-
int _TASK_ = 0;
93-
94-
/*────────────────────────────────────────────────────────────────────────────*/
95-
9691
#define forEach( X, ITEM ) for( auto& X : ITEM )
9792
#define forEver() for (;;)
9893
#define elif else if

include/nodepp/nodepp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ namespace nodepp { namespace process {
2626

2727
/*─······································································─*/
2828

29-
inline ulong size(){ return _TASK_ + _loop_.size() + _poll_.size() + _foop_.size(); }
29+
inline ulong size(){ return _TASK_.get() + _loop_.size() + _poll_.size() + _foop_.size(); }
3030

31-
inline void clear(){ _TASK_=0; _loop_.clear(); _poll_.clear(); _foop_.clear(); }
31+
inline void clear(){ _TASK_.set(0); _loop_.clear(); _poll_.clear(); _foop_.clear(); }
3232

3333
inline bool empty(){ return size() <= 0; }
3434

include/nodepp/posix/worker.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,22 @@
1717
namespace nodepp { class worker_t {
1818
private:
1919

20-
struct waiter { bool blk; bool out; };
20+
mutex_t& mut(){ static mutex_t mut; return mut; }
21+
struct waiter { bool blk; bool out; };
2122

2223
protected:
2324

2425
struct NODE {
2526
function_t<int> cb;
2627
bool* out, state=0;
2728
pthread_t /*-*/ id;
28-
mutex_t /*--*/ mtx;
2929
}; ptr_t<NODE> obj;
3030

3131
static void* callback( void* arg ){
32-
auto self = type::cast<worker_t>(arg);
33-
self->obj->mtx.emit([=](){ self->obj->state=1; return -1; });
32+
auto self = type::cast<worker_t>(arg); self->obj->state=1;
3433
while( self->obj->cb.emit() >= 0 ){ worker::yield(); }
35-
self->obj->mtx.emit([=](){ self->free(); return -1; });
36-
/*-------*/ delete self; worker::exit();
34+
self->mut().emit([=](){ self->free(); return -1; });
35+
/*----*/ delete self; worker::exit();
3736
return nullptr; }
3837

3938
public:

include/nodepp/ptr.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
namespace nodepp { template< class T > class ptr_t {
1818
private:
1919

20-
struct NODE { ulong count, length; T* value; };
20+
struct NODE { atomic_t<ulong> count, length; T* value; };
2121

2222
inline int _free_( NODE* address ) const noexcept {
2323
if( address /*--*/ == nullptr ){ return -1; }
@@ -204,8 +204,8 @@ namespace nodepp { template< class T > class ptr_t {
204204

205205
/*─······································································─*/
206206

207-
ulong size() const noexcept { return null() ? 0 /*-*/ : address->length; }
208-
ulong count() const noexcept { return null() ? 0 /*-*/ : address->count; }
207+
ulong size() const noexcept { return null() ? 0 /*-*/ : address->length.get(); }
208+
ulong count() const noexcept { return null() ? 0 /*-*/ : address->count .get(); }
209209

210210
T* data() const noexcept { return null() ? nullptr : address->value; }
211211
T* get() const noexcept { return null() ? nullptr : address->value; }
@@ -233,4 +233,16 @@ namespace nodepp { template< class T > class ptr_t {
233233

234234
/*────────────────────────────────────────────────────────────────────────────*/
235235

236+
namespace nodepp { namespace type {
237+
238+
template< class T, class V > T* cast( ptr_t<V>& object ){ if( object==nullptr ){ return nullptr; } return static_cast<T*>(object.get()); }
239+
240+
template<class T> ptr_t<T> bind( ptr_t<T>& object ){ if( object==nullptr ){ return nullptr; } return object.copy(); }
241+
template<class T> ptr_t<T> bind( T* object ){ if( object==nullptr ){ return nullptr; } return new T( *object ); }
242+
template<class T> ptr_t<T> bind( T object ){ return new T( object ); }
243+
244+
}}
245+
246+
/*────────────────────────────────────────────────────────────────────────────*/
247+
236248
#endif

include/nodepp/type.h

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@
1414

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

17+
namespace nodepp { namespace type {
18+
19+
template< class T, class V > T* cast( V* object ){ if( object==nullptr ){ return nullptr; } return static_cast<T*>(object); }
20+
template< class T, class V > T cast( V object ){ return static_cast<T>( object ); }
21+
22+
}}
23+
24+
/*────────────────────────────────────────────────────────────────────────────*/
25+
1726
namespace nodepp { namespace type {
1827

1928
struct false_type { static constexpr bool value = false; using type = false_type; };
@@ -409,22 +418,4 @@ namespace nodepp { namespace type {
409418

410419
/*────────────────────────────────────────────────────────────────────────────*/
411420

412-
#include "ptr.h"
413-
414-
/*────────────────────────────────────────────────────────────────────────────*/
415-
416-
namespace nodepp { namespace type {
417-
418-
template< class T, class V > T* cast( ptr_t<V>& object ){ if( object==nullptr ){ return nullptr; } return static_cast<T*>(object.get()); }
419-
template< class T, class V > T* cast( V* object ){ if( object==nullptr ){ return nullptr; } return static_cast<T*>(object); }
420-
template< class T, class V > T cast( V object ){ return static_cast<T>( object ); }
421-
422-
template<class T> ptr_t<T> bind( ptr_t<T>& object ){ if( object==nullptr ){ return nullptr; } return object.copy(); }
423-
template<class T> ptr_t<T> bind( T* object ){ if( object==nullptr ){ return nullptr; } return new T( *object ); }
424-
template<class T> ptr_t<T> bind( T object ){ return new T( object ); }
425-
426-
}}
427-
428-
/*────────────────────────────────────────────────────────────────────────────*/
429-
430421
#endif

include/nodepp/windows/worker.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace nodepp { class worker_t {
1818
private:
1919

20+
mutex_t& mut(){ static mutex_t mut; return mut; }
2021
struct waiter { bool blk; bool out; };
2122

2223
protected:
@@ -26,15 +27,13 @@ namespace nodepp { class worker_t {
2627
HANDLE /**/ thread;
2728
bool *out, state=0;
2829
DWORD /*-----*/ id;
29-
mutex_t /*--*/ mtx;
3030
}; ptr_t<NODE> obj;
3131

3232
static DWORD WINAPI callback( LPVOID arg ){
33-
auto self = type::cast<worker_t>(arg);
34-
self->obj->mtx.emit([=](){ self->obj->state=1; return -1; });
35-
while( self->obj->cb.emit() >= 0 ){ worker::yield(); }
36-
self->obj->mtx.emit([=](){ self->free(); return -1; });
37-
/*-------*/ delete self; worker::exit();
33+
auto self = type::cast<worker_t>(arg); self->obj->state=1;
34+
while( self->obj->cb.emit() >= 0 ){ worker::yield(); }
35+
self->mut().emit([=](){ self->free(); return -1; });
36+
/*----*/ delete self; worker::exit();
3837
return 0; }
3938

4039
public:

0 commit comments

Comments
 (0)