This repository has been archived by the owner on Oct 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththreads.cpp
118 lines (102 loc) · 3.36 KB
/
threads.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "stdafx.h"
#include "threads.h"
#include "main.h"
#include "debug.h"
#include "imessage.h"
#include "plugins.h"
namespace Konnekt {
cTLS<cUserThread> TLSU;
tThreads threads;
Stamina::CriticalSection threadsCS;
Stamina::Thread mainThread;
class __initializer {
public:
__initializer() {
threads[GetCurrentThreadId()] = mainThread.getHandle();
}
} _initializer;
uintptr_t konnektBeginThread (const char*name, void * sec, unsigned stack, cCtrl::fBeginThread cb, void * args, unsigned flag, unsigned * addr) {
return (uintptr_t)Ctrl->BeginThread(name, sec, stack, cb, args, flag, addr);
}
Stamina::oThreadRunner threadRunner = new Stamina::ThreadRunner(konnektBeginThread);
void sIMTS::enterMsg (sIMessage_base * _msg , unsigned int _plugID) {
Thread->error.code = 0;
inMessage++;msg=_msg; plugID=_plugID;
#if defined(__DEBUG)
debug.size = _msg->s_size;
debug.id = _msg->id;
debug.sender = _msg->sender;
debug.rcvr = _plugID;
if (debug.size >= sizeof(sIMessage_2params)) {
debug.p1 = (static_cast<sIMessage_2params*>(_msg))->p1;
debug.p2 = (static_cast<sIMessage_2params*>(_msg))->p2;
} else {debug.p1 = debug.p2 = 0;}
#endif
}
void sIMTS::leaveMsg() {
if (Thread->error.code && Thread->error.position != inMessage) {Thread->error.code = 0;}
inMessage--;
msg=0;
}
void sIMTS::set(struct sIMessage_base * _msg) {
msg = _msg;
}
// ************************************* THREAD SAFE
VOID CALLBACK IMTSRecaller(ULONG_PTR param) {
isWaiting = false;
sIMTS * imts = (sIMTS*)param;
if (!imts->msg) return;
// IMLOG("_ ThreadSafe Recaller plugID=0x%x msg_id=%d" , imts->plugID , imts->msg->id);
IMLOG("<T=%x" , imts->msg->id);
if (!imts->plugID) {
imts->retVal = IMCore(imts->msg);
TLSU().lastIM.leaveMsg();
} else {
imts->retVal = IMessageDirect(Plug[imts->plugID] , imts->msg);
}
if (imts->waitEvent) {
SetEvent(imts->waitEvent);
} else {free(imts->msg);delete imts;}
}
int RecallIMTS(sIMTS & _im , HANDLE thread , bool wait) {
if (!thread) thread = hMainThread;
sIMTS * imts = new sIMTS;
*imts = _im; // Robimy kopie imts'a
// IMLOG("_ Thread Safe Switch %s! %s" , wait?"":"NO Wait" , isWaiting?"W":"NW");
IMLOG(">T=%x %s" , imts->msg->id , wait?"":" NW");
imts->retVal = 0;
imts->waitEvent = 0;
if (wait) imts->waitEvent = CreateEvent(0 , 0 , 0 , 0);
else {
sIMessage_base * temp = static_cast<sIMessage_base*>(malloc(imts->msg->s_size)); // Robimy kopiê wiadomoœci
memcpy(temp , imts->msg , imts->msg->s_size);
imts->msg = temp;
}
QueueUserAPC(IMTSRecaller , thread , (ULONG_PTR)imts);
if (wait) {
while (WaitForSingleObjectEx(imts->waitEvent , INFINITE , 1)!=WAIT_OBJECT_0) {}
int r = imts->retVal;
CloseHandle(imts->waitEvent);
delete imts;
return r;
} else {
TLSU().setError(IMERROR_THREADSAFE);
return 0;
}
}
int userThreadCount = 0;
cUserThread::cUserThread() {
error.code=0;
error.position=0;
lastIM.Thread=this;
//int v = 0xFFFFFF - ((userThreadCount) * 0x80);
//v = max(0x505050, v);
//v = min(0xffffff, v);
color = getUniqueColor(userThreadCount, 5, 0x80, true);
OutputDebugString(_sprintf("TCOLOR = %x c=%d\n", color, userThreadCount));
userThreadCount++;
}
cUserThread::~cUserThread() {
userThreadCount --;
}
};