[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Help-glpk] Re: re-entrant test version of GLPK
From: |
Andrew Makhorin |
Subject: |
[Help-glpk] Re: re-entrant test version of GLPK |
Date: |
Mon, 6 Oct 2003 15:20:52 +0300 |
Joonas,
Below here is a Win32 thread-safe version of glplib1 I made from your
patch. It is an example of what I meant talking about platform-specific
versions for various platforms. I think it would be much better to
choose an appropriate version of the code in Makefile rather than using
#ifdef programming.
Andrew Makhorin
------------------------------------------------------------------------
/* glplib1.c (Win32 thread-safe version) */
#include <stdio.h>
#include <windows.h>
#include "glplib.h"
static volatile int initialized=0;
static DWORD key=0;
static volatile DWORD l1=0;
static volatile DWORD l2=0;
void initialize_thread_objects_if_needed( void )
{
while ( initialized )
{
/* we could not allocate TslAlloc
* when the 1st thread was created
* so we do a little black magic,
* in order to make an atomic operation
* without locking primitives, since
* they are likewise required to
* be created before threads.
*
* I am doing what is called by 2 flag locking.
*/
DWORD me=GetCurrentThreadId();
if( l1 == 0)
{
l1=me;
if( l2 == 0 )
{
l2= me;
if( l1 == me)
{
if ( initialized )
{ key=TlsAlloc();
initialized=1;
}
l2=0;
if( l1 == me)
l1=0;
break;
}
else l2=0;
} else
l1=0;
}
Sleep(0);
}
}
void lib_set_ptr(void *ptr)
{
initialize_thread_objects_if_needed( ) ;
if( TlsSetValue( key , (LPVOID) ptr)== 0 )
{ printf("Tls write failed !\n");
exit( 1 );
}
}
void* lib_get_ptr(void)
{
void* aa; // (LPVOID)
initialize_thread_objects_if_needed( ) ;
aa=TlsGetValue(key );
return aa;
}
/* eof */