summaryrefslogtreecommitdiff
path: root/plugins/Modernb/modern_sync.cpp
blob: 1941b376f264522898935bfe14e6cabed78cab4b (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "hdr/modern_commonheaders.h"
#include "hdr/modern_sync.h"

typedef struct tagSYNCCALLITEM
{
	WPARAM  wParam;
	LPARAM  lParam;
	int     nResult;
	HANDLE  hDoneEvent;
	PSYNCCALLBACKPROC pfnProc;    
} SYNCCALLITEM;
static void CALLBACK _SyncCallerUserAPCProc(ULONG_PTR dwParam)
{
	SYNCCALLITEM* item = (SYNCCALLITEM*) dwParam;
	item->nResult = item->pfnProc(item->wParam, item->lParam);
	SetEvent(item->hDoneEvent);
}
static INT_PTR SyncCaller(WPARAM proc, LPARAM lParam)
{
	typedef int (*P0PARAMFUNC)();
	typedef int (*P1PARAMFUNC)(WPARAM);
	typedef int (*P2PARAMFUNC)(WPARAM, LPARAM);
	typedef int (*P3PARAMFUNC)(WPARAM, LPARAM, LPARAM);
	typedef int (*P4PARAMFUNC)(WPARAM, LPARAM, LPARAM, LPARAM);

	LPARAM * params=(LPARAM *)lParam;
	int count=params[0];
	switch (count)
	{
	case 0:			
		{
			P0PARAMFUNC pfnProc=(P0PARAMFUNC)proc;
			return pfnProc();
		}
	case 1:
		{
			P1PARAMFUNC pfnProc=(P1PARAMFUNC)proc;
			return pfnProc((WPARAM)params[1]);
		}
	case 2:
		{
			P2PARAMFUNC pfnProc=(P2PARAMFUNC)proc;
			return pfnProc((WPARAM)params[1],(LPARAM)params[2]);
		}
	case 3:
		{
			P3PARAMFUNC pfnProc=(P3PARAMFUNC)proc;
			return pfnProc((WPARAM)params[1],(LPARAM)params[2], (LPARAM)params[3]);
		}
	case 4:
		{
			P4PARAMFUNC pfnProc=(P4PARAMFUNC)proc;
			return pfnProc((WPARAM)params[1],(LPARAM)params[2], (LPARAM)params[3], (LPARAM)params[4]);
		}
	}
	return 0;
}


int SyncCall(void * vproc, int count, ... )
{
	LPARAM params[5];
	va_list va;
	int i;
	params[0]=(LPARAM)count;
	va_start(va, count);
	for (i=0; i<count && i<SIZEOF(params)-1; i++)
	{
		params[i+1]=va_arg(va,LPARAM);
	}
	va_end(va);
	return SyncCallProxy(SyncCaller, (WPARAM)vproc, (LPARAM) params);
}

int SyncCallProxy(PSYNCCALLBACKPROC pfnProc, WPARAM wParam, LPARAM lParam, CRITICAL_SECTION * cs /* = NULL */)
{  
	SYNCCALLITEM item={0};
	
	int nReturn = 0;

	if ( cs != NULL )
	{
		if ( !fnTryEnterCriticalSection ) // for poor OSes like Win98
		{
			EnterCriticalSection( cs );
			int result = pfnProc( wParam, lParam );
			LeaveCriticalSection( cs );
			return result;
		}

		if ( fnTryEnterCriticalSection( cs ))
		{   //simple call (Fastest)
			int result=pfnProc(wParam,lParam);
			LeaveCriticalSection( cs );
			return result;
		}
		else
		{	//Window SendMessage Call(Middle)
			if ( SyncCallWinProcProxy( pfnProc, wParam, lParam, nReturn ) == S_OK)
				return nReturn;
		}
	}
	if ( SyncCallAPCProxy( pfnProc, wParam, lParam, nReturn ) == S_OK)
		return nReturn;
	
	return NULL;
}

HRESULT SyncCallWinProcProxy( PSYNCCALLBACKPROC pfnProc, WPARAM wParam, LPARAM lParam, int& nReturn )
{
	nReturn = 0;
	if (pcli->hwndContactList == NULL )
		return E_FAIL;
	
	SYNCCALLITEM item={0};
	item.wParam = wParam;
	item.lParam = lParam;
	item.pfnProc = pfnProc;
	nReturn = SendMessage(pcli->hwndContactList, UM_SYNCCALL, (WPARAM)&item,0);
	
	return S_OK;
}

HRESULT SyncCallAPCProxy( PSYNCCALLBACKPROC pfnProc, WPARAM wParam, LPARAM lParam, int& hReturn )
{
	hReturn = 0;

	if (g_hMainThread==NULL || pfnProc==NULL) 
		return E_FAIL;

	SYNCCALLITEM item={0};

	if (GetCurrentThreadId()!=g_dwMainThreadID)
	{
		item.wParam = wParam;
		item.lParam = lParam;
		item.pfnProc = pfnProc;
		item.hDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

		QueueUserAPC(_SyncCallerUserAPCProc, g_hMainThread, (ULONG_PTR) &item);	
		PostMessage(pcli->hwndContactList,WM_NULL,0,0); // let this get processed in its own time
		
		WaitForSingleObject(item.hDoneEvent, INFINITE);
		CloseHandle(item.hDoneEvent);

		hReturn = item.nResult;

		return S_OK;
	}
	/* else */

	hReturn = pfnProc(wParam, lParam);
	return S_OK;
}





LRESULT SyncOnWndProcCall( WPARAM wParam )
{
	SYNCCALLITEM *psci=(SYNCCALLITEM *)wParam;
	if (psci)
		return psci->pfnProc( psci->wParam, psci->lParam );
	return 0;
}

int DoCall( PSYNCCALLBACKPROC pfnProc, WPARAM wParam, LPARAM lParam )
{
	return SyncCallProxy( pfnProc, 0, lParam );
}