public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
* Thread local storage problem, and fix
@ 2001-08-22 11:51 Phil Frisbie, Jr.
  2001-08-22 22:46 ` Ross Johnson
  2001-08-23  0:41 ` Thomas Pfaff
  0 siblings, 2 replies; 5+ messages in thread
From: Phil Frisbie, Jr. @ 2001-08-22 11:51 UTC (permalink / raw)
  To: pthreads-win32

While converting the simple per thread error storage in HawkNL over to POSIX and
native Windows thread local storage, I came across some problems. I store a
HawkNL error code for the thread, and if it is a Winsock error I set the error
to NL_SOCKET_ERROR so the application can retrieve the real error code. But
Winsock errors seemed to be cleared whenever pthread_getspecific() was called.
They were also cleared by calling the native Windows TlsGetValue().

So, I looked at the Pthreads-win32 source code for pthread_getspecific(), and
found this:

void *
pthread_getspecific (pthread_key_t key)
{
  int lasterror = GetLastError();

  void *ptr = TlsGetValue (key->key);

  SetLastError( lasterror );

  return ptr;
}

I guess someone already found out TlsGetValue() clears out the last error, but
they did not know it also clears out the last Winsock error. The fix is to
change the code to:

void *
pthread_getspecific (pthread_key_t key)
{
  int lasterror = GetLastError();
  int lastWSAerror = WSAGetLastError();

  void *ptr = TlsGetValue (key->key);

  SetLastError( lasterror );
  WSASetLastError( lastWSAerror );

  return ptr;
}

I have added a work around to HawkNL until this gets fixed.


Phil Frisbie, Jr.
Lead Developer, Hawk Software
http://www.hawksoft.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Thread local storage problem, and fix
  2001-08-22 11:51 Thread local storage problem, and fix Phil Frisbie, Jr.
@ 2001-08-22 22:46 ` Ross Johnson
  2001-08-23  0:41 ` Thomas Pfaff
  1 sibling, 0 replies; 5+ messages in thread
From: Ross Johnson @ 2001-08-22 22:46 UTC (permalink / raw)
  To: Phil Frisbie, Jr.; +Cc: pthreads-win32

Hi,

I've added your change to the current CVS repository version.

Thanks.
Ross

"Phil Frisbie, Jr." wrote:
> 
> While converting the simple per thread error storage in HawkNL over to POSIX and
> native Windows thread local storage, I came across some problems. I store a
> HawkNL error code for the thread, and if it is a Winsock error I set the error
> to NL_SOCKET_ERROR so the application can retrieve the real error code. But
> Winsock errors seemed to be cleared whenever pthread_getspecific() was called.
> They were also cleared by calling the native Windows TlsGetValue().
> 
> So, I looked at the Pthreads-win32 source code for pthread_getspecific(), and
> found this:
> 
> void *
> pthread_getspecific (pthread_key_t key)
> {
>   int lasterror = GetLastError();
> 
>   void *ptr = TlsGetValue (key->key);
> 
>   SetLastError( lasterror );
> 
>   return ptr;
> }
> 
> I guess someone already found out TlsGetValue() clears out the last error, but
> they did not know it also clears out the last Winsock error. The fix is to
> change the code to:
> 
> void *
> pthread_getspecific (pthread_key_t key)
> {
>   int lasterror = GetLastError();
>   int lastWSAerror = WSAGetLastError();
> 
>   void *ptr = TlsGetValue (key->key);
> 
>   SetLastError( lasterror );
>   WSASetLastError( lastWSAerror );
> 
>   return ptr;
> }
> 
> I have added a work around to HawkNL until this gets fixed.
> 
> Phil Frisbie, Jr.
> Lead Developer, Hawk Software
> http://www.hawksoft.com

-- 
+-------------------------+---+
| Ross Johnson            |   | "Come down off the cross
| Management & Technology |___|  We can use the wood" - Tom Waits
| Building 11                 |
| University of Canberra      | eMail: rpj@ise.canberra.edu.au
| ACT    2601                 | WWW:  
http://public.ise.canberra.edu.au/~rpj/
| AUSTRALIA                   |
+-----------------------------+

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Thread local storage problem, and fix
  2001-08-22 11:51 Thread local storage problem, and fix Phil Frisbie, Jr.
  2001-08-22 22:46 ` Ross Johnson
@ 2001-08-23  0:41 ` Thomas Pfaff
  2001-08-23  7:43   ` Phil Frisbie, Jr.
  1 sibling, 1 reply; 5+ messages in thread
From: Thomas Pfaff @ 2001-08-23  0:41 UTC (permalink / raw)
  To: Phil Frisbie, Jr.; +Cc: pthreads-win32

On Wed, 22 Aug 2001, Phil Frisbie, Jr. wrote:

> While converting the simple per thread error storage in HawkNL over to
> POSIX and
> native Windows thread local storage, I came across some problems. I
> store a
> HawkNL error code for the thread, and if it is a Winsock error I set the
> error
> to NL_SOCKET_ERROR so the application can retrieve the real error code.
> But
> Winsock errors seemed to be cleared whenever pthread_getspecific() was
> called.
> They were also cleared by calling the native Windows TlsGetValue().
>
> So, I looked at the Pthreads-win32 source code for
> pthread_getspecific(), and
> found this:
>
> void *
> pthread_getspecific (pthread_key_t key)
> {
>   int lasterror = GetLastError();
>
>   void *ptr = TlsGetValue (key->key);
>
>   SetLastError( lasterror );
>
>   return ptr;
> }
>
> I guess someone already found out TlsGetValue() clears out the last
> error, but
> they did not know it also clears out the last Winsock error. The fix is
> to
> change the code to:
>
> void *
> pthread_getspecific (pthread_key_t key)
> {
>   int lasterror = GetLastError();
>   int lastWSAerror = WSAGetLastError();
>
>   void *ptr = TlsGetValue (key->key);
>
>   SetLastError( lasterror );
>   WSASetLastError( lastWSAerror );
>
>   return ptr;
> }
>
> I have added a work around to HawkNL until this gets fixed.
>
>
> Phil Frisbie, Jr.
> Lead Developer, Hawk Software
> http://www.hawksoft.com
>

strange,

at least on NT4 the WSALastError equals to Win32 LastError when i used
Winsock2, so i did not saved it too.

I will have a look if you tell me your platform.

Greetings,

Thomas





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Thread local storage problem, and fix
  2001-08-23  0:41 ` Thomas Pfaff
