Author Topic: The static data segment and threading...  (Read 9229 times)

Offline soulvomit

  • Jr. Member
  • *
  • Posts: 31
The static data segment and threading...
« on: December 15, 2016, 07:55:02 PM »
Hi,

So I've finished my nasm version of the astar path-finding algorithm. I've created some wrappers for C, C++ and C#, so I can test my code easily. The preliminary tests show that it is roughly 10x faster then my C++ implementation, compiled with the msvc++ cl compiler. Test were run in an single threaded environment. Very encouraging!

But I've hit a bit of an snag when splitting the workload to multiple execution units. When the algorithm runs it stores most of the data on the stack. So each algorithm I run in a thread gets its own shadow space on the stack. Therefore all of this data should be "thread safe".

My problem is that I store a bit of data in the .data segment (mainly pointers to top and bottom nodes on my stack and a node counter; same for my linked list). I didn't anticipate that this data is, of course, not "thread safe" (and indeed just wrong, since each threaded algorithm should work on a its own unique copy of this data). So my questions are; What would be the best approach to fix this? Can you mutex lock the data in a data segment? (This would not fix my problem, but it would get rid of the errors)

I guess my options are;

1) To store the data on the heap for each thread. This would require Windows specific code (VirtualAlloc, VirtualFree), which would ruin my Linux compatibility.

2) To store the data on the stack for each thread. My current favorite option, although it will require some additional POPs and PUSHes.

3) Use the TLS (thread local storage) to store the data. I believe this is also PE specific and thereby Windows specific, correct me if I'm wrong. Another hurdle would be, that I'm not too familiar with this approach.

Are there any other options?

Thank you.

EDIT: And sorry, I realize this question is more threading related, then nasm related :)

EDIT2:
From the EFL documentation:
Quote
.tdata    
This section holds initialized thread-local data that contributes to the program's memory image. A copy of its contents is instantiated by the system for each new execution flow. Implementations need not support thread-local storage.
Anything like this for PE?
« Last Edit: December 15, 2016, 08:40:46 PM by soulvomit »