darion-yaphet commented on code in PR #3742:
URL: https://github.com/apache/brpc/pull/3742#discussion_r4695793402


##########
src/brpc/details/server_tls_task_binding.cpp:
##########
@@ -0,0 +1,193 @@
+// 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.
+
+#include "brpc/details/server_tls_task_binding.h"
+
+#include <errno.h>
+#include <map>
+#include <new>
+
+#include "bthread/task_lifecycle.h"
+#include "butil/scoped_lock.h"
+#include "butil/synchronization/lock.h"
+#include "brpc/details/server_tls_context.h"
+
+namespace brpc {
+namespace details {
+namespace {
+
+struct TaskToken {
+    explicit TaskToken(const scoped_refptr<ServerTLSContext>& context_in)
+        : context(context_in) {
+    }
+
+    scoped_refptr<ServerTLSContext> context;
+    ServerTLSContext::Lease lease;
+};
+
+struct TaskPoolRegistry {
+    butil::Mutex mutex;
+    // A null context is a permanent retired tombstone.
+    std::map<bthread_keytable_pool_t*, scoped_refptr<ServerTLSContext> > 
entries;
+};
+
+TaskPoolRegistry* GetTaskPoolRegistry() {
+    // Process-lifetime storage prevents registry destruction from racing late
+    // task admission or release during program shutdown.
+    static TaskPoolRegistry* const registry = new TaskPoolRegistry;
+    return registry;
+}
+
+int AdmitTask(bthread_keytable_pool_t* pool,
+              const void* parent_token,
+              void** output) {
+    if (output == nullptr) {
+        return EINVAL;
+    }
+    *output = nullptr;
+
+    scoped_refptr<ServerTLSContext> context;
+    {
+        TaskPoolRegistry* const registry = GetTaskPoolRegistry();
+        BAIDU_SCOPED_LOCK(registry->mutex);
+        const auto it = registry->entries.find(pool);
+        if (it == registry->entries.end()) {
+            // Unregistered pools retain their legacy behavior.
+            return 0;
+        }
+        if (it->second == nullptr) {
+            return ECANCELED;
+        }
+        context = it->second;
+    }
+
+    TaskToken* const token = new (std::nothrow) TaskToken(context);
+    if (token == nullptr) {
+        return ENOMEM;
+    }

Review Comment:
   > `std::nothrow` is not needed. It is recommended to remove it.
   
   std::nothrow is intentional here: task admission reports allocation failure 
through the ENOMEM return code. Removing it would make allocation failure throw 
std::bad_alloc, bypassing the null check below. We could use ordinary new with 
a try/catch, but nothrow keeps this path simpler.



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to