git-svn-id: svn://db.shs.com.ru/pip@802 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2019-06-17 18:32:02 +00:00
parent 6812b645d9
commit 71128017dd
24 changed files with 904 additions and 547 deletions

View File

@@ -10,8 +10,7 @@ include(PIPMacros.cmake)
# Options
option(ICU "Unicode support" ON)
option(STD_IOSTREAM "Building with std iostream operators support" OFF)
option(INTROSPECTION_CONTAINERS "Build with containers introspection" OFF)
option(INTROSPECTION_THREADS "Build with threads introspection" OFF)
option(INTROSPECTION "Build with introspection" OFF)
option(LIB "System install" ON)
option(STATIC_LIB OFF)
option(DEBUG "Build with -g3" OFF)
@@ -133,7 +132,7 @@ get_filename_component(C_COMPILER "${CMAKE_C_COMPILER}" NAME)
# Sources
# Main lib
set(PIP_FOLDERS "." "core" "containers" "thread" "system" "io_devices" "io_utils" "console" "math" "code" "geo" "resources" "opencl" "crypt")
set(PIP_FOLDERS "." "core" "containers" "thread" "system" "io_devices" "io_utils" "console" "math" "code" "geo" "resources" "opencl" "crypt" "introspection")
if(PIP_FREERTOS)
#list(REMOVE_ITEM PIP_FOLDERS "console")
#include_directories("${PIP_SRC_MAIN}/console")
@@ -250,21 +249,13 @@ else()
endif()
# Check if PIP should be built with containers introspection
if(INTROSPECTION_CONTAINERS)
message(STATUS "Building PIP with containers introspection")
add_definitions(-DPIP_INTROSPECTION_CONTAINERS)
# Check if PIP should be built with introspection
if(INTROSPECTION)
message(STATUS "Building PIP with introspection")
message(STATUS "Warning: Introspection reduces the performance!")
add_definitions(-DPIP_INTROSPECTION)
else()
message(STATUS "Building PIP without containers introspection")
endif()
# Check if PIP should be built with threads introspection
if(INTROSPECTION_THREADS)
message(STATUS "Building PIP with threads introspection")
add_definitions(-DPIP_INTROSPECTION_THREADS)
else()
message(STATUS "Building PIP without threads introspection")
message(STATUS "Building PIP without introspection")
endif()

View File

