对于Linux平台下C语言开发中__sync_函数的认识

 

 

reference:http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html#Atomic-Builtins

A built-in function is a coding extension to C and C++ that allows a programmer to use the syntax of C function calls and C variables to access the instruction set of the processor of the compiling machine.

The following builtins are intended to be compatible with those described in the Intel Itanium Processor-specific Application Binary Interface, section 7.4. As such, they depart from the normal GCC practice of using the “__builtin_” prefix, and further that they are overloaded such that they work on multiple types.
为了适用不同的处理器,buildin将GCC中"__builtin_"的前缀改为“__sync_"前缀的函数来扩展支持。

The definition given in the Intel documentation allows only for the use of the types int, long, long long as well as their unsigned counterparts. GCC will allow any integral scalar or pointer type that is 1, 2, 4 or 8 bytes in length.


Not all operations are supported by all target processors. If a particular operation cannot be implemented on the target processor, a warning will be generated and a call an external function will be generated. The external function will carry the same name as the builtin, with an additional suffix `_n' where n is the size of the data type.
正是由于GCC对处理器所能处理的数据类型进行扩展,buildin需要将GCC进行重载。比较常见的就是数据类型如unit32_t。

In most cases, these builtins are considered a full barrier. That is, no memory operand will be moved across the operation, either forward or backward. Further, instructions will be issued as necessary to prevent the processor from speculating loads across the operation and from queuing stores after the operation.
buildin被视为全barrier,即没有内存在操作之间的移动。

All of the routines are are described in the Intel documentation to take “an optional list of variables protected by the memory barrier”. It's not clear what is meant by that; it could mean that only the following variables are protected, or it could mean that these variables should in addition be protected. At present GCC ignores this list and protects all variables which are globally accessible. If in the future we make some use of this list, an empty list will continue to mean all globally accessible variables.
这种原因是由于GCC和具体的处理器之间的约定不确定导致,对全局变量访问保护不一致。

type __sync_fetch_and_add (type *ptr, type value, ...)
type __sync_fetch_and_sub (type *ptr, type value, ...)
type __sync_fetch_and_or (type *ptr, type value, ...)
type __sync_fetch_and_and (type *ptr, type value, ...)
type __sync_fetch_and_xor (type *ptr, type value, ...)
type __sync_fetch_and_nand (type *ptr, type value, ...)
    These builtins perform the operation suggested by the name, and returns the value that had previously been in memory. That is,

              { tmp = *ptr; *ptr op= value; return tmp; } 
    //not understand, maybe mistake of op.    
              { tmp = *ptr; *ptr = ~tmp & value; return tmp; }   // nand
         
__sys_fetch_and_XXX控制对内存的读写后的操作,保证原子性。
下面的__sys_XXX_and_fetch同样控制对内存写操作后的读,保证原子性。

type __sync_add_and_fetch (type *ptr, type value, ...)
type __sync_sub_and_fetch (type *ptr, type value, ...)
type __sync_or_and_fetch (type *ptr, type value, ...)
type __sync_and_and_fetch (type *ptr, type value, ...)
type __sync_xor_and_fetch (type *ptr, type value, ...)
type __sync_nand_and_fetch (type *ptr, type value, ...)
    These builtins perform the operation suggested by the name, and return the new value. That is,

              { *ptr op= value; return *ptr; }
              { *ptr = ~*ptr & value; return *ptr; }   // nand    

bool __sync_bool_compare_and_swap (type *ptr, type oldval type newval, ...)
type __sync_val_compare_and_swap (type *ptr, type oldval type newval, ...)
    These builtins perform an atomic compare and swap. That is, if the current value of *ptr is oldval, then write newval into *ptr.

    The “bool” version returns true if the comparison is successful and newval was written. The “val” version returns the contents of *ptr before the operation.
对于这种函数,从名字就可以看出其意义。

__sync_synchronize (...)
    This builtin issues a full memory barrier.

type __sync_lock_test_and_set (type *ptr, type value, ...)
    This builtin, as described by Intel, is not a traditional test-and-set operation, but rather an atomic exchange operation. It writes value into *ptr, and returns the previous contents of *ptr.


对于锁的支持比较少,由些只存储当前为1的有效的常量值。lock和release对合局变量的进行加锁和解锁。

    Many targets have only minimal support for such locks, and do not support a full exchange operation. In this case, a target may support reduced functionality here by which the only valid value to store is the immediate constant 1. The exact value actually stored in *ptr is implementation defined.

    This builtin is not a full barrier, but rather an acquire barrier. This means that references after the builtin cannot move to (or be speculated to) before the builtin, but previous memory stores may not be globally visible yet, and previous memory loads may not yet be satisfied.
void __sync_lock_release (type *ptr, ...)
    This builtin releases the lock acquired by __sync_lock_test_and_set. Normally this means writing the constant 0 to *ptr.

    This builtin is not a full barrier, but rather a release barrier. This means that all previous memory stores are globally visible, and all previous memory loads have been satisfied, but following memory reads are not prevented from being speculated to before the barrier. 


除了sync and aotmic build-in functions, 还有其它的,如:
Cache-related build-in functions, Block-related built-in functions等等。
像Block-related built-in functions其中的一个,如_bzero,与bzero相比如下:
/* We define this function always since `bzero' is sometimes needed when
   the namespace rules does not allow this. */
extern void __bzero (void *__s, size_t __n) __THROW __nonnull ((1));

/* Set N bytes of S to 0. */
extern void bzero (void *__s, size_t __n) __THROW __nonnull ((1));

转载于:https://www.cnblogs.com/liyulong1982/p/5489360.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux 系统,可以使用 libevdev 库来编写系统钩子函数,以下是一个简单的示例代码,用于监控鼠标事件: ``` #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <linux/input.h> #include <libevdev/libevdev.h> int main() { int fd = open("/dev/input/event0", O_RDONLY); if(fd < 0) { perror("open"); exit(EXIT_FAILURE); } struct libevdev* dev = NULL; int rc = libevdev_new_from_fd(fd, &dev); if(rc < 0) { perror("libevdev_new_from_fd"); exit(EXIT_FAILURE); } do { struct input_event ev; rc = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, &ev); if(rc == LIBEVDEV_READ_STATUS_SUCCESS) { if(ev.type == EV_KEY && ev.value == 1) { printf("Key %d pressed\n", ev.code); } else if(ev.type == EV_REL && ev.code == REL_X) { printf("Mouse moved in X direction: %d\n", ev.value); } else if(ev.type == EV_REL && ev.code == REL_Y) { printf("Mouse moved in Y direction: %d\n", ev.value); } } } while(rc == LIBEVDEV_READ_STATUS_SYNC || rc == LIBEVDEV_READ_STATUS_SUCCESS || rc == -EAGAIN); libevdev_free(dev); close(fd); return 0; } ``` 在上面的代码,我们通过调用`open`函数打开鼠标设备文件`/dev/input/event0`,并且使用`libevdev_new_from_fd`函数将设备文件转换为`libevdev`结构体。在一个循环,我们不断调用`libevdev_next_event`函数获取鼠标事件,并且判断事件类型和事件值,如果是鼠标移动事件则输出移动的距离,如果是鼠标按键事件则输出按下的按键码。最后,我们通过调用`libevdev_free`函数释放`libevdev`结构体,关闭设备文件。 需要注意的是,在使用钩子函数时需要特别小心,因为钩子函数可能会影响系统的性能和稳定性,如果使用不当可能会导致系统崩溃。因此,在编写钩子函数时需要仔细考虑其实现方式,遵循安全和稳定性的原则。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值