kernel32/tests: Add more partial DBCS character tests that contains a null character.
[wine.git] / dlls / msvcp90 / misc.c
bloba41d35775e86b8ba3622e0e964ae9b637ca5be8e
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
366 typedef int MSVCRT_long;
368 /* xtime */
369 typedef struct {
370 __time64_t sec;
371 MSVCRT_long nsec;
372 } xtime;
374 /* _Xtime_get_ticks */
375 LONGLONG __cdecl _Xtime_get_ticks(void)
377 FILETIME ft;
379 TRACE("\n");
381 GetSystemTimeAsFileTime(&ft);
382 return ((LONGLONG)ft.dwHighDateTime<<32) + ft.dwLowDateTime - TICKS_1601_TO_1970;
385 /* _xtime_get */
386 int __cdecl xtime_get(xtime* t, int unknown)
388 LONGLONG ticks;
390 TRACE("(%p)\n", t);
392 if(unknown != 1)
393 return 0;
395 ticks = _Xtime_get_ticks();
396 t->sec = ticks / TICKSPERSEC;
397 t->nsec = ticks % TICKSPERSEC * 100;
398 return 1;
401 /* _Xtime_diff_to_millis2 */
402 MSVCRT_long __cdecl _Xtime_diff_to_millis2(const xtime *t1, const xtime *t2)
404 __time64_t diff_sec;
405 MSVCRT_long diff_nsec, ret;
407 TRACE("(%p, %p)\n", t1, t2);
409 diff_sec = t1->sec - t2->sec;
410 diff_nsec = t1->nsec - t2->nsec;
411 ret = diff_sec * MILLISEC_PER_SEC +
412 (diff_nsec + NANOSEC_PER_MILLISEC - 1) / NANOSEC_PER_MILLISEC;
413 return ret > 0 ? ret : 0;
416 /* _Xtime_diff_to_millis */
417 MSVCRT_long __cdecl _Xtime_diff_to_millis(const xtime *t)
419 xtime now;
421 TRACE("%p\n", t);
423 xtime_get(&now, 1);
424 return _Xtime_diff_to_millis2(t, &now);
426 #endif
428 #if _MSVCP_VER >= 90
429 unsigned int __cdecl _Random_device(void)
431 unsigned int ret;
433 TRACE("\n");
435 /* TODO: throw correct exception in case of failure */
436 if(rand_s(&ret))
437 throw_exception(EXCEPTION, "random number generator failed\n");
438 return ret;
440 #endif
442 #if _MSVCP_VER >= 110
443 #if defined(__i386__) && !defined(__arm__)
445 extern void *call_thiscall_func;
446 __ASM_GLOBAL_FUNC(call_thiscall_func,
447 "popl %eax\n\t"
448 "popl %edx\n\t"
449 "popl %ecx\n\t"
450 "pushl %eax\n\t"
451 "jmp *%edx\n\t")
453 #define call_func1(func,this) ((void* (WINAPI*)(void*,void*))&call_thiscall_func)(func,this)
455 #else /* __i386__ */
457 #define call_func1(func,this) func(this)
459 #endif /* __i386__ */
461 #define MTX_MULTI_LOCK 0x100
462 #define MTX_LOCKED 3
463 typedef struct
465 DWORD flags;
466 critical_section cs;
467 DWORD thread_id;
468 DWORD count;
469 } *_Mtx_t;
471 int __cdecl _Mtx_init(_Mtx_t *mtx, int flags)
473 if(flags & ~MTX_MULTI_LOCK)
474 FIXME("unknown flags ignorred: %x\n", flags);
476 *mtx = MSVCRT_operator_new(sizeof(**mtx));
477 (*mtx)->flags = flags;
478 call_func1(critical_section_ctor, &(*mtx)->cs);
479 (*mtx)->thread_id = -1;
480 (*mtx)->count = 0;
481 return 0;
484 void __cdecl _Mtx_destroy(_Mtx_t *mtx)
486 call_func1(critical_section_dtor, &(*mtx)->cs);
487 MSVCRT_operator_delete(*mtx);
490 int __cdecl _Mtx_current_owns(_Mtx_t *mtx)
492 return (*mtx)->thread_id == GetCurrentThreadId();
495 int __cdecl _Mtx_lock(_Mtx_t *mtx)
497 if((*mtx)->thread_id != GetCurrentThreadId()) {
498 call_func1(critical_section_lock, &(*mtx)->cs);
499 (*mtx)->thread_id = GetCurrentThreadId();
500 }else if(!((*mtx)->flags & MTX_MULTI_LOCK)) {
501 return MTX_LOCKED;
504 (*mtx)->count++;
505 return 0;
508 int __cdecl _Mtx_unlock(_Mtx_t *mtx)
510 if(--(*mtx)->count)
511 return 0;
513 (*mtx)->thread_id = -1;
514 call_func1(critical_section_unlock, &(*mtx)->cs);
515 return 0;
518 int __cdecl _Mtx_trylock(_Mtx_t *mtx)
520 if((*mtx)->thread_id != GetCurrentThreadId()) {
521 if(!call_func1(critical_section_trylock, &(*mtx)->cs))
522 return MTX_LOCKED;
523 (*mtx)->thread_id = GetCurrentThreadId();
524 }else if(!((*mtx)->flags & MTX_MULTI_LOCK)) {
525 return MTX_LOCKED;
528 (*mtx)->count++;
529 return 0;
532 critical_section* __cdecl _Mtx_getconcrtcs(_Mtx_t *mtx)
534 return &(*mtx)->cs;
537 static inline LONG interlocked_dec_if_nonzero( LONG *dest )
539 LONG val, tmp;
540 for (val = *dest;; val = tmp)
542 if (!val || (tmp = InterlockedCompareExchange( dest, val - 1, val )) == val)
543 break;
545 return val;
548 #define CND_TIMEDOUT 2
550 typedef struct
552 CONDITION_VARIABLE cv;
553 } *_Cnd_t;
555 static HANDLE keyed_event;
557 int __cdecl _Cnd_init(_Cnd_t *cnd)
559 *cnd = MSVCRT_operator_new(sizeof(**cnd));
560 InitializeConditionVariable(&(*cnd)->cv);
562 if(!keyed_event) {
563 HANDLE event;
565 NtCreateKeyedEvent(&event, GENERIC_READ|GENERIC_WRITE, NULL, 0);
566 if(InterlockedCompareExchangePointer(&keyed_event, event, NULL) != NULL)
567 NtClose(event);
570 return 0;
573 int __cdecl _Cnd_wait(_Cnd_t *cnd, _Mtx_t *mtx)
575 CONDITION_VARIABLE *cv = &(*cnd)->cv;
577 InterlockedExchangeAdd( (LONG *)&cv->Ptr, 1 );
578 _Mtx_unlock(mtx);
580 NtWaitForKeyedEvent(keyed_event, &cv->Ptr, FALSE, NULL);
582 _Mtx_lock(mtx);
583 return 0;
586 int __cdecl _Cnd_timedwait(_Cnd_t *cnd, _Mtx_t *mtx, const xtime *xt)
588 CONDITION_VARIABLE *cv = &(*cnd)->cv;
589 LARGE_INTEGER timeout;
590 NTSTATUS status;
592 InterlockedExchangeAdd( (LONG *)&cv->Ptr, 1 );
593 _Mtx_unlock(mtx);
595 timeout.QuadPart = (ULONGLONG)_Xtime_diff_to_millis(xt) * -10000;
596 status = NtWaitForKeyedEvent(keyed_event, &cv->Ptr, FALSE, &timeout);
597 if (status)
599 if (!interlocked_dec_if_nonzero( (LONG *)&cv->Ptr ))
600 status = NtWaitForKeyedEvent( keyed_event, &cv->Ptr, FALSE, NULL );
603 _Mtx_lock(mtx);
604 return status ? CND_TIMEDOUT : 0;
607 int __cdecl _Cnd_broadcast(_Cnd_t *cnd)
609 CONDITION_VARIABLE *cv = &(*cnd)->cv;
610 LONG val = InterlockedExchange( (LONG *)&cv->Ptr, 0 );
611 while (val-- > 0)
612 NtReleaseKeyedEvent( keyed_event, &cv->Ptr, FALSE, NULL );
613 return 0;
616 int __cdecl _Cnd_signal(_Cnd_t *cnd)
618 CONDITION_VARIABLE *cv = &(*cnd)->cv;
619 if (interlocked_dec_if_nonzero( (LONG *)&cv->Ptr ))
620 NtReleaseKeyedEvent( keyed_event, &cv->Ptr, FALSE, NULL );
621 return 0;
624 void __cdecl _Cnd_destroy(_Cnd_t *cnd)
626 if(cnd) {
627 _Cnd_broadcast(cnd);
628 MSVCRT_operator_delete(*cnd);
631 #endif
633 #if _MSVCP_VER == 100
634 typedef struct {
635 const vtable_ptr *vtable;
636 } error_category;
638 typedef struct {
639 error_category base;
640 const char *type;
641 } custom_category;
642 static custom_category iostream_category;
644 DEFINE_RTTI_DATA0(error_category, 0, ".?AVerror_category@std@@")
645 DEFINE_RTTI_DATA1(iostream_category, 0, &error_category_rtti_base_descriptor, ".?AV_Iostream_error_category@std@@")
647 extern const vtable_ptr MSVCP_iostream_category_vtable;
649 static void iostream_category_ctor(custom_category *this)
651 this->base.vtable = &MSVCP_iostream_category_vtable;
652 this->type = "iostream";
655 DEFINE_THISCALL_WRAPPER(custom_category_vector_dtor, 8)
656 custom_category* __thiscall custom_category_vector_dtor(custom_category *this, unsigned int flags)
658 TRACE("(%p %x)\n", this, flags);
659 if(flags & 2) {
660 /* we have an array, with the number of elements stored before the first object */
661 INT_PTR i, *ptr = (INT_PTR *)this-1;
663 for(i=*ptr-1; i>=0; i--)
664 MSVCRT_operator_delete(ptr);
665 } else {
666 if(flags & 1)
667 MSVCRT_operator_delete(this);
670 return this;
673 DEFINE_THISCALL_WRAPPER(custom_category_name, 4)
674 const char* __thiscall custom_category_name(const custom_category *this)
676 return this->type;
679 DEFINE_THISCALL_WRAPPER(custom_category_message, 12)
680 basic_string_char* __thiscall custom_category_message(const custom_category *this,
681 basic_string_char *ret, int err)
683 return MSVCP_basic_string_char_ctor_cstr(ret, strerror(err));
686 DEFINE_THISCALL_WRAPPER(custom_category_default_error_condition, 12)
687 /*error_condition*/void* __thiscall custom_category_default_error_condition(
688 custom_category *this, /*error_condition*/void *ret, int code)
690 FIXME("(%p %p %x) stub\n", this, ret, code);
691 return NULL;
694 DEFINE_THISCALL_WRAPPER(custom_category_equivalent, 12)
695 MSVCP_bool __thiscall custom_category_equivalent(const custom_category *this,
696 int code, const /*error_condition*/void *condition)
698 FIXME("(%p %x %p) stub\n", this, code, condition);
699 return FALSE;
702 DEFINE_THISCALL_WRAPPER(custom_category_equivalent_code, 12)
703 MSVCP_bool __thiscall custom_category_equivalent_code(custom_category *this,
704 const /*error_code*/void *code, int condition)
706 FIXME("(%p %p %x) stub\n", this, code, condition);
707 return FALSE;
710 DEFINE_THISCALL_WRAPPER(iostream_category_message, 12)
711 basic_string_char* __thiscall iostream_category_message(const custom_category *this,
712 basic_string_char *ret, int err)
714 if(err == 1) return MSVCP_basic_string_char_ctor_cstr(ret, "iostream error");
715 return MSVCP_basic_string_char_ctor_cstr(ret, strerror(err));
718 /* ?iostream_category@std@@YAABVerror_category@1@XZ */
719 /* ?iostream_category@std@@YAAEBVerror_category@1@XZ */
720 const error_category* __cdecl std_iostream_category(void)
722 TRACE("()\n");
723 return &iostream_category.base;
726 static custom_category system_category;
727 DEFINE_RTTI_DATA1(system_category, 0, &error_category_rtti_base_descriptor, ".?AV_System_error_category@std@@")
729 extern const vtable_ptr MSVCP_system_category_vtable;
731 static void system_category_ctor(custom_category *this)
733 this->base.vtable = &MSVCP_system_category_vtable;
734 this->type = "system";
737 /* ?system_category@std@@YAABVerror_category@1@XZ */
738 /* ?system_category@std@@YAAEBVerror_category@1@XZ */
739 const error_category* __cdecl std_system_category(void)
741 TRACE("()\n");
742 return &system_category.base;
745 static custom_category generic_category;
746 DEFINE_RTTI_DATA1(generic_category, 0, &error_category_rtti_base_descriptor, ".?AV_Generic_error_category@std@@")
748 extern const vtable_ptr MSVCP_generic_category_vtable;
750 static void generic_category_ctor(custom_category *this)
752 this->base.vtable = &MSVCP_generic_category_vtable;
753 this->type = "generic";
756 /* ?generic_category@std@@YAABVerror_category@1@XZ */
757 /* ?generic_category@std@@YAAEBVerror_category@1@XZ */
758 const error_category* __cdecl std_generic_category(void)
760 TRACE("()\n");
761 return &generic_category.base;
763 #endif
765 #if _MSVCP_VER >= 110
766 static CRITICAL_SECTION call_once_cs;
767 static CRITICAL_SECTION_DEBUG call_once_cs_debug =
769 0, 0, &call_once_cs,
770 { &call_once_cs_debug.ProcessLocksList, &call_once_cs_debug.ProcessLocksList },
771 0, 0, { (DWORD_PTR)(__FILE__ ": call_once_cs") }
773 static CRITICAL_SECTION call_once_cs = { &call_once_cs_debug, -1, 0, 0, 0, 0 };
775 void __cdecl _Call_onceEx(int *once, void (__cdecl *func)(void*), void *argv)
777 TRACE("%p %p %p\n", once, func, argv);
779 EnterCriticalSection(&call_once_cs);
780 if(!*once) {
781 /* FIXME: handle exceptions */
782 func(argv);
783 *once = 1;
785 LeaveCriticalSection(&call_once_cs);
788 static void __cdecl call_once_func_wrapper(void *func)
790 ((void (__cdecl*)(void))func)();
793 void __cdecl _Call_once(int *once, void (__cdecl *func)(void))
795 TRACE("%p %p\n", once, func);
796 _Call_onceEx(once, call_once_func_wrapper, func);
799 void __cdecl _Do_call(void *this)
801 CALL_VTBL_FUNC(this, 0, void, (void*), (this));
803 #endif
805 #if _MSVCP_VER >= 110
806 typedef struct
808 HANDLE hnd;
809 DWORD id;
810 } _Thrd_t;
812 typedef int (__cdecl *_Thrd_start_t)(void*);
814 #define _THRD_ERROR 4
816 int __cdecl _Thrd_equal(_Thrd_t a, _Thrd_t b)
818 TRACE("(%p %u %p %u)\n", a.hnd, a.id, b.hnd, b.id);
819 return a.id == b.id;
822 int __cdecl _Thrd_lt(_Thrd_t a, _Thrd_t b)
824 TRACE("(%p %u %p %u)\n", a.hnd, a.id, b.hnd, b.id);
825 return a.id < b.id;
828 void __cdecl _Thrd_sleep(const xtime *t)
830 TRACE("(%p)\n", t);
831 Sleep(_Xtime_diff_to_millis(t));
834 void __cdecl _Thrd_yield(void)
836 TRACE("()\n");
837 Sleep(0);
840 static _Thrd_t thread_current(void)
842 _Thrd_t ret;
844 if(DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
845 GetCurrentProcess(), &ret.hnd, 0, FALSE, DUPLICATE_SAME_ACCESS)) {
846 CloseHandle(ret.hnd);
847 } else {
848 ret.hnd = 0;
850 ret.id = GetCurrentThreadId();
852 TRACE("(%p %u)\n", ret.hnd, ret.id);
853 return ret;
856 #ifndef __i386__
857 _Thrd_t __cdecl _Thrd_current(void)
859 return thread_current();
861 #else
862 ULONGLONG __cdecl _Thrd_current(void)
864 union {
865 _Thrd_t thr;
866 ULONGLONG ull;
867 } ret;
869 C_ASSERT(sizeof(_Thrd_t) <= sizeof(ULONGLONG));
871 ret.thr = thread_current();
872 return ret.ull;
874 #endif
876 int __cdecl _Thrd_join(_Thrd_t thr, int *code)
878 TRACE("(%p %u %p)\n", thr.hnd, thr.id, code);
879 if (WaitForSingleObject(thr.hnd, INFINITE))
880 return _THRD_ERROR;
882 if (code)
883 GetExitCodeThread(thr.hnd, (DWORD *)code);
885 CloseHandle(thr.hnd);
886 return 0;
889 int __cdecl _Thrd_start(_Thrd_t *thr, LPTHREAD_START_ROUTINE proc, void *arg)
891 TRACE("(%p %p %p)\n", thr, proc, arg);
892 thr->hnd = CreateThread(NULL, 0, proc, arg, 0, &thr->id);
893 return thr->hnd ? 0 : _THRD_ERROR;
896 typedef struct
898 _Thrd_start_t proc;
899 void *arg;
900 } thread_proc_arg;
902 static DWORD WINAPI thread_proc_wrapper(void *arg)
904 thread_proc_arg wrapped_arg = *((thread_proc_arg*)arg);
905 free(arg);
906 return wrapped_arg.proc(wrapped_arg.arg);
909 int __cdecl _Thrd_create(_Thrd_t *thr, _Thrd_start_t proc, void *arg)
911 thread_proc_arg *wrapped_arg;
912 int ret;
914 TRACE("(%p %p %p)\n", thr, proc, arg);
916 wrapped_arg = malloc(sizeof(*wrapped_arg));
917 if(!wrapped_arg)
918 return _THRD_ERROR; /* TODO: probably different error should be returned here */
920 wrapped_arg->proc = proc;
921 wrapped_arg->arg = arg;
922 ret = _Thrd_start(thr, thread_proc_wrapper, wrapped_arg);
923 if(ret) free(wrapped_arg);
924 return ret;
927 int __cdecl _Thrd_detach(_Thrd_t thr)
929 return CloseHandle(thr.hnd) ? 0 : _THRD_ERROR;
932 typedef struct
934 const vtable_ptr *vtable;
935 _Cnd_t cnd;
936 _Mtx_t mtx;
937 MSVCP_bool launched;
938 } _Pad;
940 DEFINE_RTTI_DATA0(_Pad, 0, ".?AV_Pad@std@@")
942 /* ??_7_Pad@std@@6B@ */
943 extern const vtable_ptr MSVCP__Pad_vtable;
945 /* ??0_Pad@std@@QAE@XZ */
946 /* ??0_Pad@std@@QEAA@XZ */
947 DEFINE_THISCALL_WRAPPER(_Pad_ctor, 4)
948 _Pad* __thiscall _Pad_ctor(_Pad *this)
950 TRACE("(%p)\n", this);
952 this->vtable = &MSVCP__Pad_vtable;
953 _Cnd_init(&this->cnd);
954 _Mtx_init(&this->mtx, 0);
955 this->launched = FALSE;
956 _Mtx_lock(&this->mtx);
957 return this;
960 /* ??4_Pad@std@@QAEAAV01@ABV01@@Z */
961 /* ??4_Pad@std@@QEAAAEAV01@AEBV01@@Z */
962 DEFINE_THISCALL_WRAPPER(_Pad_op_assign, 8)
963 _Pad* __thiscall _Pad_op_assign(_Pad *this, const _Pad *copy)
965 TRACE("(%p %p)\n", this, copy);
967 this->cnd = copy->cnd;
968 this->mtx = copy->mtx;
969 this->launched = copy->launched;
970 return this;
973 /* ??0_Pad@std@@QAE@ABV01@@Z */
974 /* ??0_Pad@std@@QEAA@AEBV01@@Z */
975 DEFINE_THISCALL_WRAPPER(_Pad_copy_ctor, 8)
976 _Pad* __thiscall _Pad_copy_ctor(_Pad *this, const _Pad *copy)
978 TRACE("(%p %p)\n", this, copy);
980 this->vtable = &MSVCP__Pad_vtable;
981 return _Pad_op_assign(this, copy);
984 /* ??1_Pad@std@@QAE@XZ */
985 /* ??1_Pad@std@@QEAA@XZ */
986 DEFINE_THISCALL_WRAPPER(_Pad_dtor, 4)
987 void __thiscall _Pad_dtor(_Pad *this)
989 TRACE("(%p)\n", this);
991 _Mtx_unlock(&this->mtx);
992 _Mtx_destroy(&this->mtx);
993 _Cnd_destroy(&this->cnd);
996 DEFINE_THISCALL_WRAPPER(_Pad__Go, 4)
997 #define call__Pad__Go(this) CALL_VTBL_FUNC(this, 0, unsigned int, (_Pad*), (this))
998 unsigned int __thiscall _Pad__Go(_Pad *this)
1000 ERR("(%p) should not be called\n", this);
1001 return 0;
1004 static DWORD WINAPI launch_thread_proc(void *arg)
1006 _Pad *this = arg;
1007 return call__Pad__Go(this);
1010 /* ?_Launch@_Pad@std@@QAEXPAU_Thrd_imp_t@@@Z */
1011 /* ?_Launch@_Pad@std@@QEAAXPEAU_Thrd_imp_t@@@Z */
1012 DEFINE_THISCALL_WRAPPER(_Pad__Launch, 8)
1013 void __thiscall _Pad__Launch(_Pad *this, _Thrd_t *thr)
1015 TRACE("(%p %p)\n", this, thr);
1017 _Thrd_start(thr, launch_thread_proc, this);
1018 _Cnd_wait(&this->cnd, &this->mtx);
1021 /* ?_Release@_Pad@std@@QAEXXZ */
1022 /* ?_Release@_Pad@std@@QEAAXXZ */
1023 DEFINE_THISCALL_WRAPPER(_Pad__Release, 4)
1024 void __thiscall _Pad__Release(_Pad *this)
1026 TRACE("(%p)\n", this);
1028 _Mtx_lock(&this->mtx);
1029 this->launched = TRUE;
1030 _Cnd_signal(&this->cnd);
1031 _Mtx_unlock(&this->mtx);
1033 #endif
1035 #ifndef __GNUC__
1036 void __asm_dummy_vtables(void) {
1037 #endif
1038 #if _MSVCP_VER == 100
1039 __ASM_VTABLE(iostream_category,
1040 VTABLE_ADD_FUNC(custom_category_vector_dtor)
1041 VTABLE_ADD_FUNC(custom_category_name)
1042 VTABLE_ADD_FUNC(iostream_category_message)
1043 VTABLE_ADD_FUNC(custom_category_default_error_condition)
1044 VTABLE_ADD_FUNC(custom_category_equivalent)
1045 VTABLE_ADD_FUNC(custom_category_equivalent_code));
1046 __ASM_VTABLE(system_category,
1047 VTABLE_ADD_FUNC(custom_category_vector_dtor)
1048 VTABLE_ADD_FUNC(custom_category_name)
1049 VTABLE_ADD_FUNC(custom_category_message)
1050 VTABLE_ADD_FUNC(custom_category_default_error_condition)
1051 VTABLE_ADD_FUNC(custom_category_equivalent)
1052 VTABLE_ADD_FUNC(custom_category_equivalent_code));
1053 __ASM_VTABLE(generic_category,
1054 VTABLE_ADD_FUNC(custom_category_vector_dtor)
1055 VTABLE_ADD_FUNC(custom_category_name)
1056 VTABLE_ADD_FUNC(custom_category_message)
1057 VTABLE_ADD_FUNC(custom_category_default_error_condition)
1058 VTABLE_ADD_FUNC(custom_category_equivalent)
1059 VTABLE_ADD_FUNC(custom_category_equivalent_code));
1060 #endif
1061 #if _MSVCP_VER >= 110
1062 __ASM_VTABLE(_Pad,
1063 VTABLE_ADD_FUNC(_Pad__Go));
1064 #endif
1065 #ifndef __GNUC__
1067 #endif
1069 void init_misc(void *base)
1071 #ifdef __x86_64__
1072 #if _MSVCP_VER == 100
1073 init_error_category_rtti(base);
1074 init_iostream_category_rtti(base);
1075 init_system_category_rtti(base);
1076 init_generic_category_rtti(base);
1077 #endif
1078 #if _MSVCP_VER >= 110
1079 init__Pad_rtti(base);
1080 #endif
1081 #endif
1083 #if _MSVCP_VER == 100
1084 iostream_category_ctor(&iostream_category);
1085 system_category_ctor(&system_category);
1086 generic_category_ctor(&generic_category);
1087 #endif
1090 void free_misc(void)
1092 #if _MSVCP_VER >= 110
1093 if(keyed_event)
1094 NtClose(keyed_event);
1095 #endif