By liran bh | 5/16/2016 | Linux User Space

Futex Examples

Header file:

#ifndef SYSCALLSMAC_H_
#define SYSCALLSMAC_H_
#define __syscall_return(type, res) \
do { \
    if ((unsigned long)(res) >= (unsigned long)(-(128 + 1))) { \
        errno = -(res); \
        res = -1; \
    } \
    return (type) (res); \
} while (0)
 
#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
      type5,arg5,type6,arg6) \
type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5,type6 arg6) \
{ \
long __res; \
__asm__ volatile ("push %%ebp ; movl %%eax,%%ebp ; movl %1,%%eax ; int $0x80 ; pop %%ebp" \
    : "=a" (__res) \
    : "i" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
      "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5)), \
      "0" ((long)(arg6)) : "memory"); \
__syscall_return(type,__res); \
}
 
#endif /* SYSCALLSMAC_H_ */

Example 1

#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<errno.h>
#include<sys/syscall.h>
#include<linux/futex.h>
#include<linux/unistd.h>
#include "syscallsmac.h"
 
_syscall6(int,futex,void *,addr1,int,op,int,val1,struct timespec*,timeout,void*,addr2,int,val3)
 
int m1=1;
 
void *threadfn1(void *p)
{
    puts("thread1 start");
    futex(&m1,FUTEX_WAIT,m1,NULL,NULL,0);
    puts("thread1 after lock");
    sleep(2);
 
    return NULL;
}
 
void *threadfn2(void *p)
{
    sleep(2);
    puts("thread2 before lock");
    futex(&m1,FUTEX_WAKE,1,NULL,NULL,0);
    return NULL;
}
 
int main()
{
    pthread_t t1,t2;
    pthread_create(&t1,NULL,threadfn1,NULL);
    pthread_create(&t2,NULL,threadfn2,NULL);
    sleep(15);
//    puts("end test");
    return 0;
}

Example 2

#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<errno.h>
#include<sys/syscall.h>
#include<linux/futex.h>
#include<linux/unistd.h>
#include "syscallsmac.h"
 
_syscall6(int,futex,void *,addr1,int,op,int,val1,struct timespec*,timeout,void*,addr2,int,val3)
 
int m1=1;
 
void *threadfn1(void *p)
{
    puts("thread1 start");
    futex(&m1,FUTEX_WAIT,m1,NULL,NULL,0);
    puts("thread1 after lock");
    return NULL;
}
 
void *threadfn2(void *p)
{
    sleep(2);
    puts("thread2 before lock");
    futex(&m1,FUTEX_WAKE,2,NULL,NULL,0);
    return NULL;
}
 
int main()
{
    pthread_t t1,t2;
    pthread_create(&t1,NULL,threadfn1,NULL);
    pthread_create(&t1,NULL,threadfn1,NULL);
    pthread_create(&t2,NULL,threadfn2,NULL);
    sleep(15);
//    puts("end test");
    return 0;
}

 

Example 3

#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<errno.h>
#include<sys/syscall.h>
#include<linux/futex.h>
#include<linux/unistd.h>
#include "syscallsmac.h"
 
_syscall6(int,futex,void *,addr1,int,op,int,val1,struct timespec*,timeout,void*,addr2,int,val3)
 
int m1=1;
int m2=1;
 
void *threadfn11(void *p)
{
    puts("thread11 start");
    futex(&m1,FUTEX_WAIT,m1,NULL,NULL,0);
    puts("thread11 after lock");
    return NULL;
}
void *threadfn12(void *p)
{
    puts("thread12 start");
    futex(&m2,FUTEX_WAIT,m2,NULL,NULL,0);
    puts("thread12 after lock");
    return NULL;
}
 
void *threadfn2(void *p)
{
    sleep(2);
    puts("thread2 before lock");
    futex(&m1,FUTEX_WAKE_OP,1,NULL,&m2,FUTEX_OP(FUTEX_OP_ADD,2,FUTEX_OP_CMP_EQ,1));
    printf("m1=%d m2=%d\n",m1,m2);
    return NULL;
}
 
int main()
{
    pthread_t t1,t2;
    pthread_create(&t1,NULL,threadfn11,NULL);
    pthread_create(&t1,NULL,threadfn12,NULL);
    pthread_create(&t2,NULL,threadfn2,NULL);
    sleep(15);
//    puts("end test");
    return 0;
}

{{CommentsModel.TotalCount}} Comments

Your Comment

{{CommentsModel.Message}}

Recent Stories

Top DiscoverSDK Experts

User photo
3355
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
3220
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
User photo
3060
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
Show All
X

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.

{{compareToolModel.Error}}

Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now