温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么基于linuxthreads2.0.1线程源码分析specific.c

发布时间:2021-12-09 09:39:31 来源:亿速云 阅读:134 作者:柒染 栏目:大数据

这篇文章将为大家详细讲解有关怎么基于linuxthreads2.0.1线程源码分析specific.c,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

该文件是线程私有数据的实现。在线程tcb里有一个数组,保存了一系列的键对值。从而实现了线程的私有数据存储。线程想拥有自己的数据时,首先获取一个键,然后在tcb中保存一个键对值即可。

   
     
 
    
    

/

/* Thread-specific data */

#include <errno.h>
#include <stddef.h>
#include "pthread.h"
#include "internals.h"

typedef void (*destr_function)(void *);

/* Table of keys. */

struct pthread_key_struct {
 int in_use;                   /* already allocated? */
 destr_function destr;         /* destruction routine */
};

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

/* Mutex to protect access to pthread_keys */

static pthread_mutex_t pthread_keys_mutex = PTHREAD_MUTEX_INITIALIZER;

/* Create a new key */
// 创建一个key
int __pthread_key_create(pthread_key_t * key, destr_function destr)
{
 int i;
 // 加锁
 pthread_mutex_lock(&pthread_keys_mutex);
 // 从列表中找一项空闲的
 for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
   if (! pthread_keys[i].in_use) {
     pthread_keys[i].in_use = 1;
     pthread_keys[i].destr = destr;
     // 找到则解锁并返回键值
     pthread_mutex_unlock(&pthread_keys_mutex);
     *key = i;
     return 0;
   }
 }
 // 找不到则解锁
 pthread_mutex_unlock(&pthread_keys_mutex);
 return EAGAIN;
}
weak_alias (__pthread_key_create, pthread_key_create)

/* Delete a key */
// 删除一个键对应的项
int pthread_key_delete(pthread_key_t key)
{
 pthread_mutex_lock(&pthread_keys_mutex);
 if (key >= PTHREAD_KEYS_MAX || !pthread_keys[key].in_use) {
   pthread_mutex_unlock(&pthread_keys_mutex);
   return EINVAL;
 }
 pthread_keys[key].in_use = 0;
 pthread_keys[key].destr = NULL;
 pthread_mutex_unlock(&pthread_keys_mutex);
 return 0;
}

/* Set the value of a key */
// 关联键对应的值
int __pthread_setspecific(pthread_key_t key, const void * pointer)
{
 pthread_t self = thread_self();
 if (key >= PTHREAD_KEYS_MAX) return EINVAL;
 self->p_specific[key] = (void *) pointer;
 return 0;
}
weak_alias (__pthread_setspecific, pthread_setspecific)

/* Get the value of a key */

void * __pthread_getspecific(pthread_key_t key)
{
 pthread_t self = thread_self();
 if (key >= PTHREAD_KEYS_MAX)
   return NULL;
 else
   return self->p_specific[key];
}
weak_alias (__pthread_getspecific, pthread_getspecific)

/* Call the destruction routines on all keys */
// 逐个调用pthread_keys数组中的destr函数,并以线程关联的value为参数
void __pthread_destroy_specifics()
{
 int i;
 pthread_t self = thread_self();
 destr_function destr;
 void * data;

 for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
   // 销毁时执行的函数
   destr = pthread_keys[i].destr;
   // 获取键对应的值
   data = self->p_specific[i];
   // 执行
   if (destr != NULL && data != NULL) destr(data);
 }
}    

关于怎么基于linuxthreads2.0.1线程源码分析specific.c就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI