This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Graduate research - how GCC compiles PTHREAD Mutex routines into x86/Linux Kernel 2.6 assembler


On Mon, 21 Feb 2011 22:26:09 -0500
Edmon Begoli <ebegoli@gmail.com> wrote:

> Hi,
> 
> My name is Edmon Begoli and I am a graduate student in computer science.
> 
> I am writing a graduate research paper on how are concurrent routines complied
> in different languages. I want to start with a sort of a base case showing how
> are Mutexes and thread initiation routines in C compiled by GCC into assembler.
> 

You question very probably belongs more to gcc-help@

> I tried to look into a assembly and I did search for several days
> forums and forums and manuals
> but I could not find some simple example or a piece of a source code
> that shows how may
> piece of code like this:
> 
> http://www.cs.cf.ac.uk/Dave/C/node31.html#SECTION003117100000000000000
> 
> be compiled into for example some basic version of Intel x86 assembler
> on Linux 2.6 kernel.

your example there is

#include <pthread.h> 
pthread_mutex_t count_mutex; 
long long count; 
void increment_count() 
   { pthread_mutex_lock(&count_mutex); 
     count = count + 1;
     pthread_mutex_unlock(&count_mutex); 
   }   

As far as I know, there is nothing special in GCC to compile such a code. All the interesting stuff happens in pthread routines (like pthread_mutex_lock). To understand in detail what GCC is doing, you could save the above code chunk in a file lockedcount.c -inside an otherwise emptyy directory- and compile that file with
  gcc -phtread -S -O2 -fdump-tree-all -fverbose-asm lockedcount.c

Then you'll see the generated assembly code lockedcount.s and many "dump" files which are showing the various internal representations (Gimple, Gimple/SSA, ...) of the code within GCC.

On my computer the function gets compiled to

	.p2align 4,,15
.globl increment_count
	.type	increment_count, @function
increment_count:
.LFB1:
	.cfi_startproc
	subq	$8, %rsp	#,
	.cfi_def_cfa_offset 16
	movl	$count_mutex, %edi	#,
	call	pthread_mutex_lock	#
	addq	$1, count(%rip)	#, count
	movl	$count_mutex, %edi	#,
	addq	$8, %rsp	#,
	.cfi_def_cfa_offset 8
	jmp	pthread_mutex_unlock	#
	.cfi_endproc
.LFE1:

As you can see, the synchronisation routines (pthread_mutex_lock...) get called like any other ordinary routine. All the synchronisation magic happens inside them (in the libpthread.so library). You'll need to dig the source code of glibc/NPTL to understand the details.

When your work is done, please publish it (in English) on some publicly accessible web page. I'm sure that would interest some people. 

Regards, good luck.
-- 
Basile STARYNKEVITCH         http://starynkevitch.net/Basile/
email: basile<at>starynkevitch<dot>net mobile: +33 6 8501 2359
8, rue de la Faiencerie, 92340 Bourg La Reine, France
*** opinions {are only mine, sont seulement les miennes} ***


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]