save-buffer commented on code in PR #13669:
URL: https://github.com/apache/arrow/pull/13669#discussion_r956368290


##########
cpp/src/arrow/compute/exec/spilling_join.h:
##########
@@ -0,0 +1,100 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#pragma once
+
+#include <bitset>
+
+#include "arrow/compute/exec/query_context.h"
+#include "arrow/compute/exec/hash_join.h"
+#include "arrow/compute/exec/accumulation_queue.h"
+
+namespace arrow
+{
+    namespace compute
+    {
+        class SpillingHashJoin
+        {
+        public:
+            using OutputBatchCallback = std::function<void(int64_t, 
ExecBatch)>;
+            using BuildFinishedCallback = std::function<Status(size_t)>;
+            using FinishedCallback = std::function<void(int64_t)>;
+            using RegisterTaskGroupCallback = std::function<int(
+                std::function<Status(size_t, int64_t)>, 
std::function<Status(size_t)>)>;
+            using StartTaskGroupCallback = std::function<Status(int, int64_t)>;
+            using PauseProbeSideCallback = std::function<void(int)>;
+            using ResumeProbeSideCallback = std::function<void(int)>;
+            using AbortContinuationImpl = std::function<void()>;
+
+            struct CallbackRecord
+            {
+                OutputBatchCallback output_batch_callback;
+                BuildFinishedCallback build_finished_callback;
+                FinishedCallback finished_callback;
+                RegisterTaskGroupCallback register_task_group_;
+                StartTaskGroupCallback start_task_group_callback;
+                PauseProbeSideCallback pause_probe_side_callback;
+                AbortContinuationImpl abort_callback;
+            };

Review Comment:
   I personally find these pure-virtual classes cumbersome to deal with as they 
remove the callback record from being near the site where I invoke `Init`. 
They're also less flexible and don't let me reuse functions (like HashJoinImpl 
and SpillingHashJoin reuse a lot of the same callbacks, I can just assign the 
same stuff between the two callback records).  



##########
cpp/src/arrow/util/atomic_util.h:
##########
@@ -0,0 +1,157 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#pragma once
+
+#include <atomic>
+#include <type_traits>
+
+namespace arrow
+{
+    namespace util
+    {
+#if defined(__clang) || defined(__GNUC__)
+        template <typename T>
+        inline T AtomicLoad(T *addr, std::memory_order order = 
std::memory_order_seq_cst) noexcept
+        {
+            T ret;
+            __atomic_load(addr, &ret, order);
+            return ret;
+        }
+
+        template <typename T>
+        inline void AtomicStore(T *addr, T &val, std::memory_order order = 
std::memory_order_seq_cst) noexcept
+        {
+            __atomic_store(addr, val, order);
+        }
+
+        template <typename T>
+        inline T AtomicFetchAdd(T *addr, T &val, std::memory_order order = 
std::memory_order_seq_cst) noexcept
+        {
+            static_assert(std::is_integral<T>::value, "AtomicFetchAdd can only 
be used on integral types");
+            return __atomic_fetch_add(addr, val, order);
+        }
+
+        template <typename T>
+        inline T AtomicFetchSub(T *addr, T &val, std::memory_order order = 
std::memory_order_seq_cst) noexcept
+        {
+            static_assert(std::is_integral<T>::value, "AtomicFetchSub can only 
be used on integral types");
+            return __atomic_fetch_sub(addr, val, order);
+        }
+
+#elif defined(_MSC_VER)
+        #include <intrin.h>
+        template <typename T>
+        inline T AtomicLoad(T *addr, std::memory_order /*order*/) noexcept
+        {
+            T val = *addr;
+            _ReadWriteBarrier();
+            return val;
+        }
+
+        template <typename T>
+        inline void AtomicStore(T *addr, T &val, std::memory_order /*order*/) 
noexcept
+        {
+            _ReadWriteBarrier();
+            *addr = val;
+        }
+
+        template <typename T, typename std::enable_if<sizeof(T) == 1>::type>
+        inline T AtomicFetchAdd(T *addr, T &val, std::memory_order /*order*/) 
noexcepet
+        {
+            static_assert(std::is_integral<T>::value, "AtomicFetchAdd can only 
be used on integral types");
+            return _InterlockedExchangeAdd8(addr, val);
+        }
+
+        template <typename T, typename std::enable_if<sizeof(T) == 2>::type>
+        inline T AtomicFetchAdd(T *addr, T &val, std::memory_order /*order*/) 
noexcept
+        {
+            static_assert(std::is_integral<T>::value, "AtomicFetchAdd can only 
be used on integral types");
+            return _InterlockedExchangeAdd16(addr, val);
+        }
+
+        template <typename T, typename std::enable_if<sizeof(T) == 4>::type>
+        inline T AtomicFetchAdd(T *addr, T &val, std::memory_order /*order*/) 
noexcept
+        {
+            static_assert(std::is_integral<T>::value, "AtomicFetchAdd can only 
be used on integral types");
+            return _InterlockedExchangeAdd(addr, val);
+        }
+
+#if _WIN64
+        template <typename T, typename std::enable_if<sizeof(T) == 8>::type>
+        inline T AtomicFetchAdd(T *addr, T &val, std::memory_order /*order*/) 
noexcept
+        {
+            static_assert(std::is_integral<T>::value, "AtomicFetchAdd can only 
be used on integral types");
+            return _InterlockedExchangeAdd64(addr, val);
+        }
+#else
+        template <typename T, typename std::enable_if<sizeof(T) == 8>::type>
+        inline T AtomicFetchAdd(T *addr, T &val, std::memory_order /*order*/) 
noexcept
+        {
+            static_assert(std::is_integral<T>::value, "AtomicFetchAdd can only 
be used on integral types");
+            _ReadWriteBarrier();
+            T expected = *addr;
+            for(;;)
+            {
+                T new_val = expected + val;
+                T prev = _InterlockedCompareExchange64(addr, new_val, 
expected);
+                if(prev == expected)
+                    return prev;
+                expected = prev;
+            }
+        }
+#endif
+
+        template <typename T, typename std::enable_if<sizeof(T) == 1>::type>
+        inline T AtomicFetchSub(T *addr, T &val, std::memory_order /*order*/) 
noexcept
+        {
+            static_assert(std::is_integral<T>::value, "AtomicFetchSub can only 
be used on integral types");
+            return _InterlockedExchangeAdd8(addr, -val);
+        }
+
+        template <typename T, typename std::enable_if<sizeof(T) == 2>::type>
+        inline T AtomicFetchSub(T *addr, T &val, std::memory_order /*order*/) 
noexcept
+        {
+            static_assert(std::is_integral<T>::value, "AtomicFetchSub can only 
be used on integral types");
+            return _InterlockedExchangeAdd16(addr, -val);
+        }
+
+        template <typename T, typename std::enable_if<sizeof(T) == 4>::type>
+        inline T AtomicFetchSub(T *addr, T &val, std::memory_order /*order*/) 
noexcept
+        {
+            static_assert(std::is_integral<T>::value, "AtomicFetchSub can only 
be used on integral types");
+            return _InterlockedExchangeAdd(addr, -val);
+        }
+
+#if _WIN64
+        template <typename T, typename std::enable_if<sizeof(T) == 8>::type>
+        inline T AtomicFetchSub(T *addr, T &val, std::memory_order /*order*/) 
noexcept
+        {
+            static_assert(std::is_integral<T>::value, "AtomicFetchSub can only 
be used on integral types");
+            return _InterlockedExchangeAdd64(addr, -val);
+        }
+#else
+        template <typename T, typename std::enable_if<sizeof(T) == 8>::type>
+        inline T AtomicFetchSub(T *addr, T &val, std::memory_order /*order*/) 
noexcept
+        {
+            static_assert(std::is_integral<T>::value, "AtomicFetchSub can only 
be used on integral types");
+            return AtomicFetchAdd(addr, -val, std::memory_order_seq_cst);
+        }        
+#endif
+#endif

Review Comment:
   Sometimes you don't want to always have an atomic, and only do an atomic 
operation in a specific situation. In other words, using these you can upgrade 
a non-atomic to an atomic temporarily, but you can't downgrade an atomic to a 
non-atomic. C++20 has `std::atomic_ref` to solve this, but we're still on 11. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to