myhandle
#ifndef my_handle_h
#define my_handle_h #include <stdint.h>
#include "mydef.h"
#include "mem.h" typedef struct tag_my_handle {
int ref;
int stack;
intptr_t detached;
void* ptr;
free_t free;
} my_handle; static inline my_handle* debug_handle_attach(void* ptr, free_t fp_free, const char* func, int line)
{
my_handle* handle = (my_handle *) debug_malloc(sizeof(my_handle), func, line);
if (handle != NULL) {
handle->ref = 1;
handle->stack = 1; handle->ptr = ptr;
handle->free = fp_free;
handle->detached = 0;
}
return handle;
}
#define handle_attach(ptr, fptr) debug_handle_attach(ptr, fptr, __FUNCTION__, __LINE__) #define handle_put(handle) \
do { \
int n = __sync_sub_and_fetch(&(handle)->stack, 1); \
my_assert(n >= 0); \
if (n == 0) { \
void* handle_ptr = (void *) __sync_lock_test_and_set(&(handle)->ptr, NULL); \
if ((handle)->free && handle_ptr) { \
(handle)->free(handle_ptr); \
} \
} \
} while (0) static inline void* handle_get_with(my_handle* handle, int detach, const char* func, int line)
{
int n = __sync_add_and_fetch(&handle->stack, 1);
int b = __sync_bool_compare_and_swap((void **) &handle->detached, (void *) 0, (void *) (intptr_t) detach);
if (!b) {
handle_put(handle);
return NULL;
} void* ptr = handle->ptr;
my_assert2(n > 1 && ptr != NULL, "%d, %p, caller %d@%s", n, ptr, line, func);
return ptr;
}
#define handle_get(handle) handle_get_with((handle), 0, __FUNCTION__, __LINE__) static inline int handle_clone(my_handle* handle)
{
int n = __sync_add_and_fetch(&handle->ref, 1);
my_assert(n > 0);
return n;
} static inline int handle_release(my_handle* handle)
{
int n = __sync_sub_and_fetch(&handle->ref, 1);
my_assert2(n >= 0, "%p %d:%d", handle, handle->ref, handle->stack);
if (n > 0) {
return n;
} // it should be safe to simplely check whether detached equals 0.
// we know __sync_xxx functions have mb() semantics, and
// if some one set handle->detached = 1, it only can be called in
// handle_dettach while still holds a reference of handle.
// ok, now we have seen handle->ref = 0 above, meaning
// we have observed the latter handle_release in handle_dettach.
// then, the mb() assure that
// we have observed the earlier handle_get_with(handle, 1).
if (handle->detached == 0) {
handle_put(handle);
}
my_assert(handle->stack == 0); my_free(handle);
return n;
} static inline int debug_handle_dettach(my_handle* handle, const char* func, int line)
{
void* ptr = handle_get_with(handle, 1, func, line);
if (ptr != NULL) {
__sync_sub_and_fetch(&handle->stack, 1);
handle_put(handle);
}
return handle_release(handle);
}
#define handle_dettach(handle) debug_handle_dettach((handle), __FUNCTION__, __LINE__) #endif
myhandle的更多相关文章
- Java开发中的23种设计模式详解
[放弃了原文访问者模式的Demo,自己写了一个新使用场景的Demo,加上了自己的理解] [源码地址:https://github.com/leon66666/DesignPattern] 一.设计模式 ...
- Java开发中的23种设计模式详解(转)
设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
- java中的23中设计模式(转)
设计模式(Design Patterns) --可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
- Python 程序员经常犯的 10 个错误
关于PythonPython是一种解释性.面向对象并具有动态语义的高级程序语言.它内建了高级的数据结构,结合了动态类型和动态绑定的优点,这使得... 关于Python Python是一种解释性.面向对 ...
- xml文档解析
XML文档解主要分为四种解析方式,官方提供的两种分别是:DOM 和 SAX,第三方分别是:JDOM 和 DOM4j 测试用的xml文档: <?xml version="1.0" ...
- 转:python signal信号
转自:http://www.jb51.net/article/74844.htm 在liunx系统中要想每隔一分钟执行一个命令,最普遍的方法就是crontab了,如果不想使用crontab,经同事指点 ...
- java开发中的23中设计模式详解--大话设计模式
设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
- java基础语法要点<一>(基于1.8)
http://yishouce.com/java/run http://www.shucunwang.com/RunCode/java/ 数据类型 8种基本数据类型及对应的 类型封装器 byte, s ...
- Python开发最常犯错误总结10种
不管是在学习还是工作过程中,人都会犯错.虽然Python的语法简单.灵活,但也一样存在一些不小的坑,一不小心,初学者和资深Python程序员都有可能会栽跟头.本文是Toptal网站的程序员梳理的10大 ...
随机推荐
- CentOS 安装 Dubbo 管理控制台
安装之前需要安装zookeeper详情http://www.cnblogs.com/zuge/p/6141101.html Dubbo 管控台可以对注册到 zookeeper 注册中心的服务或服务消费 ...
- Xcode 8 Swift 类似插件方法
Xcode8 Swift使用技巧 1 option + cmd + / 可以弹出注释 2 color 然后加 enter 可以弹出颜色选择 3 #FIXME: 警告 4 #MARK: 备注 ...
- 将自己库添加Cocoapods支持
给库添加Cocoapods支持, 使这个工具使用起来更加方便, 更好的使用Cocoapods, 助力iOS程序开发, 下面进入正题, 想要实现这个过程, 绝对不虚此读. 首先写好一个要添加Cocoap ...
- Linux C++线程池
.为什么需要线程池? 部分应用程序需要执行很多细小的任务,对于每个任务都创建一个线程来完成,任务完成后销毁线程,而这就会产生一个问题:当执行的任务所需要的时间T1小于等于创建线程时间T2和销毁线程时间 ...
- .Net架构必备工具列表
★微软MSDN:每个开发人员现在应该下载的十种必备工具 点此进入 ★网友总结.Net架构必备工具列表 Visual Studio 这个似乎是不言而喻的,只是从严谨的角度,也列在这.实际上,现在也有一个 ...
- 注册码_EditPlus3
1.来自"http://jingyan.baidu.com/article/67508eb4d78cfe9cca1ce488.html" Name: www.cnzz.cc Co ...
- iOS tabBar双击事件
思路: 在tabBarController的代理方法 shouldSelectViewController 中, 通过判断tabBar选中的控制器是否是当前控制器 并 比对两次点击的时间间隔 来判断是 ...
- ROS学习笔记(六)——创建、编译包
$ cd ~/catkin_ws #如果新开一个终端,就需要先吧ROS的工作空间配置好 $ source devel/setup.bash #工作空间的环境配置 #正式开始创建包 $ catkin_c ...
- nodejs&npm等概念梳理
nodejs node node版本 npm nvmw\gnvm等多版本管理 CommonJS.AMD.requirejs grunt.gulp package.json .npmrc npm\nod ...
- centos 忘记密码
装了个 centos 6.8 安装的时候 要输入 新用户和密码 用 新的用户密码 进去后 各种没权限 重新修改 root 密码 一切OK 步骤 1.重新启动Centos,在启动过程中,长按“ ...