Linux 原子操作CAS与锁实现

发布时间:2023年12月18日

1、互斥锁(mutex)

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define __USE_GNU

#include <sched.h>
#include <unistd.h>
#include <setjmp.h>
#include <sys/syscall.h>

#define THREAD_COUNT 30

pthread_mutex_t mutex;
pthread_spinlock_t spinlock;

void* callback(void* data){
    int* pdata = (int*)data;
    for(int i=0;i<100000;i++){
#if 1
        pthread_mutex_lock(&mutex);
        (*pdata)++;
        pthread_mutex_unlock(&mutex);
#else
    pthread_spin_lock(&spinlock);
    (*pdata)++;
    pthread_spin_unlock(&spinlock);
#endif
    }
    return NULL;
}

int main()
{
    pthread_mutex_init(&mutex,NULL);
    pthread_spin_init(&spinlock,PTHREAD_PROCESS_SHARED);
    pthread_t tid[THREAD_COUNT] = {0};
    int ptcount = 0;
    for(int i=0;i<THREAD_COUNT;i++){
        pthread_create(&tid[i],NULL,callback,(void*)&ptcount);
    }
    
    for(int i=0;i<100;i++){
        printf("ptcount:%d\n",ptcount);
        sleep(1);
    }
    return 0;
}


2、自旋锁(spinlock)

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define __USE_GNU

#include <sched.h>
#include <unistd.h>
#include <setjmp.h>
#include <sys/syscall.h>

#define THREAD_COUNT 30

pthread_mutex_t mutex;
pthread_spinlock_t spinlock;

void* callback(void* data){
    int* pdata = (int*)data;
    for(int i=0;i<100000;i++){
#if 0
        pthread_mutex_lock(&mutex);
        (*pdata)++;
        pthread_mutex_unlock(&mutex);
#else
    pthread_spin_lock(&spinlock);
    (*pdata)++;
    pthread_spin_unlock(&spinlock);
#endif
    }
    return NULL;
}

int main()
{
    pthread_mutex_init(&mutex,NULL);
    pthread_spin_init(&spinlock,PTHREAD_PROCESS_SHARED);
    pthread_t tid[THREAD_COUNT] = {0};
    int ptcount = 0;
    for(int i=0;i<THREAD_COUNT;i++){
        pthread_create(&tid[i],NULL,callback,(void*)&ptcount);
    }
    
    for(int i=0;i<100;i++){
        printf("ptcount:%d\n",ptcount);
        sleep(1);
    }
    return 0;
}


3、原子操作

int inc(int *value, int add) {

	int old;
	__asm__ volatile (
		"lock; xaddl %2, %1;"
		: "=a" (old)
		: "m" (*value), "a" (add)
		: "cc", "memory"
	);
	return old;

}

void *callback(void *data){
    int* pdata = (int*)data;
    for(int i=0;i<100000;i++){
        inc(pdata,1);
#if 0
        pthread_mutex_lock(&mutex);
        (*pdata)++;
        pthread_mutex_unlock(&mutex);
#elif 0
    pthread_spin_lock(&spinlock);
    (*pdata)++;
    pthread_spin_unlock(&spinlock);
#endif
    }
    return NULL;
}

4、线程私有空间(pthread_key)

1、pthread_key_create() 用来创建线程私有数据。该函数从 TSD 池中分配一项,将其地址值赋给 key 供以后访问使用,第2个参数,是指定销毁函数,可以设置为NULL,设置为NULL的情况下,系统使用默认的销毁函数对数据进行销毁,不为空的时候,当线程退出时(pthread_exit()),将key对应的数据调出来,去释放。
2、不论哪个线程调用了 pthread_key_create(),所创建的 key 都是所有线程可以访问的,但各个线程可以根据自己的需要往 key 中填入不同的值,相当于提供了一个同名而不同值的全局变量(这个全局变量相对于拥有这个变量的线程来说)。
3、注销一个 TSD 使用 pthread_key_delete() 函数。该函数并不检查当前是否有线程正在使用该 TSD,也不会调用清理函数(destructor function),而只是将 TSD 释放以供下一次调用 pthread_key_create() 使用。在 LinuxThread 中,它还会将与之相关的线程数据项设置为 NULL。

key被创建之后,因为是全局变量,所以所有的线程都可以访问。各个线程可以根据需求往key中,填入不同的值,这就相当于提供了一个同名而值不同的全局变量,即一键多值。一键多值依靠的一个结构体数组,即

static struct pthread_key_struct pthread_keys[PTHREAD_KEYS_MAX] ={{0,NULL}};

// 定义
struct pthread_key_struct
{
  /* Sequence numbers.  Even numbers indicated vacant entries.  Note
     that zero is even.  We use uintptr_t to not require padding on
     32- and 64-bit machines.  On 64-bit machines it helps to avoid
     wrapping, too.  */
  uintptr_t seq;

  /* Destructor for the data.  */
  void (*destr) (void *);
};
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define __USE_GNU

