From mboxrd@z Thu Jan 1 00:00:00 1970 From: "J.A. Magallon" Subject: Re: per-thread global variables Date: Sat, 13 Jul 2002 02:12:34 +0200 Sender: linux-smp-owner@vger.kernel.org Message-ID: <20020713001234.GA1675@werewolf.able.es> References: Mime-Version: 1.0 Content-Transfer-Encoding: 7BIT Return-path: Content-Disposition: inline In-Reply-To: ; from hyatt@cis.uab.edu on Fri, Jul 12, 2002 at 21:17:01 +0200 List-Id: Content-Type: text/plain; charset="us-ascii" To: "Robert M. Hyatt" Cc: Lista Linux-SMP On 2002.07.12 Robert M. Hyatt wrote: > > >I do this in Crafty. The approach I chose is to create a structure >for each thread (I also use clone() since the glibc guys broke >pthreads a while back) and then let each thread set its pointer to >its own private structure (really shared, but since only one thread >has a pointer to each structure, it is like a private but global >group of variables). > >Doing it like this will guarantee it will work with any compiler, >which might be a plus... > I think something is very dark for me...The hard way in pseudo-C short id[PID_MAX]; #define setself(k) do { tid[getpid()]=k; } while(0) #define self() (id[getpid()]) // master setself(-1); // slaves for (i in 0..nslaves-1) clone(f(),CLONE_VM,i); f(void *arg) { setself((int)arg); print(self()); } But how about the 32K*2 array ??? (hash table, ordered vector and binary search...) Quoting your answer: >pthreads a while back) and then let each thread set its pointer to ^^^ >its own private structure (really shared, but since only one thread How do you define 'its' pointer ? That is like if I just defined short id; and tried to set 'its id' for each thread ? Whichever method you use, at the end you always need one, the last, the unique variable (one int, a pointer to a data area), declared as a global variable in C that you need to be distinct for each thread. The only thing (in my short undestanding) that can distinguish a thread from one other is _pid_. Of course, I don't want to mess passing an info struct to all functions in my code. On IRIX, you have a // Fixed address space always private for each process/thread #define PRDA ((struct prda *)0x00200000L) struct prda* thrprda; int* self; f(int k) { thrprda = (struct prda*)PRDA; // The magic global pointer // different for each thread self = &(thrprda->user_area); *self = k; } If Linux supported this it will make many many things simpler. A fixed zone of virtual address space that is never shared. Or perhaps... Could I do in Linux: #define PRDA ((void*)0x00200000L) // Or get any free address space int* self = PRDA; // will be shared over clone(), so we fix it before for (i in 0..nslaves-1) clone(f(),CLONE_VM,i); f(void *arg) { // Get a chunk, mapped on a fixed address, and do not propagate the // map to parent or siblings mmap(self,1024,PROT_??,MAP_ANONYMOUS|MAP_FIXED|MAP_PRIVATE,0,0); *self = (int)arg; } ?? TIA -- J.A. Magallon \ Software is like sex: It's better when it's free mailto:jamagallon@able.es \ -- Linus Torvalds, FSF T-shirt Linux werewolf 2.4.19-rc1-jam3, Mandrake Linux 8.3 (Cooker) for i586 gcc (GCC) 3.1.1 (Mandrake Linux 8.3 3.1.1-0.7mdk)