@@ -1,15 +1,62 @@
#include "pip.h"
#include <winsock2.h>
class CL: public PIObject {
PIOBJECT(CL)
public:
EVENT_HANDLER1(void, nc, PIEthernet * , client) {
piCout << "client" << client;
}
};
int main() {
PIVector<double> v;
for (int i=0; i<9; ++i) v << i;
PIVector2D<double> v2;//(3,3,v);
piCout << v2;
v2.addRow(v);
v2.addRow(PIVector<double>() << 99 << 88 << 77 << 66);
v2.addRow(v2.row(0));
piCout << v2;
PIKbdListener kbd;
int main(int argc, char * argv[]) {
PICLI cli(argc, argv);
cli.setDebug(false);
cli.addArgument("send");
//PISystemInfo::machineID();
kbd.enableExitCapture();
if (cli.hasArgument("send")) {
/*piCout << "send mode";
PIEthernet eth;
eth.setSendAddress(cli.rawArguments().back() + ":15123");
eth.open();
while (!kbd.exiting) {
eth.send(PIByteArray("test string", 12));
piMSleep(500);
}*/
PIEthernet eth(PIEthernet::TCP_Server);
eth.listen(cli.rawArguments().back() + ":15123", true);
//eth.open();
CL cl;
CONNECTU(&eth, newConnection, &cl, nc);
WAIT_FOR_EXIT;
piCout << "exiting ...";
} else {
piCout << "recv mode";
PIEthernet eth(PIEthernet::TCP_Client);
//eth.setReadAddress(cli.rawArguments().back() + ":15123");
eth.startThreadedRead();
int s = eth.socket();
piCout << s;
piCout << "connect ...";
eth.connect(cli.rawArguments().back() + /*":13361"*/":15123");
piCout << "connect done" << eth.isConnected();
//fcntl(fd, F_SETFL, O_NONBLOCK);
WAIT_FOR_EXIT;
piCout << "exiting ...";
}
/*PIThread t;
t.start(10);
//WAIT_FOR_EXIT;
piSleep(20.);
t.stop(true);*/
piCout << "exit main ...";
return 0;
}

View File

@@ -27,6 +27,7 @@
#define PICONTAINERS_H
#include "picout.h"
#include "piintrospection_containers.h"
#ifdef PIP_DEBUG
# include <cassert>
#endif

View File

@@ -26,7 +26,6 @@
#define PIDEQUE_H
#include "picontainers.h"
#include "piintrospection_proxy.h"
#if !defined(PIP_CONTAINERS_STL) || defined(DOXYGEN)
@@ -36,33 +35,33 @@ template <typename T>
class PIDeque {
public:
inline PIDeque(): pid_data(0), pid_size(0), pid_rsize(0), pid_start(0) {
PIINTROSPECTION_CONTAINER_NEW()
PIINTROSPECTION_CONTAINER_NEW(T)
//printf("new vector 1 %p (%s) ... !{\n", this, typeid(T).name());
//printf("(s=%d, d=%p) }!\n", int(pid_size), pid_data);
}
inline PIDeque(const PIDeque<T> & other): pid_data(0), pid_size(0), pid_rsize(0), pid_start(0) {
PIINTROSPECTION_CONTAINER_NEW()
PIINTROSPECTION_CONTAINER_NEW(T)
//printf("new vector 2 %p (%s) ... !{\n", this, typeid(T).name());
alloc(other.pid_size, true);
newT(pid_data + pid_start, other.pid_data + other.pid_start, pid_size);
//printf("(s=%d, d=%p) }!\n", int(pid_size), pid_data);
}
inline PIDeque(const T * data, size_t size): pid_data(0), pid_size(0), pid_rsize(0), pid_start(0) {
PIINTROSPECTION_CONTAINER_NEW()
PIINTROSPECTION_CONTAINER_NEW(T)
//printf("new vector 2 %p (%s) ... !{\n", this, typeid(T).name());
alloc(size, true);
newT(pid_data + pid_start, data, pid_size);
//printf("(s=%d, d=%p) }!\n", int(pid_size), pid_data);
}
inline PIDeque(size_t pid_size, const T & f = T()): pid_data(0), pid_size(0), pid_rsize(0), pid_start(0) {
PIINTROSPECTION_CONTAINER_NEW()
PIINTROSPECTION_CONTAINER_NEW(T)
//printf("new vector 3 %p (%s) ... !{\n", this, typeid(T).name());
resize(pid_size, f);
//printf("(s=%d, d=%p) }!\n", int(pid_size), pid_data);
}
inline ~PIDeque() {
PIINTROSPECTION_CONTAINER_DELETE()
PIINTROSPECTION_CONTAINER_FREE((pid_rsize)*sizeof(T))
PIINTROSPECTION_CONTAINER_DELETE(T)
PIINTROSPECTION_CONTAINER_FREE(T, (pid_rsize)*sizeof(T))
//printf("delete deque %p (%s) (s=%d, rs=%d, st=%d, d=%p) ... ~{\n", this, typeid(T).name(), int(pid_size), int(pid_rsize), int(pid_start), pid_data);
deleteT(pid_data + pid_start, pid_size);
dealloc();
@@ -209,7 +208,7 @@ public:
alloc(new_size, true);
//if (sizeof(T) == 1) memset(&(pid_data[os]), f, ds);
//zeroRaw(&(pid_data[os]), new_size - os);
PIINTROSPECTION_CONTAINER_USED((new_size-os)*sizeof(T))
PIINTROSPECTION_CONTAINER_USED(T, (new_size-os)*sizeof(T))
for (size_t i = os + pid_start; i < new_size + pid_start; ++i) elementNew(pid_data + i, f);
}
return *this;
@@ -244,7 +243,7 @@ public:
memmove((void*)(&(pid_data[pid_start])), (const void*)(&(pid_data[pid_start + 1])), index * sizeof(T));
}
//piCout << "insert" << pid_start << index << (pid_start + ssize_t(index)) << pid_size << ">!";
PIINTROSPECTION_CONTAINER_USED(sizeof(T))
PIINTROSPECTION_CONTAINER_USED(T, sizeof(T))
elementNew(pid_data + pid_start + index, v);
return *this;
}
@@ -306,7 +305,7 @@ public:
inline PIDeque<T> & removeOne(const T & v) {for (size_t i = 0; i < pid_size; ++i) if (pid_data[i + pid_start] == v) {remove(i); return *this;} return *this;}
inline PIDeque<T> & removeAll(const T & v) {for (ssize_t i = 0; i < ssize_t(pid_size); ++i) if (pid_data[i + pid_start] == v) {remove(i); --i;} return *this;}
inline PIDeque<T> & push_back(const T & v) {alloc(pid_size + 1, true); PIINTROSPECTION_CONTAINER_USED(sizeof(T)); elementNew(pid_data + pid_start + pid_size - 1, v); return *this;}
inline PIDeque<T> & push_back(const T & v) {alloc(pid_size + 1, true); PIINTROSPECTION_CONTAINER_USED(T, sizeof(T)); elementNew(pid_data + pid_start + pid_size - 1, v); return *this;}
inline PIDeque<T> & append(const T & v) {return push_back(v);}
inline PIDeque<T> & append(const PIDeque<T> & t) {
size_t ps = pid_size;
@@ -361,7 +360,7 @@ private:
return (1 << t);
}
inline void newT(T * dst, const T * src, size_t s) {
PIINTROSPECTION_CONTAINER_USED(s*sizeof(T))
PIINTROSPECTION_CONTAINER_USED(T, s*sizeof(T))
for (size_t i = 0; i < s; ++i)
elementNew(dst + i, src[i]);
}
@@ -378,7 +377,7 @@ private:
else pid_tdata = (T*)(realloc(pid_tdata, s * sizeof(T)));
}*/
inline void deleteT(T * d, size_t sz) {
PIINTROSPECTION_CONTAINER_UNUSED(sz*sizeof(T))
PIINTROSPECTION_CONTAINER_UNUSED(T, sz*sizeof(T))
//std::cout << " ~[("<<this<<")deleteT " << std::dec << sz << " elements " << " start " << pid_start << std::hex << " 0x" << (llong)d << " ... <" << std::endl;
if ((uchar*)d != 0) {
for (size_t i = 0; i < sz; ++i)
@@ -451,7 +450,7 @@ private:
//if(as > 1000) piCout << "too much deque size" << new_size << as;
if (as != pid_rsize) {
//printf("(%p) realloc %d -> %d (%p)\n", this, pid_rsize, as, pid_data);
PIINTROSPECTION_CONTAINER_ALLOC((as-pid_rsize)*sizeof(T))
PIINTROSPECTION_CONTAINER_ALLOC(T, (as-pid_rsize)*sizeof(T))
T * p_d = (T*)(realloc((void*)(pid_data), as*sizeof(T)));
//if(!p_d) printf("(%p) realloc (%d)%d -> %d (%p) %d\n", this, (int)pid_start, (int)pid_rsize, (int)as, pid_data, (int)new_size);
assert(p_d);
@@ -475,7 +474,7 @@ private:
T * td = newRaw(as);
ssize_t ns = pid_start + as - pid_rsize;
//printf("%X pid_start ost=%d ors=%d nst=%d nrs=%d\n", this, pid_start, pid_rsize, ns, as);
PIINTROSPECTION_CONTAINER_ALLOC((as-pid_rsize)*sizeof(T))
PIINTROSPECTION_CONTAINER_ALLOC(T, (as-pid_rsize)*sizeof(T))
if (pid_rsize > 0 && pid_data != 0) {
//printf("%X copy from %p + %d to %p + %d %d el\n", this, pid_data, pid_start, td, ns, pid_size);
memcpy((void*)(td + ns), (const void*)(pid_data + pid_start), pid_size * sizeof(T));
@@ -500,8 +499,8 @@ private:
};
#define __PIDEQUE_SIMPLE_TYPE__(T) \
template<> inline void PIDeque<T>::newT(T * dst, const T * src, size_t s) {PIINTROSPECTION_CONTAINER_USED(s*sizeof(T)); memcpy((void*)(dst), (const void*)(src), s * sizeof(T));} \
template<> inline void PIDeque<T>::deleteT(T * d, size_t sz) {PIINTROSPECTION_CONTAINER_UNUSED(sz*sizeof(T));} \
template<> inline void PIDeque<T>::newT(T * dst, const T * src, size_t s) {PIINTROSPECTION_CONTAINER_USED(T, s*sizeof(T)); memcpy((void*)(dst), (const void*)(src), s * sizeof(T));} \
template<> inline void PIDeque<T>::deleteT(T * d, size_t sz) {PIINTROSPECTION_CONTAINER_UNUSED(T, sz*sizeof(T));} \
template<> inline void PIDeque<T>::elementNew(T * to, const T & from) {(*to) = from;} \
template<> inline void PIDeque<T>::elementDelete(T & from) {;} \
template<> inline PIDeque<T> & PIDeque<T>::_resizeRaw(size_t new_size) {if (new_size > pid_size) alloc(new_size, true); return *this;} \

View File

@@ -56,7 +56,6 @@ handler A: event to event
*/
PIString PIObject::__MetaFunc::arguments() const {
return types.join(",");
}
@@ -74,19 +73,23 @@ PIString PIObject::__MetaFunc::fullFormat() const {
PIObject::PIObject(const PIString & name): _signature_(__PIOBJECT_SIGNATURE__), emitter_(0), thread_safe_(false), proc_event_queue(false) {
piMonitor.objects++;
//__PIVariantInitBuiltin__();
setName(name);
setDebug(true);
mutexObjects().lock();
piMonitor.objects++;
objects() << this;
mutexObjects().unlock();
//piCout << "new" << this;
}
PIObject::~PIObject() {
//piCout << "delete" << this;
mutexObjects().lock();
piMonitor.objects--;
objects().removeAll(this);
mutexObjects().unlock();
piDisconnect(this);
}
@@ -387,6 +390,7 @@ void PIObject::piDisconnect(PIObject * src) {
void PIObject::updateConnectors() {
//piCout << "*** updateConnectors" << this;
connectors.clear();
PIMutexLocker _ml(mutexObjects());
piForeach (PIObject * o, objects()) {
if (o == this) continue;
PIVector<__Connection> & oc(o->connections);
@@ -469,6 +473,12 @@ PIVector<PIObject * > & PIObject::objects() {
}
PIMutex & PIObject::mutexObjects() {
static PIMutex ret;
return ret;
}
void PIObject::callAddrV(void * slot, void * obj, int args, const PIVector<PIVariant> & vl) {
args = piMini(args, vl.size_s());
switch (args) {
@@ -561,6 +571,7 @@ void PIObject::dump(const PIString & line_prefix) const {
void dumpApplication() {
PIMutexLocker _ml(PIObject::mutexObjects());
//printf("dump application ...\n");
PIDateTime cd = PIDateTime::current();
PISystemInfo * pi = PISystemInfo::instance();

View File

@@ -774,6 +774,7 @@ public:
//! Returns PIObject* with name "name" or 0, if there is no object found
static PIObject * findByName(const PIString & name) {
PIMutexLocker _ml(mutexObjects());
piForeach (PIObject * i, PIObject::objects()) {
if (i->name() != name) continue;
return i;
@@ -894,7 +895,9 @@ private:
virtual int ptrOffset() const {return 0;}
static PIVector<PIObject * > & objects();
static PIMutex & mutexObjects();
static void callAddrV(void * slot, void * obj, int args, const PIVector<PIVariant> & vl);
PIVector<__Connection> connections;
PIMap<PIString, PIVariant> properties_;

View File

@@ -0,0 +1,34 @@
/*
PIP - Platform Independent Primitives
Introspection module - base macros and types
Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIINTROSPECTION_BASE_H
#define PIINTROSPECTION_BASE_H
#include "pibase.h"
class PIString;
class PIMutex;
class PIThread;
class PITimer;
class PIPeer;
#define __PIINTROSPECTION_SINGLETON__(T) \
static PIIntrospection##T##Interface * instance() {static PIIntrospection##T##Interface ret; return &ret;} \
#endif // PIINTROSPECTION_BASE_H

View File

@@ -0,0 +1,61 @@
/*
PIP - Platform Independent Primitives
Introspection module - interface for containers
Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piintrospection_containers.h"
#include "piintrospection_containers_p.h"
PIIntrospectionContainersInterface::PIIntrospectionContainersInterface() {
p = new PIIntrospectionContainers();
}
PIIntrospectionContainersInterface::~PIIntrospectionContainersInterface() {
delete p;
}
void PIIntrospectionContainersInterface::containerNew(const char * tn) {
p->containerNew(tn);
}
void PIIntrospectionContainersInterface::containerDelete(const char * tn) {
p->containerDelete(tn);
}
void PIIntrospectionContainersInterface::containerAlloc(const char * tn, ullong cnt) {
p->containerAlloc(tn, cnt);
}
void PIIntrospectionContainersInterface::containerFree(const char * tn, ullong cnt) {
p->containerFree(tn, cnt);
}
void PIIntrospectionContainersInterface::containerUsed(const char * tn, ullong cnt) {
p->containerUsed(tn, cnt);
}
void PIIntrospectionContainersInterface::containerUnused(const char * tn, ullong cnt) {
p->containerUnused(tn, cnt);
}

View File

@@ -0,0 +1,77 @@
/*
PIP - Platform Independent Primitives
Introspection module - interface for containers
Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIINTROSPECTION_CONTAINERS_H
#define PIINTROSPECTION_CONTAINERS_H
#include "piintrospection_base.h"
class PIIntrospectionContainers;
#define PIINTROSPECTION_CONTAINERS (PIIntrospectionContainersInterface::instance())
//#if defined(__PIIS__)
//# undef __PIIS__
//#endif
//# if defined(PIP_INTROSPECTION) && !defined(PIP_FORCE_NO_PIINTROSPECTION)
//#endif
#ifdef CC_GCC
# include <typeinfo>
# define _PIIS_TYPENAME_(t) typeid(t).name()
#else
# define _PIIS_TYPENAME_(t) ""
#endif
#if defined(PIP_INTROSPECTION) && !defined(PIP_FORCE_NO_PIINTROSPECTION)
# define PIINTROSPECTION_CONTAINER_NEW(t) PIINTROSPECTION_CONTAINERS->containerNew (_PIIS_TYPENAME_(t));
# define PIINTROSPECTION_CONTAINER_DELETE(t) PIINTROSPECTION_CONTAINERS->containerDelete(_PIIS_TYPENAME_(t));
# define PIINTROSPECTION_CONTAINER_ALLOC(t, cnt) PIINTROSPECTION_CONTAINERS->containerAlloc (_PIIS_TYPENAME_(t), cnt);
# define PIINTROSPECTION_CONTAINER_FREE(t, cnt) PIINTROSPECTION_CONTAINERS->containerFree (_PIIS_TYPENAME_(t), cnt);
# define PIINTROSPECTION_CONTAINER_USED(t, cnt) PIINTROSPECTION_CONTAINERS->containerUsed (_PIIS_TYPENAME_(t), cnt);
# define PIINTROSPECTION_CONTAINER_UNUSED(t, cnt) PIINTROSPECTION_CONTAINERS->containerUnused(_PIIS_TYPENAME_(t), cnt);
#else
# define PIINTROSPECTION_CONTAINER_NEW(t)
# define PIINTROSPECTION_CONTAINER_DELETE(t)
# define PIINTROSPECTION_CONTAINER_ALLOC(t, cnt)
# define PIINTROSPECTION_CONTAINER_FREE(t, cnt)
# define PIINTROSPECTION_CONTAINER_USED(t, cnt)
# define PIINTROSPECTION_CONTAINER_UNUSED(t, cnt)
#endif
class PIP_EXPORT PIIntrospectionContainersInterface {
public:
__PIINTROSPECTION_SINGLETON__(Containers)
void containerNew (const char * tn);
void containerDelete(const char * tn);
void containerAlloc (const char * tn, ullong cnt);
void containerFree (const char * tn, ullong cnt);
void containerUsed (const char * tn, ullong cnt);
void containerUnused(const char * tn, ullong cnt);
private:
PIIntrospectionContainersInterface();
~PIIntrospectionContainersInterface();
PIIntrospectionContainers * p;
};
#endif // PIINTROSPECTION_CONTAINERS_H

View File

@@ -0,0 +1,92 @@
/*
PIP - Platform Independent Primitives
Introspection module - implementation of containers
Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piintrospection_containers_p.h"
PIIntrospectionContainers::Type::Type() {
count = items = 0u;
bytes_allocated = bytes_used = 0U;
}
PIIntrospectionContainers::PIIntrospectionContainers() {
}
void PIIntrospectionContainers::containerNew(const char * tn) {
PIMutexLocker _ml(mutex);
uint id = typeID(tn);
typenames[id] = PIStringAscii(tn);
data[id].count++;
}
void PIIntrospectionContainers::containerDelete(const char * tn) {
PIMutexLocker _ml(mutex);
data[typeID(tn)].count--;
}
void PIIntrospectionContainers::containerAlloc(const char * tn, ullong cnt) {
PIMutexLocker _ml(mutex);
data[typeID(tn)].bytes_allocated += cnt;
}
void PIIntrospectionContainers::containerFree(const char * tn, ullong cnt) {
PIMutexLocker _ml(mutex);
data[typeID(tn)].bytes_allocated -= cnt;
}
void PIIntrospectionContainers::containerUsed(const char * tn, ullong cnt) {
PIMutexLocker _ml(mutex);
data[typeID(tn)].bytes_used += cnt;
}
void PIIntrospectionContainers::containerUnused(const char * tn, ullong cnt) {
PIMutexLocker _ml(mutex);
data[typeID(tn)].bytes_used -= cnt;
}
uint PIIntrospectionContainers::typeID(const char * tn) {
if (!tn) return 0u;
size_t l = strlen(tn);
if (l == 0) return 0u;
return crc.calculate(tn, l);
}
PIByteArray & operator <<(PIByteArray & s, const PIIntrospectionContainers::Type & v) {
s << v.count << v.items << v.bytes_allocated << v.bytes_used;
return s;
}
PIByteArray & operator >>(PIByteArray & s, PIIntrospectionContainers::Type & v) {
s >> v.count >> v.items >> v.bytes_allocated >> v.bytes_used;
return s;
}

View File

@@ -0,0 +1,62 @@
/*
PIP - Platform Independent Primitives
Introspection module - implementation of containers
Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIINTROSPECTION_CONTAINERS_P_H
#define PIINTROSPECTION_CONTAINERS_P_H
#define PIP_FORCE_NO_PIINTROSPECTION
#include "pimutex.h"
#include "pimap.h"
#include "picrc.h"
class PIP_EXPORT PIIntrospectionContainers {
public:
PIIntrospectionContainers();
void containerNew (const char * tn);
void containerDelete(const char * tn);
void containerAlloc (const char * tn, ullong cnt);
void containerFree (const char * tn, ullong cnt);
void containerUsed (const char * tn, ullong cnt);
void containerUnused(const char * tn, ullong cnt);
uint typeID(const char * tn);
struct Type {
Type();
uint count;
uint items;
ullong bytes_allocated;
ullong bytes_used;
};
PIMap<uint, Type> data;
PIMap<uint, PIString> typenames;
PIMutex mutex;
CRC_32 crc;
};
PIByteArray & operator <<(PIByteArray & s, const PIIntrospectionContainers::Type & v);
PIByteArray & operator >>(PIByteArray & s, PIIntrospectionContainers::Type & v);
#undef PIP_FORCE_NO_PIINTROSPECTION
#endif // PIINTROSPECTION_CONTAINERS_P_H

View File

@@ -0,0 +1,42 @@
/*
PIP - Platform Independent Primitives
Introspection module
Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piintrospection_server.h"
PIIntrospectionServer::PIIntrospectionServer(): PIPeer(genName()) {
CONNECTU(&itimer, tickEvent, this, timerEvent)
itimer.start(100);
}
PIString PIIntrospectionServer::genName() {
randomize();
return "__introspection__server_" + PIString::fromNumber(randomi() % 1000);
}
void PIIntrospectionServer::timerEvent() {
PIByteArray ba;
/* PIINTROSPECTION_THREADS->mutex.lock();
ba << appname << *(PIINTROSPECTION_CONTAINERS) << PIINTROSPECTION_THREADS->threads.values();
PIINTROSPECTION_THREADS->mutex.unlock();*/
//piCout << "send" << appname;
send("__introspection_client__", ba);
}

View File

@@ -0,0 +1,41 @@
/*
PIP - Platform Independent Primitives
Introspection module
Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIINTROSPECTION_SERVER_H
#define PIINTROSPECTION_SERVER_H
#include "pipeer.h"
#include "pimutex.h"
class __PIIntrospectionServer__;
class PIP_EXPORT PIIntrospectionServer: public PIPeer {
PIOBJECT_SUBCLASS(PIIntrospectionServer, PIPeer)
friend class __PIIntrospectionServer__;
PIIntrospectionServer();
public:
PIString appname;
private:
EVENT_HANDLER(void, timerEvent);
PIString genName();
PITimer itimer;
};
#endif // PIINTROSPECTION_SERVER_H

View File

@@ -0,0 +1,66 @@
/*
PIP - Platform Independent Primitives
Introspection module - interface for threads
Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piintrospection_threads.h"
#include "piintrospection_threads_p.h"
PIIntrospectionThreadsInterface::PIIntrospectionThreadsInterface() {
p = new PIIntrospectionThreads();
}
PIIntrospectionThreadsInterface::~PIIntrospectionThreadsInterface() {
delete p;
}
void PIIntrospectionThreadsInterface::threadNew(PIThread * t) {
p->threadNew(t);
}
void PIIntrospectionThreadsInterface::threadDelete(PIThread * t) {
p->threadDelete(t);
}
void PIIntrospectionThreadsInterface::threadStart(PIThread * t) {
p->threadStart(t);
}
void PIIntrospectionThreadsInterface::threadRun(PIThread * t) {
p->threadRun(t);
}
void PIIntrospectionThreadsInterface::threadWait(PIThread * t) {
p->threadWait(t);
}
void PIIntrospectionThreadsInterface::threadStop(PIThread * t) {
p->threadStop(t);
}
void PIIntrospectionThreadsInterface::threadRunDone(PIThread * t, ullong us) {
p->threadRunDone(t, us);
}

View File

@@ -0,0 +1,67 @@
/*
PIP - Platform Independent Primitives
Introspection module - interface for threads
Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIINTROSPECTION_THREADS_H
#define PIINTROSPECTION_THREADS_H
#include "piintrospection_base.h"
class PIIntrospectionThreads;
#define PIINTROSPECTION_THREADS (PIIntrospectionThreadsInterface::instance())
#if defined(PIP_INTROSPECTION) && !defined(PIP_FORCE_NO_PIINTROSPECTION)
# define PIINTROSPECTION_THREAD_NEW(t) PIINTROSPECTION_THREADS->threadNew (t);
# define PIINTROSPECTION_THREAD_DELETE(t) PIINTROSPECTION_THREADS->threadDelete (t);
# define PIINTROSPECTION_THREAD_START(t) PIINTROSPECTION_THREADS->threadStart (t);
# define PIINTROSPECTION_THREAD_RUN(t) PIINTROSPECTION_THREADS->threadRun (t);
# define PIINTROSPECTION_THREAD_WAIT(t) PIINTROSPECTION_THREADS->threadWait (t);
# define PIINTROSPECTION_THREAD_STOP(t) PIINTROSPECTION_THREADS->threadStop (t);
# define PIINTROSPECTION_THREAD_RUN_DONE(t,us) PIINTROSPECTION_THREADS->threadRunDone(t,us);
#else
# define PIINTROSPECTION_THREAD_NEW(t)
# define PIINTROSPECTION_THREAD_DELETE(t)
# define PIINTROSPECTION_THREAD_START(t)
# define PIINTROSPECTION_THREAD_RUN(t)
# define PIINTROSPECTION_THREAD_WAIT(t)
# define PIINTROSPECTION_THREAD_STOP(t)
# define PIINTROSPECTION_THREAD_RUN_DONE(t,us)
#endif
class PIP_EXPORT PIIntrospectionThreadsInterface {
public:
__PIINTROSPECTION_SINGLETON__(Threads)
void threadNew (PIThread * t);
void threadDelete (PIThread * t);
void threadStart (PIThread * t);
void threadRun (PIThread * t);
void threadWait (PIThread * t);
void threadStop (PIThread * t);
void threadRunDone(PIThread * t, ullong us);
private:
PIIntrospectionThreadsInterface();
~PIIntrospectionThreadsInterface();
PIIntrospectionThreads * p;
};
#endif // PIINTROSPECTION_THREADS_H

View File

@@ -0,0 +1,92 @@
/*
PIP - Platform Independent Primitives
Introspection module - implementation of threads
Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piintrospection_threads_p.h"
PIIntrospectionThreads::ThreadInfo::ThreadInfo() {
id = 0;
state = sStopped;
priority = 0;
}
PIIntrospectionThreads::PIIntrospectionThreads() {
}
void PIIntrospectionThreads::threadNew(PIThread * t) {
mutex.lock();
ThreadInfo & ti(threads[t]);
ti.id = t->tid();
ti.priority = t->priority();
ti.name = t->name();
//piCout << "register thread" << id << name;
mutex.unlock();
}
void PIIntrospectionThreads::threadDelete(PIThread * t) {
mutex.lock();
threads.remove(t);
mutex.unlock();
}
void PIIntrospectionThreads::threadStart(PIThread * t) {
}
void PIIntrospectionThreads::threadRun(PIThread * t) {
}
void PIIntrospectionThreads::threadWait(PIThread * t) {
}
void PIIntrospectionThreads::threadStop(PIThread * t) {
}
void PIIntrospectionThreads::threadRunDone(PIThread * t, ullong us) {
}
PIByteArray & operator <<(PIByteArray & b, const PIIntrospectionThreads::ThreadInfo & v) {
b << v.name << v.id << int(v.state) << v.priority;
return b;
}
PIByteArray & operator >>(PIByteArray & b, PIIntrospectionThreads::ThreadInfo & v) {
int st(0);
b >> v.id >> v.priority >> st >> v.name;
v.state = (PIIntrospectionThreads::ThreadState)st;
return b;
}

View File

@@ -0,0 +1,61 @@
/*
PIP - Platform Independent Primitives
Introspection module - implementation of threads
Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIINTROSPECTION_THREADS_P_H
#define PIINTROSPECTION_THREADS_P_H
#include "pimap.h"
#include "pithread.h"
class PIP_EXPORT PIIntrospectionThreads {
public:
PIIntrospectionThreads();
enum ThreadState {
sStopped = 1,
sStarting,
sRunning,
sWaiting,
};
struct ThreadInfo {
ThreadInfo();
PIString name;
int id;
ThreadState state;
short priority;
};
void threadNew (PIThread * t);
void threadDelete (PIThread * t);
void threadStart (PIThread * t);
void threadRun (PIThread * t);
void threadWait (PIThread * t);
void threadStop (PIThread * t);
void threadRunDone(PIThread * t, ullong us);
PIMap<PIThread*, ThreadInfo> threads;
PIMutex mutex;
};
PIByteArray & operator <<(PIByteArray & b, const PIIntrospectionThreads::ThreadInfo & v);
PIByteArray & operator >>(PIByteArray & b, PIIntrospectionThreads::ThreadInfo & v);
#endif // PIINTROSPECTION_THREADS_P_H

View File

@@ -1,119 +0,0 @@
/*
PIP - Platform Independent Primitives
Introspection module
Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piintrospection.h"
#include "piincludes.h"
#include "pisysteminfo.h"
PIIntrospectionContainers::PIIntrospectionContainers() {
count = 0;
bytes_allocated = bytes_used = 0;
}
PIIntrospectionThreads::PIIntrospectionThreads() {
}
PIIntrospectionContainers * __PIIntrospectionContainers__::p = 0;
PIIntrospectionThreads * __PIIntrospectionThreads__::p = 0;
PIIntrospectionServer * __PIIntrospectionServer__::p = 0;
void PIIntrospectionThreads::registerThread(int id, short prior, const PIString & name) {
mutex.lock();
ThreadInfo & ti(threads[id]);
ti.id = id;
ti.priority = prior;
ti.name = name;
//piCout << "register thread" << id << name;
mutex.unlock();
}
void PIIntrospectionThreads::unregisterThread(int id) {
mutex.lock();
threads.remove(id);
mutex.unlock();
}
void PIIntrospectionContainers::containerNew() {
mutex.lock();
count++;
mutex.unlock();
}
void PIIntrospectionContainers::containerDelete() {
mutex.lock();
count--;
mutex.unlock();
}
void PIIntrospectionContainers::containerAlloc(ullong cnt) {
mutex.lock();
bytes_allocated += cnt;
mutex.unlock();
}
void PIIntrospectionContainers::containerFree(ullong cnt) {
mutex.lock();
bytes_allocated -= cnt;
mutex.unlock();
}
void PIIntrospectionContainers::containerUsed(ullong cnt) {
mutex.lock();
bytes_used += cnt;
mutex.unlock();
}
void PIIntrospectionContainers::containerUnused(ullong cnt) {
mutex.lock();
bytes_used -= cnt;
mutex.unlock();
}
PIIntrospectionServer::PIIntrospectionServer(): PIPeer(genName()) {
CONNECTU(&itimer, tickEvent, this, timerEvent)
itimer.start(100);
}
PIString PIIntrospectionServer::genName() {
randomize();
return "__introspection__server_" + PIString::fromNumber(randomi() % 1000);
}
void PIIntrospectionServer::timerEvent() {
PIByteArray ba;
PIINTROSPECTION_THREADS->mutex.lock();
ba << appname << *(PIINTROSPECTION_CONTAINERS) << PIINTROSPECTION_THREADS->threads.values();
PIINTROSPECTION_THREADS->mutex.unlock();
//piCout << "send" << appname;
send("__introspection_client__", ba);
}

View File

@@ -1,115 +0,0 @@
/*
PIP - Platform Independent Primitives
Introspection module
Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIINTROSPECTION_H
#define PIINTROSPECTION_H
#include "pipeer.h"
#include "pimutex.h"
class PIP_EXPORT PIIntrospectionThreads
{
friend class __PIIntrospectionThreads__;
public:
PIIntrospectionThreads();
struct ThreadInfo {
ThreadInfo() {id = 0; priority = 0;}
PIString name;
int id;
short priority;
};
void registerThread(int id, short prior, const PIString & name);
void unregisterThread(int id);
PIMap<int, ThreadInfo> threads;
PIMutex mutex;
};
class PIP_EXPORT PIIntrospectionContainers
{
friend class __PIIntrospectionContainers__;
public:
PIIntrospectionContainers();
void containerNew();
void containerDelete();
void containerAlloc(ullong cnt);
void containerFree(ullong cnt);
void containerUsed(ullong cnt);
void containerUnused(ullong cnt);
uint count;
ullong bytes_allocated;
ullong bytes_used;
PIMutex mutex;
};
class PIP_EXPORT PIIntrospectionServer: public PIPeer
{
PIOBJECT_SUBCLASS(PIIntrospectionServer, PIPeer)
friend class __PIIntrospectionServer__;
PIIntrospectionServer();
public:
PIString appname;
private:
EVENT_HANDLER(void, timerEvent);
PIString genName();
PITimer itimer;
};
class __PIIntrospectionContainers__ {
public:
__PIIntrospectionContainers__() {if (!p) p = new PIIntrospectionContainers();}
static PIIntrospectionContainers * get() {static __PIIntrospectionContainers__ * r = new __PIIntrospectionContainers__(); return r->p;}
static PIIntrospectionContainers * p;
};
class __PIIntrospectionThreads__ {
public:
__PIIntrospectionThreads__() {if (!p) p = new PIIntrospectionThreads();}
static PIIntrospectionThreads * get() {static __PIIntrospectionThreads__ * r = new __PIIntrospectionThreads__(); return r->p;}
static PIIntrospectionThreads * p;
};
class __PIIntrospectionServer__ {
public:
__PIIntrospectionServer__() {if (!p) p = new PIIntrospectionServer();}
~__PIIntrospectionServer__() {if (!p) return; delete p; p = 0;}
static PIIntrospectionServer * get() {static __PIIntrospectionServer__ * r = new __PIIntrospectionServer__(); return r->p;}
static PIIntrospectionServer * p;
};
inline PIByteArray & operator <<(PIByteArray & b, const PIIntrospectionContainers & v) {b << v.count << v.bytes_allocated << v.bytes_used; return b;}
inline PIByteArray & operator <<(PIByteArray & b, const PIIntrospectionThreads::ThreadInfo & v) {b << v.id << v.priority << v.name; return b;}
inline PIByteArray & operator >>(PIByteArray & b, PIIntrospectionContainers & v) {b >> v.count >> v.bytes_allocated >> v.bytes_used; return b;}
inline PIByteArray & operator >>(PIByteArray & b, PIIntrospectionThreads::ThreadInfo & v) {b >> v.id >> v.priority >> v.name; return b;}
#define PIINTROSPECTION_CONTAINERS __PIIntrospectionContainers__::get()
#define PIINTROSPECTION_THREADS __PIIntrospectionThreads__::get()
#define PIINTROSPECTION_SERVER __PIIntrospectionServer__::get()
#endif // PIINTROSPECTION_H

View File

@@ -1,32 +0,0 @@
/*
PIP - Platform Independent Primitives
Introspection module
Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piintrospection_proxy.h"
#include "piintrospection.h"
#include "pisysteminfo.h"
void __PIIntrospection__registerThread(int id, short prior, const PIString & name) {PIINTROSPECTION_THREADS->registerThread(id, prior, name);}
void __PIIntrospection__unregisterThread(int id) {PIINTROSPECTION_THREADS->unregisterThread(id);}
void __PIIntrospection__containerNew() {PIINTROSPECTION_CONTAINERS->containerNew();}
void __PIIntrospection__containerDelete() {PIINTROSPECTION_CONTAINERS->containerDelete();}
void __PIIntrospection__containerAlloc(ullong cnt) {PIINTROSPECTION_CONTAINERS->containerAlloc(cnt);}
void __PIIntrospection__containerFree(ullong cnt) {PIINTROSPECTION_CONTAINERS->containerFree(cnt);}
void __PIIntrospection__containerUsed(ullong cnt) {PIINTROSPECTION_CONTAINERS->containerUsed(cnt);}
void __PIIntrospection__containerUnused(ullong cnt) {PIINTROSPECTION_CONTAINERS->containerUnused(cnt);}
void __PIIntrospection__start() {PIINTROSPECTION_SERVER->appname = PISystemInfo::instance()->execCommand;}

View File

@@ -1,69 +0,0 @@
/*
PIP - Platform Independent Primitives
Introspection module
Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIINTROSPECTION_PROXY_H
#define PIINTROSPECTION_PROXY_H
#include "pibase.h"
class PIString;
void __PIIntrospection__registerThread(int id, short prior, const PIString & name);
void __PIIntrospection__unregisterThread(int id);
void __PIIntrospection__containerNew();
void __PIIntrospection__containerDelete();
void __PIIntrospection__containerAlloc(ullong cnt);
void __PIIntrospection__containerFree(ullong cnt);
void __PIIntrospection__containerUsed(ullong cnt);
void __PIIntrospection__containerUnused(ullong cnt);
void __PIIntrospection__start();
#ifdef PIP_INTROSPECTION_CONTAINERS
# define PIINTROSPECTION_CONTAINER_NEW() __PIIntrospection__containerNew();
# define PIINTROSPECTION_CONTAINER_DELETE() __PIIntrospection__containerDelete();
# define PIINTROSPECTION_CONTAINER_USED(cnt) __PIIntrospection__containerUsed(cnt);
# define PIINTROSPECTION_CONTAINER_UNUSED(cnt) __PIIntrospection__containerUnused(cnt);
# define PIINTROSPECTION_CONTAINER_ALLOC(cnt) __PIIntrospection__containerAlloc(cnt);
# define PIINTROSPECTION_CONTAINER_FREE(cnt) __PIIntrospection__containerFree(cnt);
#else
# define PIINTROSPECTION_CONTAINER_NEW()
# define PIINTROSPECTION_CONTAINER_DELETE()
# define PIINTROSPECTION_CONTAINER_USED(tcnt)
# define PIINTROSPECTION_CONTAINER_UNUSED(cnt)
# define PIINTROSPECTION_CONTAINER_ALLOC(cnt)
# define PIINTROSPECTION_CONTAINER_FREE(cnt)
#endif
#ifdef PIP_INTROSPECTION_THREADS
# define PIINTROSPECTION_REGISTER_THREAD(id, pr, name) __PIIntrospection__registerThread(id, pr, name);
# define PIINTROSPECTION_UNREGISTER_THREAD(id) __PIIntrospection__unregisterThread(id);
# define PIINTROSPECTION_START __PIIntrospection__start();
#else
# define PIINTROSPECTION_REGISTER_THREAD(id, pr, name)
# define PIINTROSPECTION_UNREGISTER_THREAD(id)
# define PIINTROSPECTION_START
#endif
#if defined(PIP_INTROSPECTION_CONTAINERS) || defined(PIP_INTROSPECTION_THREADS)
# define PIINTROSPECTION_START __PIIntrospection__start();
#else
# define PIINTROSPECTION_START
#endif
#endif // PIINTROSPECTION_PROXY_H

View File

@@ -20,7 +20,7 @@
#include "piincludes_p.h"
#include "pithread.h"
#include "pisystemtests.h"
#include "piintrospection_proxy.h"
#include "piintrospection_threads.h"
#include <signal.h>
#ifdef WINDOWS
# define __THREAD_FUNC_RET__ uint __stdcall
@@ -160,6 +160,7 @@ PRIVATE_DEFINITION_END(PIThread)
PIThread::PIThread(void * data, ThreadFunc func, bool startNow, int timer_delay): PIObject() {
PIINTROSPECTION_THREAD_NEW(this);
piMonitor.threads++;
tid_ = -1;
PRIVATE->thread = 0;
@@ -173,6 +174,7 @@ PIThread::PIThread(void * data, ThreadFunc func, bool startNow, int timer_delay)
PIThread::PIThread(bool startNow, int timer_delay): PIObject() {
PIINTROSPECTION_THREAD_NEW(this);
piMonitor.threads++;
tid_ = -1;
PRIVATE->thread = 0;
@@ -185,6 +187,7 @@ PIThread::PIThread(bool startNow, int timer_delay): PIObject() {
PIThread::~PIThread() {
PIINTROSPECTION_THREAD_DELETE(this);
piMonitor.threads--;
if (!running_ || PRIVATE->thread == 0) return;
#ifdef FREERTOS
@@ -220,91 +223,15 @@ void PIThread::stop(bool wait) {
bool PIThread::start(int timer_delay) {
if (running_) return false;
//if (terminating) waitForFinish();
terminating = false;
running_ = true;
delay_ = timer_delay;
#ifndef WINDOWS
pthread_attr_t attr;
pthread_attr_init(&attr);
# ifndef ANDROID
//pthread_attr_setschedparam(&attr, &(PRIVATE->sparam));
# endif
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
int ret = pthread_create(&PRIVATE->thread, &attr, thread_function, this);
//PICout(PICoutManipulators::DefaultControls) << "pthread_create" << PRIVATE->thread;
pthread_attr_destroy(&attr);
if (ret == 0) {
pthread_setname_np(PRIVATE->thread, ((PIString&)name().resize(15, '\0')).dataAscii());
# ifdef MAC_OS
pthread_threadid_np(PRIVATE->thread, (__uint64_t*)&tid_);
# else
# ifdef FREERTOS
tid_ = PRIVATE->thread;
# endif
# endif
#else
if (PRIVATE->thread != 0) CloseHandle(PRIVATE->thread);
# ifdef CC_GCC
PRIVATE->thread = (void *)_beginthreadex(0, 0, thread_function, this, 0, 0);
# else
PRIVATE->thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)thread_function, this, 0, 0);
# endif
if (PRIVATE->thread != 0) {
#endif
setPriority(priority_);
return true;
} else {
running_ = false;
PRIVATE->thread = 0;
piCoutObj << "Error: Can`t start new thread:" << errorString();
}
running_ = false;
return false;
return _startThread((void*)thread_function);
}
bool PIThread::startOnce() {
if (terminating) waitForFinish();
if (running_) return false;
terminating = false;
running_ = true;
#ifndef WINDOWS
pthread_attr_t attr;
pthread_attr_init(&attr);
# ifndef ANDROID
//pthread_attr_setschedparam(&attr, &(PRIVATE->sparam));
# endif
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
int ret = pthread_create(&(PRIVATE->thread), &attr, thread_function_once, this);
//PICout(PICoutManipulators::DefaultControls) << "pthread_create" << PRIVATE->thread;
pthread_attr_destroy(&attr);
if (ret == 0) {
pthread_setname_np(PRIVATE->thread, ((PIString&)name().resize(15, '\0')).dataAscii());
# ifdef MAC_OS
pthread_threadid_np(PRIVATE->thread, (__uint64_t*)&tid_);
# else
# ifdef FREERTOS
tid_ = PRIVATE->thread;
# endif
# endif
#else
if (PRIVATE->thread != 0) CloseHandle(PRIVATE->thread);
# ifdef CC_GCC
PRIVATE->thread = (void *)_beginthreadex(0, 0, thread_function_once, this, 0, 0);
# else
PRIVATE->thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)thread_function_once, this, 0, 0);
# endif
if (PRIVATE->thread != 0) {
#endif
setPriority(priority_);
return true;
} else {
running_ = false;
PRIVATE->thread = 0;
piCoutObj << "Error: Can`t start new thread:" << errorString();
}
running_ = false;
return false;
return _startThread((void*)thread_function_once);
}
@@ -317,7 +244,6 @@ void PIThread::terminate() {
#else
if (PRIVATE->thread == 0) return;
UNREGISTER_THREAD(this);
PIINTROSPECTION_UNREGISTER_THREAD(tid());
terminating = running_ = false;
tid_ = -1;
//PICout(PICoutManipulators::DefaultControls) << "terminate" << PRIVATE->thread;
@@ -337,6 +263,7 @@ void PIThread::terminate() {
PRIVATE->thread = 0;
end();
#endif
PIINTROSPECTION_THREAD_STOP(this);
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "terminate ok" << running_;
}
@@ -370,6 +297,50 @@ int PIThread::priority2System(PIThread::Priority p) {
}
bool PIThread::_startThread(void * func) {
//if (terminating) waitForFinish();
terminating = false;
running_ = true;
#ifndef WINDOWS
pthread_attr_t attr;
pthread_attr_init(&attr);
# ifndef ANDROID
//pthread_attr_setschedparam(&attr, &(PRIVATE->sparam));
# endif
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
int ret = pthread_create(&PRIVATE->thread, &attr, (void*(*)(void*))func, this);
//PICout(PICoutManipulators::DefaultControls) << "pthread_create" << PRIVATE->thread;
pthread_attr_destroy(&attr);
if (ret == 0) {
pthread_setname_np(PRIVATE->thread, ((PIString&)name().resize(15, '\0')).dataAscii());
# ifdef MAC_OS
pthread_threadid_np(PRIVATE->thread, (__uint64_t*)&tid_);
# else
# ifdef FREERTOS
tid_ = PRIVATE->thread;
# endif
# endif
#else
if (PRIVATE->thread) CloseHandle(PRIVATE->thread);
# ifdef CC_GCC
PRIVATE->thread = (void *)_beginthreadex(0, 0, (unsigned(__stdcall*)(void*))func, this, 0, 0);
# else
PRIVATE->thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)func, this, 0, 0);
# endif
if (PRIVATE->thread != 0) {
#endif
setPriority(priority_);
return true;
} else {
running_ = false;
PRIVATE->thread = 0;
piCoutObj << "Error: Can`t start new thread:" << errorString();
}
running_ = false;
return false;
}
void PIThread::setPriority(PIThread::Priority prior) {
#ifndef FREERTOS // FreeRTOS can't change priority runtime
priority_ = prior;
@@ -451,7 +422,7 @@ bool PIThread::waitForStart(int timeout_msecs) {
}
void PIThread::__thread_func__() {
void PIThread::_beginThread() {
#ifndef WINDOWS
# if !defined(ANDROID) && !defined(FREERTOS)
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
@@ -466,50 +437,35 @@ void PIThread::__thread_func__() {
#ifdef LINUX
tid_ = gettid();
#endif
PIINTROSPECTION_REGISTER_THREAD(tid(), priority(), name());
PIINTROSPECTION_THREAD_START(this);
REGISTER_THREAD(this);
running_ = true;
if (lockRun) mutex_.lock();
begin();
if (lockRun) mutex_.unlock();
started();
while (!terminating) {
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "queued" << "...";
maybeCallQueuedEvents();
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "queued" << "ok";
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "lock" << "...";
if (lockRun) mutex_.lock();
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "lock" << "ok";
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "run" << "...";
run();
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "run" << "ok";
//printf("thread %p tick\n", this);
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "ret_func" << "...";
if (ret_func != 0) ret_func(data_);
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "ret_func" << "ok";
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "unlock" << "...";
if (lockRun) mutex_.unlock();
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "unlock" << "ok";
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "wait" << "...";
if (delay_ > 0) {
tmr_.reset();
double sl(0.);
while (1) {
sl = piMind(delay_ - tmr_.elapsed_m(), PIP_MIN_MSLEEP);
#ifdef WINDOWS
/*if (sl <= 1. && sl >= 0.) {
piMSleep(PIP_MIN_MSLEEP);
continue;
}*/
#endif
//printf("%f %f %f\n", double(delay_), tmr_.elapsed_m(), sl);
if (terminating) break;
if (sl <= 0.) break;
piMSleep(sl);
}
}
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "wait" << "ok";
}
}
void PIThread::_runThread() {
PIINTROSPECTION_THREAD_RUN(this);
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "lock" << "...";
if (lockRun) mutex_.lock();
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "lock" << "ok";
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "run" << "...";
run();
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "run" << "ok";
//printf("thread %p tick\n", this);
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "ret_func" << "...";
if (ret_func != 0) ret_func(data_);
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "ret_func" << "ok";
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "unlock" << "...";
if (lockRun) mutex_.unlock();
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "unlock" << "ok";
}
void PIThread::_endThread() {
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "stop" << "...";
stopped();
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "stop" << "ok";
@@ -524,10 +480,10 @@ void PIThread::__thread_func__() {
//cout << "thread " << t << " exiting ... " << endl;
//PICout(PICoutManipulators::DefaultControls) << "pthread_exit" << (__privateinitializer__.p)->thread;
UNREGISTER_THREAD(this);
PIINTROSPECTION_UNREGISTER_THREAD(tid());
PIINTROSPECTION_THREAD_STOP(this);
#ifndef WINDOWS
pthread_detach((__privateinitializer__.p)->thread);
(__privateinitializer__.p)->thread = 0;
pthread_detach(PRIVATE->thread);
PRIVATE->thread = 0;
#endif
#ifndef WINDOWS
pthread_exit(0);
@@ -541,50 +497,34 @@ void PIThread::__thread_func__() {
}
void PIThread::__thread_func__() {
_beginThread();
while (!terminating) {
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "queued" << "...";
maybeCallQueuedEvents();
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "queued" << "ok";
_runThread();
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "wait" << "...";
PIINTROSPECTION_THREAD_WAIT(this);
if (delay_ > 0) {
tmr_.reset();
double sl(0.);
while (1) {
sl = piMind(delay_ - tmr_.elapsed_m(), PIP_MIN_MSLEEP);
if (terminating) break;
if (sl <= 0.) break;
piMSleep(sl);
}
}
//PICout(PICoutManipulators::DefaultControls) << "thread" << this << "wait" << "ok";
}
_endThread();
}
void PIThread::__thread_func_once__() {
#ifndef WINDOWS
# if !defined(ANDROID) && !defined(FREERTOS)
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
# endif
#else
//__PISetTimerResolution();
#endif
#ifdef WINDOWS
tid_ = GetCurrentThreadId();
#endif
#ifdef LINUX
tid_ = gettid();
#endif
PIINTROSPECTION_REGISTER_THREAD(tid(), priority(), name());
REGISTER_THREAD(this);
running_ = true;
begin();
started();
if (lockRun) mutex_.lock();
run();
if (ret_func != 0) ret_func(data_);
if (lockRun) mutex_.unlock();
stopped();
end();
terminating = running_ = false;
tid_ = -1;
//cout << "thread " << t << " exiting ... " << endl;
//PICout(PICoutManipulators::DefaultControls) << "pthread_exit" << (__privateinitializer__.p)->thread;
UNREGISTER_THREAD(this);
PIINTROSPECTION_UNREGISTER_THREAD(tid());
#ifndef WINDOWS
pthread_detach((__privateinitializer__.p)->thread);
(__privateinitializer__.p)->thread = 0;
#endif
#ifndef WINDOWS
pthread_exit(0);
#else
# ifdef CC_GCC
_endthreadex(0);
# else
ExitThread(0);
# endif
#endif
_beginThread();
_runThread();
_endThread();
}

View File

@@ -225,6 +225,11 @@ protected:
private:
NO_COPY_CLASS(PIThread)
bool _startThread(void * func);
void _beginThread();
void _runThread();
void _endThread();
};

View File

@@ -388,7 +388,7 @@ int main(int argc, char * argv[]) {
PIProcess::execIndependent(exe, args);
return 0;
}
PIINTROSPECTION_START
/// PIINTROSPECTION_START
//cli.addArgument("");
screen = new PIScreen(false);
screen->setMouseEnabled(true);