#include <sched.h>
#include <unistd.h>
#include <setjmp.h>
#include <sys/syscall.h>

#define THREAD_COUNT 3
pthread_key_t key;

typedef void* (*thread_cb)(void*);

void *func1(void *data){
    int i = 9;
    pthread_setspecific(key,&i);

    int *p = pthread_getspecific(key);
    printf("p=%d\n",*p);
    return NULL;
}

void *func2(void *data){
    char *str = "hello world";
    pthread_setspecific(key,str);

    char *ptr = pthread_getspecific(key);
    printf("ptr=%s\n",ptr);
    return NULL;
}

struct pair {
	int x;
	int y;
};

void *func3(void *data){
    struct pair p1 = {1, 2};
	pthread_setspecific(key, &p1);

    struct pair *p = (struct pair *)pthread_getspecific(key);
    printf("pair x=%d,y=%d\n",p->x,p->y);
    return NULL;
}



int main()
{
    thread_cb callback_cb[THREAD_COUNT] = {
        func1,
        func2,
        func3
    };
    pthread_key_create(&key,NULL);

    pthread_t tid[THREAD_COUNT] = {0};
    int count = 0;
    for(int i=0;i<THREAD_COUNT;i++){
        pthread_create(&tid[i],NULL,callback_cb[i],&count);
    }

    for(int i=0;i<THREAD_COUNT;i++){
        pthread_join(tid[i],NULL);
    }
    pthread_key_delete  ( key );
    return 0;
}

5、共享内存
待续

6、cpu的亲缘性(affinity)

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define __USE_GNU

#include <sched.h>
#include <unistd.h>
#include <setjmp.h>
#include <sys/syscall.h>
#define THREAD_COUNT 3

void process_affinity(int num) {
	//gettid();
	pid_t selfid = syscall(__NR_gettid);
    printf("pid=%d\n",selfid);
	cpu_set_t mask;
	CPU_ZERO(&mask);
    // cpu编号从0开始
	CPU_SET(num, &mask);

	// 也可以填入selfid
	sched_setaffinity(0, sizeof(mask), &mask);

	while(1) ;
}

int main()
{
    int num = sysconf(_SC_NPROCESSORS_CONF);
    int i = 0;
    pid_t pid = 0;
    for(int i=0;i<num/2;i++){
        pid = fork();
        if(pid<0)
            break;
    }
    if(pid == 0){
        printf("main pid=%d\n",pid);
        process_affinity(num);
    }

    while (1) usleep(1);
    return 0;
}

7、setjmp/longjmp

int setjmp(jmp_buf env);
调用setjmp将堆栈上下文保存在jmp_buf结构体中(入栈),供longjmp稍后使用(出栈)。如果直接调用返回0,如果使用保存的上下文从longjmp返回,则返回保存值非零。
void longjmp(jmp_buf env, int val);
调用longjmp程序跳转到最后一次使用相应env参数调用setjmp处,调用longjmp后setjmp不能返回0,如果longjmp第二个参数设置0,则将返回1

#include <setjmp.h>
#include <stdio.h>
 
int main(int argc, char *argv[]) {
 
        int idx = 0;
        jmp_buf env;
        int count = 0;
 
 
        count = setjmp(env);
        if (count == 0) {
                printf("count:%d\n", count);
                longjmp(env, ++idx);
        } else if (count == 1) {
                printf("count:%d\n", count);
                longjmp(env, ++idx);
        } else if (count == 2) {
                printf("count:%d\n", count);
                longjmp(env, ++idx);
        } else {
                printf("other count:%d\n",count);
        }
 
        return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <stdarg.h>

#include <pthread.h>
#include <setjmp.h>



#define ntyThreadData		pthread_key_t
#define ntyThreadDataSet(key, value)	pthread_setspecific((key), (value))
#define ntyThreadDataGet(key)		pthread_getspecific((key))
#define ntyThreadDataCreate(key)	pthread_key_create(&(key), NULL)


#define EXCEPTIN_MESSAGE_LENGTH		512

typedef struct _ntyException {
	const char *name;
} ntyException; 

ntyException SQLException = {"SQLException"};
ntyException TimeoutException = {"TimeoutException"};

ntyThreadData ExceptionStack;


typedef struct _ntyExceptionFrame {
	jmp_buf env;

	int line;
	const char *func;
	const char *file;

	ntyException *exception;
	struct _ntyExceptionFrame *prev;
	
	char message[EXCEPTIN_MESSAGE_LENGTH+1];

} ntyExceptionFrame;

#define ntyExceptionPopStack	\
	ntyThreadDataSet(ExceptionStack, ((ntyExceptionFrame*)ntyThreadDataGet(ExceptionStack))->prev)

#define ReThrow					ntyExceptionThrow(frame.exception, frame.func, frame.file, frame.line, NULL)
#define Throw(e, cause, ...) 	ntyExceptionThrow(&(e), __func__, __FILE__, __LINE__, cause, ##__VA_ARGS__, NULL)


enum {
	ExceptionEntered = 0,
	ExceptionThrown,
	ExceptionHandled,
	ExceptionFinalized
};


#define Try do {							\
			volatile int Exception_flag;	\
			ntyExceptionFrame frame;		\
			frame.message[0] = 0;			\
			frame.prev = (ntyExceptionFrame*)ntyThreadDataGet(ExceptionStack);	\
			ntyThreadDataSet(ExceptionStack, &frame);	\
			Exception_flag = setjmp(frame.env);			\
			if (Exception_flag == ExceptionEntered) {	
			

#define Catch(e) \
				if (Exception_flag == ExceptionEntered) ntyExceptionPopStack; \
			} else if (frame.exception == &(e)) { \
				Exception_flag = ExceptionHandled;


#define Finally \
				if (Exception_flag == ExceptionEntered) ntyExceptionPopStack; \
			} { \
				if (Exception_flag == ExceptionEntered)	\
					Exception_flag = ExceptionFinalized; 

#define EndTry \
				if (Exception_flag == ExceptionEntered) ntyExceptionPopStack; \
			} if (Exception_flag == ExceptionThrown) ReThrow; \
        	} while (0)	


