#include<pthread.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<math.h>
#include<sys/uio.h>
#include<time.h>
#include<string.h>
#include<sys/timeb.h>
#include <netinet/tcp.h>
#include <semaphore.h>
#include <signal.h>
#include <sys/syscall.h>
volatile int lock = 0;
int glbint = 0 ;
struct Mars_
{
int glbintx1 ;
//char pad[64] ;
int glbintx2 ;
};
struct Mars_ Mval ;
struct strspin
{
pthread_mutex_t Mutex1 ;
int v1 ;
char pad[64] ;
int v2 ;
pthread_mutex_t Mutex2 ;
} __attribute__((aligned(64))) ;
strspin spincell ;
pid_t gettid( void )
{
return syscall( __NR_gettid );
}
void *thread1F(void *param)
{
int i ;
int proc_num = (int)(long)param ;
cpu_set_t set;
CPU_ZERO( &set );
CPU_SET( proc_num, &set );
if (sched_setaffinity( gettid(), sizeof( cpu_set_t ), &set ))
{
perror( "sched_setaffinity" );
return NULL;
}
for(i=0;i<100000000;i++)
{
/*
if( __sync_bool_compare_and_swap(&spincell.v1,1,1) )
Mval.glbintx1 = Mval.glbintx1 + 1 ;
*/
pthread_mutex_lock(&spincell.Mutex1);
Mval.glbintx1 = Mval.glbintx1 + 1 ;
pthread_mutex_unlock(&spincell.Mutex1);
}//for
}
void *thread2F(void *param)
{
int i ;
int proc_num = (int)(long)param ;
cpu_set_t set;
CPU_ZERO( &set );
CPU_SET( proc_num, &set );
if (sched_setaffinity( gettid(), sizeof( cpu_set_t ), &set ))
{
perror( "sched_setaffinity" );
return NULL;
}
for(i=0;i<100000000;i++)
{
/*
if( __sync_bool_compare_and_swap(&spincell.v2,1,1) )
Mval.glbintx2 = Mval.glbintx2 + 1 ;
*/
pthread_mutex_lock(&spincell.Mutex2);
Mval.glbintx2 = Mval.glbintx2 + 1 ;
pthread_mutex_unlock(&spincell.Mutex2);
if(Mval.glbintx2%10000000==0)
printf("(%d)\n",Mval.glbintx2) ;
}//for
}
int main (int argc, char *argv[])
{
/*
int v1 = 100,v2=101,v3=200,v4 ;
bool b1 ;
b1 = __sync_bool_compare_and_swap(&v1,v2,v3) ;
printf("(%d)(%s)\n",v1,b1?"True":"False");
exit(0) ;
*/
spincell.v1 = 1 ;
spincell.v2 = 1 ;
pthread_t tid[2] ;
pthread_mutex_init(&spincell.Mutex1,NULL) ;
pthread_mutex_init(&spincell.Mutex2,NULL) ;
pthread_create(&tid[0] , NULL, thread1F, (void*)(long)0);
pthread_create(&tid[1] , NULL, thread2F, (void*)(long)1);
pthread_join(tid[0], NULL );
pthread_join(tid[1], NULL );
printf("glbint=(%d)\n",glbint) ;
printf("glbintx=(%d)(%d)\n",Mval.glbintx1,Mval.glbintx2) ;
}
留言列表