TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / dlls / msvcp90 / misc.c
blobb2adc72e5f98359ff01377c82b65310eb21eae23
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_lock(_Mtx_t *mtx)
492 if((*mtx)->thread_id != GetCurrentThreadId()) {
493 call_func1(critical_section_lock, &(*mtx)->cs);
494 (*mtx)->thread_id = GetCurrentThreadId();
495 }else if(!((*mtx)->flags & MTX_MULTI_LOCK)) {
496 return MTX_LOCKED;
499 (*mtx)->count++;
500 return 0;
503 int __cdecl _Mtx_unlock(_Mtx_t *mtx)
505 if(--(*mtx)->count)
506 return 0;
508 (*mtx)->thread_id = -1;
509 call_func1(critical_section_unlock, &(*mtx)->cs);
510 return 0;
513 int __cdecl _Mtx_trylock(_Mtx_t *mtx)
515 if((*mtx)->thread_id != GetCurrentThreadId()) {
516 if(!call_func1(critical_section_trylock, &(*mtx)->cs))
517 return MTX_LOCKED;
518 (*mtx)->thread_id = GetCurrentThreadId();
519 }else if(!((*mtx)->flags & MTX_MULTI_LOCK)) {
520 return MTX_LOCKED;
523 (*mtx)->count++;
524 return 0;
527 critical_section* __cdecl _Mtx_getconcrtcs(_Mtx_t *mtx)
529 return &(*mtx)->cs;
532 static inline LONG interlocked_dec_if_nonzero( LONG *dest )
534 LONG val, tmp;
535 for (val = *dest;; val = tmp)
537 if (!val || (tmp = InterlockedCompareExchange( dest, val - 1, val )) == val)
538 break;
540 return val;
543 #define CND_TIMEDOUT 2
545 typedef struct
547 CONDITION_VARIABLE cv;
548 } *_Cnd_t;
550 static HANDLE keyed_event;
552 int __cdecl _Cnd_init(_Cnd_t *cnd)
554 *cnd = MSVCRT_operator_new(sizeof(**cnd));
555 InitializeConditionVariable(&(*cnd)->cv);
557 if(!keyed_event) {
558 HANDLE event;
560 NtCreateKeyedEvent(&event, GENERIC_READ|GENERIC_WRITE, NULL, 0);
561 if(InterlockedCompareExchangePointer(&keyed_event, event, NULL) != NULL)
562 NtClose(event);
565 return 0;
568 int __cdecl _Cnd_wait(_Cnd_t *cnd, _Mtx_t *mtx)
570 CONDITION_VARIABLE *cv = &(*cnd)->cv;
572 InterlockedExchangeAdd( (LONG *)&cv->Ptr, 1 );
573 _Mtx_unlock(mtx);
575 NtWaitForKeyedEvent(keyed_event, &cv->Ptr, FALSE, NULL);
577 _Mtx_lock(mtx);
578 return 0;
581 int __cdecl _Cnd_timedwait(_Cnd_t *cnd, _Mtx_t *mtx, const xtime *xt)
583 CONDITION_VARIABLE *cv = &(*cnd)->cv;
584 LARGE_INTEGER timeout;
585 NTSTATUS status;
587 InterlockedExchangeAdd( (LONG *)&cv->Ptr, 1 );
588 _Mtx_unlock(mtx);
590 timeout.QuadPart = (ULONGLONG)_Xtime_diff_to_millis(xt) * -10000;
591 status = NtWaitForKeyedEvent(keyed_event, &cv->Ptr, FALSE, &timeout);
592 if (status)
594 if (!interlocked_dec_if_nonzero( (LONG *)&cv->Ptr ))
595 status = NtWaitForKeyedEvent( keyed_event, &cv->Ptr, FALSE, NULL );
598 _Mtx_lock(mtx);
599 return status ? CND_TIMEDOUT : 0;
602 int __cdecl _Cnd_broadcast(_Cnd_t *cnd)
604 CONDITION_VARIABLE *cv = &(*cnd)->cv;
605 LONG val = InterlockedExchange( (LONG *)&cv->Ptr, 0 );
606 while (val-- > 0)
607 NtReleaseKeyedEvent( keyed_event, &cv->Ptr, FALSE, NULL );
608 return 0;
611 int __cdecl _Cnd_signal(_Cnd_t *cnd)
613 CONDITION_VARIABLE *cv = &(*cnd)->cv;
614 if (interlocked_dec_if_nonzero( (LONG *)&cv->Ptr ))
615 NtReleaseKeyedEvent( keyed_event, &cv->Ptr, FALSE, NULL );
616 return 0;
619 void __cdecl _Cnd_destroy(_Cnd_t *cnd)
621 if(cnd) {
622 _Cnd_broadcast(cnd);
623 MSVCRT_operator_delete(*cnd);
626 #endif
628 #if _MSVCP_VER == 100
629 typedef struct {
630 const vtable_ptr *vtable;
631 } error_category;
633 typedef struct {
634 error_category base;
635 const char *type;
636 } custom_category;
637 static custom_category iostream_category;
639 DEFINE_RTTI_DATA0(error_category, 0, ".?AVerror_category@std@@")
640 DEFINE_RTTI_DATA1(iostream_category, 0, &error_category_rtti_base_descriptor, ".?AV_Iostream_error_category@std@@")
642 extern const vtable_ptr MSVCP_iostream_category_vtable;
644 static void iostream_category_ctor(custom_category *this)
646 this->base.vtable = &MSVCP_iostream_category_vtable;
647 this->type = "iostream";
650 DEFINE_THISCALL_WRAPPER(custom_category_vector_dtor, 8)
651 custom_category* __thiscall custom_category_vector_dtor(custom_category *this, unsigned int flags)
653 TRACE("(%p %x)\n", this, flags);
654 if(flags & 2) {
655 /* we have an array, with the number of elements stored before the first object */
656 INT_PTR i, *ptr = (INT_PTR *)this-1;
658 for(i=*ptr-1; i>=0; i--)
659 MSVCRT_operator_delete(ptr);
660 } else {
661 if(flags & 1)
662 MSVCRT_operator_delete(this);
665 return this;
668 DEFINE_THISCALL_WRAPPER(custom_category_name, 4)
669 const char* __thiscall custom_category_name(const custom_category *this)
671 return this->type;
674 DEFINE_THISCALL_WRAPPER(custom_category_message, 12)
675 basic_string_char* __thiscall custom_category_message(const custom_category *this,
676 basic_string_char *ret, int err)
678 if(err == 1) return MSVCP_basic_string_char_ctor_cstr(ret, "iostream error");
679 return MSVCP_basic_string_char_ctor_cstr(ret, strerror(err));
682 DEFINE_THISCALL_WRAPPER(custom_category_default_error_condition, 12)
683 /*error_condition*/void* __thiscall custom_category_default_error_condition(
684 custom_category *this, /*error_condition*/void *ret, int code)
686 FIXME("(%p %p %x) stub\n", this, ret, code);
687 return NULL;
690 DEFINE_THISCALL_WRAPPER(custom_category_equivalent, 12)
691 MSVCP_bool __thiscall custom_category_equivalent(const custom_category *this,
692 int code, const /*error_condition*/void *condition)
694 FIXME("(%p %x %p) stub\n", this, code, condition);
695 return FALSE;
698 DEFINE_THISCALL_WRAPPER(custom_category_equivalent_code, 12)
699 MSVCP_bool __thiscall custom_category_equivalent_code(custom_category *this,
700 const /*error_code*/void *code, int condition)
702 FIXME("(%p %p %x) stub\n", this, code, condition);
703 return FALSE;
706 /* ?iostream_category@std@@YAABVerror_category@1@XZ */
707 /* ?iostream_category@std@@YAAEBVerror_category@1@XZ */
708 const error_category* __cdecl std_iostream_category(void)
710 TRACE("()\n");
711 return &iostream_category.base;
714 #ifndef __GNUC__
715 void __asm_dummy_vtables(void) {
716 #endif
717 __ASM_VTABLE(iostream_category,
718 VTABLE_ADD_FUNC(custom_category_vector_dtor)
719 VTABLE_ADD_FUNC(custom_category_name)
720 VTABLE_ADD_FUNC(custom_category_message)
721 VTABLE_ADD_FUNC(custom_category_default_error_condition)
722 VTABLE_ADD_FUNC(custom_category_equivalent)
723 VTABLE_ADD_FUNC(custom_category_equivalent_code));
724 #ifndef __GNUC__
726 #endif
727 #endif
729 #if _MSVCP_VER >= 110
730 static CRITICAL_SECTION call_once_cs;
731 static CRITICAL_SECTION_DEBUG call_once_cs_debug =
733 0, 0, &call_once_cs,
734 { &call_once_cs_debug.ProcessLocksList, &call_once_cs_debug.ProcessLocksList },
735 0, 0, { (DWORD_PTR)(__FILE__ ": call_once_cs") }
737 static CRITICAL_SECTION call_once_cs = { &call_once_cs_debug, -1, 0, 0, 0, 0 };
739 void __cdecl _Call_onceEx(int *once, void (__cdecl *func)(void*), void *argv)
741 TRACE("%p %p %p\n", once, func, argv);
743 EnterCriticalSection(&call_once_cs);
744 if(!*once) {
745 /* FIXME: handle exceptions */
746 func(argv);
747 *once = 1;
749 LeaveCriticalSection(&call_once_cs);
752 static void __cdecl call_once_func_wrapper(void *func)
754 ((void (__cdecl*)(void))func)();
757 void __cdecl _Call_once(int *once, void (__cdecl *func)(void))
759 TRACE("%p %p\n", once, func);
760 _Call_onceEx(once, call_once_func_wrapper, func);
763 void __cdecl _Do_call(void *this)
765 CALL_VTBL_FUNC(this, 0, void, (void*), (this));
767 #endif
769 void init_misc(void *base)
771 #ifdef __x86_64__
772 #if _MSVCP_VER == 100
773 init_error_category_rtti(base);
774 init_iostream_category_rtti(base);
775 #endif
776 #endif
778 #if _MSVCP_VER == 100
779 iostream_category_ctor(&iostream_category);
780 #endif
783 void free_misc(void)
785 #if _MSVCP_VER >= 110
786 if(keyed_event)
787 NtClose(keyed_event);
788 #endif
791 #if _MSVCP_VER >= 110
792 typedef struct
794 HANDLE hnd;
795 DWORD id;
796 } _Thrd_t;
798 typedef int (__cdecl *_Thrd_start_t)(void*);
800 #define _THRD_ERROR 4
802 int __cdecl _Thrd_equal(_Thrd_t a, _Thrd_t b)
804 TRACE("(%p %u %p %u)\n", a.hnd, a.id, b.hnd, b.id);
805 return a.id == b.id;
808 int __cdecl _Thrd_lt(_Thrd_t a, _Thrd_t b)
810 TRACE("(%p %u %p %u)\n", a.hnd, a.id, b.hnd, b.id);
811 return a.id < b.id;
814 void __cdecl _Thrd_sleep(const xtime *t)
816 TRACE("(%p)\n", t);
817 Sleep(_Xtime_diff_to_millis(t));
820 void __cdecl _Thrd_yield(void)
822 TRACE("()\n");
823 Sleep(0);
826 static _Thrd_t thread_current(void)
828 _Thrd_t ret;
830 if(DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
831 GetCurrentProcess(), &ret.hnd, 0, FALSE, DUPLICATE_SAME_ACCESS)) {
832 CloseHandle(ret.hnd);
833 } else {
834 ret.hnd = 0;
836 ret.id = GetCurrentThreadId();
838 TRACE("(%p %u)\n", ret.hnd, ret.id);
839 return ret;
842 #ifndef __i386__
843 _Thrd_t __cdecl _Thrd_current(void)
845 return thread_current();
847 #else
848 ULONGLONG __cdecl _Thrd_current(void)
850 union {
851 _Thrd_t thr;
852 ULONGLONG ull;
853 } ret;
855 C_ASSERT(sizeof(_Thrd_t) <= sizeof(ULONGLONG));
857 ret.thr = thread_current();
858 return ret.ull;
860 #endif
862 int __cdecl _Thrd_join(_Thrd_t thr, int *code)
864 TRACE("(%p %u %p)\n", thr.hnd, thr.id, code);
865 if (WaitForSingleObject(thr.hnd, INFINITE))
866 return _THRD_ERROR;
868 if (code)
869 GetExitCodeThread(thr.hnd, (DWORD *)code);
871 CloseHandle(thr.hnd);
872 return 0;
875 int __cdecl _Thrd_create(_Thrd_t *thr, _Thrd_start_t proc, void *arg)
877 TRACE("(%p %p %p)\n", thr, proc, arg);
878 thr->hnd = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)proc, arg, 0, &thr->id);
879 return !thr->hnd;
881 #endif