标准C++库的原子操作函数

类别:编程语言 点击:0 评论:0 推荐:

标准C++库的原子操作函数

以下代码来自GUN的libstdc++-v3.0.97

 

原子操作所在的文件,我们可以看到在配置文件夹cpu下有各种平台的实现文件

atomicity.h



















#ifndef _BITS_ATOMICITY_H

#define _BITS_ATOMICITY_H    1

 

typedef int _Atomic_word;

 

static inline _Atomic_word

__attribute__ ((__unused__))

__exchange_and_add (volatile _Atomic_word *__mem, int __val)

{

  register _Atomic_word __result;

  __asm__ __volatile__ ("lock; xaddl %0,%2"

                     : "=r" (__result)

                        : "0" (__val), "m" (*__mem)

                        : "memory");

  return __result;

}

 

static inline void

__attribute__ ((__unused__))

__atomic_add (volatile _Atomic_word* __mem, int __val)

{

  __asm__ __volatile__ ("lock; addl %0,%1"

                     : : "ir" (__val), "m" (*__mem) : "memory");

}

 

#endif /* atomicity.h */

 

 

ios_base中的异常类定义

00166     class failure : public exception

00167     {

00168     public:

00169 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS

00170       //48.  Use of non-existent exception constructor

00171       explicit

00172       failure(const string& __str) throw();

00173

00174       // This declaration is not useless:

00175       // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118

00176       virtual

00177       ~failure() throw();

00178

00179       virtual const char*

00180       what() const throw();

00181      

00182     private:

00183       enum { _M_bufsize = 256 };

00184       char _M_name[_M_bufsize];

00185 #endif

00186     };

 

 

ios_base中的回调结构定义,其中涉及原子操作,在前面介绍过

 

00381     struct _Callback_list

00382     {

00383       // Data Members

00384       _Callback_list*       _M_next;

00385       ios_base::event_callback  _M_fn;

00386       int           _M_index;

00387       _Atomic_word      _M_refcount;  // 0 means one reference.

00388    

00389       _Callback_list(ios_base::event_callback __fn, int __index,

00390              _Callback_list* __cb)

00391       : _M_next(__cb), _M_fn(__fn), _M_index(__index), _M_refcount(0) { }

00392      

00393       void

00394       _M_add_reference() { __atomic_add(&_M_refcount, 1); }

00395

00396       // 0 => OK to delete.

00397       int

00398       _M_remove_reference() { return __exchange_and_add(&_M_refcount, -1); }

00399     };

00400

00401      _Callback_list*    _M_callbacks;

00402

00403     void

00404     _M_call_callbacks(event __ev) throw();

00405

00406     void

00407     _M_dispose_callbacks(void);

 

 

ios_base初始化类定义

00448     class Init

00449     {

00450       friend class ios_base;

00451     public:

00452       Init();

00453       ~Init();

00454      

00455       static void

00456       _S_ios_create(bool __sync);

00457      

00458       static void

00459       _S_ios_destroy();

00460

00461     private:

00462       static int    _S_ios_base_init;

00463       static bool   _S_synced_with_stdio;

00464     };

 

本文地址:http://com.8s8s.com/it/it27049.htm