Atomic operating function of standard C ++ libraries

xiaoxiao2021-03-06  46

Atomic operating function of standard C libraries

The following code from Gun's libstdc - v3.0.97

The file where the atomic operation is located, we can see the implementation files of various platforms under the configuration folder CPU

Atomicity.h

#ifndef _bits_atomicity_h

#define _bits_atomicity_h 1

TypedEf INT _TOMIC_WORD;

Static inline _tomic_word

__ATTRIBUTE__ (__unused__)))

__exchange_and_add (volatile _tomic_word * __ MEM, INT __VAL)

{

Register _tomic_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 _tomic_word * __mem, int __val)

{

__ASM__ __Volatile__ ("Lock; AddL% 0,% 1"

:: "IR" (__val), "M" (* __MEM): "Memory");

}

#ENDIF / * Atomicity.h * /

Anomaly class definition in iOS_Base

00166 Class Failure: Public Exception: PUBLIC EXCEPTION

00167 {

00168 public:

00169 #ifDef _glibcpp_resolve_lib_defects

00170 // 48. Use of non-evtructor

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 *

0000 () const throw ();

00181

00182 private:

00183 enum {_m_bufsize = 256};

00184 char _m_name [_m_bufsize];

00185 #ENDIF

00186};

The callback structure definition in iOS_BASE, involving atomic operations, introduced earlier

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 _tomic_word _m_refcount; // 0 means one reason.00388

00389 _Callback_List (iOS_BASE :: Event_Callback __fn, int __index,

00390 _Callback_list * __cb)

00391: _m_next (__ cb), _m_fn (__fn), _m_index (_ ingx), _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 initialization class definition

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};

转载请注明原文地址:https://www.9cbs.com/read-112317.html

New Post(0)