• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 gRPC authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# GRPC contains the General RPC module.
16module GRPC
17  # Notifier is useful high-level synchronization primitive.
18  class Notifier
19    attr_reader :payload, :notified
20    alias_method :notified?, :notified
21
22    def initialize
23      @mutex    = Mutex.new
24      @cvar     = ConditionVariable.new
25      @notified = false
26      @payload  = nil
27    end
28
29    def wait
30      @mutex.synchronize do
31        @cvar.wait(@mutex) until notified?
32      end
33    end
34
35    def notify(payload)
36      @mutex.synchronize do
37        return Error.new('already notified') if notified?
38        @payload  = payload
39        @notified = true
40        @cvar.signal
41        return nil
42      end
43    end
44  end
45end
46