1 /* 2 * 3 * Copyright 2019 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 #include <grpc/support/port_platform.h> 20 21 #include <functional> 22 23 #include "src/core/lib/debug/trace.h" 24 #include "src/core/lib/gprpp/atomic.h" 25 #include "src/core/lib/gprpp/debug_location.h" 26 #include "src/core/lib/gprpp/mpscq.h" 27 #include "src/core/lib/gprpp/orphanable.h" 28 #include "src/core/lib/gprpp/ref_counted.h" 29 #include "src/core/lib/iomgr/exec_ctx.h" 30 31 #ifndef GRPC_CORE_LIB_IOMGR_WORK_SERIALIZER_H 32 #define GRPC_CORE_LIB_IOMGR_WORK_SERIALIZER_H 33 34 namespace grpc_core { 35 36 // WorkSerializer is a mechanism to schedule callbacks in a synchronized manner. 37 // All callbacks scheduled on a WorkSerializer instance will be executed 38 // serially in a borrowed thread. The API provides a FIFO guarantee to the 39 // execution of callbacks scheduled on the thread. 40 // When a thread calls Run() with a callback, the thread is considered borrowed. 41 // The callback might run inline, or it might run asynchronously in a different 42 // thread that is already inside of Run(). If the callback runs directly inline, 43 // other callbacks from other threads might also be executed before Run() 44 // returns. Since an arbitrary set of callbacks might be executed when Run() is 45 // called, generally no locks should be held while calling Run(). 46 class WorkSerializer { 47 public: 48 WorkSerializer(); 49 50 ~WorkSerializer(); 51 52 // TODO(yashkt): Replace grpc_core::DebugLocation with absl::SourceLocation 53 // once we can start using it directly. 54 void Run(std::function<void()> callback, 55 const grpc_core::DebugLocation& location); 56 57 private: 58 class WorkSerializerImpl; 59 60 OrphanablePtr<WorkSerializerImpl> impl_; 61 }; 62 63 } /* namespace grpc_core */ 64 65 #endif /* GRPC_CORE_LIB_IOMGR_WORK_SERIALIZER_H */ 66