Changeset baed175 in mainline


Ignore:
Timestamp:
2018-07-05T21:41:23Z (6 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
09e02ee
Parents:
db54a9d
git-author:
Dzejrou <dzejrou@…> (2018-05-09 23:06:44)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:23)
Message:

cpp: added weak_ptr

Location:
uspace/lib/cpp/include/internal/memory
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/include/internal/memory/shared_payload.hpp

    rdb54a9d rbaed175  
    7272            virtual refcount_t weak_refs() const noexcept = 0;
    7373            virtual bool expired() const noexcept = 0;
     74            virtual shared_payload_base* lock() noexcept = 0;
    7475
    7576            virtual ~shared_payload_base() = default;
     
    145146            {
    146147                if (__atomic_sub_fetch(&refcount_, 1, __ATOMIC_ACQ_REL) == 0)
    147                     return decrement_weak();
     148                {
     149                    /**
     150                     * First call to destroy() will delete the held object,
     151                     * so it doesn't matter what the weak_refcount_ is,
     152                     * but we added one and we need to remove it now.
     153                     */
     154                    decrement_weak();
     155
     156                    return true;
     157                }
    148158                else
    149159                    return false;
     
    168178            {
    169179                return refs() == 0;
     180            }
     181
     182            shared_payload_base<T>* lock() noexcept override
     183            {
     184                refcount_t rfs = refs();
     185                while (rfs != 0L)
     186                {
     187                    if (__atomic_compare_exchange_n(&refcount_, &rfs, rfs + 1,
     188                                                    true, __ATOMIC_RELAXED,
     189                                                    __ATOMIC_RELAXED))
     190                    {
     191                        return this;
     192                    }
     193                }
     194
     195                return nullptr;
    170196            }
    171197
  • uspace/lib/cpp/include/internal/memory/shared_ptr.hpp

    rdb54a9d rbaed175  
    3131
    3232#include <exception>
    33 #include <functional>
     33#include <internal/functional/arithmetic_operations.hpp>
    3434#include <internal/memory/allocator_arg.hpp>
    3535#include <internal/memory/shared_payload.hpp>
     
    333333            {
    334334                if (payload_)
    335                     return payload_->refcount();
     335                    return payload_->refs();
    336336                else
    337337                    return 0L;
Note: See TracChangeset for help on using the changeset viewer.