Don't return from overlapped ReadFile on EAGAIN and other non-fatal
[wine/gsoc_dplay.git] / dlls / msvcrt / main.c
blob1dad326ef13c3da52bba429a2ee463572c2c31b5
1 /*
2 * msvcrt.dll initialisation functions
4 * Copyright 2000 Jon Griffiths
5 */
6 #include "msvcrt.h"
8 #include "msvcrt/locale.h"
9 #include "msvcrt/stdio.h"
12 DEFAULT_DEBUG_CHANNEL(msvcrt);
14 /* Index to TLS */
15 DWORD MSVCRT_tls_index;
17 /* MT locks */
18 CRITICAL_SECTION MSVCRT_heap_cs;
19 CRITICAL_SECTION MSVCRT_file_cs;
20 CRITICAL_SECTION MSVCRT_exit_cs;
21 CRITICAL_SECTION MSVCRT_console_cs;
22 CRITICAL_SECTION MSVCRT_locale_cs;
24 static inline BOOL msvcrt_init_tls(void);
25 static inline BOOL msvcrt_free_tls(void);
26 static inline void msvcrt_init_critical_sections(void);
27 static inline void msvcrt_free_critical_sections(void);
28 const char* msvcrt_get_reason(DWORD reason) WINE_UNUSED;
30 void msvcrt_init_io(void);
31 void msvcrt_init_console(void);
32 void msvcrt_free_console(void);
33 void msvcrt_init_args(void);
34 void msvcrt_free_args(void);
35 void msvcrt_init_vtables(void);
38 /*********************************************************************
39 * Init
41 BOOL WINAPI MSVCRT_Init(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
43 MSVCRT_thread_data *tls;
45 TRACE("(0x%08x, %s, %p) pid(%ld), tid(%ld), tls(%ld)\n",
46 hinstDLL, msvcrt_get_reason(fdwReason), lpvReserved,
47 (long)GetCurrentProcessId(), (long)GetCurrentThreadId(),
48 (long)MSVCRT_tls_index);
50 switch (fdwReason)
52 case DLL_PROCESS_ATTACH:
53 if (!msvcrt_init_tls())
54 return FALSE;
55 msvcrt_init_vtables();
56 msvcrt_init_critical_sections();
57 msvcrt_init_io();
58 msvcrt_init_console();
59 msvcrt_init_args();
60 MSVCRT_setlocale(0, "C");
61 TRACE("finished process init\n");
62 /* FALL THROUGH for Initial TLS allocation!! */
63 case DLL_THREAD_ATTACH:
64 TRACE("starting thread init\n");
65 /* Create TLS */
66 tls = (MSVCRT_thread_data*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
67 sizeof(MSVCRT_thread_data));
68 if (!tls || !TlsSetValue(MSVCRT_tls_index, tls))
70 ERR("TLS init failed! error = %ld\n", GetLastError());
71 return FALSE;
73 TRACE("finished thread init\n");
74 break;
75 case DLL_PROCESS_DETACH:
76 msvcrt_free_critical_sections();
77 _fcloseall();
78 msvcrt_free_console();
79 msvcrt_free_args();
80 if (!msvcrt_free_tls())
81 return FALSE;
82 TRACE("finished process free\n");
83 break;
84 case DLL_THREAD_DETACH:
85 /* Free TLS */
86 tls = TlsGetValue(MSVCRT_tls_index);
88 if (!tls)
90 ERR("TLS free failed! error = %ld\n", GetLastError());
91 return FALSE;
93 HeapFree(GetProcessHeap(), 0, tls);
94 TRACE("finished thread free\n");
95 break;
97 return TRUE;
100 static inline BOOL msvcrt_init_tls(void)
102 MSVCRT_tls_index = TlsAlloc();
104 if (MSVCRT_tls_index == TLS_OUT_OF_INDEXES)
106 ERR("TlsAlloc() failed!\n");
107 return FALSE;
109 return TRUE;
112 static inline BOOL msvcrt_free_tls(void)
114 if (!TlsFree(MSVCRT_tls_index))
116 ERR("TlsFree() failed!\n");
117 return FALSE;
119 return TRUE;
122 static inline void msvcrt_init_critical_sections(void)
124 InitializeCriticalSectionAndSpinCount(&MSVCRT_heap_cs, 4000);
125 InitializeCriticalSection(&MSVCRT_file_cs);
126 InitializeCriticalSection(&MSVCRT_exit_cs);
127 InitializeCriticalSection(&MSVCRT_console_cs);
128 InitializeCriticalSection(&MSVCRT_locale_cs);
131 static inline void msvcrt_free_critical_sections(void)
133 DeleteCriticalSection(&MSVCRT_locale_cs);
134 DeleteCriticalSection(&MSVCRT_console_cs);
135 DeleteCriticalSection(&MSVCRT_exit_cs);
136 DeleteCriticalSection(&MSVCRT_file_cs);
137 DeleteCriticalSection(&MSVCRT_heap_cs);
140 const char* msvcrt_get_reason(DWORD reason)
142 switch (reason)
144 case DLL_PROCESS_ATTACH: return "DLL_PROCESS_ATTACH";
145 case DLL_PROCESS_DETACH: return "DLL_PROCESS_DETACH";
146 case DLL_THREAD_ATTACH: return "DLL_THREAD_ATTACH";
147 case DLL_THREAD_DETACH: return "DLL_THREAD_DETACH";
149 return "UNKNOWN";
153 /*********************************************************************
154 * $I10_OUTPUT (MSVCRT.@)
155 * Function not really understood but needed to make the DLL work
157 void MSVCRT_I10_OUTPUT(void)
159 /* FIXME: This is probably data, not a function */
162 /*********************************************************************
163 * __unDName (MSVCRT.@)
164 * Function not really understood but needed to make the DLL work
166 void MSVCRT___unDName(void)
168 /* Called by all VC compiled progs on startup. No idea what it does */
171 /*********************************************************************
172 * __unDNameEx (MSVCRT.@)
173 * Function not really understood but needed to make the DLL work
175 void MSVCRT___unDNameEx(void)
177 /* As above */