static pthread_once_t once_control = PTHREAD_ONCE_INIT;

static void init_once(void) { 
	ntyThreadDataCreate(ExceptionStack); 
}


void ntyExceptionInit(void) {
	pthread_once(&once_control, init_once);
}


void ntyExceptionThrow(ntyException *excep, const char *func, const char *file, int line, const char *cause, ...) {

	va_list ap;
	ntyExceptionFrame *frame = (ntyExceptionFrame*)ntyThreadDataGet(ExceptionStack);

	if (frame) {

		frame->exception = excep;
		frame->func = func;
		frame->file = file;
		frame->line = line;

		if (cause) {
			va_start(ap, cause);
			vsnprintf(frame->message, EXCEPTIN_MESSAGE_LENGTH, cause, ap);
			va_end(ap);
		}

		ntyExceptionPopStack;

		longjmp(frame->env, ExceptionThrown);
		
	} else if (cause) {

		char message[EXCEPTIN_MESSAGE_LENGTH+1];

		va_start(ap, cause);
		vsnprintf(message, EXCEPTIN_MESSAGE_LENGTH, cause, ap);
		va_end(ap);

		printf("%s: %s\n raised in %s at %s:%d\n", excep->name, message, func ? func : "?", file ? file : "?", line);
		
	} else {

		printf("%s: %p\n raised in %s at %s:%d\n", excep->name, excep, func ? func : "?", file ? file : "?", line);
		
	}

}


/* ** **** ******** **************** debug **************** ******** **** ** */

ntyException A = {"AException"};
ntyException B = {"BException"};
ntyException C = {"CException"};
ntyException D = {"DException"};

void *thread(void *args) {

	pthread_t selfid = pthread_self();

	Try {

		Throw(A, "A");
		
	} Catch (A) {

		printf("catch A : %ld\n", selfid);
		
	} EndTry;

	Try {

		Throw(B, "B");
		
	} Catch (B) {

		printf("catch B : %ld\n", selfid);
		
	} EndTry;

	Try {

		Throw(C, "C");
		
	} Catch (C) {

		printf("catch C : %ld\n", selfid);
		
	} EndTry;

	Try {

		Throw(D, "D");
		
	} Catch (D) {

		printf("catch D : %ld\n", selfid);
		
	} EndTry;

	Try {

		Throw(A, "A Again");
		Throw(B, "B Again");
		Throw(C, "C Again");
		Throw(D, "D Again");

	} Catch (A) {

		printf("catch A again : %ld\n", selfid);
	
	} Catch (B) {

		printf("catch B again : %ld\n", selfid);

	} Catch (C) {

		printf("catch C again : %ld\n", selfid);
		
	} Catch (D) {
	
		printf("catch B again : %ld\n", selfid);
		
	} EndTry;
	
}


#define THREADS		50

int main(void) {

	ntyExceptionInit();

	Throw(D, NULL);

	Throw(C, "null C");

	printf("\n\n=> Test1: Try-Catch\n");

	Try {

		Try {
			Throw(B, "recall B");
		} Catch (B) {
			printf("recall B \n");
		} EndTry;
		
		Throw(A, NULL);

	} Catch(A) {

		printf("\tResult: Ok\n");
		
	} EndTry;

	printf("=> Test1: Ok\n\n");

	printf("=> Test2: Test Thread-safeness\n");
#if 1
	int i = 0;
	pthread_t threads[THREADS];
	
	for (i = 0;i < THREADS;i ++) {
		pthread_create(&threads[i], NULL, thread, NULL);
	}

	for (i = 0;i < THREADS;i ++) {
		pthread_join(threads[i], NULL);
	}
#endif
	printf("=> Test2: Ok\n\n");

} 



文章来源:https://blog.csdn.net/weixin_45715405/article/details/133690623
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。