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
29 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcp
);
32 struct __Container_proxy
;
35 struct __Container_proxy
*proxy
;
38 typedef struct __Iterator_base12
{
39 struct __Container_proxy
*proxy
;
40 struct __Iterator_base12
*next
;
43 typedef struct __Container_proxy
{
44 const _Container_base12
*cont
;
45 _Iterator_base12
*head
;
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
));
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");
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
)
98 /* ?_Mutex_Unlock@_Mutex@std@@CAXPAV12@@Z */
99 /* ?_Mutex_Unlock@_Mutex@std@@CAXPEAV12@@Z */
100 void CDECL
mutex_mutex_unlock(mutex
*m
)
105 /* ?_Mutex_ctor@_Mutex@std@@CAXPAV12@@Z */
106 /* ?_Mutex_ctor@_Mutex@std@@CAXPEAV12@@Z */
107 void CDECL
mutex_mutex_ctor(mutex
*m
)
112 /* ?_Mutex_dtor@_Mutex@std@@CAXPAV12@@Z */
113 /* ?_Mutex_dtor@_Mutex@std@@CAXPEAV12@@Z */
114 void CDECL
mutex_mutex_dtor(mutex
*m
)
119 static CRITICAL_SECTION lockit_cs
[_MAX_LOCK
];
122 static inline int get_locktype( _Lockit
*lockit
) { return lockit
->locktype
; }
123 static inline void set_locktype( _Lockit
*lockit
, int type
) { lockit
->locktype
= type
; }
125 static inline int get_locktype( _Lockit
*lockit
) { return 0; }
126 static inline void set_locktype( _Lockit
*lockit
, int type
) { }
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) {
145 for(i
=0; i
<_MAX_LOCK
; i
++)
149 void free_lockit(void) {
152 for(i
=0; i
<_MAX_LOCK
; 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
);
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);
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);
205 unsigned short __cdecl
wctype(const char *property
)
207 static const struct {
211 { "alnum", _DIGIT
|_ALPHA
},
213 { "cntrl", _CONTROL
},
215 { "graph", _DIGIT
|_PUNCT
|_ALPHA
},
217 { "print", _DIGIT
|_PUNCT
|_BLANK
|_ALPHA
},
225 for(i
=0; i
<sizeof(properties
)/sizeof(properties
[0]); i
++)
226 if(!strcmp(property
, properties
[i
].name
))
227 return properties
[i
].mask
;
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
)
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
);
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
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
)
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
)
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)
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
)
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
;
347 this->proxy
= that
->proxy
;
351 this->proxy
->cont
= this;
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
;
374 /* _Xtime_get_ticks */
375 LONGLONG __cdecl
_Xtime_get_ticks(void)
381 GetSystemTimeAsFileTime(&ft
);
382 return ((LONGLONG
)ft
.dwHighDateTime
<<32) + ft
.dwLowDateTime
- TICKS_1601_TO_1970
;
386 int __cdecl
xtime_get(xtime
* t
, int unknown
)
395 ticks
= _Xtime_get_ticks();
396 t
->sec
= ticks
/ TICKSPERSEC
;
397 t
->nsec
= ticks
% TICKSPERSEC
* 100;
401 /* _Xtime_diff_to_millis2 */
402 MSVCRT_long __cdecl
_Xtime_diff_to_millis2(const xtime
*t1
, const xtime
*t2
)
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
)
424 return _Xtime_diff_to_millis2(t
, &now
);
429 unsigned int __cdecl
_Random_device(void)
435 /* TODO: throw correct exception in case of failure */
437 throw_exception(EXCEPTION
, "random number generator failed\n");
442 #if _MSVCP_VER >= 110
443 #if defined(__i386__) && !defined(__arm__)
445 extern void *call_thiscall_func
;
446 __ASM_GLOBAL_FUNC(call_thiscall_func
,
453 #define call_func1(func,this) ((void* (WINAPI*)(void*,void*))&call_thiscall_func)(func,this)
457 #define call_func1(func,this) func(this)
459 #endif /* __i386__ */
461 #define MTX_MULTI_LOCK 0x100
471 #if _MSVCP_VER >= 140
472 typedef _Mtx_t _Mtx_arg_t
;
473 #define MTX_T_FROM_ARG(m) (m)
474 #define MTX_T_TO_ARG(m) (m)
476 typedef _Mtx_t
*_Mtx_arg_t
;
477 #define MTX_T_FROM_ARG(m) (*(m))
478 #define MTX_T_TO_ARG(m) (&(m))
481 void __cdecl
_Mtx_init_in_situ(_Mtx_t mtx
, int flags
)
483 if(flags
& ~MTX_MULTI_LOCK
)
484 FIXME("unknown flags ignored: %x\n", flags
);
487 call_func1(critical_section_ctor
, &mtx
->cs
);
492 int __cdecl
_Mtx_init(_Mtx_t
*mtx
, int flags
)
494 *mtx
= MSVCRT_operator_new(sizeof(**mtx
));
495 _Mtx_init_in_situ(*mtx
, flags
);
499 void __cdecl
_Mtx_destroy_in_situ(_Mtx_t mtx
)
501 call_func1(critical_section_dtor
, &mtx
->cs
);
504 void __cdecl
_Mtx_destroy(_Mtx_arg_t mtx
)
506 call_func1(critical_section_dtor
, &MTX_T_FROM_ARG(mtx
)->cs
);
507 MSVCRT_operator_delete(MTX_T_FROM_ARG(mtx
));
510 int __cdecl
_Mtx_current_owns(_Mtx_arg_t mtx
)
512 return MTX_T_FROM_ARG(mtx
)->thread_id
== GetCurrentThreadId();
515 int __cdecl
_Mtx_lock(_Mtx_arg_t mtx
)
517 if(MTX_T_FROM_ARG(mtx
)->thread_id
!= GetCurrentThreadId()) {
518 call_func1(critical_section_lock
, &MTX_T_FROM_ARG(mtx
)->cs
);
519 MTX_T_FROM_ARG(mtx
)->thread_id
= GetCurrentThreadId();
520 }else if(!(MTX_T_FROM_ARG(mtx
)->flags
& MTX_MULTI_LOCK
)) {
524 MTX_T_FROM_ARG(mtx
)->count
++;
528 int __cdecl
_Mtx_unlock(_Mtx_arg_t mtx
)
530 if(--MTX_T_FROM_ARG(mtx
)->count
)
533 MTX_T_FROM_ARG(mtx
)->thread_id
= -1;
534 call_func1(critical_section_unlock
, &MTX_T_FROM_ARG(mtx
)->cs
);
538 int __cdecl
_Mtx_trylock(_Mtx_arg_t mtx
)
540 if(MTX_T_FROM_ARG(mtx
)->thread_id
!= GetCurrentThreadId()) {
541 if(!call_func1(critical_section_trylock
, &MTX_T_FROM_ARG(mtx
)->cs
))
543 MTX_T_FROM_ARG(mtx
)->thread_id
= GetCurrentThreadId();
544 }else if(!(MTX_T_FROM_ARG(mtx
)->flags
& MTX_MULTI_LOCK
)) {
548 MTX_T_FROM_ARG(mtx
)->count
++;
552 critical_section
* __cdecl
_Mtx_getconcrtcs(_Mtx_arg_t mtx
)
554 return &MTX_T_FROM_ARG(mtx
)->cs
;
557 static inline LONG
interlocked_dec_if_nonzero( LONG
*dest
)
560 for (val
= *dest
;; val
= tmp
)
562 if (!val
|| (tmp
= InterlockedCompareExchange( dest
, val
- 1, val
)) == val
)
568 #define CND_TIMEDOUT 2
572 CONDITION_VARIABLE cv
;
575 #if _MSVCP_VER >= 140
576 typedef _Cnd_t _Cnd_arg_t
;
577 #define CND_T_FROM_ARG(c) (c)
578 #define CND_T_TO_ARG(c) (c)
580 typedef _Cnd_t
*_Cnd_arg_t
;
581 #define CND_T_FROM_ARG(c) (*(c))
582 #define CND_T_TO_ARG(c) (&(c))
585 static HANDLE keyed_event
;
587 void __cdecl
_Cnd_init_in_situ(_Cnd_t cnd
)
589 InitializeConditionVariable(&cnd
->cv
);
594 NtCreateKeyedEvent(&event
, GENERIC_READ
|GENERIC_WRITE
, NULL
, 0);
595 if(InterlockedCompareExchangePointer(&keyed_event
, event
, NULL
) != NULL
)
600 int __cdecl
_Cnd_init(_Cnd_t
*cnd
)
602 *cnd
= MSVCRT_operator_new(sizeof(**cnd
));
603 _Cnd_init_in_situ(*cnd
);
607 int __cdecl
_Cnd_wait(_Cnd_arg_t cnd
, _Mtx_arg_t mtx
)
609 CONDITION_VARIABLE
*cv
= &CND_T_FROM_ARG(cnd
)->cv
;
611 InterlockedExchangeAdd( (LONG
*)&cv
->Ptr
, 1 );
614 NtWaitForKeyedEvent(keyed_event
, &cv
->Ptr
, FALSE
, NULL
);
620 int __cdecl
_Cnd_timedwait(_Cnd_arg_t cnd
, _Mtx_arg_t mtx
, const xtime
*xt
)
622 CONDITION_VARIABLE
*cv
= &CND_T_FROM_ARG(cnd
)->cv
;
623 LARGE_INTEGER timeout
;
626 InterlockedExchangeAdd( (LONG
*)&cv
->Ptr
, 1 );
629 timeout
.QuadPart
= (ULONGLONG
)_Xtime_diff_to_millis(xt
) * -10000;
630 status
= NtWaitForKeyedEvent(keyed_event
, &cv
->Ptr
, FALSE
, &timeout
);
633 if (!interlocked_dec_if_nonzero( (LONG
*)&cv
->Ptr
))
634 status
= NtWaitForKeyedEvent( keyed_event
, &cv
->Ptr
, FALSE
, NULL
);
638 return status
? CND_TIMEDOUT
: 0;
641 int __cdecl
_Cnd_broadcast(_Cnd_arg_t cnd
)
643 CONDITION_VARIABLE
*cv
= &CND_T_FROM_ARG(cnd
)->cv
;
644 LONG val
= InterlockedExchange( (LONG
*)&cv
->Ptr
, 0 );
646 NtReleaseKeyedEvent( keyed_event
, &cv
->Ptr
, FALSE
, NULL
);
650 int __cdecl
_Cnd_signal(_Cnd_arg_t cnd
)
652 CONDITION_VARIABLE
*cv
= &CND_T_FROM_ARG(cnd
)->cv
;
653 if (interlocked_dec_if_nonzero( (LONG
*)&cv
->Ptr
))
654 NtReleaseKeyedEvent( keyed_event
, &cv
->Ptr
, FALSE
, NULL
);
658 void __cdecl
_Cnd_destroy_in_situ(_Cnd_t cnd
)
660 _Cnd_broadcast(CND_T_TO_ARG(cnd
));
663 void __cdecl
_Cnd_destroy(_Cnd_arg_t cnd
)
667 MSVCRT_operator_delete(CND_T_FROM_ARG(cnd
));
675 struct _to_broadcast
{
681 } broadcast_at_thread_exit
;
683 static CRITICAL_SECTION broadcast_at_thread_exit_cs
;
684 static CRITICAL_SECTION_DEBUG broadcast_at_thread_exit_cs_debug
=
686 0, 0, &broadcast_at_thread_exit_cs
,
687 { &broadcast_at_thread_exit_cs_debug
.ProcessLocksList
, &broadcast_at_thread_exit_cs_debug
.ProcessLocksList
},
688 0, 0, { (DWORD_PTR
)(__FILE__
": broadcast_at_thread_exit_cs") }
690 static CRITICAL_SECTION broadcast_at_thread_exit_cs
= { &broadcast_at_thread_exit_cs_debug
, -1, 0, 0, 0, 0 };
692 void __cdecl
_Cnd_register_at_thread_exit(_Cnd_arg_t cnd
, _Mtx_arg_t mtx
, int *p
)
694 struct _to_broadcast
*add
;
696 TRACE("(%p %p %p)\n", cnd
, mtx
, p
);
698 EnterCriticalSection(&broadcast_at_thread_exit_cs
);
699 if(!broadcast_at_thread_exit
.size
) {
700 broadcast_at_thread_exit
.to_broadcast
= HeapAlloc(GetProcessHeap(),
701 0, 8*sizeof(broadcast_at_thread_exit
.to_broadcast
[0]));
702 if(!broadcast_at_thread_exit
.to_broadcast
) {
703 LeaveCriticalSection(&broadcast_at_thread_exit_cs
);
706 broadcast_at_thread_exit
.size
= 8;
707 } else if(broadcast_at_thread_exit
.size
== broadcast_at_thread_exit
.used
) {
708 add
= HeapReAlloc(GetProcessHeap(), 0, broadcast_at_thread_exit
.to_broadcast
,
709 broadcast_at_thread_exit
.size
*2*sizeof(broadcast_at_thread_exit
.to_broadcast
[0]));
711 LeaveCriticalSection(&broadcast_at_thread_exit_cs
);
714 broadcast_at_thread_exit
.to_broadcast
= add
;
715 broadcast_at_thread_exit
.size
*= 2;
718 add
= broadcast_at_thread_exit
.to_broadcast
+ broadcast_at_thread_exit
.used
++;
719 add
->thread_id
= GetCurrentThreadId();
723 LeaveCriticalSection(&broadcast_at_thread_exit_cs
);
726 void __cdecl
_Cnd_unregister_at_thread_exit(_Mtx_arg_t mtx
)
730 TRACE("(%p)\n", mtx
);
732 EnterCriticalSection(&broadcast_at_thread_exit_cs
);
733 for(i
=0; i
<broadcast_at_thread_exit
.used
; i
++) {
734 if(broadcast_at_thread_exit
.to_broadcast
[i
].mtx
!= mtx
)
737 memmove(broadcast_at_thread_exit
.to_broadcast
+i
, broadcast_at_thread_exit
.to_broadcast
+i
+1,
738 (broadcast_at_thread_exit
.used
-i
-1)*sizeof(broadcast_at_thread_exit
.to_broadcast
[0]));
739 broadcast_at_thread_exit
.used
--;
742 LeaveCriticalSection(&broadcast_at_thread_exit_cs
);
745 void __cdecl
_Cnd_do_broadcast_at_thread_exit(void)
747 int i
, id
= GetCurrentThreadId();
751 EnterCriticalSection(&broadcast_at_thread_exit_cs
);
752 for(i
=0; i
<broadcast_at_thread_exit
.used
; i
++) {
753 if(broadcast_at_thread_exit
.to_broadcast
[i
].thread_id
!= id
)
756 _Mtx_unlock(broadcast_at_thread_exit
.to_broadcast
[i
].mtx
);
757 _Cnd_broadcast(broadcast_at_thread_exit
.to_broadcast
[i
].cnd
);
758 if(broadcast_at_thread_exit
.to_broadcast
[i
].p
)
759 *broadcast_at_thread_exit
.to_broadcast
[i
].p
= 1;
761 memmove(broadcast_at_thread_exit
.to_broadcast
+i
, broadcast_at_thread_exit
.to_broadcast
+i
+1,
762 (broadcast_at_thread_exit
.used
-i
-1)*sizeof(broadcast_at_thread_exit
.to_broadcast
[0]));
763 broadcast_at_thread_exit
.used
--;
766 LeaveCriticalSection(&broadcast_at_thread_exit_cs
);
771 #if _MSVCP_VER == 100
773 const vtable_ptr
*vtable
;
780 static custom_category iostream_category
;
782 DEFINE_RTTI_DATA0(error_category
, 0, ".?AVerror_category@std@@")
783 DEFINE_RTTI_DATA1(iostream_category
, 0, &error_category_rtti_base_descriptor
, ".?AV_Iostream_error_category@std@@")
785 extern const vtable_ptr MSVCP_iostream_category_vtable
;
787 static void iostream_category_ctor(custom_category
*this)
789 this->base
.vtable
= &MSVCP_iostream_category_vtable
;
790 this->type
= "iostream";
793 DEFINE_THISCALL_WRAPPER(custom_category_vector_dtor
, 8)
794 custom_category
* __thiscall
custom_category_vector_dtor(custom_category
*this, unsigned int flags
)
796 TRACE("(%p %x)\n", this, flags
);
798 /* we have an array, with the number of elements stored before the first object */
799 INT_PTR i
, *ptr
= (INT_PTR
*)this-1;
801 for(i
=*ptr
-1; i
>=0; i
--)
802 MSVCRT_operator_delete(ptr
);
805 MSVCRT_operator_delete(this);
811 DEFINE_THISCALL_WRAPPER(custom_category_name
, 4)
812 const char* __thiscall
custom_category_name(const custom_category
*this)
817 DEFINE_THISCALL_WRAPPER(custom_category_message
, 12)
818 basic_string_char
* __thiscall
custom_category_message(const custom_category
*this,
819 basic_string_char
*ret
, int err
)
821 return MSVCP_basic_string_char_ctor_cstr(ret
, strerror(err
));
824 DEFINE_THISCALL_WRAPPER(custom_category_default_error_condition
, 12)
825 /*error_condition*/void* __thiscall
custom_category_default_error_condition(
826 custom_category
*this, /*error_condition*/void *ret
, int code
)
828 FIXME("(%p %p %x) stub\n", this, ret
, code
);
832 DEFINE_THISCALL_WRAPPER(custom_category_equivalent
, 12)
833 MSVCP_bool __thiscall
custom_category_equivalent(const custom_category
*this,
834 int code
, const /*error_condition*/void *condition
)
836 FIXME("(%p %x %p) stub\n", this, code
, condition
);
840 DEFINE_THISCALL_WRAPPER(custom_category_equivalent_code
, 12)
841 MSVCP_bool __thiscall
custom_category_equivalent_code(custom_category
*this,
842 const /*error_code*/void *code
, int condition
)
844 FIXME("(%p %p %x) stub\n", this, code
, condition
);
848 DEFINE_THISCALL_WRAPPER(iostream_category_message
, 12)
849 basic_string_char
* __thiscall
iostream_category_message(const custom_category
*this,
850 basic_string_char
*ret
, int err
)
852 if(err
== 1) return MSVCP_basic_string_char_ctor_cstr(ret
, "iostream error");
853 return MSVCP_basic_string_char_ctor_cstr(ret
, strerror(err
));
856 /* ?iostream_category@std@@YAABVerror_category@1@XZ */
857 /* ?iostream_category@std@@YAAEBVerror_category@1@XZ */
858 const error_category
* __cdecl
std_iostream_category(void)
861 return &iostream_category
.base
;
864 static custom_category system_category
;
865 DEFINE_RTTI_DATA1(system_category
, 0, &error_category_rtti_base_descriptor
, ".?AV_System_error_category@std@@")
867 extern const vtable_ptr MSVCP_system_category_vtable
;
869 static void system_category_ctor(custom_category
*this)
871 this->base
.vtable
= &MSVCP_system_category_vtable
;
872 this->type
= "system";
875 /* ?system_category@std@@YAABVerror_category@1@XZ */
876 /* ?system_category@std@@YAAEBVerror_category@1@XZ */
877 const error_category
* __cdecl
std_system_category(void)
880 return &system_category
.base
;
883 static custom_category generic_category
;
884 DEFINE_RTTI_DATA1(generic_category
, 0, &error_category_rtti_base_descriptor
, ".?AV_Generic_error_category@std@@")
886 extern const vtable_ptr MSVCP_generic_category_vtable
;
888 static void generic_category_ctor(custom_category
*this)
890 this->base
.vtable
= &MSVCP_generic_category_vtable
;
891 this->type
= "generic";
894 /* ?generic_category@std@@YAABVerror_category@1@XZ */
895 /* ?generic_category@std@@YAAEBVerror_category@1@XZ */
896 const error_category
* __cdecl
std_generic_category(void)
899 return &generic_category
.base
;
903 #if _MSVCP_VER >= 110
904 static CRITICAL_SECTION call_once_cs
;
905 static CRITICAL_SECTION_DEBUG call_once_cs_debug
=
908 { &call_once_cs_debug
.ProcessLocksList
, &call_once_cs_debug
.ProcessLocksList
},
909 0, 0, { (DWORD_PTR
)(__FILE__
": call_once_cs") }
911 static CRITICAL_SECTION call_once_cs
= { &call_once_cs_debug
, -1, 0, 0, 0, 0 };
913 void __cdecl
_Call_onceEx(int *once
, void (__cdecl
*func
)(void*), void *argv
)
915 TRACE("%p %p %p\n", once
, func
, argv
);
917 EnterCriticalSection(&call_once_cs
);
919 /* FIXME: handle exceptions */
923 LeaveCriticalSection(&call_once_cs
);
926 static void __cdecl
call_once_func_wrapper(void *func
)
928 ((void (__cdecl
*)(void))func
)();
931 void __cdecl
_Call_once(int *once
, void (__cdecl
*func
)(void))
933 TRACE("%p %p\n", once
, func
);
934 _Call_onceEx(once
, call_once_func_wrapper
, func
);
937 void __cdecl
_Do_call(void *this)
939 CALL_VTBL_FUNC(this, 0, void, (void*), (this));
943 #if _MSVCP_VER >= 110
950 typedef int (__cdecl
*_Thrd_start_t
)(void*);
952 #define _THRD_ERROR 4
954 int __cdecl
_Thrd_equal(_Thrd_t a
, _Thrd_t b
)
956 TRACE("(%p %u %p %u)\n", a
.hnd
, a
.id
, b
.hnd
, b
.id
);
960 int __cdecl
_Thrd_lt(_Thrd_t a
, _Thrd_t b
)
962 TRACE("(%p %u %p %u)\n", a
.hnd
, a
.id
, b
.hnd
, b
.id
);
966 void __cdecl
_Thrd_sleep(const xtime
*t
)
969 Sleep(_Xtime_diff_to_millis(t
));
972 void __cdecl
_Thrd_yield(void)
978 static _Thrd_t
thread_current(void)
982 if(DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
983 GetCurrentProcess(), &ret
.hnd
, 0, FALSE
, DUPLICATE_SAME_ACCESS
)) {
984 CloseHandle(ret
.hnd
);
988 ret
.id
= GetCurrentThreadId();
990 TRACE("(%p %u)\n", ret
.hnd
, ret
.id
);
995 _Thrd_t __cdecl
_Thrd_current(void)
997 return thread_current();
1000 ULONGLONG __cdecl
_Thrd_current(void)
1007 C_ASSERT(sizeof(_Thrd_t
) <= sizeof(ULONGLONG
));
1009 ret
.thr
= thread_current();
1014 int __cdecl
_Thrd_join(_Thrd_t thr
, int *code
)
1016 TRACE("(%p %u %p)\n", thr
.hnd
, thr
.id
, code
);
1017 if (WaitForSingleObject(thr
.hnd
, INFINITE
))
1021 GetExitCodeThread(thr
.hnd
, (DWORD
*)code
);
1023 CloseHandle(thr
.hnd
);
1027 int __cdecl
_Thrd_start(_Thrd_t
*thr
, LPTHREAD_START_ROUTINE proc
, void *arg
)
1029 TRACE("(%p %p %p)\n", thr
, proc
, arg
);
1030 thr
->hnd
= CreateThread(NULL
, 0, proc
, arg
, 0, &thr
->id
);
1031 return thr
->hnd
? 0 : _THRD_ERROR
;
1040 static DWORD WINAPI
thread_proc_wrapper(void *arg
)
1042 thread_proc_arg wrapped_arg
= *((thread_proc_arg
*)arg
);
1044 return wrapped_arg
.proc(wrapped_arg
.arg
);
1047 int __cdecl
_Thrd_create(_Thrd_t
*thr
, _Thrd_start_t proc
, void *arg
)
1049 thread_proc_arg
*wrapped_arg
;
1052 TRACE("(%p %p %p)\n", thr
, proc
, arg
);
1054 wrapped_arg
= malloc(sizeof(*wrapped_arg
));
1056 return _THRD_ERROR
; /* TODO: probably different error should be returned here */
1058 wrapped_arg
->proc
= proc
;
1059 wrapped_arg
->arg
= arg
;
1060 ret
= _Thrd_start(thr
, thread_proc_wrapper
, wrapped_arg
);
1061 if(ret
) free(wrapped_arg
);
1065 int __cdecl
_Thrd_detach(_Thrd_t thr
)
1067 return CloseHandle(thr
.hnd
) ? 0 : _THRD_ERROR
;
1072 const vtable_ptr
*vtable
;
1075 MSVCP_bool launched
;
1078 DEFINE_RTTI_DATA0(_Pad
, 0, ".?AV_Pad@std@@")
1080 /* ??_7_Pad@std@@6B@ */
1081 extern const vtable_ptr MSVCP__Pad_vtable
;
1083 unsigned int __cdecl
_Thrd_hardware_concurrency(void)
1085 static unsigned int val
= -1;
1093 val
= si
.dwNumberOfProcessors
;
1099 unsigned int __cdecl
_Thrd_id(void)
1102 return GetCurrentThreadId();
1105 /* ??0_Pad@std@@QAE@XZ */
1106 /* ??0_Pad@std@@QEAA@XZ */
1107 DEFINE_THISCALL_WRAPPER(_Pad_ctor
, 4)
1108 _Pad
* __thiscall
_Pad_ctor(_Pad
*this)
1110 TRACE("(%p)\n", this);
1112 this->vtable
= &MSVCP__Pad_vtable
;
1113 _Cnd_init(&this->cnd
);
1114 _Mtx_init(&this->mtx
, 0);
1115 this->launched
= FALSE
;
1116 _Mtx_lock(MTX_T_TO_ARG(this->mtx
));
1120 /* ??4_Pad@std@@QAEAAV01@ABV01@@Z */
1121 /* ??4_Pad@std@@QEAAAEAV01@AEBV01@@Z */
1122 DEFINE_THISCALL_WRAPPER(_Pad_op_assign
, 8)
1123 _Pad
* __thiscall
_Pad_op_assign(_Pad
*this, const _Pad
*copy
)
1125 TRACE("(%p %p)\n", this, copy
);
1127 this->cnd
= copy
->cnd
;
1128 this->mtx
= copy
->mtx
;
1129 this->launched
= copy
->launched
;
1133 /* ??0_Pad@std@@QAE@ABV01@@Z */
1134 /* ??0_Pad@std@@QEAA@AEBV01@@Z */
1135 DEFINE_THISCALL_WRAPPER(_Pad_copy_ctor
, 8)
1136 _Pad
* __thiscall
_Pad_copy_ctor(_Pad
*this, const _Pad
*copy
)
1138 TRACE("(%p %p)\n", this, copy
);
1140 this->vtable
= &MSVCP__Pad_vtable
;
1141 return _Pad_op_assign(this, copy
);
1144 /* ??1_Pad@std@@QAE@XZ */
1145 /* ??1_Pad@std@@QEAA@XZ */
1146 DEFINE_THISCALL_WRAPPER(_Pad_dtor
, 4)
1147 void __thiscall
_Pad_dtor(_Pad
*this)
1149 TRACE("(%p)\n", this);
1151 _Mtx_unlock(MTX_T_TO_ARG(this->mtx
));
1152 _Mtx_destroy(MTX_T_TO_ARG(this->mtx
));
1153 _Cnd_destroy(CND_T_TO_ARG(this->cnd
));
1156 DEFINE_THISCALL_WRAPPER(_Pad__Go
, 4)
1157 #define call__Pad__Go(this) CALL_VTBL_FUNC(this, 0, unsigned int, (_Pad*), (this))
1158 unsigned int __thiscall
_Pad__Go(_Pad
*this)
1160 ERR("(%p) should not be called\n", this);
1164 static DWORD WINAPI
launch_thread_proc(void *arg
)
1167 return call__Pad__Go(this);
1170 /* ?_Launch@_Pad@std@@QAEXPAU_Thrd_imp_t@@@Z */
1171 /* ?_Launch@_Pad@std@@QEAAXPEAU_Thrd_imp_t@@@Z */
1172 DEFINE_THISCALL_WRAPPER(_Pad__Launch
, 8)
1173 void __thiscall
_Pad__Launch(_Pad
*this, _Thrd_t
*thr
)
1175 TRACE("(%p %p)\n", this, thr
);
1177 _Thrd_start(thr
, launch_thread_proc
, this);
1178 _Cnd_wait(CND_T_TO_ARG(this->cnd
), MTX_T_TO_ARG(this->mtx
));
1181 /* ?_Release@_Pad@std@@QAEXXZ */
1182 /* ?_Release@_Pad@std@@QEAAXXZ */
1183 DEFINE_THISCALL_WRAPPER(_Pad__Release
, 4)
1184 void __thiscall
_Pad__Release(_Pad
*this)
1186 TRACE("(%p)\n", this);
1188 _Mtx_lock(MTX_T_TO_ARG(this->mtx
));
1189 this->launched
= TRUE
;
1190 _Cnd_signal(CND_T_TO_ARG(this->cnd
));
1191 _Mtx_unlock(MTX_T_TO_ARG(this->mtx
));
1196 void __asm_dummy_vtables(void) {
1198 #if _MSVCP_VER == 100
1199 __ASM_VTABLE(iostream_category
,
1200 VTABLE_ADD_FUNC(custom_category_vector_dtor
)
1201 VTABLE_ADD_FUNC(custom_category_name
)
1202 VTABLE_ADD_FUNC(iostream_category_message
)
1203 VTABLE_ADD_FUNC(custom_category_default_error_condition
)
1204 VTABLE_ADD_FUNC(custom_category_equivalent
)
1205 VTABLE_ADD_FUNC(custom_category_equivalent_code
));
1206 __ASM_VTABLE(system_category
,
1207 VTABLE_ADD_FUNC(custom_category_vector_dtor
)
1208 VTABLE_ADD_FUNC(custom_category_name
)
1209 VTABLE_ADD_FUNC(custom_category_message
)
1210 VTABLE_ADD_FUNC(custom_category_default_error_condition
)
1211 VTABLE_ADD_FUNC(custom_category_equivalent
)
1212 VTABLE_ADD_FUNC(custom_category_equivalent_code
));
1213 __ASM_VTABLE(generic_category
,
1214 VTABLE_ADD_FUNC(custom_category_vector_dtor
)
1215 VTABLE_ADD_FUNC(custom_category_name
)
1216 VTABLE_ADD_FUNC(custom_category_message
)
1217 VTABLE_ADD_FUNC(custom_category_default_error_condition
)
1218 VTABLE_ADD_FUNC(custom_category_equivalent
)
1219 VTABLE_ADD_FUNC(custom_category_equivalent_code
));
1221 #if _MSVCP_VER >= 110
1223 VTABLE_ADD_FUNC(_Pad__Go
));
1229 /*********************************************************************
1230 * __crtInitializeCriticalSectionEx (MSVCP140.@)
1232 BOOL CDECL
MSVCP__crtInitializeCriticalSectionEx(
1233 CRITICAL_SECTION
*cs
, DWORD spin_count
, DWORD flags
)
1235 TRACE("(%p %x %x)\n", cs
, spin_count
, flags
);
1236 return InitializeCriticalSectionEx(cs
, spin_count
, flags
);
1239 /*********************************************************************
1240 * __crtCreateEventExW (MSVCP140.@)
1242 HANDLE CDECL
MSVCP__crtCreateEventExW(
1243 SECURITY_ATTRIBUTES
*attribs
, LPCWSTR name
, DWORD flags
, DWORD access
)
1245 TRACE("(%p %s 0x%08x 0x%08x)\n", attribs
, debugstr_w(name
), flags
, access
);
1246 return CreateEventExW(attribs
, name
, flags
, access
);
1249 /*********************************************************************
1250 * __crtGetTickCount64 (MSVCP140.@)
1252 ULONGLONG CDECL
MSVCP__crtGetTickCount64(void)
1254 return GetTickCount64();
1257 /*********************************************************************
1258 * __crtCreateSemaphoreExW (MSVCP140.@)
1260 HANDLE CDECL
MSVCP__crtCreateSemaphoreExW(
1261 SECURITY_ATTRIBUTES
*attribs
, LONG initial_count
, LONG max_count
, LPCWSTR name
,
1262 DWORD flags
, DWORD access
)
1264 TRACE("(%p %d %d %s 0x%08x 0x%08x)\n", attribs
, initial_count
, max_count
, debugstr_w(name
),
1266 return CreateSemaphoreExW(attribs
, initial_count
, max_count
, name
, flags
, access
);
1269 /* ?_Execute_once@std@@YAHAAUonce_flag@1@P6GHPAX1PAPAX@Z1@Z */
1270 /* ?_Execute_once@std@@YAHAEAUonce_flag@1@P6AHPEAX1PEAPEAX@Z1@Z */
1271 BOOL __cdecl
_Execute_once(INIT_ONCE
*flag
, PINIT_ONCE_FN func
, void *param
)
1273 return InitOnceExecuteOnce(flag
, func
, param
, NULL
);
1276 void init_misc(void *base
)
1279 #if _MSVCP_VER == 100
1280 init_error_category_rtti(base
);
1281 init_iostream_category_rtti(base
);
1282 init_system_category_rtti(base
);
1283 init_generic_category_rtti(base
);
1285 #if _MSVCP_VER >= 110
1286 init__Pad_rtti(base
);
1290 #if _MSVCP_VER == 100
1291 iostream_category_ctor(&iostream_category
);
1292 system_category_ctor(&system_category
);
1293 generic_category_ctor(&generic_category
);
1297 void free_misc(void)
1299 #if _MSVCP_VER >= 110
1301 NtClose(keyed_event
);
1302 HeapFree(GetProcessHeap(), 0, broadcast_at_thread_exit
.to_broadcast
);
1306 #if _MSVCP_VER >= 140
1307 LONGLONG __cdecl
_Query_perf_counter(void)
1310 QueryPerformanceCounter(&li
);
1314 LONGLONG __cdecl
_Query_perf_frequency(void)
1317 QueryPerformanceFrequency(&li
);
1322 void __cdecl
threads__Mtx_new(void **mtx
)
1324 *mtx
= MSVCRT_operator_new(sizeof(CRITICAL_SECTION
));
1325 InitializeCriticalSection(*mtx
);
1328 void __cdecl
threads__Mtx_delete(void *mtx
)
1330 DeleteCriticalSection(mtx
);
1333 void __cdecl
threads__Mtx_lock(void *mtx
)
1335 EnterCriticalSection(mtx
);
1338 void __cdecl
threads__Mtx_unlock(void *mtx
)
1340 LeaveCriticalSection(mtx
);