gphoto2.ds: Set supported groups.
[wine.git] / dlls / msvcp90 / misc.c
blob901930ba96d823810de6d62c9afc71bbdb20c2c2
1 /*
2 * Copyright 2010 Piotr Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
21 #include <stdarg.h>
22 #include <limits.h>
24 #include "msvcp90.h"
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winternl.h"
29 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcp);
32 struct __Container_proxy;
34 typedef struct {
35 struct __Container_proxy *proxy;
36 } _Container_base12;
38 typedef struct __Iterator_base12 {
39 struct __Container_proxy *proxy;
40 struct __Iterator_base12 *next;
41 } _Iterator_base12;
43 typedef struct __Container_proxy {
44 const _Container_base12 *cont;
45 _Iterator_base12 *head;
46 } _Container_proxy;
48 /* ??0_Mutex@std@@QAE@XZ */
49 /* ??0_Mutex@std@@QEAA@XZ */
50 DEFINE_THISCALL_WRAPPER(mutex_ctor, 4)
51 mutex* __thiscall mutex_ctor(mutex *this)
53 CRITICAL_SECTION *cs = MSVCRT_operator_new(sizeof(*cs));
54 if(!cs) {
55 ERR("Out of memory\n");
56 throw_exception(EXCEPTION_BAD_ALLOC, NULL);
59 InitializeCriticalSection(cs);
60 cs->DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": _Mutex critical section");
61 this->mutex = cs;
62 return this;
65 /* ??1_Mutex@std@@QAE@XZ */
66 /* ??1_Mutex@std@@QEAA@XZ */
67 DEFINE_THISCALL_WRAPPER(mutex_dtor, 4)
68 void __thiscall mutex_dtor(mutex *this)
70 ((CRITICAL_SECTION*)this->mutex)->DebugInfo->Spare[0] = 0;
71 DeleteCriticalSection(this->mutex);
72 MSVCRT_operator_delete(this->mutex);
75 /* ?_Lock@_Mutex@std@@QAEXXZ */
76 /* ?_Lock@_Mutex@std@@QEAAXXZ */
77 DEFINE_THISCALL_WRAPPER(mutex_lock, 4)
78 void __thiscall mutex_lock(mutex *this)
80 EnterCriticalSection(this->mutex);
83 /* ?_Unlock@_Mutex@std@@QAEXXZ */
84 /* ?_Unlock@_Mutex@std@@QEAAXXZ */
85 DEFINE_THISCALL_WRAPPER(mutex_unlock, 4)
86 void __thiscall mutex_unlock(mutex *this)
88 LeaveCriticalSection(this->mutex);
91 /* ?_Mutex_Lock@_Mutex@std@@CAXPAV12@@Z */
92 /* ?_Mutex_Lock@_Mutex@std@@CAXPEAV12@@Z */
93 void CDECL mutex_mutex_lock(mutex *m)
95 mutex_lock(m);
98 /* ?_Mutex_Unlock@_Mutex@std@@CAXPAV12@@Z */
99 /* ?_Mutex_Unlock@_Mutex@std@@CAXPEAV12@@Z */
100 void CDECL mutex_mutex_unlock(mutex *m)
102 mutex_unlock(m);
105 /* ?_Mutex_ctor@_Mutex@std@@CAXPAV12@@Z */
106 /* ?_Mutex_ctor@_Mutex@std@@CAXPEAV12@@Z */
107 void CDECL mutex_mutex_ctor(mutex *m)
109 mutex_ctor(m);
112 /* ?_Mutex_dtor@_Mutex@std@@CAXPAV12@@Z */
113 /* ?_Mutex_dtor@_Mutex@std@@CAXPEAV12@@Z */
114 void CDECL mutex_mutex_dtor(mutex *m)
116 mutex_dtor(m);
119 static CRITICAL_SECTION lockit_cs[_MAX_LOCK];
121 #if _MSVCP_VER >= 70
122 static inline int get_locktype( _Lockit *lockit ) { return lockit->locktype; }
123 static inline void set_locktype( _Lockit *lockit, int type ) { lockit->locktype = type; }
124 #else
125 static inline int get_locktype( _Lockit *lockit ) { return 0; }
126 static inline void set_locktype( _Lockit *lockit, int type ) { }
127 #endif
129 /* ?_Lockit_ctor@_Lockit@std@@SAXH@Z */
130 void __cdecl _Lockit_init(int locktype) {
131 InitializeCriticalSection(&lockit_cs[locktype]);
132 lockit_cs[locktype].DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": _Lockit critical section");
135 /* ?_Lockit_dtor@_Lockit@std@@SAXH@Z */
136 void __cdecl _Lockit_free(int locktype)
138 lockit_cs[locktype].DebugInfo->Spare[0] = 0;
139 DeleteCriticalSection(&lockit_cs[locktype]);
142 void init_lockit(void) {
143 int i;
145 for(i=0; i<_MAX_LOCK; i++)
146 _Lockit_init(i);
149 void free_lockit(void) {
150 int i;
152 for(i=0; i<_MAX_LOCK; i++)
153 _Lockit_free(i);
156 /* ?_Lockit_ctor@_Lockit@std@@CAXPAV12@H@Z */
157 /* ?_Lockit_ctor@_Lockit@std@@CAXPEAV12@H@Z */
158 void __cdecl _Lockit__Lockit_ctor_locktype(_Lockit *lockit, int locktype)
160 set_locktype( lockit, locktype );
161 EnterCriticalSection(&lockit_cs[locktype]);
164 /* ?_Lockit_ctor@_Lockit@std@@CAXPAV12@@Z */
165 /* ?_Lockit_ctor@_Lockit@std@@CAXPEAV12@@Z */
166 void __cdecl _Lockit__Lockit_ctor(_Lockit *lockit)
168 _Lockit__Lockit_ctor_locktype(lockit, 0);
171 /* ??0_Lockit@std@@QAE@H@Z */
172 /* ??0_Lockit@std@@QEAA@H@Z */
173 DEFINE_THISCALL_WRAPPER(_Lockit_ctor_locktype, 8)
174 _Lockit* __thiscall _Lockit_ctor_locktype(_Lockit *this, int locktype)
176 _Lockit__Lockit_ctor_locktype(this, locktype);
177 return this;
180 /* ??0_Lockit@std@@QAE@XZ */
181 /* ??0_Lockit@std@@QEAA@XZ */
182 DEFINE_THISCALL_WRAPPER(_Lockit_ctor, 4)
183 _Lockit* __thiscall _Lockit_ctor(_Lockit *this)
185 _Lockit__Lockit_ctor_locktype(this, 0);
186 return this;
189 /* ?_Lockit_dtor@_Lockit@std@@CAXPAV12@@Z */
190 /* ?_Lockit_dtor@_Lockit@std@@CAXPEAV12@@Z */
191 void __cdecl _Lockit__Lockit_dtor(_Lockit *lockit)
193 LeaveCriticalSection(&lockit_cs[get_locktype( lockit )]);
196 /* ??1_Lockit@std@@QAE@XZ */
197 /* ??1_Lockit@std@@QEAA@XZ */
198 DEFINE_THISCALL_WRAPPER(_Lockit_dtor, 4)
199 void __thiscall _Lockit_dtor(_Lockit *this)
201 _Lockit__Lockit_dtor(this);
204 /* wctype */
205 unsigned short __cdecl wctype(const char *property)
207 static const struct {
208 const char *name;
209 unsigned short mask;
210 } properties[] = {
211 { "alnum", _DIGIT|_ALPHA },
212 { "alpha", _ALPHA },
213 { "cntrl", _CONTROL },
214 { "digit", _DIGIT },
215 { "graph", _DIGIT|_PUNCT|_ALPHA },
216 { "lower", _LOWER },
217 { "print", _DIGIT|_PUNCT|_BLANK|_ALPHA },
218 { "punct", _PUNCT },
219 { "space", _SPACE },
220 { "upper", _UPPER },
221 { "xdigit", _HEX }
223 unsigned int i;
225 for(i=0; i<sizeof(properties)/sizeof(properties[0]); i++)
226 if(!strcmp(property, properties[i].name))
227 return properties[i].mask;
229 return 0;
232 typedef void (__cdecl *MSVCP_new_handler_func)(void);
233 static MSVCP_new_handler_func MSVCP_new_handler;
234 static int __cdecl new_handler_wrapper(MSVCP_size_t unused)
236 MSVCP_new_handler();
237 return 1;
240 /* ?set_new_handler@std@@YAP6AXXZP6AXXZ@Z */
241 MSVCP_new_handler_func __cdecl set_new_handler(MSVCP_new_handler_func new_handler)
243 MSVCP_new_handler_func old_handler = MSVCP_new_handler;
245 TRACE("%p\n", new_handler);
247 MSVCP_new_handler = new_handler;
248 MSVCRT_set_new_handler(new_handler ? new_handler_wrapper : NULL);
249 return old_handler;
252 /* ?set_new_handler@std@@YAP6AXXZH@Z */
253 MSVCP_new_handler_func __cdecl set_new_handler_reset(int unused)
255 return set_new_handler(NULL);
258 /* _Container_base0 is used by apps compiled without iterator checking
259 * (i.e. with _ITERATOR_DEBUG_LEVEL=0 ).
260 * It provides empty versions of methods used by visual c++'s stl's
261 * iterator checking.
262 * msvcr100 has to provide them in case apps are compiled with /Od
263 * or the optimizer fails to inline those (empty) calls.
266 /* ?_Orphan_all@_Container_base0@std@@QAEXXZ */
267 /* ?_Orphan_all@_Container_base0@std@@QEAAXXZ */
268 DEFINE_THISCALL_WRAPPER(Container_base0_Orphan_all, 4)
269 void __thiscall Container_base0_Orphan_all(void *this)
273 /* ?_Swap_all@_Container_base0@std@@QAEXAAU12@@Z */
274 /* ?_Swap_all@_Container_base0@std@@QEAAXAEAU12@@Z */
275 DEFINE_THISCALL_WRAPPER(Container_base0_Swap_all, 8)
276 void __thiscall Container_base0_Swap_all(void *this, void *that)
280 /* ??4_Container_base0@std@@QAEAAU01@ABU01@@Z */
281 /* ??4_Container_base0@std@@QEAAAEAU01@AEBU01@@Z */
282 DEFINE_THISCALL_WRAPPER(Container_base0_op_assign, 8)
283 void* __thiscall Container_base0_op_assign(void *this, const void *that)
285 return this;
288 /* ??0_Container_base12@std@@QAE@ABU01@@Z */
289 /* ??0_Container_base12@std@@QEAA@AEBU01@@Z */
290 DEFINE_THISCALL_WRAPPER(_Container_base12_copy_ctor, 8)
291 _Container_base12* __thiscall _Container_base12_copy_ctor(
292 _Container_base12 *this, _Container_base12 *that)
294 this->proxy = NULL;
295 return this;
298 /* ??0_Container_base12@std@@QAE@XZ */
299 /* ??0_Container_base12@std@@QEAA@XZ */
300 DEFINE_THISCALL_WRAPPER(_Container_base12_ctor, 4)
301 _Container_base12* __thiscall _Container_base12_ctor(_Container_base12 *this)
303 this->proxy = NULL;
304 return this;
307 /* ??1_Container_base12@std@@QAE@XZ */
308 /* ??1_Container_base12@std@@QEAA@XZ */
309 DEFINE_THISCALL_WRAPPER(_Container_base12_dtor, 4)
310 void __thiscall _Container_base12_dtor(_Container_base12 *this)
314 /* ??4_Container_base12@std@@QAEAAU01@ABU01@@Z */
315 /* ??4_Container_base12@std@@QEAAAEAU01@AEBU01@@ */
316 DEFINE_THISCALL_WRAPPER(_Container_base12_op_assign, 8)
317 _Container_base12* __thiscall _Container_base12_op_assign(
318 _Container_base12 *this, const _Container_base12 *that)
320 return this;
323 /* ?_Getpfirst@_Container_base12@std@@QBEPAPAU_Iterator_base12@2@XZ */
324 /* ?_Getpfirst@_Container_base12@std@@QEBAPEAPEAU_Iterator_base12@2@XZ */
325 DEFINE_THISCALL_WRAPPER(_Container_base12__Getpfirst, 4)
326 _Iterator_base12** __thiscall _Container_base12__Getpfirst(_Container_base12 *this)
328 return this->proxy ? &this->proxy->head : NULL;
331 /* ?_Orphan_all@_Container_base12@std@@QAEXXZ */
332 /* ?_Orphan_all@_Container_base12@std@@QEAAXXZ */
333 DEFINE_THISCALL_WRAPPER(_Container_base12__Orphan_all, 4)
334 void __thiscall _Container_base12__Orphan_all(_Container_base12 *this)
338 /* ?_Swap_all@_Container_base12@std@@QAEXAAU12@@Z */
339 /* ?_Swap_all@_Container_base12@std@@QEAAXAEAU12@@Z */
340 DEFINE_THISCALL_WRAPPER(_Container_base12__Swap_all, 8)
341 void __thiscall _Container_base12__Swap_all(
342 _Container_base12 *this, _Container_base12 *that)
344 _Container_proxy *tmp;
346 tmp = this->proxy;
347 this->proxy = that->proxy;
348 that->proxy = tmp;
350 if(this->proxy)
351 this->proxy->cont = this;
352 if(that->proxy)
353 that->proxy->cont = that;
356 #if _MSVCP_VER >= 110
358 #define SECSPERDAY 86400
359 /* 1601 to 1970 is 369 years plus 89 leap days */
360 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
361 #define TICKSPERSEC 10000000
362 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
363 #define NANOSEC_PER_MILLISEC 1000000
364 #define MILLISEC_PER_SEC 1000
365 #define NANOSEC_PER_SEC (NANOSEC_PER_MILLISEC * MILLISEC_PER_SEC)
367 typedef int MSVCRT_long;
369 /* xtime */
370 typedef struct {
371 __time64_t sec;
372 MSVCRT_long nsec;
373 } xtime;
375 /* _Xtime_get_ticks */
376 LONGLONG __cdecl _Xtime_get_ticks(void)
378 FILETIME ft;
380 TRACE("\n");
382 GetSystemTimeAsFileTime(&ft);
383 return ((LONGLONG)ft.dwHighDateTime<<32) + ft.dwLowDateTime - TICKS_1601_TO_1970;
386 /* _xtime_get */
387 int __cdecl xtime_get(xtime* t, int unknown)
389 LONGLONG ticks;
391 TRACE("(%p)\n", t);
393 if(unknown != 1)
394 return 0;
396 ticks = _Xtime_get_ticks();
397 t->sec = ticks / TICKSPERSEC;
398 t->nsec = ticks % TICKSPERSEC * 100;
399 return 1;
402 /* _Xtime_diff_to_millis2 */
403 MSVCRT_long __cdecl _Xtime_diff_to_millis2(const xtime *t1, const xtime *t2)
405 LONGLONG diff_sec, diff_nsec;
407 TRACE("(%p, %p)\n", t1, t2);
409 diff_sec = t1->sec - t2->sec;
410 diff_nsec = t1->nsec - t2->nsec;
412 diff_sec += diff_nsec / NANOSEC_PER_SEC;
413 diff_nsec %= NANOSEC_PER_SEC;
414 if (diff_nsec < 0) {
415 diff_sec -= 1;
416 diff_nsec += NANOSEC_PER_SEC;
419 if (diff_sec<0 || (diff_sec==0 && diff_nsec<0))
420 return 0;
421 return diff_sec * MILLISEC_PER_SEC +
422 (diff_nsec + NANOSEC_PER_MILLISEC - 1) / NANOSEC_PER_MILLISEC;
425 /* _Xtime_diff_to_millis */
426 MSVCRT_long __cdecl _Xtime_diff_to_millis(const xtime *t)
428 xtime now;
430 TRACE("%p\n", t);
432 xtime_get(&now, 1);
433 return _Xtime_diff_to_millis2(t, &now);
435 #endif
437 #if _MSVCP_VER >= 90
438 unsigned int __cdecl _Random_device(void)
440 unsigned int ret;
442 TRACE("\n");
444 /* TODO: throw correct exception in case of failure */
445 if(rand_s(&ret))
446 throw_exception(EXCEPTION, "random number generator failed\n");
447 return ret;
449 #endif
451 #if _MSVCP_VER >= 110
452 #if defined(__i386__) && !defined(__arm__)
454 extern void *call_thiscall_func;
455 __ASM_GLOBAL_FUNC(call_thiscall_func,
456 "popl %eax\n\t"
457 "popl %edx\n\t"
458 "popl %ecx\n\t"
459 "pushl %eax\n\t"
460 "jmp *%edx\n\t")
462 #define call_func1(func,this) ((void* (WINAPI*)(void*,void*))&call_thiscall_func)(func,this)
464 #else /* __i386__ */
466 #define call_func1(func,this) func(this)
468 #endif /* __i386__ */
470 #define MTX_MULTI_LOCK 0x100
471 #define MTX_LOCKED 3
472 typedef struct
474 DWORD flags;
475 critical_section cs;
476 DWORD thread_id;
477 DWORD count;
478 } *_Mtx_t;
480 #if _MSVCP_VER >= 140
481 typedef _Mtx_t _Mtx_arg_t;
482 #define MTX_T_FROM_ARG(m) (m)
483 #define MTX_T_TO_ARG(m) (m)
484 #else
485 typedef _Mtx_t *_Mtx_arg_t;
486 #define MTX_T_FROM_ARG(m) (*(m))
487 #define MTX_T_TO_ARG(m) (&(m))
488 #endif
490 void __cdecl _Mtx_init_in_situ(_Mtx_t mtx, int flags)
492 if(flags & ~MTX_MULTI_LOCK)
493 FIXME("unknown flags ignored: %x\n", flags);
495 mtx->flags = flags;
496 call_func1(critical_section_ctor, &mtx->cs);
497 mtx->thread_id = -1;
498 mtx->count = 0;
501 int __cdecl _Mtx_init(_Mtx_t *mtx, int flags)
503 *mtx = MSVCRT_operator_new(sizeof(**mtx));
504 _Mtx_init_in_situ(*mtx, flags);
505 return 0;
508 void __cdecl _Mtx_destroy_in_situ(_Mtx_t mtx)
510 call_func1(critical_section_dtor, &mtx->cs);
513 void __cdecl _Mtx_destroy(_Mtx_arg_t mtx)
515 call_func1(critical_section_dtor, &MTX_T_FROM_ARG(mtx)->cs);
516 MSVCRT_operator_delete(MTX_T_FROM_ARG(mtx));
519 int __cdecl _Mtx_current_owns(_Mtx_arg_t mtx)
521 return MTX_T_FROM_ARG(mtx)->thread_id == GetCurrentThreadId();
524 int __cdecl _Mtx_lock(_Mtx_arg_t mtx)
526 if(MTX_T_FROM_ARG(mtx)->thread_id != GetCurrentThreadId()) {
527 call_func1(critical_section_lock, &MTX_T_FROM_ARG(mtx)->cs);
528 MTX_T_FROM_ARG(mtx)->thread_id = GetCurrentThreadId();
529 }else if(!(MTX_T_FROM_ARG(mtx)->flags & MTX_MULTI_LOCK)) {
530 return MTX_LOCKED;
533 MTX_T_FROM_ARG(mtx)->count++;
534 return 0;
537 int __cdecl _Mtx_unlock(_Mtx_arg_t mtx)
539 if(--MTX_T_FROM_ARG(mtx)->count)
540 return 0;
542 MTX_T_FROM_ARG(mtx)->thread_id = -1;
543 call_func1(critical_section_unlock, &MTX_T_FROM_ARG(mtx)->cs);
544 return 0;
547 int __cdecl _Mtx_trylock(_Mtx_arg_t mtx)
549 if(MTX_T_FROM_ARG(mtx)->thread_id != GetCurrentThreadId()) {
550 if(!call_func1(critical_section_trylock, &MTX_T_FROM_ARG(mtx)->cs))
551 return MTX_LOCKED;
552 MTX_T_FROM_ARG(mtx)->thread_id = GetCurrentThreadId();
553 }else if(!(MTX_T_FROM_ARG(mtx)->flags & MTX_MULTI_LOCK)) {
554 return MTX_LOCKED;
557 MTX_T_FROM_ARG(mtx)->count++;
558 return 0;
561 critical_section* __cdecl _Mtx_getconcrtcs(_Mtx_arg_t mtx)
563 return &MTX_T_FROM_ARG(mtx)->cs;
566 static inline LONG interlocked_dec_if_nonzero( LONG *dest )
568 LONG val, tmp;
569 for (val = *dest;; val = tmp)
571 if (!val || (tmp = InterlockedCompareExchange( dest, val - 1, val )) == val)
572 break;
574 return val;
577 #define CND_TIMEDOUT 2
579 typedef struct
581 CONDITION_VARIABLE cv;
582 } *_Cnd_t;
584 #if _MSVCP_VER >= 140
585 typedef _Cnd_t _Cnd_arg_t;
586 #define CND_T_FROM_ARG(c) (c)
587 #define CND_T_TO_ARG(c) (c)
588 #else
589 typedef _Cnd_t *_Cnd_arg_t;
590 #define CND_T_FROM_ARG(c) (*(c))
591 #define CND_T_TO_ARG(c) (&(c))
592 #endif
594 static HANDLE keyed_event;
596 void __cdecl _Cnd_init_in_situ(_Cnd_t cnd)
598 InitializeConditionVariable(&cnd->cv);
600 if(!keyed_event) {
601 HANDLE event;
603 NtCreateKeyedEvent(&event, GENERIC_READ|GENERIC_WRITE, NULL, 0);
604 if(InterlockedCompareExchangePointer(&keyed_event, event, NULL) != NULL)
605 NtClose(event);
609 int __cdecl _Cnd_init(_Cnd_t *cnd)
611 *cnd = MSVCRT_operator_new(sizeof(**cnd));
612 _Cnd_init_in_situ(*cnd);
613 return 0;
616 int __cdecl _Cnd_wait(_Cnd_arg_t cnd, _Mtx_arg_t mtx)
618 CONDITION_VARIABLE *cv = &CND_T_FROM_ARG(cnd)->cv;
620 InterlockedExchangeAdd( (LONG *)&cv->Ptr, 1 );
621 _Mtx_unlock(mtx);
623 NtWaitForKeyedEvent(keyed_event, &cv->Ptr, FALSE, NULL);
625 _Mtx_lock(mtx);
626 return 0;
629 int __cdecl _Cnd_timedwait(_Cnd_arg_t cnd, _Mtx_arg_t mtx, const xtime *xt)
631 CONDITION_VARIABLE *cv = &CND_T_FROM_ARG(cnd)->cv;
632 LARGE_INTEGER timeout;
633 NTSTATUS status;
635 InterlockedExchangeAdd( (LONG *)&cv->Ptr, 1 );
636 _Mtx_unlock(mtx);
638 timeout.QuadPart = (ULONGLONG)_Xtime_diff_to_millis(xt) * -10000;
639 status = NtWaitForKeyedEvent(keyed_event, &cv->Ptr, FALSE, &timeout);
640 if (status)
642 if (!interlocked_dec_if_nonzero( (LONG *)&cv->Ptr ))
643 status = NtWaitForKeyedEvent( keyed_event, &cv->Ptr, FALSE, NULL );
646 _Mtx_lock(mtx);
647 return status ? CND_TIMEDOUT : 0;
650 int __cdecl _Cnd_broadcast(_Cnd_arg_t cnd)
652 CONDITION_VARIABLE *cv = &CND_T_FROM_ARG(cnd)->cv;
653 LONG val = InterlockedExchange( (LONG *)&cv->Ptr, 0 );
654 while (val-- > 0)
655 NtReleaseKeyedEvent( keyed_event, &cv->Ptr, FALSE, NULL );
656 return 0;
659 int __cdecl _Cnd_signal(_Cnd_arg_t cnd)
661 CONDITION_VARIABLE *cv = &CND_T_FROM_ARG(cnd)->cv;
662 if (interlocked_dec_if_nonzero( (LONG *)&cv->Ptr ))
663 NtReleaseKeyedEvent( keyed_event, &cv->Ptr, FALSE, NULL );
664 return 0;
667 void __cdecl _Cnd_destroy_in_situ(_Cnd_t cnd)
669 _Cnd_broadcast(CND_T_TO_ARG(cnd));
672 void __cdecl _Cnd_destroy(_Cnd_arg_t cnd)
674 if(cnd) {
675 _Cnd_broadcast(cnd);
676 MSVCRT_operator_delete(CND_T_FROM_ARG(cnd));
680 static struct {
681 int used;
682 int size;
684 struct _to_broadcast {
685 DWORD thread_id;
686 _Cnd_arg_t cnd;
687 _Mtx_arg_t mtx;
688 int *p;
689 } *to_broadcast;
690 } broadcast_at_thread_exit;
692 static CRITICAL_SECTION broadcast_at_thread_exit_cs;
693 static CRITICAL_SECTION_DEBUG broadcast_at_thread_exit_cs_debug =
695 0, 0, &broadcast_at_thread_exit_cs,
696 { &broadcast_at_thread_exit_cs_debug.ProcessLocksList, &broadcast_at_thread_exit_cs_debug.ProcessLocksList },
697 0, 0, { (DWORD_PTR)(__FILE__ ": broadcast_at_thread_exit_cs") }
699 static CRITICAL_SECTION broadcast_at_thread_exit_cs = { &broadcast_at_thread_exit_cs_debug, -1, 0, 0, 0, 0 };
701 void __cdecl _Cnd_register_at_thread_exit(_Cnd_arg_t cnd, _Mtx_arg_t mtx, int *p)
703 struct _to_broadcast *add;
705 TRACE("(%p %p %p)\n", cnd, mtx, p);
707 EnterCriticalSection(&broadcast_at_thread_exit_cs);
708 if(!broadcast_at_thread_exit.size) {
709 broadcast_at_thread_exit.to_broadcast = HeapAlloc(GetProcessHeap(),
710 0, 8*sizeof(broadcast_at_thread_exit.to_broadcast[0]));
711 if(!broadcast_at_thread_exit.to_broadcast) {
712 LeaveCriticalSection(&broadcast_at_thread_exit_cs);
713 return;
715 broadcast_at_thread_exit.size = 8;
716 } else if(broadcast_at_thread_exit.size == broadcast_at_thread_exit.used) {
717 add = HeapReAlloc(GetProcessHeap(), 0, broadcast_at_thread_exit.to_broadcast,
718 broadcast_at_thread_exit.size*2*sizeof(broadcast_at_thread_exit.to_broadcast[0]));
719 if(!add) {
720 LeaveCriticalSection(&broadcast_at_thread_exit_cs);
721 return;
723 broadcast_at_thread_exit.to_broadcast = add;
724 broadcast_at_thread_exit.size *= 2;
727 add = broadcast_at_thread_exit.to_broadcast + broadcast_at_thread_exit.used++;
728 add->thread_id = GetCurrentThreadId();
729 add->cnd = cnd;
730 add->mtx = mtx;
731 add->p = p;
732 LeaveCriticalSection(&broadcast_at_thread_exit_cs);
735 void __cdecl _Cnd_unregister_at_thread_exit(_Mtx_arg_t mtx)
737 int i;
739 TRACE("(%p)\n", mtx);
741 EnterCriticalSection(&broadcast_at_thread_exit_cs);
742 for(i=0; i<broadcast_at_thread_exit.used; i++) {
743 if(broadcast_at_thread_exit.to_broadcast[i].mtx != mtx)
744 continue;
746 memmove(broadcast_at_thread_exit.to_broadcast+i, broadcast_at_thread_exit.to_broadcast+i+1,
747 (broadcast_at_thread_exit.used-i-1)*sizeof(broadcast_at_thread_exit.to_broadcast[0]));
748 broadcast_at_thread_exit.used--;
749 i--;
751 LeaveCriticalSection(&broadcast_at_thread_exit_cs);
754 void __cdecl _Cnd_do_broadcast_at_thread_exit(void)
756 int i, id = GetCurrentThreadId();
758 TRACE("()\n");
760 EnterCriticalSection(&broadcast_at_thread_exit_cs);
761 for(i=0; i<broadcast_at_thread_exit.used; i++) {
762 if(broadcast_at_thread_exit.to_broadcast[i].thread_id != id)
763 continue;
765 _Mtx_unlock(broadcast_at_thread_exit.to_broadcast[i].mtx);
766 _Cnd_broadcast(broadcast_at_thread_exit.to_broadcast[i].cnd);
767 if(broadcast_at_thread_exit.to_broadcast[i].p)
768 *broadcast_at_thread_exit.to_broadcast[i].p = 1;
770 memmove(broadcast_at_thread_exit.to_broadcast+i, broadcast_at_thread_exit.to_broadcast+i+1,
771 (broadcast_at_thread_exit.used-i-1)*sizeof(broadcast_at_thread_exit.to_broadcast[0]));
772 broadcast_at_thread_exit.used--;
773 i--;
775 LeaveCriticalSection(&broadcast_at_thread_exit_cs);
778 #endif
780 #if _MSVCP_VER == 100
781 typedef struct {
782 const vtable_ptr *vtable;
783 } error_category;
785 typedef struct {
786 error_category base;
787 const char *type;
788 } custom_category;
789 static custom_category iostream_category;
791 DEFINE_RTTI_DATA0(error_category, 0, ".?AVerror_category@std@@")
792 DEFINE_RTTI_DATA1(iostream_category, 0, &error_category_rtti_base_descriptor, ".?AV_Iostream_error_category@std@@")
794 extern const vtable_ptr MSVCP_iostream_category_vtable;
796 static void iostream_category_ctor(custom_category *this)
798 this->base.vtable = &MSVCP_iostream_category_vtable;
799 this->type = "iostream";
802 DEFINE_THISCALL_WRAPPER(custom_category_vector_dtor, 8)
803 custom_category* __thiscall custom_category_vector_dtor(custom_category *this, unsigned int flags)
805 TRACE("(%p %x)\n", this, flags);
806 if(flags & 2) {
807 /* we have an array, with the number of elements stored before the first object */
808 INT_PTR i, *ptr = (INT_PTR *)this-1;
810 for(i=*ptr-1; i>=0; i--)
811 MSVCRT_operator_delete(ptr);
812 } else {
813 if(flags & 1)
814 MSVCRT_operator_delete(this);
817 return this;
820 DEFINE_THISCALL_WRAPPER(custom_category_name, 4)
821 const char* __thiscall custom_category_name(const custom_category *this)
823 return this->type;
826 DEFINE_THISCALL_WRAPPER(custom_category_message, 12)
827 basic_string_char* __thiscall custom_category_message(const custom_category *this,
828 basic_string_char *ret, int err)
830 return MSVCP_basic_string_char_ctor_cstr(ret, strerror(err));
833 DEFINE_THISCALL_WRAPPER(custom_category_default_error_condition, 12)
834 /*error_condition*/void* __thiscall custom_category_default_error_condition(
835 custom_category *this, /*error_condition*/void *ret, int code)
837 FIXME("(%p %p %x) stub\n", this, ret, code);
838 return NULL;
841 DEFINE_THISCALL_WRAPPER(custom_category_equivalent, 12)
842 MSVCP_bool __thiscall custom_category_equivalent(const custom_category *this,
843 int code, const /*error_condition*/void *condition)
845 FIXME("(%p %x %p) stub\n", this, code, condition);
846 return FALSE;
849 DEFINE_THISCALL_WRAPPER(custom_category_equivalent_code, 12)
850 MSVCP_bool __thiscall custom_category_equivalent_code(custom_category *this,
851 const /*error_code*/void *code, int condition)
853 FIXME("(%p %p %x) stub\n", this, code, condition);
854 return FALSE;
857 DEFINE_THISCALL_WRAPPER(iostream_category_message, 12)
858 basic_string_char* __thiscall iostream_category_message(const custom_category *this,
859 basic_string_char *ret, int err)
861 if(err == 1) return MSVCP_basic_string_char_ctor_cstr(ret, "iostream error");
862 return MSVCP_basic_string_char_ctor_cstr(ret, strerror(err));
865 /* ?iostream_category@std@@YAABVerror_category@1@XZ */
866 /* ?iostream_category@std@@YAAEBVerror_category@1@XZ */
867 const error_category* __cdecl std_iostream_category(void)
869 TRACE("()\n");
870 return &iostream_category.base;
873 static custom_category system_category;
874 DEFINE_RTTI_DATA1(system_category, 0, &error_category_rtti_base_descriptor, ".?AV_System_error_category@std@@")
876 extern const vtable_ptr MSVCP_system_category_vtable;
878 static void system_category_ctor(custom_category *this)
880 this->base.vtable = &MSVCP_system_category_vtable;
881 this->type = "system";
884 /* ?system_category@std@@YAABVerror_category@1@XZ */
885 /* ?system_category@std@@YAAEBVerror_category@1@XZ */
886 const error_category* __cdecl std_system_category(void)
888 TRACE("()\n");
889 return &system_category.base;
892 static custom_category generic_category;
893 DEFINE_RTTI_DATA1(generic_category, 0, &error_category_rtti_base_descriptor, ".?AV_Generic_error_category@std@@")
895 extern const vtable_ptr MSVCP_generic_category_vtable;
897 static void generic_category_ctor(custom_category *this)
899 this->base.vtable = &MSVCP_generic_category_vtable;
900 this->type = "generic";
903 /* ?generic_category@std@@YAABVerror_category@1@XZ */
904 /* ?generic_category@std@@YAAEBVerror_category@1@XZ */
905 const error_category* __cdecl std_generic_category(void)
907 TRACE("()\n");
908 return &generic_category.base;
910 #endif
912 #if _MSVCP_VER >= 110
913 static CRITICAL_SECTION call_once_cs;
914 static CRITICAL_SECTION_DEBUG call_once_cs_debug =
916 0, 0, &call_once_cs,
917 { &call_once_cs_debug.ProcessLocksList, &call_once_cs_debug.ProcessLocksList },
918 0, 0, { (DWORD_PTR)(__FILE__ ": call_once_cs") }
920 static CRITICAL_SECTION call_once_cs = { &call_once_cs_debug, -1, 0, 0, 0, 0 };
922 void __cdecl _Call_onceEx(int *once, void (__cdecl *func)(void*), void *argv)
924 TRACE("%p %p %p\n", once, func, argv);
926 EnterCriticalSection(&call_once_cs);
927 if(!*once) {
928 /* FIXME: handle exceptions */
929 func(argv);
930 *once = 1;
932 LeaveCriticalSection(&call_once_cs);
935 static void __cdecl call_once_func_wrapper(void *func)
937 ((void (__cdecl*)(void))func)();
940 void __cdecl _Call_once(int *once, void (__cdecl *func)(void))
942 TRACE("%p %p\n", once, func);
943 _Call_onceEx(once, call_once_func_wrapper, func);
946 void __cdecl _Do_call(void *this)
948 CALL_VTBL_FUNC(this, 0, void, (void*), (this));
950 #endif
952 #if _MSVCP_VER >= 110
953 typedef struct
955 HANDLE hnd;
956 DWORD id;
957 } _Thrd_t;
959 typedef int (__cdecl *_Thrd_start_t)(void*);
961 #define _THRD_ERROR 4
963 int __cdecl _Thrd_equal(_Thrd_t a, _Thrd_t b)
965 TRACE("(%p %u %p %u)\n", a.hnd, a.id, b.hnd, b.id);
966 return a.id == b.id;
969 int __cdecl _Thrd_lt(_Thrd_t a, _Thrd_t b)
971 TRACE("(%p %u %p %u)\n", a.hnd, a.id, b.hnd, b.id);
972 return a.id < b.id;
975 void __cdecl _Thrd_sleep(const xtime *t)
977 TRACE("(%p)\n", t);
978 Sleep(_Xtime_diff_to_millis(t));
981 void __cdecl _Thrd_yield(void)
983 TRACE("()\n");
984 Sleep(0);
987 static _Thrd_t thread_current(void)
989 _Thrd_t ret;
991 if(DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
992 GetCurrentProcess(), &ret.hnd, 0, FALSE, DUPLICATE_SAME_ACCESS)) {
993 CloseHandle(ret.hnd);
994 } else {
995 ret.hnd = 0;
997 ret.id = GetCurrentThreadId();
999 TRACE("(%p %u)\n", ret.hnd, ret.id);
1000 return ret;
1003 #ifndef __i386__
1004 _Thrd_t __cdecl _Thrd_current(void)
1006 return thread_current();
1008 #else
1009 ULONGLONG __cdecl _Thrd_current(void)
1011 union {
1012 _Thrd_t thr;
1013 ULONGLONG ull;
1014 } ret;
1016 C_ASSERT(sizeof(_Thrd_t) <= sizeof(ULONGLONG));
1018 ret.thr = thread_current();
1019 return ret.ull;
1021 #endif
1023 int __cdecl _Thrd_join(_Thrd_t thr, int *code)
1025 TRACE("(%p %u %p)\n", thr.hnd, thr.id, code);
1026 if (WaitForSingleObject(thr.hnd, INFINITE))
1027 return _THRD_ERROR;
1029 if (code)
1030 GetExitCodeThread(thr.hnd, (DWORD *)code);
1032 CloseHandle(thr.hnd);
1033 return 0;
1036 int __cdecl _Thrd_start(_Thrd_t *thr, LPTHREAD_START_ROUTINE proc, void *arg)
1038 TRACE("(%p %p %p)\n", thr, proc, arg);
1039 thr->hnd = CreateThread(NULL, 0, proc, arg, 0, &thr->id);
1040 return thr->hnd ? 0 : _THRD_ERROR;
1043 typedef struct
1045 _Thrd_start_t proc;
1046 void *arg;
1047 } thread_proc_arg;
1049 static DWORD WINAPI thread_proc_wrapper(void *arg)
1051 thread_proc_arg wrapped_arg = *((thread_proc_arg*)arg);
1052 free(arg);
1053 return wrapped_arg.proc(wrapped_arg.arg);
1056 int __cdecl _Thrd_create(_Thrd_t *thr, _Thrd_start_t proc, void *arg)
1058 thread_proc_arg *wrapped_arg;
1059 int ret;
1061 TRACE("(%p %p %p)\n", thr, proc, arg);
1063 wrapped_arg = malloc(sizeof(*wrapped_arg));
1064 if(!wrapped_arg)
1065 return _THRD_ERROR; /* TODO: probably different error should be returned here */
1067 wrapped_arg->proc = proc;
1068 wrapped_arg->arg = arg;
1069 ret = _Thrd_start(thr, thread_proc_wrapper, wrapped_arg);
1070 if(ret) free(wrapped_arg);
1071 return ret;
1074 int __cdecl _Thrd_detach(_Thrd_t thr)
1076 return CloseHandle(thr.hnd) ? 0 : _THRD_ERROR;
1079 typedef struct
1081 const vtable_ptr *vtable;
1082 _Cnd_t cnd;
1083 _Mtx_t mtx;
1084 MSVCP_bool launched;
1085 } _Pad;
1087 DEFINE_RTTI_DATA0(_Pad, 0, ".?AV_Pad@std@@")
1089 /* ??_7_Pad@std@@6B@ */
1090 extern const vtable_ptr MSVCP__Pad_vtable;
1092 unsigned int __cdecl _Thrd_hardware_concurrency(void)
1094 static unsigned int val = -1;
1096 TRACE("()\n");
1098 if(val == -1) {
1099 SYSTEM_INFO si;
1101 GetSystemInfo(&si);
1102 val = si.dwNumberOfProcessors;
1105 return val;
1108 unsigned int __cdecl _Thrd_id(void)
1110 TRACE("()\n");
1111 return GetCurrentThreadId();
1114 /* ??0_Pad@std@@QAE@XZ */
1115 /* ??0_Pad@std@@QEAA@XZ */
1116 DEFINE_THISCALL_WRAPPER(_Pad_ctor, 4)
1117 _Pad* __thiscall _Pad_ctor(_Pad *this)
1119 TRACE("(%p)\n", this);
1121 this->vtable = &MSVCP__Pad_vtable;
1122 _Cnd_init(&this->cnd);
1123 _Mtx_init(&this->mtx, 0);
1124 this->launched = FALSE;
1125 _Mtx_lock(MTX_T_TO_ARG(this->mtx));
1126 return this;
1129 /* ??4_Pad@std@@QAEAAV01@ABV01@@Z */
1130 /* ??4_Pad@std@@QEAAAEAV01@AEBV01@@Z */
1131 DEFINE_THISCALL_WRAPPER(_Pad_op_assign, 8)
1132 _Pad* __thiscall _Pad_op_assign(_Pad *this, const _Pad *copy)
1134 TRACE("(%p %p)\n", this, copy);
1136 this->cnd = copy->cnd;
1137 this->mtx = copy->mtx;
1138 this->launched = copy->launched;
1139 return this;
1142 /* ??0_Pad@std@@QAE@ABV01@@Z */
1143 /* ??0_Pad@std@@QEAA@AEBV01@@Z */
1144 DEFINE_THISCALL_WRAPPER(_Pad_copy_ctor, 8)
1145 _Pad* __thiscall _Pad_copy_ctor(_Pad *this, const _Pad *copy)
1147 TRACE("(%p %p)\n", this, copy);
1149 this->vtable = &MSVCP__Pad_vtable;
1150 return _Pad_op_assign(this, copy);
1153 /* ??1_Pad@std@@QAE@XZ */
1154 /* ??1_Pad@std@@QEAA@XZ */
1155 DEFINE_THISCALL_WRAPPER(_Pad_dtor, 4)
1156 void __thiscall _Pad_dtor(_Pad *this)
1158 TRACE("(%p)\n", this);
1160 _Mtx_unlock(MTX_T_TO_ARG(this->mtx));
1161 _Mtx_destroy(MTX_T_TO_ARG(this->mtx));
1162 _Cnd_destroy(CND_T_TO_ARG(this->cnd));
1165 DEFINE_THISCALL_WRAPPER(_Pad__Go, 4)
1166 #define call__Pad__Go(this) CALL_VTBL_FUNC(this, 0, unsigned int, (_Pad*), (this))
1167 unsigned int __thiscall _Pad__Go(_Pad *this)
1169 ERR("(%p) should not be called\n", this);
1170 return 0;
1173 static DWORD WINAPI launch_thread_proc(void *arg)
1175 _Pad *this = arg;
1176 return call__Pad__Go(this);
1179 /* ?_Launch@_Pad@std@@QAEXPAU_Thrd_imp_t@@@Z */
1180 /* ?_Launch@_Pad@std@@QEAAXPEAU_Thrd_imp_t@@@Z */
1181 DEFINE_THISCALL_WRAPPER(_Pad__Launch, 8)
1182 void __thiscall _Pad__Launch(_Pad *this, _Thrd_t *thr)
1184 TRACE("(%p %p)\n", this, thr);
1186 _Thrd_start(thr, launch_thread_proc, this);
1187 _Cnd_wait(CND_T_TO_ARG(this->cnd), MTX_T_TO_ARG(this->mtx));
1190 /* ?_Release@_Pad@std@@QAEXXZ */
1191 /* ?_Release@_Pad@std@@QEAAXXZ */
1192 DEFINE_THISCALL_WRAPPER(_Pad__Release, 4)
1193 void __thiscall _Pad__Release(_Pad *this)
1195 TRACE("(%p)\n", this);
1197 _Mtx_lock(MTX_T_TO_ARG(this->mtx));
1198 this->launched = TRUE;
1199 _Cnd_signal(CND_T_TO_ARG(this->cnd));
1200 _Mtx_unlock(MTX_T_TO_ARG(this->mtx));
1202 #endif
1204 #ifndef __GNUC__
1205 void __asm_dummy_vtables(void) {
1206 #endif
1207 #if _MSVCP_VER == 100
1208 __ASM_VTABLE(iostream_category,
1209 VTABLE_ADD_FUNC(custom_category_vector_dtor)
1210 VTABLE_ADD_FUNC(custom_category_name)
1211 VTABLE_ADD_FUNC(iostream_category_message)
1212 VTABLE_ADD_FUNC(custom_category_default_error_condition)
1213 VTABLE_ADD_FUNC(custom_category_equivalent)
1214 VTABLE_ADD_FUNC(custom_category_equivalent_code));
1215 __ASM_VTABLE(system_category,
1216 VTABLE_ADD_FUNC(custom_category_vector_dtor)
1217 VTABLE_ADD_FUNC(custom_category_name)
1218 VTABLE_ADD_FUNC(custom_category_message)
1219 VTABLE_ADD_FUNC(custom_category_default_error_condition)
1220 VTABLE_ADD_FUNC(custom_category_equivalent)
1221 VTABLE_ADD_FUNC(custom_category_equivalent_code));
1222 __ASM_VTABLE(generic_category,
1223 VTABLE_ADD_FUNC(custom_category_vector_dtor)
1224 VTABLE_ADD_FUNC(custom_category_name)
1225 VTABLE_ADD_FUNC(custom_category_message)
1226 VTABLE_ADD_FUNC(custom_category_default_error_condition)
1227 VTABLE_ADD_FUNC(custom_category_equivalent)
1228 VTABLE_ADD_FUNC(custom_category_equivalent_code));
1229 #endif
1230 #if _MSVCP_VER >= 110
1231 __ASM_VTABLE(_Pad,
1232 VTABLE_ADD_FUNC(_Pad__Go));
1233 #endif
1234 #ifndef __GNUC__
1236 #endif
1238 /*********************************************************************
1239 * __crtInitializeCriticalSectionEx (MSVCP140.@)
1241 BOOL CDECL MSVCP__crtInitializeCriticalSectionEx(
1242 CRITICAL_SECTION *cs, DWORD spin_count, DWORD flags)
1244 TRACE("(%p %x %x)\n", cs, spin_count, flags);
1245 return InitializeCriticalSectionEx(cs, spin_count, flags);
1248 /*********************************************************************
1249 * __crtCreateEventExW (MSVCP140.@)
1251 HANDLE CDECL MSVCP__crtCreateEventExW(
1252 SECURITY_ATTRIBUTES *attribs, LPCWSTR name, DWORD flags, DWORD access)
1254 TRACE("(%p %s 0x%08x 0x%08x)\n", attribs, debugstr_w(name), flags, access);
1255 return CreateEventExW(attribs, name, flags, access);
1258 /*********************************************************************
1259 * __crtGetTickCount64 (MSVCP140.@)
1261 ULONGLONG CDECL MSVCP__crtGetTickCount64(void)
1263 return GetTickCount64();
1266 /*********************************************************************
1267 * __crtGetCurrentProcessorNumber (MSVCP140.@)
1269 DWORD CDECL MSVCP__crtGetCurrentProcessorNumber(void)
1271 return GetCurrentProcessorNumber();
1274 /*********************************************************************
1275 * __crtFlushProcessWriteBuffers (MSVCP140.@)
1277 VOID CDECL MSVCP__crtFlushProcessWriteBuffers(void)
1279 return FlushProcessWriteBuffers();
1282 /*********************************************************************
1283 * __crtCreateSemaphoreExW (MSVCP140.@)
1285 HANDLE CDECL MSVCP__crtCreateSemaphoreExW(
1286 SECURITY_ATTRIBUTES *attribs, LONG initial_count, LONG max_count, LPCWSTR name,
1287 DWORD flags, DWORD access)
1289 TRACE("(%p %d %d %s 0x%08x 0x%08x)\n", attribs, initial_count, max_count, debugstr_w(name),
1290 flags, access);
1291 return CreateSemaphoreExW(attribs, initial_count, max_count, name, flags, access);
1294 /*********************************************************************
1295 * __crtCreateThreadpoolTimer (MSVCP140.@)
1297 PTP_TIMER CDECL MSVCP__crtCreateThreadpoolTimer(PTP_TIMER_CALLBACK callback,
1298 PVOID userdata, TP_CALLBACK_ENVIRON *environment)
1300 TRACE("(%p %p %p)\n", callback, userdata, environment);
1301 return CreateThreadpoolTimer(callback, userdata, environment);
1304 /*********************************************************************
1305 * __crtCloseThreadpoolTimer (MSVCP140.@)
1307 VOID CDECL MSVCP__crtCloseThreadpoolTimer(TP_TIMER *timer)
1309 TRACE("(%p)\n", timer);
1310 CloseThreadpoolTimer(timer);
1313 /*********************************************************************
1314 * __crtSetThreadpoolTimer (MSVCP140.@)
1316 VOID CDECL MSVCP__crtSetThreadpoolTimer(TP_TIMER *timer,
1317 FILETIME *due_time, DWORD period, DWORD window_length)
1319 TRACE("(%p %p 0x%08x 0x%08x)\n", timer, due_time, period, window_length);
1320 return SetThreadpoolTimer(timer, due_time, period, window_length);
1323 /*********************************************************************
1324 * __crtWaitForThreadpoolTimerCallbacks (MSVCP140.@)
1326 VOID CDECL MSVCP__crtWaitForThreadpoolTimerCallbacks(TP_TIMER *timer, BOOL cancel)
1328 TRACE("(%p %d)\n", timer, cancel);
1329 WaitForThreadpoolTimerCallbacks(timer, cancel);
1332 /*********************************************************************
1333 * __crtCreateThreadpoolWait (MSVCP140.@)
1335 PTP_WAIT CDECL MSVCP__crtCreateThreadpoolWait(PTP_WAIT_CALLBACK callback,
1336 PVOID userdata, TP_CALLBACK_ENVIRON *environment)
1338 TRACE("(%p %p %p)\n", callback, userdata, environment);
1339 return CreateThreadpoolWait(callback, userdata, environment);
1342 /*********************************************************************
1343 * __crtCloseThreadpoolWait (MSVCP140.@)
1345 VOID CDECL MSVCP__crtCloseThreadpoolWait(TP_WAIT *wait)
1347 TRACE("(%p)\n", wait);
1348 CloseThreadpoolWait(wait);
1351 /*********************************************************************
1352 * __crtSetThreadpoolWait (MSVCP140.@)
1354 VOID CDECL MSVCP__crtSetThreadpoolWait(TP_WAIT *wait, HANDLE handle, FILETIME *due_time)
1356 TRACE("(%p %p %p)\n", wait, handle, due_time);
1357 return SetThreadpoolWait(wait, handle, due_time);
1360 /*********************************************************************
1361 * __crtFreeLibraryWhenCallbackReturns (MSVCP140.@)
1363 VOID CDECL MSVCP__crtFreeLibraryWhenCallbackReturns(PTP_CALLBACK_INSTANCE instance, HMODULE mod)
1365 TRACE("(%p %p)\n", instance, mod);
1366 FreeLibraryWhenCallbackReturns(instance, mod);
1369 /* ?_Execute_once@std@@YAHAAUonce_flag@1@P6GHPAX1PAPAX@Z1@Z */
1370 /* ?_Execute_once@std@@YAHAEAUonce_flag@1@P6AHPEAX1PEAPEAX@Z1@Z */
1371 BOOL __cdecl _Execute_once(INIT_ONCE *flag, PINIT_ONCE_FN func, void *param)
1373 return InitOnceExecuteOnce(flag, func, param, NULL);
1376 #if _MSVCP_VER >= 100
1377 void init_misc(void *base)
1379 #ifdef __x86_64__
1380 #if _MSVCP_VER == 100
1381 init_error_category_rtti(base);
1382 init_iostream_category_rtti(base);
1383 init_system_category_rtti(base);
1384 init_generic_category_rtti(base);
1385 #endif
1386 #if _MSVCP_VER >= 110
1387 init__Pad_rtti(base);
1388 #endif
1389 #endif
1391 #if _MSVCP_VER == 100
1392 iostream_category_ctor(&iostream_category);
1393 system_category_ctor(&system_category);
1394 generic_category_ctor(&generic_category);
1395 #endif
1398 void free_misc(void)
1400 #if _MSVCP_VER >= 110
1401 if(keyed_event)
1402 NtClose(keyed_event);
1403 HeapFree(GetProcessHeap(), 0, broadcast_at_thread_exit.to_broadcast);
1404 #endif
1406 #endif
1408 #if _MSVCP_VER >= 140
1409 LONGLONG __cdecl _Query_perf_counter(void)
1411 LARGE_INTEGER li;
1412 QueryPerformanceCounter(&li);
1413 return li.QuadPart;
1416 LONGLONG __cdecl _Query_perf_frequency(void)
1418 LARGE_INTEGER li;
1419 QueryPerformanceFrequency(&li);
1420 return li.QuadPart;
1422 #endif
1424 void __cdecl threads__Mtx_new(void **mtx)
1426 *mtx = MSVCRT_operator_new(sizeof(CRITICAL_SECTION));
1427 InitializeCriticalSection(*mtx);
1430 void __cdecl threads__Mtx_delete(void *mtx)
1432 DeleteCriticalSection(mtx);
1435 void __cdecl threads__Mtx_lock(void *mtx)
1437 EnterCriticalSection(mtx);
1440 void __cdecl threads__Mtx_unlock(void *mtx)
1442 LeaveCriticalSection(mtx);
1445 #if _MSVCP_VER >= 110
1446 static LONG shared_ptr_lock;
1448 void __cdecl _Lock_shared_ptr_spin_lock(void)
1450 LONG l = 0;
1452 while(InterlockedCompareExchange(&shared_ptr_lock, 1, 0) != 0) {
1453 if(l++ == 1000) {
1454 Sleep(0);
1455 l = 0;
1460 void __cdecl _Unlock_shared_ptr_spin_lock(void)
1462 shared_ptr_lock = 0;
1464 #endif
1466 #if _MSVCP_VER >= 100
1467 /* ?is_current_task_group_canceling@Concurrency@@YA_NXZ */
1468 MSVCP_bool __cdecl is_current_task_group_canceling(void)
1470 return Context_IsCurrentTaskCollectionCanceling();
1472 #endif
1474 #if _MSVCP_VER >= 140
1475 typedef struct {
1476 void *unk0;
1477 BYTE unk1;
1478 } task_continuation_context;
1480 /* ??0task_continuation_context@Concurrency@@AAE@XZ */
1481 /* ??0task_continuation_context@Concurrency@@AEAA@XZ */
1482 DEFINE_THISCALL_WRAPPER(task_continuation_context_ctor, 4)
1483 task_continuation_context* __thiscall task_continuation_context_ctor(task_continuation_context *this)
1485 TRACE("(%p)\n", this);
1486 memset(this, 0, sizeof(*this));
1487 return this;
1490 typedef struct {
1491 const vtable_ptr *vtable;
1492 void (__cdecl *func)(void);
1493 int unk[4];
1494 void *unk2[3];
1495 void *this;
1496 } function_void_cdecl_void;
1498 /* ?_Assign@_ContextCallback@details@Concurrency@@AAEXPAX@Z */
1499 /* ?_Assign@_ContextCallback@details@Concurrency@@AEAAXPEAX@Z */
1500 DEFINE_THISCALL_WRAPPER(_ContextCallback__Assign, 8)
1501 void __thiscall _ContextCallback__Assign(void *this, void *v)
1503 TRACE("(%p %p)\n", this, v);
1506 #define call_function_do_call(this) CALL_VTBL_FUNC(this, 8, void, (function_void_cdecl_void*), (this))
1507 #define call_function_do_clean(this,b) CALL_VTBL_FUNC(this, 16, void, (function_void_cdecl_void*,MSVCP_bool), (this, b))
1508 /* ?_CallInContext@_ContextCallback@details@Concurrency@@QBEXV?$function@$$A6AXXZ@std@@_N@Z */
1509 /* ?_CallInContext@_ContextCallback@details@Concurrency@@QEBAXV?$function@$$A6AXXZ@std@@_N@Z */
1510 DEFINE_THISCALL_WRAPPER(_ContextCallback__CallInContext, 48)
1511 void __thiscall _ContextCallback__CallInContext(const void *this, function_void_cdecl_void func, MSVCP_bool b)
1513 TRACE("(%p %p %x)\n", this, func.func, b);
1514 call_function_do_call(func.this);
1515 call_function_do_clean(func.this, func.this!=&func);
1518 /* ?_Capture@_ContextCallback@details@Concurrency@@AAEXXZ */
1519 /* ?_Capture@_ContextCallback@details@Concurrency@@AEAAXXZ */
1520 DEFINE_THISCALL_WRAPPER(_ContextCallback__Capture, 4)
1521 void __thiscall _ContextCallback__Capture(void *this)
1523 TRACE("(%p)\n", this);
1526 /* ?_Reset@_ContextCallback@details@Concurrency@@AAEXXZ */
1527 /* ?_Reset@_ContextCallback@details@Concurrency@@AEAAXXZ */
1528 DEFINE_THISCALL_WRAPPER(_ContextCallback__Reset, 4)
1529 void __thiscall _ContextCallback__Reset(void *this)
1531 TRACE("(%p)\n", this);
1534 /* ?_IsCurrentOriginSTA@_ContextCallback@details@Concurrency@@CA_NXZ */
1535 MSVCP_bool __cdecl _ContextCallback__IsCurrentOriginSTA(void *this)
1537 TRACE("(%p)\n", this);
1538 return FALSE;
1541 typedef struct {
1542 /*_Task_impl_base*/void *task;
1543 MSVCP_bool scheduled;
1544 MSVCP_bool started;
1545 } _TaskEventLogger;
1547 /* ?_LogCancelTask@_TaskEventLogger@details@Concurrency@@QAEXXZ */
1548 /* ?_LogCancelTask@_TaskEventLogger@details@Concurrency@@QEAAXXZ */
1549 DEFINE_THISCALL_WRAPPER(_TaskEventLogger__LogCancelTask, 4)
1550 void __thiscall _TaskEventLogger__LogCancelTask(_TaskEventLogger *this)
1552 TRACE("(%p)\n", this);
1555 /* ?_LogScheduleTask@_TaskEventLogger@details@Concurrency@@QAEX_N@Z */
1556 /* ?_LogScheduleTask@_TaskEventLogger@details@Concurrency@@QEAAX_N@Z */
1557 DEFINE_THISCALL_WRAPPER(_TaskEventLogger__LogScheduleTask, 8)
1558 void __thiscall _TaskEventLogger__LogScheduleTask(_TaskEventLogger *this, MSVCP_bool continuation)
1560 TRACE("(%p %x)\n", this, continuation);
1563 /* ?_LogTaskCompleted@_TaskEventLogger@details@Concurrency@@QAEXXZ */
1564 /* ?_LogTaskCompleted@_TaskEventLogger@details@Concurrency@@QEAAXXZ */
1565 DEFINE_THISCALL_WRAPPER(_TaskEventLogger__LogTaskCompleted, 4)
1566 void __thiscall _TaskEventLogger__LogTaskCompleted(_TaskEventLogger *this)
1568 TRACE("(%p)\n", this);
1571 /* ?_LogTaskExecutionCompleted@_TaskEventLogger@details@Concurrency@@QAEXXZ */
1572 /* ?_LogTaskExecutionCompleted@_TaskEventLogger@details@Concurrency@@QEAAXXZ */
1573 DEFINE_THISCALL_WRAPPER(_TaskEventLogger__LogTaskExecutionCompleted, 4)
1574 void __thiscall _TaskEventLogger__LogTaskExecutionCompleted(_TaskEventLogger *this)
1576 TRACE("(%p)\n", this);
1579 /* ?_LogWorkItemCompleted@_TaskEventLogger@details@Concurrency@@QAEXXZ */
1580 /* ?_LogWorkItemCompleted@_TaskEventLogger@details@Concurrency@@QEAAXXZ */
1581 DEFINE_THISCALL_WRAPPER(_TaskEventLogger__LogWorkItemCompleted, 4)
1582 void __thiscall _TaskEventLogger__LogWorkItemCompleted(_TaskEventLogger *this)
1584 TRACE("(%p)\n", this);
1587 /* ?_LogWorkItemStarted@_TaskEventLogger@details@Concurrency@@QAEXXZ */
1588 /* ?_LogWorkItemStarted@_TaskEventLogger@details@Concurrency@@QEAAXXZ */
1589 DEFINE_THISCALL_WRAPPER(_TaskEventLogger__LogWorkItemStarted, 4)
1590 void __thiscall _TaskEventLogger__LogWorkItemStarted(_TaskEventLogger *this)
1592 TRACE("(%p)\n", this);
1595 typedef struct {
1596 PTP_WORK work;
1597 void (__cdecl *callback)(void*);
1598 void *arg;
1599 } _Threadpool_chore;
1601 /* ?_Reschedule_chore@details@Concurrency@@YAHPBU_Threadpool_chore@12@@Z */
1602 /* ?_Reschedule_chore@details@Concurrency@@YAHPEBU_Threadpool_chore@12@@Z */
1603 int __cdecl _Reschedule_chore(const _Threadpool_chore *chore)
1605 TRACE("(%p)\n", chore);
1607 SubmitThreadpoolWork(chore->work);
1608 return 0;
1611 static void WINAPI threadpool_callback(PTP_CALLBACK_INSTANCE instance, void *context, PTP_WORK work)
1613 _Threadpool_chore *chore = context;
1614 TRACE("calling chore callback: %p\n", chore);
1615 if (chore->callback)
1616 chore->callback(chore->arg);
1619 /* ?_Schedule_chore@details@Concurrency@@YAHPAU_Threadpool_chore@12@@Z */
1620 /* ?_Schedule_chore@details@Concurrency@@YAHPEAU_Threadpool_chore@12@@Z */
1621 int __cdecl _Schedule_chore(_Threadpool_chore *chore)
1623 TRACE("(%p)\n", chore);
1625 chore->work = CreateThreadpoolWork(threadpool_callback, chore, NULL);
1626 /* FIXME: what should be returned in case of error */
1627 if(!chore->work)
1628 return -1;
1630 return _Reschedule_chore(chore);
1633 /* ?_Release_chore@details@Concurrency@@YAXPAU_Threadpool_chore@12@@Z */
1634 /* ?_Release_chore@details@Concurrency@@YAXPEAU_Threadpool_chore@12@@Z */
1635 void __cdecl _Release_chore(_Threadpool_chore *chore)
1637 TRACE("(%p)\n", chore);
1639 if(!chore->work) return;
1640 CloseThreadpoolWork(chore->work);
1641 chore->work = NULL;
1643 #endif
1645 #if _MSVCP_VER >= 110 && _MSVCP_VER <= 120
1646 typedef struct {
1647 char dummy;
1648 } _Ph;
1650 /* ?_1@placeholders@std@@3V?$_Ph@$00@2@A */
1651 /* ?_20@placeholders@std@@3V?$_Ph@$0BE@@2@A */
1652 _Ph _Ph_1 = {0}, _Ph_2 = {0}, _Ph_3 = {0}, _Ph_4 = {0}, _Ph_5 = {0};
1653 _Ph _Ph_6 = {0}, _Ph_7 = {0}, _Ph_8 = {0}, _Ph_9 = {0}, _Ph_10 = {0};
1654 _Ph _Ph_11 = {0}, _Ph_12 = {0}, _Ph_13 = {0}, _Ph_14 = {0}, _Ph_15 = {0};
1655 _Ph _Ph_16 = {0}, _Ph_17 = {0}, _Ph_18 = {0}, _Ph_19 = {0}, _Ph_20 = {0};
1656 #endif
1658 #if _MSVCP_VER >= 100
1659 /* based on wined3d_log2i from wined3d.h */
1660 /* Return the integer base-2 logarithm of (x|1). Result is 0 for x == 0. */
1661 static inline unsigned int log2i(unsigned int x)
1663 #ifdef HAVE___BUILTIN_CLZ
1664 return __builtin_clz(x|1) ^ 0x1f;
1665 #else
1666 static const unsigned int l[] =
1668 ~0u, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
1669 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
1670 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
1671 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
1672 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
1673 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
1674 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
1675 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
1676 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1677 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1678 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1679 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1680 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1681 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1682 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1683 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1685 unsigned int i;
1687 x |= 1;
1688 return (i = x >> 16) ? (x = i >> 8) ? l[x] + 24 : l[i] + 16 : (i = x >> 8) ? l[i] + 8 : l[x];
1689 #endif
1692 /* ?_Segment_index_of@_Concurrent_vector_base_v4@details@Concurrency@@KAII@Z */
1693 /* ?_Segment_index_of@_Concurrent_vector_base_v4@details@Concurrency@@KA_K_K@Z */
1694 MSVCP_size_t __cdecl _vector_base_v4__Segment_index_of(MSVCP_size_t x)
1696 unsigned int half;
1698 TRACE("(%lu)\n", x);
1700 if((sizeof(x) == 8) && (half = x >> 32))
1701 return log2i(half) + 32;
1703 return log2i(x);
1706 /* ?_Internal_throw_exception@_Concurrent_vector_base_v4@details@Concurrency@@IBEXI@Z */
1707 /* ?_Internal_throw_exception@_Concurrent_vector_base_v4@details@Concurrency@@IEBAX_K@Z */
1708 DEFINE_THISCALL_WRAPPER(_vector_base_v4__Internal_throw_exception, 8)
1709 void __thiscall _vector_base_v4__Internal_throw_exception(void/*_vector_base_v4*/ *this, MSVCP_size_t idx)
1711 static const struct {
1712 exception_type type;
1713 const char *msg;
1714 } exceptions[] = {
1715 { EXCEPTION_OUT_OF_RANGE, "Index out of range" },
1716 { EXCEPTION_OUT_OF_RANGE, "Index out of segments table range" },
1717 { EXCEPTION_RANGE_ERROR, "Index is inside segment which failed to be allocated" },
1720 TRACE("(%p %lu)\n", this, idx);
1722 if(idx < sizeof(exceptions)/sizeof(exceptions[0]))
1723 throw_exception(exceptions[idx].type, exceptions[idx].msg);
1725 #endif