Skip to content
This repository has been archived by the owner on Jun 1, 2022. It is now read-only.

Use a constructor in pread.c to ensure the critical section is always initialized #47

Merged
merged 2 commits into from
Jan 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libGeoIP/Makefile.vc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CFLAGS=-DWIN32 -DGEOIP_EXPORTS -MD -nologo

GEOIPINC = -I..\libGeoIP

CC1 = $(COMPILER) $(CFLAGS) $(GEOIPINC) -DGEOIPDATADIR=\"$(GEOIPDATADIR)\" -DPACKAGE_VERSION=\"1.6.0\"
CC1 = $(COMPILER) $(CFLAGS) $(GEOIPINC) -DGEOIPDATADIR=\"$(GEOIPDATADIR)\" -DPACKAGE_VERSION=\"1.6.3\"

LINKER=link

Expand Down
36 changes: 30 additions & 6 deletions libGeoIP/pread.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,32 @@
*/

#include <windows.h>
#include <stdio.h>
#include <io.h>

#include "pread.h"

CRITICAL_SECTION preadsc;
static CRITICAL_SECTION preadsc;

/* http://stackoverflow.com/a/2390626/1392778 */

#ifdef _MSC_VER

#pragma section(".CRT$XCU",read)

#define INITIALIZER(f) \
static void __cdecl f(void); \
__declspec(allocate(".CRT$XCU")) void (__cdecl*f##_)(void) = f; \
static void __cdecl f(void)

#elif defined(__GNUC__)

#define INITIALIZER(f) \
static void f(void) __attribute__((constructor)); \
static void f(void)

#endif


#ifdef _WIN64
int pread(int fd, void *buf, unsigned int nbyte, __int64 offset)
Expand Down Expand Up @@ -71,10 +92,13 @@ int pread(int fd, void *buf, unsigned int nbyte, long offset)
}
#endif

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved )
static void deinitialize(void)
{
if (fdwReason == DLL_PROCESS_ATTACH) {
InitializeCriticalSection(&preadsc);
}
return TRUE;
DeleteCriticalSection(&preadsc);
}

INITIALIZER(initialize)
{
InitializeCriticalSection(&preadsc);
atexit(deinitialize);
}
2 changes: 0 additions & 2 deletions libGeoIP/pread.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,3 @@ int pread(int fd, void *buf, unsigned int nbyte, long offset);
#endif

#define HAVE_PREAD

extern CRITICAL_SECTION preadsc;