@ 2001-08-23  7:43   ` Phil Frisbie, Jr.
  0 siblings, 0 replies; 5+ messages in thread
From: Phil Frisbie, Jr. @ 2001-08-23  7:43 UTC (permalink / raw)
  To: Thomas Pfaff; +Cc: pthreads-win32

I am using Winsock 1.1 on Windows NT 4. I have not yet tried Windows
9x/ME or 2000.

Thomas Pfaff wrote:
> 
> On Wed, 22 Aug 2001, Phil Frisbie, Jr. wrote:
>
> strange,
> 
> at least on NT4 the WSALastError equals to Win32 LastError when i used
> Winsock2, so i did not saved it too.
> 
> I will have a look if you tell me your platform.
> 
> Greetings,
> 
> Thomas


Phil Frisbie, Jr.
Hawk Software
http://www.hawksoft.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Thread local storage problem, and fix
       [not found] <F0E13277A26BD311944600500454CCD034003B@antarctica.intern.net>
@ 2001-08-27  2:05 ` Thomas Pfaff
  0 siblings, 0 replies; 5+ messages in thread
From: Thomas Pfaff @ 2001-08-27  2:05 UTC (permalink / raw)
  To: Phil Frisbie, Jr.; +Cc: pthreads-win32

[-- Attachment #1: Type: text/plain, Size: 784 bytes --]

I can not reproduce your error. I have written a small test program and
regardless whether i link against ws2_32.lib or wsock32.lib WSALastError
and Win32 LastError returns the same value. I have attached it, so you can
compile it and send me its output.

Cheers,

Thomas

On Thu, 23 Aug 2001, Phil Frisbie, Jr. wrote:

> I am using Winsock 1.1 on Windows NT 4. I have not yet tried Windows
> 9x/ME or 2000.
>
> Thomas Pfaff wrote:
> >
> > On Wed, 22 Aug 2001, Phil Frisbie, Jr. wrote:
> >
> > strange,
> >
> > at least on NT4 the WSALastError equals to Win32 LastError when i used
> > Winsock2, so i did not saved it too.
> >
> > I will have a look if you tell me your platform.
> >
> > Greetings,
> >
> > Thomas
>
>
> Phil Frisbie, Jr.
> Hawk Software
> http://www.hawksoft.com
>
>

[-- Attachment #2: main.c --]
[-- Type: text/x-c, Size: 794 bytes --]

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <winsock.h>

#include <stdio.h>

static void TlsGet( void );

static DWORD tlsIdx;
  
int main( void )
{
  SetLastError(WSAEHOSTUNREACH);
  printf("LastError %d\n", GetLastError());
  printf("WSALastError %d\n", WSAGetLastError());
  WSASetLastError(WSAEHOSTUNREACH);
  printf("LastError %d\n", GetLastError());
  printf("WSALastError %d\n", WSAGetLastError());

  tlsIdx = TlsAlloc();
  TlsSetValue( tlsIdx, (void *) 0x1234 );
  TlsGet();

  printf("LastError %d\n", GetLastError());
  printf("WSALastError %d\n", WSAGetLastError());
  
  return 0;
}


static void TlsGet( void )
{
  DWORD lastError = GetLastError();
  TlsGetValue(tlsIdx);
  printf("LastError after TlsGetValue : %d\n", GetLastError());
  SetLastError(lastError);
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2001-08-27  2:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-08-22 11:51 Thread local storage problem, and fix Phil Frisbie, Jr.
2001-08-22 22:46 ` Ross Johnson
2001-08-23  0:41 ` Thomas Pfaff
2001-08-23  7:43   ` Phil Frisbie, Jr.
     [not found] <F0E13277A26BD311944600500454CCD034003B@antarctica.intern.net>
2001-08-27  2:05 ` Thomas Pfaff

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).