Skip to content

Commit de68fbd

Browse files
committed
Merge branch 'support_cpp11_atomic'
2 parents cf1487f + 0482e4f commit de68fbd

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

include/msgpack/unpack.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
#include <memory>
2929
#include <stdexcept>
3030

31+
#if !defined(MSGPACK_USE_CPP03)
32+
#include <atomic>
33+
#endif
34+
35+
3136
#if defined(_MSC_VER)
3237
// avoiding confliction std::max, std::min, and macro in windows.h
3338
#ifndef NOMINMAX
@@ -349,25 +354,46 @@ class unpack_stack {
349354

350355
inline void init_count(void* buffer)
351356
{
357+
#if defined(MSGPACK_USE_CPP03)
352358
*reinterpret_cast<volatile _msgpack_atomic_counter_t*>(buffer) = 1;
359+
#else // defined(MSGPACK_USE_CPP03)
360+
new (buffer) std::atomic<unsigned int>(1);
361+
#endif // defined(MSGPACK_USE_CPP03)
353362
}
354363

355364
inline void decr_count(void* buffer)
356365
{
366+
#if defined(MSGPACK_USE_CPP03)
357367
if(_msgpack_sync_decr_and_fetch(reinterpret_cast<volatile _msgpack_atomic_counter_t*>(buffer)) == 0) {
358368
free(buffer);
359369
}
370+
#else // defined(MSGPACK_USE_CPP03)
371+
if (--*reinterpret_cast<std::atomic<unsigned int>*>(buffer) == 0) {
372+
free(buffer);
373+
}
374+
#endif // defined(MSGPACK_USE_CPP03)
360375
}
361376

362377
inline void incr_count(void* buffer)
363378
{
379+
#if defined(MSGPACK_USE_CPP03)
364380
_msgpack_sync_incr_and_fetch(reinterpret_cast<volatile _msgpack_atomic_counter_t*>(buffer));
381+
#else // defined(MSGPACK_USE_CPP03)
382+
++*reinterpret_cast<std::atomic<unsigned int>*>(buffer);
383+
#endif // defined(MSGPACK_USE_CPP03)
365384
}
366385

386+
#if defined(MSGPACK_USE_CPP03)
367387
inline _msgpack_atomic_counter_t get_count(void* buffer)
368388
{
369389
return *reinterpret_cast<volatile _msgpack_atomic_counter_t*>(buffer);
370390
}
391+
#else // defined(MSGPACK_USE_CPP03)
392+
inline std::atomic<unsigned int> const& get_count(void* buffer)
393+
{
394+
return *reinterpret_cast<std::atomic<unsigned int>*>(buffer);
395+
}
396+
#endif // defined(MSGPACK_USE_CPP03)
371397

372398
struct fix_tag {
373399
char f1[65]; // FIXME unique size is required. or use is_same meta function.

0 commit comments

Comments
 (0)