Re: [PR] New DoublyBufferedData for bthread (brpc)
chenBright commented on code in PR #2225: URL: https://github.com/apache/brpc/pull/2225#discussion_r1200619203 ## src/butil/containers/doubly_buffered_data.h: ## @@ -49,10 +51,38 @@ namespace butil { // foreground and background, lock thread-local mutexes one by one to make // sure all existing Read() finish and later Read() see new foreground, // then modify background(foreground before flip) again. +// +// But, when `AllowSuspendBthread=false', it is not allowed to suspend bthread +// while reading. Otherwise, it may cause deadlock. +// +// +// --- `AllowSuspendBthread=true' --- +// It is allowed to suspend bthread while reading. +// It is not allowed to use non-Void TLS. +// If bthread will not be suspended while reading, it also makes Read() almost +// lock-free by making Modify() *much* slower. +// If bthread will be suspended while reading, there is competition among +// bthreads using the same Wrapper. +// +// Read(): Begin with thread-local reference count of foreground instance +// incremented by one which be protected by a thread-local mutex, then read +// the foreground instance which will not be changed before its all thread-local +// reference count become zero. At last, after the query completes, thread-local +// reference count of foreground instance will be decremented by one, and if +// it becomes zero, notifies Modify(). +// +// Modify(): Modify background instance which is not used by any Read(), flip +// foreground and background, lock thread-local mutexes one by one and wait +// until thread-local reference counts which be protected by a thread-local +// mutex become 0 to make sure all existing Read() finish and later Read() +// see new foreground, then modify background(foreground before flip) again. class Void { }; -template +template struct is_Void : false_type { }; Review Comment: done ## src/butil/containers/doubly_buffered_data.h: ## @@ -302,35 +345,47 @@ class DoublyBufferedData::WrapperTLSGroup { static __thread std::vector* _s_tls_blocks; }; -template -pthread_mutex_t DoublyBufferedData::WrapperTLSGroup::_s_mutex = PTHREAD_MUTEX_INITIALIZER; +template +pthread_mutex_t DoublyBufferedData::WrapperTLSGroup::_s_mutex = PTHREAD_MUTEX_INITIALIZER; -template -std::deque::WrapperTLSId>* -DoublyBufferedData::WrapperTLSGroup::_s_free_ids = NULL; +template +std::deque::WrapperTLSId>* +DoublyBufferedData::WrapperTLSGroup::_s_free_ids = NULL; -template -typename DoublyBufferedData::WrapperTLSId -DoublyBufferedData::WrapperTLSGroup::_s_id = 0; +template +typename DoublyBufferedData::WrapperTLSId +DoublyBufferedData::WrapperTLSGroup::_s_id = 0; -template -__thread std::vector::WrapperTLSGroup::ThreadBlock*>* -DoublyBufferedData::WrapperTLSGroup::_s_tls_blocks = NULL; +template +__thread std::vector::WrapperTLSGroup::ThreadBlock*>* +DoublyBufferedData::WrapperTLSGroup::_s_tls_blocks = NULL; -template -class DoublyBufferedData::Wrapper +template +class DoublyBufferedData::Wrapper : public DoublyBufferedDataWrapperBase { friend class DoublyBufferedData; public: -explicit Wrapper() : _control(NULL) { +explicit Wrapper() +: _control(NULL) +, _modify_wait(false) { pthread_mutex_init(&_mutex, NULL); +pthread_cond_init(&_cond[0], NULL); Review Comment: done -- 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: dev-unsubscr...@brpc.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org - To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org For additional commands, e-mail: dev-h...@brpc.apache.org
Re: [PR] New DoublyBufferedData for bthread (brpc)
chenBright commented on code in PR #2225: URL: https://github.com/apache/brpc/pull/2225#discussion_r1200414234 ## src/butil/containers/doubly_buffered_data.h: ## @@ -49,10 +51,38 @@ namespace butil { // foreground and background, lock thread-local mutexes one by one to make // sure all existing Read() finish and later Read() see new foreground, // then modify background(foreground before flip) again. +// +// But, when `AllowSuspendBthread=false', it is not allowed to suspend bthread +// while reading. Otherwise, it may cause deadlock. +// +// +// --- `AllowSuspendBthread=true' --- +// It is allowed to suspend bthread while reading. +// It is not allowed to use non-Void TLS. +// If bthread will not be suspended while reading, it also makes Read() almost +// lock-free by making Modify() *much* slower. +// If bthread will be suspended while reading, there is competition among +// bthreads using the same Wrapper. +// +// Read(): Begin with thread-local reference count of foreground instance +// incremented by one which be protected by a thread-local mutex, then read +// the foreground instance which will not be changed before its all thread-local +// reference count become zero. At last, after the query completes, thread-local +// reference count of foreground instance will be decremented by one, and if +// it becomes zero, notifies Modify(). +// +// Modify(): Modify background instance which is not used by any Read(), flip +// foreground and background, lock thread-local mutexes one by one and wait +// until thread-local reference counts which be protected by a thread-local +// mutex become 0 to make sure all existing Read() finish and later Read() +// see new foreground, then modify background(foreground before flip) again. class Void { }; -template +template struct is_Void : false_type { }; Review Comment: 也可以 -- 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: dev-unsubscr...@brpc.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org - To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org For additional commands, e-mail: dev-h...@brpc.apache.org
Re: [PR] New DoublyBufferedData for bthread (brpc)
wwbmmm commented on code in PR #2225: URL: https://github.com/apache/brpc/pull/2225#discussion_r1200411426 ## src/butil/containers/doubly_buffered_data.h: ## @@ -49,10 +51,38 @@ namespace butil { // foreground and background, lock thread-local mutexes one by one to make // sure all existing Read() finish and later Read() see new foreground, // then modify background(foreground before flip) again. +// +// But, when `AllowSuspendBthread=false', it is not allowed to suspend bthread +// while reading. Otherwise, it may cause deadlock. +// +// +// --- `AllowSuspendBthread=true' --- +// It is allowed to suspend bthread while reading. +// It is not allowed to use non-Void TLS. +// If bthread will not be suspended while reading, it also makes Read() almost +// lock-free by making Modify() *much* slower. +// If bthread will be suspended while reading, there is competition among +// bthreads using the same Wrapper. +// +// Read(): Begin with thread-local reference count of foreground instance +// incremented by one which be protected by a thread-local mutex, then read +// the foreground instance which will not be changed before its all thread-local +// reference count become zero. At last, after the query completes, thread-local +// reference count of foreground instance will be decremented by one, and if +// it becomes zero, notifies Modify(). +// +// Modify(): Modify background instance which is not used by any Read(), flip +// foreground and background, lock thread-local mutexes one by one and wait +// until thread-local reference counts which be protected by a thread-local +// mutex become 0 to make sure all existing Read() finish and later Read() +// see new foreground, then modify background(foreground before flip) again. class Void { }; -template +template struct is_Void : false_type { }; Review Comment: IsVoid 可以么 -- 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: dev-unsubscr...@brpc.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org - To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org For additional commands, e-mail: dev-h...@brpc.apache.org
Re: [PR] New DoublyBufferedData for bthread (brpc)
chenBright commented on code in PR #2225: URL: https://github.com/apache/brpc/pull/2225#discussion_r1200244979 ## src/butil/containers/doubly_buffered_data.h: ## @@ -49,10 +51,38 @@ namespace butil { // foreground and background, lock thread-local mutexes one by one to make // sure all existing Read() finish and later Read() see new foreground, // then modify background(foreground before flip) again. +// +// But, when `AllowSuspendBthread=false', it is not allowed to suspend bthread +// while reading. Otherwise, it may cause deadlock. +// +// +// --- `AllowSuspendBthread=true' --- +// It is allowed to suspend bthread while reading. +// It is not allowed to use non-Void TLS. +// If bthread will not be suspended while reading, it also makes Read() almost +// lock-free by making Modify() *much* slower. +// If bthread will be suspended while reading, there is competition among +// bthreads using the same Wrapper. +// +// Read(): Begin with thread-local reference count of foreground instance +// incremented by one which be protected by a thread-local mutex, then read +// the foreground instance which will not be changed before its all thread-local +// reference count become zero. At last, after the query completes, thread-local +// reference count of foreground instance will be decremented by one, and if +// it becomes zero, notifies Modify(). +// +// Modify(): Modify background instance which is not used by any Read(), flip +// foreground and background, lock thread-local mutexes one by one and wait +// until thread-local reference counts which be protected by a thread-local +// mutex become 0 to make sure all existing Read() finish and later Read() +// see new foreground, then modify background(foreground before flip) again. class Void { }; -template +template struct is_Void : false_type { }; Review Comment: 写成小写的话,可能会被认为是c++关键字的void。要不改成is_butil_void? -- 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: dev-unsubscr...@brpc.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org - To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org For additional commands, e-mail: dev-h...@brpc.apache.org
Re: [PR] New DoublyBufferedData for bthread (brpc)
wwbmmm commented on code in PR #2225: URL: https://github.com/apache/brpc/pull/2225#discussion_r1200147854 ## src/butil/containers/doubly_buffered_data.h: ## @@ -49,10 +51,38 @@ namespace butil { // foreground and background, lock thread-local mutexes one by one to make // sure all existing Read() finish and later Read() see new foreground, // then modify background(foreground before flip) again. +// +// But, when `AllowSuspendBthread=false', it is not allowed to suspend bthread +// while reading. Otherwise, it may cause deadlock. +// +// +// --- `AllowSuspendBthread=true' --- +// It is allowed to suspend bthread while reading. +// It is not allowed to use non-Void TLS. +// If bthread will not be suspended while reading, it also makes Read() almost +// lock-free by making Modify() *much* slower. +// If bthread will be suspended while reading, there is competition among +// bthreads using the same Wrapper. +// +// Read(): Begin with thread-local reference count of foreground instance +// incremented by one which be protected by a thread-local mutex, then read +// the foreground instance which will not be changed before its all thread-local +// reference count become zero. At last, after the query completes, thread-local +// reference count of foreground instance will be decremented by one, and if +// it becomes zero, notifies Modify(). +// +// Modify(): Modify background instance which is not used by any Read(), flip +// foreground and background, lock thread-local mutexes one by one and wait +// until thread-local reference counts which be protected by a thread-local +// mutex become 0 to make sure all existing Read() finish and later Read() +// see new foreground, then modify background(foreground before flip) again. class Void { }; -template +template struct is_Void : false_type { }; Review Comment: is_Void命名有点奇怪,要么全小写,要么驼峰? ## src/butil/containers/doubly_buffered_data.h: ## @@ -302,35 +345,47 @@ class DoublyBufferedData::WrapperTLSGroup { static __thread std::vector* _s_tls_blocks; }; -template -pthread_mutex_t DoublyBufferedData::WrapperTLSGroup::_s_mutex = PTHREAD_MUTEX_INITIALIZER; +template +pthread_mutex_t DoublyBufferedData::WrapperTLSGroup::_s_mutex = PTHREAD_MUTEX_INITIALIZER; -template -std::deque::WrapperTLSId>* -DoublyBufferedData::WrapperTLSGroup::_s_free_ids = NULL; +template +std::deque::WrapperTLSId>* +DoublyBufferedData::WrapperTLSGroup::_s_free_ids = NULL; -template -typename DoublyBufferedData::WrapperTLSId -DoublyBufferedData::WrapperTLSGroup::_s_id = 0; +template +typename DoublyBufferedData::WrapperTLSId +DoublyBufferedData::WrapperTLSGroup::_s_id = 0; -template -__thread std::vector::WrapperTLSGroup::ThreadBlock*>* -DoublyBufferedData::WrapperTLSGroup::_s_tls_blocks = NULL; +template +__thread std::vector::WrapperTLSGroup::ThreadBlock*>* +DoublyBufferedData::WrapperTLSGroup::_s_tls_blocks = NULL; -template -class DoublyBufferedData::Wrapper +template +class DoublyBufferedData::Wrapper : public DoublyBufferedDataWrapperBase { friend class DoublyBufferedData; public: -explicit Wrapper() : _control(NULL) { +explicit Wrapper() +: _control(NULL) +, _modify_wait(false) { pthread_mutex_init(&_mutex, NULL); +pthread_cond_init(&_cond[0], NULL); Review Comment: cond_init和cond_destroy是不是也可以包在if (AllowSuspendBthread) 里面? -- 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: dev-unsubscr...@brpc.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org - To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org For additional commands, e-mail: dev-h...@brpc.apache.org
Re: [PR] New DoublyBufferedData for bthread (brpc)
chenBright commented on PR #2225: URL: https://github.com/apache/brpc/pull/2225#issuecomment-1555871325 > @chenBright 我看这个DoublyBufferedDataBthread大部分代码和DoublyBufferedData还是差不多的,是不是可以给DoublyBufferedData加一个bool模板参数,根据参数走不同的代码逻辑,这样改动小一些 @wwbmmm 改好了,有空看看 -- 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: dev-unsubscr...@brpc.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org - To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org For additional commands, e-mail: dev-h...@brpc.apache.org
Re: [PR] New DoublyBufferedData for bthread (brpc)
chenBright commented on code in PR #2225: URL: https://github.com/apache/brpc/pull/2225#discussion_r1199568069 ## src/butil/containers/doubly_buffered_data_bthread.h: ## @@ -0,0 +1,409 @@ +// 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. + +// Date: Mon Sep 22 22:23:13 CST 2014 + +#ifndef BUTIL_DOUBLY_BUFFERED_DATA_BTHREAD_H +#define BUTIL_DOUBLY_BUFFERED_DATA_BTHREAD_H + +#include +#include// std::vector +#include +#include "butil/containers/doubly_buffered_data_internal.h" +#include "butil/scoped_lock.h" +#include "butil/thread_local.h" +#include "butil/logging.h" +#include "butil/macros.h" +#include "butil/type_traits.h" +#include "butil/errno.h" +#include "butil/atomicops.h" +#include "butil/unique_ptr.h" + +namespace butil { + +// Compared to DoublyBufferedData, DoublyBufferedDataBthread allows to +// bthread is suspended when executing query logic. Review Comment: done ## src/butil/containers/doubly_buffered_data_bthread.h: ## @@ -0,0 +1,409 @@ +// 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. + +// Date: Mon Sep 22 22:23:13 CST 2014 + +#ifndef BUTIL_DOUBLY_BUFFERED_DATA_BTHREAD_H +#define BUTIL_DOUBLY_BUFFERED_DATA_BTHREAD_H + +#include +#include// std::vector +#include +#include "butil/containers/doubly_buffered_data_internal.h" +#include "butil/scoped_lock.h" +#include "butil/thread_local.h" +#include "butil/logging.h" +#include "butil/macros.h" +#include "butil/type_traits.h" +#include "butil/errno.h" +#include "butil/atomicops.h" +#include "butil/unique_ptr.h" + +namespace butil { + +// Compared to DoublyBufferedData, DoublyBufferedDataBthread allows to +// bthread is suspended when executing query logic. +// If bthread will not be suspended in the query logic, DoublyBufferedDataBthread +// also makes Read() almost lock-free by making Modify() *much* slower. +// If bthread will be suspended in the query logic, there is competition among +// bthreads using the same Wrapper. +// +// Read(): begin with thread-local reference count of foreground instance +// incremented by one which be protected by a thread-local mutex, then read +// the foreground instance which will not be changed before its all thread-local +// reference count become zero. At last, after the query completes, thread-local +// reference count of foreground instance will be decremented by one, and if +// it becomes zero, notifies Modify(). +// +// Modify(): Modify background instance which is not used by any Read(), flip +// foreground and background, lock thread-local mutexes one by one and wait +// until thread-local reference counts which be protected by a thread-local +// mutex become 0 to make sure all existing Read() finish and later Read() +// see new foreground, then modify background(foreground before flip) again. + +template +class DoublyBufferedDataBthread { +friend class internal::WrapperTLSGroup; +class Wrapper; +public: +typedef T DataType; + +class ScopedPtr { +friend class DoublyBufferedDataBthread; +public: +ScopedPtr() : _data(NULL), _index(0), _w(NULL) {} +~ScopedPtr() { +if (_w) { +_w->EndRead(_index); +} +} +const T* get() const { return _data; } +const T& operator*() const { return *_data; } +const T* operator->() const { return _data; } + +private: +DI
Re: [PR] New DoublyBufferedData for bthread (brpc)
wwbmmm commented on PR #2225: URL: https://github.com/apache/brpc/pull/2225#issuecomment-1550572100 > 或者加个`static_assert(!(AllowSuspendBthread && TLS != Void));`,在编译器发现这个问题,不支持这种用法? 我觉得可以 -- 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: dev-unsubscr...@brpc.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org - To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org For additional commands, e-mail: dev-h...@brpc.apache.org
Re: [PR] New DoublyBufferedData for bthread (brpc)
chenBright commented on PR #2225: URL: https://github.com/apache/brpc/pull/2225#issuecomment-1549901819 @wwbmmm ```c++ template class DoublyBufferedData; ``` 加bool模板参数的话,有一个问题:当AllowSuspendBthread=true(允许查询逻辑中挂起bthread)的时候,TLS 即使不为Void,DoublyBufferedData也是不支持自定义tls数据的。这样的实现好像不太友好,用户可能会比较迷惑为啥TLS没有生效吧。 或者加个`static_assert(!(AllowSuspendBthread && TLS != Void));`,在编译器发现这个问题,不支持这种用法? -- 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: dev-unsubscr...@brpc.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org - To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org For additional commands, e-mail: dev-h...@brpc.apache.org
Re: [PR] New DoublyBufferedData for bthread (brpc)
chenBright commented on PR #2225: URL: https://github.com/apache/brpc/pull/2225#issuecomment-1549591289 > @chenBright 我看这个DoublyBufferedDataBthread大部分代码和DoublyBufferedData还是差不多的,是不是可以给DoublyBufferedData加一个bool模板参数,根据参数走不同的代码逻辑,这样改动小一些 改动本来是不大的。 加bool模板参数应该也可以,那我先这样改改看。 -- 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: dev-unsubscr...@brpc.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org - To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org For additional commands, e-mail: dev-h...@brpc.apache.org
Re: [PR] New DoublyBufferedData for bthread (brpc)
wwbmmm commented on PR #2225: URL: https://github.com/apache/brpc/pull/2225#issuecomment-1549542249 @chenBright 我看这个DoublyBufferedDataBthread大部分代码和DoublyBufferedData还是差不多的,是不是可以给DoublyBufferedData加一个bool模板参数,根据参数走不同的代码逻辑,这样改动小一些 -- 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: dev-unsubscr...@brpc.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org - To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org For additional commands, e-mail: dev-h...@brpc.apache.org
Re: [PR] New DoublyBufferedData for bthread (brpc)
wwbmmm commented on code in PR #2225: URL: https://github.com/apache/brpc/pull/2225#discussion_r1195051271 ## src/butil/containers/doubly_buffered_data_bthread.h: ## @@ -0,0 +1,409 @@ +// 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. + +// Date: Mon Sep 22 22:23:13 CST 2014 + +#ifndef BUTIL_DOUBLY_BUFFERED_DATA_BTHREAD_H +#define BUTIL_DOUBLY_BUFFERED_DATA_BTHREAD_H + +#include +#include// std::vector +#include +#include "butil/containers/doubly_buffered_data_internal.h" +#include "butil/scoped_lock.h" +#include "butil/thread_local.h" +#include "butil/logging.h" +#include "butil/macros.h" +#include "butil/type_traits.h" +#include "butil/errno.h" +#include "butil/atomicops.h" +#include "butil/unique_ptr.h" + +namespace butil { + +// Compared to DoublyBufferedData, DoublyBufferedDataBthread allows to +// bthread is suspended when executing query logic. Review Comment: allows to suspend bthread when xxx ## src/butil/containers/doubly_buffered_data_bthread.h: ## @@ -0,0 +1,409 @@ +// 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. + +// Date: Mon Sep 22 22:23:13 CST 2014 + +#ifndef BUTIL_DOUBLY_BUFFERED_DATA_BTHREAD_H +#define BUTIL_DOUBLY_BUFFERED_DATA_BTHREAD_H + +#include +#include// std::vector +#include +#include "butil/containers/doubly_buffered_data_internal.h" +#include "butil/scoped_lock.h" +#include "butil/thread_local.h" +#include "butil/logging.h" +#include "butil/macros.h" +#include "butil/type_traits.h" +#include "butil/errno.h" +#include "butil/atomicops.h" +#include "butil/unique_ptr.h" + +namespace butil { + +// Compared to DoublyBufferedData, DoublyBufferedDataBthread allows to +// bthread is suspended when executing query logic. +// If bthread will not be suspended in the query logic, DoublyBufferedDataBthread +// also makes Read() almost lock-free by making Modify() *much* slower. +// If bthread will be suspended in the query logic, there is competition among +// bthreads using the same Wrapper. +// +// Read(): begin with thread-local reference count of foreground instance +// incremented by one which be protected by a thread-local mutex, then read +// the foreground instance which will not be changed before its all thread-local +// reference count become zero. At last, after the query completes, thread-local +// reference count of foreground instance will be decremented by one, and if +// it becomes zero, notifies Modify(). +// +// Modify(): Modify background instance which is not used by any Read(), flip +// foreground and background, lock thread-local mutexes one by one and wait +// until thread-local reference counts which be protected by a thread-local +// mutex become 0 to make sure all existing Read() finish and later Read() +// see new foreground, then modify background(foreground before flip) again. + +template +class DoublyBufferedDataBthread { +friend class internal::WrapperTLSGroup; +class Wrapper; +public: +typedef T DataType; + +class ScopedPtr { +friend class DoublyBufferedDataBthread; +public: +ScopedPtr() : _data(NULL), _index(0), _w(NULL) {} +~ScopedPtr() { +if (_w) { +_w->EndRead(_index); +} +} +const T* get() const { return _data; } +const T& operator*() const { return *_data; } +const T* operator->() const { return _data; } +
Re: [PR] New DoublyBufferedData for bthread (brpc)
chenBright commented on PR #2225: URL: https://github.com/apache/brpc/pull/2225#issuecomment-1537096087 @wwbmmm 麻烦有空看看这个PR -- 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: dev-unsubscr...@brpc.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org - To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org For additional commands, e-mail: dev-h...@brpc.apache.org