2 * @file miranda-schedule.c
6 * Copyright (C) 2010-11 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "sipe-common.h"
29 #include "sipe-backend.h"
30 #include "sipe-core.h"
32 #include "newpluginapi.h"
34 #include "m_protosvc.h"
35 #include "m_protoint.h"
36 #include "miranda-private.h"
44 void (*callback
)(gpointer
);
46 /* Private. For locking only */
51 timeout_cb_async(void *data
)
53 struct time_entry
*entry
= (struct time_entry
*)data
;
54 SIPPROTO
*pr
= entry
->pr
;
56 if (entry
->cancelled
== TRUE
)
58 SIPE_DEBUG_INFO("Entry <%08x> already cancelled. Not calling timeout function", entry
);
60 SIPE_DEBUG_INFO("Calling timeout function for entry <%08x>", entry
);
62 entry
->callback(entry
->core_data
);
65 SetEvent(entry
->hDoneEvent
);
68 static unsigned __stdcall
timeoutfunc(void* data
)
70 struct time_entry
*entry
= (struct time_entry
*)data
;
72 SIPPROTO
*pr
= entry
->pr
;
74 SIPE_DEBUG_INFO("timeout start; <%08x> timeout is <%d>", entry
, entry
->timeout
);
76 entry
->sem
= CreateSemaphore(NULL
, 0, 100, NULL
);
78 ret
= WaitForSingleObjectEx( entry
->sem
, entry
->timeout
, FALSE
);
79 if (entry
->cancelled
== TRUE
)
81 SIPE_DEBUG_INFO("<%08x> Timeout cancelled by caller", entry
);
83 else if (ret
== WAIT_TIMEOUT
)
85 SIPE_DEBUG_INFO("<%08x> about to run", entry
);
86 if (entry
->cancelled
== TRUE
)
88 SIPE_DEBUG_INFO("<%08x> Timeout cancelled by caller in the nick of time", entry
);
92 entry
->hDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
93 CallFunctionAsync(timeout_cb_async
, entry
);
94 WaitForSingleObject(entry
->hDoneEvent
, INFINITE
);
95 CloseHandle(entry
->hDoneEvent
);
97 SIPE_DEBUG_INFO("<%08x> exiting", entry
);
101 SIPE_DEBUG_INFO("<%08x> Something unexpected happened: <%d>", entry
, ret
);
104 CloseHandle(entry
->sem
);
110 gpointer
sipe_miranda_schedule_mseconds(void (*callback
)(gpointer
),
114 struct time_entry
*entry
;
116 entry
= g_new0(struct time_entry
,1);
117 entry
->timeout
= timeout
;
118 entry
->core_data
= data
;
119 entry
->cancelled
= FALSE
;
120 entry
->pr
= data
; /* FIXME: Assumes data = SIPPROTO * */
121 entry
->callback
= callback
;
123 SIPE_DEBUG_INFO("Scheduling timeout in <%u>ms for entry <%08x>", timeout
, entry
);
124 CloseHandle((HANDLE
) mir_forkthreadex( timeoutfunc
, entry
, 65536, NULL
));
129 gpointer
sipe_backend_schedule_mseconds(SIPE_UNUSED_PARAMETER
struct sipe_core_public
*sipe_public
,
133 struct time_entry
*entry
;
135 entry
= g_new0(struct time_entry
,1);
136 entry
->timeout
= timeout
;
137 entry
->core_data
= data
;
138 entry
->cancelled
= FALSE
;
139 entry
->pr
= sipe_public
->backend_private
;
140 entry
->callback
= sipe_core_schedule_execute
;
142 CloseHandle((HANDLE
) mir_forkthreadex( timeoutfunc
, entry
, 65536, NULL
));
147 gpointer
sipe_backend_schedule_seconds(SIPE_UNUSED_PARAMETER
struct sipe_core_public
*sipe_public
,
151 return sipe_backend_schedule_mseconds( sipe_public
, timeout
*1000, data
);
154 void sipe_backend_schedule_cancel(SIPE_UNUSED_PARAMETER
struct sipe_core_public
*sipe_public
,
157 struct time_entry
*entry
= (struct time_entry
*) data
;
159 if (entry
&& entry
->sem
)
161 SIPE_DEBUG_INFO("Cancelling timeout <%08x>", entry
);
162 entry
->cancelled
= TRUE
;
163 ReleaseSemaphore(entry
->sem
, 1, NULL
);